histoire
Version:
A fast story book powered by Vite
91 lines (90 loc) • 3.46 kB
JavaScript
import { performance } from 'node:perf_hooks';
import { describeProgressError } from '@histoire/cli/protocol/progress';
/** Fastest cadence of count-only updates; the final count is always published. */
const UPDATE_INTERVAL_MS = 100;
/** Observer that records nothing; used when no sink is supplied. */
export const inertProgress = {
begin() { },
update() { },
complete() { },
fail() { },
skip() { },
track: async (_phase, work) => work(),
};
/**
* Creates a progress producer.
* @param sink Receives each observation; faults are swallowed. Absent means inert.
* @param isActive Publication stops permanently the first time this returns false.
* @param now Monotonic clock in milliseconds; tests only.
*/
export function createStartupProgress(sink, isActive = () => true, now = () => performance.now()) {
if (!sink)
return inertProgress;
const running = new Map();
let superseded = false;
/** Publishes unless the owning generation was superseded. */
function publish(event) {
superseded ||= !isActive();
if (superseded)
return;
try {
sink(event);
}
catch { }
}
/** Removes a running phase and returns its monotonic duration. */
function finish(phase) {
const entry = running.get(phase);
if (!entry)
return undefined;
running.delete(phase);
return { durationMs: Math.max(0, Math.round(now() - entry.startedAt)), ...(entry.background ? { background: true } : {}) };
}
const progress = {
begin(phase, label, options) {
if (running.has(phase))
return;
const background = options?.background === true;
running.set(phase, { startedAt: now(), background, lastUpdateAt: -Infinity });
publish({ phase, status: 'started', ...(label ? { label } : {}), ...(background ? { background } : {}) });
},
update(phase, patch) {
const entry = running.get(phase);
if (!entry)
return;
const time = now();
const final = patch.total !== undefined && patch.current === patch.total;
// Per-file counts can arrive in bursts; label changes and the last count always pass.
if (patch.label === undefined && !final && time - entry.lastUpdateAt < UPDATE_INTERVAL_MS)
return;
entry.lastUpdateAt = time;
publish({ phase, status: 'updated', ...patch, ...(entry.background ? { background: true } : {}) });
},
complete(phase) {
const done = finish(phase);
if (done)
publish({ phase, status: 'completed', ...done });
},
fail(phase, error) {
const done = finish(phase);
if (done)
publish({ phase, status: 'failed', ...done, error: describeProgressError(error) });
},
skip(phase, label) {
publish({ phase, status: 'skipped', ...(label ? { label } : {}) });
},
async track(phase, work, label) {
progress.begin(phase, label);
try {
const result = await work();
progress.complete(phase);
return result;
}
catch (error) {
progress.fail(phase, error);
throw error;
}
},
};
return progress;
}