// The bench prep's TRIGGER: after a write that may need a new excerpt (a // window moved, a clip inserted, split, merged, duplicated, restored) and // after a fetch lands, queue prep for the clips it touched (benchPrep.mjs) -- // of the cut, or of a take (a write to its patch, a fetch for it). // // Never holds the writer: the writer has already written when this is called, // it returns at once, and the work -- a plan of a few stats, then at most a // detached runner started -- happens after. benchPrep.mjs is loaded lazily, so // manifest.mjs (which calls this) gains no import graph and no cycle. // // A CLI process that exits with process.exit right after its write would cut // that work off, so the CLI awaits `settleBenchPrepTriggers()` first. // UMTOOL_BENCH_PREP=0 turns every trigger off. /** @type {Set>} */ const inFlight = new Set(); /** * @typedef {(project: { id: string, dir: string }, clips: string[] | null, opts: { excerptsOnly: boolean, take?: string | null }) => Promise} QueueFn */ /** @type {QueueFn | null} a test's stand-in for queueBenchPrep */ let queueOverride = null; /** For tests: replace the queue (null restores it). @param {QueueFn | null} fn */ export function setBenchPrepQueue(fn) { queueOverride = fn; } /** * Queue prep for `clips` of the project at `dir`, without waiting for it. * * `excerptsOnly` (the default, for a manifest write: they come by the dozen * in a walk) starts a runner only when an excerpt is missing; a FETCH that * landed passes false, so the new windows' waveforms are made too. * * `take`: a write to a TAKE's patch (its walk's save, `umtool window * --take`), or a fetch for one -- the take's clips, queued for the take's * own runner (benchPrep.mjs prepStateDirOf). * * @param {string} dir the project directory (the manifest's) * @param {string[] | null} clips null: every clip * @param {{ excerptsOnly?: boolean, take?: string | null }} [opts] */ export function benchPrepAfterWrite(dir, clips, { excerptsOnly = true, take = null } = {}) { if (process.env.UMTOOL_BENCH_PREP === "0") return; const p = (async () => { const m = await import("./benchPrep.mjs"); const project = m.projectOfDir(dir); if (!project) return; const queue = queueOverride ?? m.queueBenchPrep; await queue(project, clips, { excerptsOnly, ...(take ? { take } : {}) }); })().catch(() => { /* a trigger never fails a write */ }); inFlight.add(p); void p.finally(() => inFlight.delete(p)); } /** Every trigger started so far, settled. */ export async function settleBenchPrepTriggers() { await Promise.allSettled([...inFlight]); }