Glossary
The terms of @sekhmet/dispatch's ubiquitous language, each named the way the code names it. The concepts guide explains how they fit together. Terms Sekhmet's packages share, such as Option, Poll, Vow, Panic, panic boundary, double fault, and main and mini, live in the root glossary and aren't redefined here.
The two capabilities
- Dispatch (
controller.dispatch(value)): the one-shot event that carries a typed value, like anAbortSignal's abort with aTinstead ofreason: any. The call returns the value already there, like Rust'sHashMap::insert: a mainNoneif this call dispatched,Some(first)if an earlier one had. Losing the race isn't an error. - Dispatch controller (
DispatchController<T>): the write side, likeAbortController. Holding it is the right to dispatch. It is built with a panic boundary ({ panics }), inside which its listeners run. - Dispatch signal (
DispatchSignal<T>): the read side, likeAbortSignal. Holding it is the right to hear the dispatch: listen to it, poll it, derive from it and convert it, but never dispatch it. It has no public constructor, and is covariant inT. - First dispatch wins: the rule that a dispatch happens once. A later
dispatch, including one made by a listener mid-dispatch, changes nothing and returns the first value.
Reading a signal
- Poll (
signal.poll()): reads a signal without waiting, as a mainPoll:Ready(value)once dispatched,Pendingbefore. - Listener (
Listener<T>): a synchronous function a dispatch runs once with its value. It returnsundefined, so anasynclistener is a compile error. - When dispatched (
signal.whenDispatched(listener)): runs the listener when the signal dispatches, or at once if it already has, and returns anUnsubscribe. Listeners run in the order they were added, and one added mid-dispatch joins the end of the queue. - Late listener: a listener added after the dispatch. It runs at once, where an
AbortSignallistener added after the abort never fires. - Unsubscribe (
Unsubscribe): removes a listener that hasn't run yet, reportingtruethe first time it removed one andfalseonce the listener has run or been removed. - Throwing listener: a listener with a bug. The controller runs it inside its panic boundary's
catchUnwind, so the throw is reported to the runtime's hook, the other listeners still run, anddispatchreturns normally. Only a double fault reaches the caller, after the dispatch completes.
Derived signals
- Derived signal: a signal built from others by
map,zip,DispatchSignal.anyorDispatchSignal.fromAbortSignal. It holds no listeners of its own: it forwards each listener to its sources and reads them when polled, so one nobody listens to holds no subscription. - Root boundary: the panic boundary of the controller whose dispatch runs a derived signal's listener. A derived signal needs no runtime of its own.
- Map (
signal.map(mapping)): a signal ofmapping(value). It remembers the mapped value, so the mapping runs once however many listeners and polls read it. - Poisoning: what a throwing mapping does to its mapped signal, like Rust's
LazyLock. The throw goes to whoever ran the mapping first, and every later read panics with a poison panic. - Poison panic (
Panic (Poisoned)): the panic a read of a poisoned signal throws or reports, whose cause is the mapping's original throw. Each reader gets its own. - Zip (
signal.zip(other)): a signal of both values as a pair, once both have dispatched. It holds only its first source until that one fires, then only its second. - Any (
DispatchSignal.any(signals)): a signal of the first value dispatched among its sources. It decides the first time it sees a dispatch, picking the earliest source in the array that is Ready then.any([])never dispatches. - Dispatched (
DispatchSignal.dispatched({ value })): a signal already dispatched withvalue, with no controller. Its listeners run in the caller's call, so a throw propagates to that caller. - Never (
DispatchSignal.never()): a signal that never dispatches, with no controller.
Bridges
- Wait (
signal.wait()): the value as aPromise, subscribed until the dispatch. - To vow (
signal.toVow()): the value as a new mainVowon each call, resolved inside the dispatch with no microtask in between. - To abort signal (
signal.toAbortSignal()): anAbortSignalthat aborts with the dispatched value as itsreason, forfetchand other platform APIs. It aborts at once for a signal that has already dispatched. - From abort signal (
DispatchSignal.fromAbortSignal({ signal, panics })): a derived signal of anAbortSignal's abort reason, typedunknown. It adds a platform listener only while it has listeners, and runs them insidepanics.