@sekhmet/dispatch
A typed, one-shot signal: AbortController/AbortSignal with a typed value instead of reason: any, and the same class shape. A DispatchController<T>dispatches a value once; its DispatchSignal<T> can only be read. The first dispatch wins, a listener added after it still runs, and a throwing listener is a panic reported to your panic runtime's hook instead of breaking the dispatch.
Install
The package is published on JSR.
sh
deno add jsr:@sekhmet/dispatchFor Node.js, Bun and other npm-compatible runtimes, install through JSR's npm compatibility layer:
sh
npx jsr add @sekhmet/dispatch # npm
pnpm dlx jsr add @sekhmet/dispatch # pnpm
yarn dlx jsr add @sekhmet/dispatch # yarn
bunx jsr add @sekhmet/dispatch # bunExample
ts
import { DispatchController } from "@sekhmet/dispatch";
import { Some } from "@sekhmet/enum/option";
import { definePanicRuntime } from "@sekhmet/panic";
import { assert, assertEquals } from "@std/assert";
type Cause =
| { readonly kind: "done" }
| { readonly kind: "cancelled"; readonly by: string };
// {}
const runtime = definePanicRuntime();
const end = new DispatchController<Cause>({ panics: runtime });
const log: string[] = [];
end.signal.whenDispatched((cause) => {
log.push(`ended: ${cause.kind}`);
});
const forFetch = end.signal.toAbortSignal();
assert(
end.dispatch({ kind: "cancelled", by: "user" }).isNone(),
"the first dispatch should find nothing there",
);
assertEquals(
end.dispatch({ kind: "done" }), // too late: the first dispatch wins
Some({ kind: "cancelled", by: "user" }),
"a later dispatch should return the first cause",
);
end.signal.whenDispatched((cause) => {
log.push(`late listener: ${cause.kind}`); // runs at once
});
assertEquals(
log,
["ended: cancelled", "late listener: cancelled"],
"both listeners, the late one too, should hear the first cause",
);
assertEquals(
forFetch.reason,
{ kind: "cancelled", by: "user" },
"the AbortSignal should abort with the typed cause",
);API
| Export | Does |
|---|---|
new DispatchController<T>({ panics }) | the write side; its listeners run inside the panics boundary |
controller.dispatch(value) | dispatches once: None if this call did, Some(first) if not |
controller.signal | the read side, a DispatchSignal<T> |
signal.poll() | Ready(value) once dispatched, Pending before |
signal.whenDispatched(listener) | runs listener on dispatch, or at once; returns an Unsubscribe |
signal.map(f), signal.zip(other) | a signal of f(value), or of both values once both dispatch |
DispatchSignal.any(signals) | a signal of the first value dispatched; any([]) never dispatches |
DispatchSignal.dispatched({ value }) | a signal already dispatched with value |
DispatchSignal.never() | a signal that never dispatches |
signal.wait(), signal.toVow() | the value as a Promise, or as a new Vow |
signal.toAbortSignal() | an AbortSignal that aborts with the value |
DispatchSignal.fromAbortSignal({ signal, panics }) | a signal of an AbortSignal's abort reason |
Learn more
- Tutorial: Getting started builds a cancellable download step by step: a typed end cause, listeners, the first-wins rule, a timeout raced against the user, and a bridge to
fetch. - How-to guides: Recipes for cancelling
fetchwith a typed reason, racing signals, waiting for two signals, adapting anAbortSignal, awaiting a signal, handing out a finished signal, and catching throwing listeners. - Explanation: Concepts covers why a typed signal rather than an
AbortSignal, the two capabilities, why the first dispatch wins anddispatchreturns anOption, listeners that never miss it, throwing listeners as panics, and why derived signals hold no subscription. - Reference: the API reference on JSR documents every export.
The guides are also on the docs site.
License
MIT. See LICENSE.md.