@toebean/signals
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

signals 🚥

A collection of wrappers and utility functions for working with AbortSignals.

npm package version npm package downloads typedocs license

coverage code quality minzip tree-shaking

npm test publish code coverage publish package publish docs

github twitter GitHub Sponsors donation button PayPal donation button

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.

Package Sidebar

Install

npm i @toebean/signals

Weekly Downloads

4

Version

3.0.0

License

MIT

Unpacked Size

36.5 kB

Total Files

11

Last publish

Collaborators

  • toebean