npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

@soncodi/signal

2.0.2 • Public • Published

Signal

Build Status Coverage Status Dependency Status npm version

Tiny signal utility for Node.js and browsers

Installation

npm install @soncodi/signal --save

Usage (TypeScript)

import { Signal } from '@soncodi/signal';
 
const signal = new Signal<number>();
 
const handler = (param: number) => {
  console.log(`signal fired ${param}`);
};
 
signal.on(handler);
 
signal.emit(123);
 
signal.off(handler);

Methods

on(fn: (arg: T) => void): this

Attaches a signal handler to be called whenever the signal fires.

once(fn: (arg: T) => void): this

Attaches a one-time handler which is unbound after it fires the first time.

off(fn?: (arg: T) => void): this

Detaches one instance of a given handler from the signal. If no handler is provided, detaches all handlers.

emit(arg: T): this

Fires the signal synchronously, triggering any attached handlers with the given arg.

event(arg: T): this

Fires the signal asynchronously, triggering any attached handlers with the given arg. Useful when attaching handlers later in the same event loop turn.

install

npm i @soncodi/signal

Downloadsweekly downloads

4

version

2.0.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability