@hmans/signal
v0.2.2
Published
A super duper simple signal implementation.
Downloads
860
Maintainers
Readme
@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()