UNPKG

histoire

Version:

A fast story book powered by Vite

76 lines (75 loc) • 3.7 kB
/** Live context ownership prevents cache evidence crossing config restarts or projects. */ const caches = new WeakMap(); /** * Read/write detached definitions without retaining any browser, handler, or runner resource. * @param ctx Context owning the cache. * @param source Catalog publication of the operation; no publication, no reuse. * @param verify Revalidates one story's dependency evidence; absent without an eligibility service. * @param inventory Current story files of the operation snapshot. */ export function getTestCollectionCache(ctx, source, verify, inventory) { if (!source || !verify) return undefined; let cache = caches.get(ctx); if (!cache) { cache = { stories: new Map() }; caches.set(ctx, cache); } const owned = cache; const executable = inventory.filter(file => file.story && !file.story.docsOnly); const stories = new Map(executable.map(file => [file.story.id, file.story])); for (const id of owned.stories.keys()) { if (!stories.has(id)) owned.stories.delete(id); } /** Exactly the story's current variants, all collected without error. */ function complete(id, entries) { const story = stories.get(id); if (!story || entries.length !== story.variants.length || entries.some(entry => entry.collection.error)) return false; return story.variants.every(variant => entries.some(entry => entry.target.variantId === variant.id)); } return { /** Cache misses may shrink without resetting Vite's existing dependency cache; inventory changes re-scan. */ captureOptimizerEntries(entries) { const key = executable.map(file => file.path).sort().join('\n'); if (owned.optimizer?.inventory !== key) owned.optimizer = { inventory: key, entries: [...entries] }; return [...owned.optimizer.entries]; }, /** Forced refresh, changed variants, and invalid evidence remove entries before new mounts. */ async read(ids, force = false) { const hits = await Promise.all(ids.map(async (id) => { const entry = owned.stories.get(id); if (!entry) return []; if (!force && complete(id, entry.variants) && await verify(entry.evidence)) return structuredClone(entry.variants); // A concurrent admission may have replaced the entry while verifying. if (owned.stories.get(id) === entry) owned.stories.delete(id); return []; })); return hits.flat(); }, /** * Caller confirms cancellation and provenance before this atomic admission. * @param variants Collected entries; stories missing a variant are skipped. * @param evidence Proofs of stories eligible for reuse. * @param assertCurrent Publication fence of the operation. */ write(variants, evidence, assertCurrent) { const admitted = new Map(); for (const [id, proof] of evidence) { const entries = variants.filter(entry => entry.target.storyId === id); if (complete(id, entries)) admitted.set(id, { variants: structuredClone(entries), evidence: proof }); } // Cloning can consume the final budget synchronously. Fence after all // detached evidence is prepared, before admitting any story atomically. assertCurrent(); for (const [id, entry] of admitted) owned.stories.set(id, entry); }, }; }