Package Exports
- @hmans/signal
- @hmans/signal/dist/hmans-signal.cjs.js
- @hmans/signal/dist/hmans-signal.esm.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@hmans/signal) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
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()