{"version":3,"file":"flush.cjs","names":[],"sources":["../../src/testing/flush.ts"],"sourcesContent":["/**\n * Reactive update flushing utilities for test environments.\n */\n\nimport { OreTimeoutError } from '../errors';\nimport { hasPendingWork } from '../runtime';\n\n// Defensive cap against a genuine bug (e.g. an onMounted callback that always registers\n// another onMounted, looping forever) — not a \"how much is normally enough\" guess like the\n// old fixed turn count was. Microtask polls are cheap, so this is generous.\nconst MAX_FLUSH_ITERATIONS = 1000;\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\n/**\n * Options for flushing pending reactive updates.\n */\nexport interface FlushOptions {\n  /**\n   * Logger function called with each diagnostic message during the flush.\n   * Pass `console.debug` or any `(msg: string) => void` to enable output.\n   * Omit (or pass `undefined`) to disable all logging.\n   *\n   * Prefer `debugFlush()` (also from `@vielzeug/ore/testing`) over wiring this manually.\n   */\n  logger?: (msg: string) => void;\n}\n\n// ─── Core ────────────────────────────────────────────────────────────────────\n\n/**\n * Flush pending reactive updates: waits for every tracked in-flight component\n * operation (scheduled `onMounted` microtasks — including ones registered by other\n * `onMounted` callbacks) to settle, then yields one animation frame and one final\n * microtask pass for rAF-scheduled work.\n *\n * This is deterministic, not a fixed number of guessed turns — `@vielzeug/ripple`'s\n * reactive graph itself settles synchronously on every signal write, so the only real\n * asynchrony to wait for is ore's own component scheduling (see `runtime.ts`'s\n * `hasPendingWork()`). There is no \"deep\" variant to reach for anymore: `flush()` waits\n * for exactly as much work as is actually pending, however deep that turns out to be.\n *\n * @example\n * ```ts\n * // Standard flush — waits for whatever is actually pending, shallow or deep\n * await flush();\n *\n * // Debug timing issues\n * await flush({ logger: console.debug });\n * ```\n *\n * @throws `OreTimeoutError` if pending work never settles within {@link MAX_FLUSH_ITERATIONS}\n * microtask turns — almost always a sign of a genuine bug (e.g. an `onMounted` callback that\n * keeps re-registering itself), not a case for raising a limit.\n */\nexport async function flush(options: FlushOptions = {}): Promise<void> {\n  const { logger } = options;\n\n  logger?.('[flush] draining pending component work (scheduled mount callbacks)');\n\n  let turns = 0;\n\n  while (hasPendingWork()) {\n    if (++turns > MAX_FLUSH_ITERATIONS) {\n      throw new OreTimeoutError(\n        `flush(): pending component work did not settle after ${MAX_FLUSH_ITERATIONS} microtask turns — ` +\n          `check for a component whose onMounted callback keeps re-registering itself.`,\n      );\n    }\n\n    logger?.(`[flush] pending work remains, turn ${turns}`);\n    await Promise.resolve();\n  }\n\n  logger?.('[flush] draining requestAnimationFrame');\n\n  await new Promise<void>((resolve) =>\n    typeof requestAnimationFrame !== 'undefined' ? requestAnimationFrame(() => resolve()) : resolve(),\n  );\n\n  logger?.('[flush] final microtask pass');\n\n  await Promise.resolve();\n\n  logger?.('[flush] complete');\n}\n\n/**\n * `flush()` with debug logging pre-wired to `console.debug`. Logs each phase and pending-work\n * turn so you can diagnose unexpected update order or timing issues.\n *\n * @example\n * ```ts\n * import { debugFlush } from '@vielzeug/ore/testing';\n *\n * // in a test\n * await debugFlush();\n * // [flush] draining pending component work (scheduled mount callbacks)\n * // [flush] pending work remains, turn 1\n * // ...\n * ```\n */\nexport async function debugFlush(): Promise<void> {\n  return flush({ logger: console.debug });\n}\n"],"mappings":"6DAUA,IAAM,EAAuB,IA6C7B,eAAsB,EAAM,EAAwB,CAAC,EAAkB,CACrE,GAAM,CAAE,UAAW,EAEnB,IAAS,qEAAqE,EAE9E,IAAI,EAAQ,EAEZ,KAAO,EAAA,eAAe,GAAG,CACvB,GAAI,EAAE,EAAQ,EACZ,MAAM,IAAI,EAAA,gBACR,wDAAwD,EAAqB,+FAE/E,EAGF,IAAS,sCAAsC,GAAO,EACtD,MAAM,QAAQ,QAAQ,CACxB,CAEA,IAAS,wCAAwC,EAEjD,MAAM,IAAI,QAAe,GACvB,OAAO,sBAA0B,IAAc,0BAA4B,EAAQ,CAAC,EAAI,EAAQ,CAClG,EAEA,IAAS,8BAA8B,EAEvC,MAAM,QAAQ,QAAQ,EAEtB,IAAS,kBAAkB,CAC7B,CAiBA,eAAsB,GAA4B,CAChD,OAAO,EAAM,CAAE,OAAQ,QAAQ,KAAM,CAAC,CACxC"}