arbitrary-emitter
Event emitter with Map/Set sugar for browser and node.js apps
arbitrary-emitter allows to use arbitrary values as keys for your events
arbitrary-emitter is written in vanilla ES6, so maybe you want to transpile it before using it.
Project in active development, API may change
Create a new emitter
const emitter = arbitraryEmitter()
Emitter API
on(key, method)
Add a listener for key
which will trigger method
function.
key
can be any type of value.
on
returns unsubscribe method
const obj = {}
let unsubscribe = emitter.on(obj, () => doSomething())
emitter.emit(obj)
unsubscribe()
emitter.emit(obj)
once(key, method)
Add a listener for key
which will trigger method
function just one time, then listener will be removed.
key
can be any type of value
const obj = {}
emitter.once(obj, () => doSomethingOnce())
emitter.emit(obj)
emitter.emit(obj)
emitter.emit(key[, ...args])
emit methods binded to key
, and pass the rest of arguments to it
emitter.on('test', (a, b) => console.log(a + b))
emitter.emit('test', 1, 2)
emitter.off(key[, action])
Remove action
from listener key
. If no action is specified will remove all listeners binded to key
emitter.off(key, action)
emitter.off(key)
Testing
Node
npm test
© 2016 Jacobo Tabernero - Released under MIT License