UNPKG

histoire

Version:

A fast story book powered by Vite

40 lines (39 loc) • 1.68 kB
/** * Tells when source collection is genuinely usable. The catalog's own * `updating` fence is authoritative (it closes at the first announcement of a * batch and opens only after its publication, also when nothing changed, which * no catalog subscription would report); collection events only say when to * look again. Background revalidation of a restored catalog never closes it. * The handler never awaits anything, so a collector waiting on it cannot be * held up by a test run. * @param options Catalog and collection observations. * @returns Barrier state and its detach function. */ export function createCollectionBarrier(options) { let failure; let failed = false; const off = options.onCollection((event) => { if (!event.revalidate) { if (event.phase === 'failed') { failed = true; failure = event.error; } else if (event.phase === 'completed') { failed = false; failure = undefined; } } options.changed(event); }); return { /** A completed catalog exists and no batch owns the source. */ usable: () => !failed && options.catalog?.current !== undefined && options.catalog.updating === false, /** The latest non-background batch failed and no later one completed. */ failed: () => failed, /** Message of the failure, when any. */ failure: () => String(failure instanceof Error ? failure.message : failure ?? '').split('\n')[0], /** A catalog is attached at all. */ attached: () => options.catalog !== undefined, dispose: off, }; }