@toebean/signals
v3.0.0
Published
A collection of wrappers and utility functions for working with AbortSignals.
Downloads
18
Maintainers
Readme
signals 🚥
A collection of wrappers and utility functions for working with AbortSignals.
Table of contents
Install
npm
npm i @toebean/signals
Usage
AggregateSignal
Combines several AbortSignal
instances into a signal which will be aborted as soon as any of the given signals are.
import { AggregateSignal } from "@toebean/signals";
const ac = new AbortController();
const aggregateSignal = new AggregateSignal(ac.signal, someOtherSignal);
// passing an aggregate signal into an awaitable, abortable call:
await doSomeAbortableAction({ signal: aggregateSignal.signal });
// determining which of the original signals was aborted first:
switch (aggregateSignal.abortedSignal) {
case ac.signal:
// do stuff
break;
// etc...
}
TimeoutSignal
Creates an AbortSignal
which will timeout after a given number of milliseconds, using setTimeout
under the hood.
import { AggregateSignal } from "@toebean/signals";
const timeoutSignal = new TimeoutSignal(200); // creates an AbortSignal which will abort in 200ms
// passing a timeout signal into an awaitable, abortable call:
await doSomeAbortableAction({ signal: timeoutSignal.signal });
// If for whatever reason you need to clear the underlying timeout of the TimeoutSignal, you can:
clearTimeout(timeoutSignal.timeout);
isAbortSignal
A TypeScript type guard for checking whether a given object is an AbortSignal
.
import { isAbortSignal } from "@toebean/signals";
if (isAbortSignal(someObject)) {
// within this block, someObject is typed as an AbortSignal
console.log(someObejct.aborted);
}
API reference
The full API reference for signals is available on GitHub Pages.
Quick links
License
signals is licensed under MIT © 2022 Tobey Blaber.