Git Product home page Git Product logo

signals's Introduction

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 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.

signals's People

Contributors

dependabot[bot] avatar toebeann avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.