3.0.0 • Published 1 year ago

@toebean/signals v3.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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.

2.5.1

1 year ago

3.0.0

1 year ago

2.5.0

2 years ago

2.4.1

2 years ago

2.4.0

2 years ago

2.4.3

2 years ago

2.4.2

2 years ago

2.4.5

2 years ago

2.4.4

2 years ago

2.3.5

2 years ago

2.3.4

2 years ago

2.3.3

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

2.3.0

2 years ago

2.3.0-alpha.4

2 years ago

2.3.0-alpha.3

2 years ago

2.3.0-alpha.2

2 years ago

2.3.0-alpha.1

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago