Giter Site home page Giter Site logo

any-signal's Introduction

any-signal

codecov CI

Combines an array of AbortSignals into a single signal that is aborted when any signal is

Table of contents

Install

$ npm i any-signal

Browser <script> tag

Loading this module through a script tag will make it's exports available as AnySignal in the global namespace.

<script src="https://unpkg.com/any-signal/dist/index.min.js"></script>

Usage

import { anySignal } from 'any-signal'

const userController = new AbortController()
const timeoutController = new AbortController()

const combinedSignal = anySignal([userController.signal, timeoutController.signal])
combinedSignal.addEventListener('abort', () => console.log('Abort!'))

// Abort after 1 second
const timeoutId = setTimeout(() => timeoutController.abort(), 1000)

// The user or the timeout can now abort the action
await performSomeAction({ signal: combinedSignal })
clearTimeout(timeoutId)

// Clear will clean up internal event handlers
combinedSignal.clear()

API

anySignal(signals)

Parameters

Name Type Description
signals Array<AbortSignal> The Signals that will be observed and mapped to the returned Signal

Returns

Type Description
ClearableSignal A Signal that will be aborted as soon as any one of its parent signals are aborted. Extends AbortSignal with the clear function for cleanup

The returned ClearableSignal will only be aborted once, and as soon as one of its parent signals is aborted.

ClearableSignal.clear()

Removes all internal event handlers. This must be called after abort has been called, or the signals have successfully executed, otherwise there is a risk of leaking event handlers.

Acknowledgements

The anySignal function is taken from a comment by jakearchibald

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

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.