arbitrary-emitter
High performance event emitter with Map/Set sugar for node.js and browsers (<500 bytes when gzipped)
arbitrary-emitter stores listeners and actions in ES6 Map and Sets, this allows to use arbitrary values as keys for your listeners
arbitrary-emitter is written in vanilla ES6, so you will have to transpile it before using it in old browsers or node.js < v5.9
Create a new emitter
const emitter = arbitraryEmitter()
Emitter API
on(key, action)
Add a listener with key
which will trigger action
function.
key
can be any type of value.
on
returns unsubscribe method
const obj = {}
let removeAction = emitter.on(obj, () => doSomething())
emitter.emit(obj)
removeAction()
emitter.emit(obj)
once(key, action)
Add a listener for key
which will trigger action
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)
emit(key[, ...args])
emit
methods binded to key
, and pass the rest of the arguments to it
emitter.on('test', (a, b) => console.log(a + b))
emitter.emit('test', 1, 2)
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
Browser
Build browser tests (npm run build-tests
) and open test/test.html
Building
- Build UMD file:
npm run build-umd
- Build browser tests:
npm run build-tests
- Run both builds:
npm run build
© 2016 Jacobo Tabernero - Released under MIT License