A super duper simple signal implementation.




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!)
- miniplex
- controlfreak
- Trinity
``tsx
import { Signal } from "@hmans/signal"
const signal = new Signal
signal.add((n) => console.log(n))
signal.emit()
`
Callbacks are added through add and removed through remove.
`tsx`
const callback = (n) => console.log(n)
signal.add(callback)
signal.remove(callback)
clear discards all registered listeners:
`tsx`
signal.clear()
Signals optionally accept a listener through their constructor (just a bit of syntactical sugar for convenience):
`tsx`
const signal = new Signal(() => console.log("I've been signalled!"))
signal.emit()
Interactions with Signal instances can be chained:
`tsxHello ${name}!
new Signal
.add((name) => console.log())Hi again ${name}!
.add((name) => console.log())``
.emit()