gossamer/abort_signal
Accept an AbortSignal in cancelable operations like
fetch_extra.send, then observe its state via
is_aborted / reason or await
cancellation via on_abort. Signals are produced by
new (paired with an abort function) or by the static
constructors abort, any, and
timeout.
Types
Why a signal aborted. JavaScript-supplied aborts classify by the
underlying value — an AbortError DOMException (including the
no-argument controller.abort() default) surfaces as Default, a
TimeoutError DOMException as Timeout, and any other value as
Reason(value).
pub type AbortReason {
Default
Timeout
Reason(value: dynamic.Dynamic)
}
Constructors
-
DefaultAborted with an
AbortErrorDOMException, the default when no specific reason is given. -
TimeoutAborted because a
timeoutsignal expired. -
Reason(value: dynamic.Dynamic)
A signal that communicates when an operation should be aborted.
See AbortSignal on MDN.
pub type AbortSignal
Values
pub fn abort(reason: AbortReason) -> AbortSignal
Returns an AbortSignal that is already aborted with reason.
Useful for short-circuiting operations that accept a signal
without wiring up a controllable signal. Equivalent to
JavaScript’s static AbortSignal.abort.
pub fn any(signals: List(AbortSignal)) -> AbortSignal
Returns an AbortSignal that becomes aborted as soon as any of
signals does, with the same reason as the triggering signal.
Equivalent to JavaScript’s static AbortSignal.any.
pub fn is_aborted(signal: AbortSignal) -> Bool
True once the signal has been aborted. Equivalent to JavaScript’s
signal.aborted.
pub fn new() -> #(AbortSignal, fn(AbortReason) -> Nil)
Returns a fresh AbortSignal paired with a function that aborts
it. Pass the signal to cancelable operations; call the abort
function with an AbortReason when it’s time to
cancel. The reason flows through to readers via
reason and on_abort.
pub fn on_abort(
signal: AbortSignal,
) -> promise.Promise(AbortReason)
Resolves with the classified abort reason when signal aborts,
or immediately if signal is already aborted. Each call returns
an independent Promise, so multiple awaiters can each receive
the notification. Equivalent to listening once for JavaScript’s
abort event and reading signal.reason.
pub fn reason(signal: AbortSignal) -> Result(AbortReason, Nil)
The reason the signal was aborted with, classified by source.
Returns Error(Nil) while the signal has not been aborted.
pub fn timeout(duration: duration.Duration) -> AbortSignal
Creates an AbortSignal that aborts automatically after
duration with the Timeout reason. Negative durations are
treated as zero. Panics on Node for durations above
4_294_967_295 ms (the unsigned 32-bit millisecond bound); Deno
and Bun accept values up to 9_007_199_254_740_991. Equivalent to
JavaScript’s static AbortSignal.timeout.