events 
An event emitter and helpers
featuring
- 0 production dependencies
- CJS and ESM versions
- 469 bytes minified and gzipped
install
npm i -S @nichoth/events
example
create an event bus
import { Bus } from '@nichoth/events'
const bus = new Bus()
const bus2 = new Bus(['valid', 'events'])
Bus.flatten
Get an array of the leaf node values of an object of any shape, for example the return value of Bus.createEvents.
It's recommended to use the .flatten static function to get the event name values after calling .createEvents. Or, if you pass in anything that is not an array, the constructor will call .flatten on it.
import { Bus } from '@nichoth/events'
const events = Bus.createEvents({
a: {
_: [1, 2, 3]
b: {
c: [1,2,3]
}
}
})
const bus = new Bus(Bus.flatten(events))
const bust2 = new Bus(events)
create namespaced events
Take an object of arrays of strings, and return a new object where the leaf nodes are strings containing the full object path.
import { Bus } from '@nichoth/events'
Bus.createEvents({
a: {
_: [1, 2, 3]
b: {
c: [1,2,3]
}
}
})
subscribe
import { Bus } from '@nichoth/events'
const bus = new Bus()
const off = bus.on(events.a['1'], (data) => {
t.equal(data, 'test data', 'first listener gets the event')
off()
})
emit events
import { Bus } from '@nichoth/events'
const bus = new Bus()
bus.emit(events.a['1'], 'test data')
You can partially apply the the .emit function
const emitFoo = bus.emit('foo')
bus.on('foo', data => {
console.log(data)
})
emitFoo({ example: 'data' })
develop
Install dev deps with --legacy-peer-deps.
npm i --legacy--peer-deps
test
npm test