UNPKG

histoire

Version:

A fast story book powered by Vite

24 lines (23 loc) • 919 B
import { ExecutionError } from '../runtime/execution-types.js'; /** Fail at an owned setup/read boundary before acquiring or publishing work. */ export function throwIfTestAborted(signal) { if (signal?.aborted) throw new ExecutionError('CANCELLED', 'Histoire test run cancelled'); } /** Interrupt active work; caller must still confirm cleanup before releasing its lane. */ export async function withTestAbort(work, signal) { throwIfTestAborted(signal); if (!signal) return work(); let listener; const aborted = new Promise((_, reject) => { listener = () => reject(new ExecutionError('CANCELLED', 'Histoire test run cancelled')); signal.addEventListener('abort', listener, { once: true }); }); try { return await Promise.race([Promise.resolve().then(work), aborted]); } finally { signal.removeEventListener('abort', listener); } }