import assert from "node:assert/strict"; /** A one-shot, observable scheduling boundary. An unexercised gate is a failure. */ export class Checkpoint { private readonly reached = Promise.withResolvers(); private readonly released = Promise.withResolvers(); public fired = false; public resumed = false; public value?: T; public async enter(value: T, signal?: AbortSignal | null): Promise { assert(!this.fired, "Checkpoint entered twice"); this.fired = true; this.value = structuredClone(value); this.reached.resolve(this.value); signal?.throwIfAborted(); let abort = () => {}; try { await Promise.race([ this.released.promise, new Promise((_, reject) => { abort = () => reject( signal?.reason ?? new Error("Checkpoint aborted") ); signal?.addEventListener("abort", abort, { once: true }); }) ]); } finally { signal?.removeEventListener("abort", abort); } } public wait(): Promise { return this.reached.promise; } public resume(): void { this.resumed = true; this.released.resolve(); } public assertConsumed(): void { assert(this.fired, "Checkpoint never reached"); assert(this.resumed, "Checkpoint never released"); } }