@hmans/signal
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

Tests Version Downloads Bundle Size

@hmans/signal

A super duper simple signal implementation that I use in many of my other projects. It doesn't do anything terribly exciting, but provides some API niceties for added convenience.

Signals represent distinct signals -- or events -- that allow interested parties to add callbacks to them that will be evoked whenever the signal is emitted. Unlike eventemitter and friends, distinct signals are expressed as separate instances -- there is no key-based routing (in fact, there aren't even any keys to begin with!)

Projects using @hmans/signal

Usage

import { Signal } from "@hmans/signal"

const signal = new Signal<number>()
signal.add((n) => console.log(n))
signal.emit()

Callbacks are added through add and removed through remove.

const callback = (n) => console.log(n)
signal.add(callback)
signal.remove(callback)

clear discards all registered listeners:

signal.clear()

Signals optionally accept a listener through their constructor (just a bit of syntactical sugar for convenience):

const signal = new Signal(() => console.log("I've been signalled!"))
signal.emit()

Interactions with Signal instances can be chained:

new Signal<string>()
  .add((name) => console.log(`Hello ${name}!`))
  .add((name) => console.log(`Hi again ${name}!`))
  .emit()

Package Sidebar

Install

npm i @hmans/signal

Weekly Downloads

88

Version

0.2.2

License

MIT

Unpacked Size

17.1 kB

Total Files

14

Last publish

Collaborators

  • hendrik.mans