Guardedtypeno test imports ittype Guarded<T> = { readonly kind: "done"; readonly value: T } | { readonly kind: "stopped"; readonly signal: StopSignal; readonly code: number };How guarded work ended: it finished, or a signal came first.
A run stopped by the person or the system (commons headless 0004-headless-cli FR-009): SIGINT ends it with 130 and SIGTERM with 143, the statuses a shell already reads as "killed by a signal"; the command is told to stop through an AbortSignal, the run's cleanup runs once, and the run ends with the stop mark and the word Stopped instead of with a failure. The signals come through a port, so a test sends them.
import { guarded } from "./signals.ts";
const r = await guarded(undefined, new AbortController(), Promise.resolve(7));
r; // { kind: "done", value: 7 }Guardedtypeno test imports ittype Guarded<T> = { readonly kind: "done"; readonly value: T } | { readonly kind: "stopped"; readonly signal: StopSignal; readonly code: number };How guarded work ended: it finished, or a signal came first.
guardedfunctionfunction guarded<T>(port: SignalPort | undefined, controller: AbortController, work: Promise<T>): Promise<Guarded<T>>Races the work against the signals: the first signal aborts controller (so that the work can stop itself) and ends the race with its status; where the work ends first, the listeners are removed. The work is left to finish on its own after a signal; the caller does not wait for it.