@jrc03c/emitter
v0.0.6
Published
a small event emitter helper class
Downloads
12
Readme
intro
a small event emitter class
installation
npm install --save @jrc03c/emitterusage
import { Emitter } from "@jrc03c/emitter"
const emitter = new Emitter()
emitter.on("random-number", v => {
console.log(v)
})
emitter.emit("random-number", Math.random())api
Emitter
Emitter() (constructor)
takes no arguments.
properties
subscriptions
an object whose key-value pairs represent channel names and arrays of callbacks, respectively.
methods
emit(channel, value1, value2, value3, ...)
calls all of the callbacks subscribed to channel and passes to them as arguments the values value1, value2, value3, ...
off(channel, callback)
unsubscribes callback from channel.
on(channel, callback)
subscribes callback to channel. returns a function that, when called, will unsubscribe callback from channel. for example:
import { Emitter } from "@jrc03c/emitter"
const emitter = new Emitter()
const unsubscribe = emitter.on("just-once", () => {
console.log("once!")
unsubscribe()
})
for (let i = 0; i < 100; i++) {
emitter.emit("just-once")
}