All files / player-browser/src/compute yield.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25                122x 122x 88x 87x 87x 87x 87x 87x   87x     1x 1x      
/**
 * Resolves after a task boundary, so the host can paint and dispatch pending events before the caller continues.
 *
 * `scheduler.yield()` where the host has it; otherwise a `MessageChannel` message, which starts a new task without the
 * clamp nested `setTimeout(…, 0)` calls get (4 ms in browsers, 1 ms in Node); `setTimeout` only as the last resort.
 * `queueMicrotask` would not do: microtasks run before the host gets a render opportunity.
 */
export function yieldToTask(): Promise<void> {
  const scheduler = (globalThis as { scheduler?: { yield?: () => Promise<void> } }).scheduler;
  if (scheduler?.yield !== undefined) return scheduler.yield();
  if (typeof MessageChannel === 'function') {
    return new Promise((resolve) => {
      const channel = new MessageChannel();
      channel.port1.onmessage = () => {
        channel.port1.close();
        resolve();
      };
      channel.port2.postMessage(undefined);
    });
  }
  return new Promise((resolve) => {
    setTimeout(resolve, 0);
  });
}