IF Commons GitHub

surfaces/cli/signals

Written to: 1 requirement: 1 proved, 0 partly, 0 failing, 0 with no test recorded. 0004-headless-cli FR-009

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 }

Source: commons/headless/src/surfaces/cli/signals.ts

1 of 2 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

Guardedtypeno test imports it
type 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.

guardedfunction
function 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.

Imported by 16 tests of the run (16 passed, 0 failed): the library's results

Titles, headings, exports, tables, columns and requirements. Esc closes.