All files / kernel-utils/src wait-quiescent.ts

100% Statements 8/8
100% Branches 3/3
100% Functions 3/3
100% Lines 5/5

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 26 27 28 29                                        127x 127x 39x   88x   127x    
import 'setimmediate';
import { makePromiseKit } from '@endo/promise-kit';
 
// Note: This can only be imported from the Start Compartment, where the tricks
// used by the 'setimmediate' package are available.
 
/**
 * Return a promise that waits until the microtask queue is empty. When this
 * promise resolves, the holder can be assured that the environment no longer
 * has agency.
 *
 * @param delay - Optional delay (in ms) to wait for things to catch up.
 *
 * @returns a Promise that can await the compartment becoming quiescent.
 */
export async function waitUntilQuiescent(delay: number = 0): Promise<void> {
  // the delivery might cause some number of (native) Promises to be
  // created and resolved, so we use the IO queue to detect when the
  // Promise queue is empty. The IO queue (setImmediate and setTimeout) is
  // lower-priority than the Promise queue on browsers.
  const { promise: queueEmptyP, resolve } = makePromiseKit<void>();
  if (delay > 0) {
    setTimeout(() => resolve(), delay);
  } else {
    setImmediate(() => resolve());
  }
  return queueEmptyP;
}