A generic multi-level event emitter with granular control over event emitting level.
yarn add @magic8bot/event-bus
or
npm i @magic8bot/event-bus
Method | Return | Example |
---|---|---|
new EventBus |
EventBus |
const eventBus = new EventBus() |
get |
EventBus |
eventBus.get('eventName') |
eventBus.get('eventName')('foo') |
||
eventBus.get('eventName')('foo')('bar') |
||
emit |
void |
eventBus.emit(data) |
eventBus.get('eventName').emit(data) |
||
eventBus.get('eventName')('foo').emit(data) |
||
listen |
unlisten() |
eventBus.listen(listenerFn) |
eventBus.get('eventName').listen(listenerFn) |
||
eventBus.get('eventName')('foo').listen(listenerFn) |
||
eventBus.get('eventName')('foo')('bar').listen(listenerFn) |
Creates a new root
event bus node.
Returns a new child node EventBus
.
- Can be curried infinitely.
Will emit
an event to it's level
and all descendant nodes.
Will listen
to events on it's level
and all ancestors leading to root
.
The return value is an unlisten
function. When invoked, it will stop this
listener from receiving any more events.
The most basic single level event bus.
import {} from '@magic8bot/event-bus'
const eventBus = new EventBus()
const emitter = eventBus.emit
const listener = eventBus.listen
listener((data) => {
// ...Do something with data
})
listener((data) => {
// ...Do something else with data
})
emitter({ foo: 'bar' })
emitter({ fizz: 'bang' })
A dual level tiered event bus.
- All
root
level events will bubble up. - Events can be granular and be sent only into a specific level.
import {} from '@magic8bot/event-bus'
const eventBus = new EventBus()
const emitter = eventBus.emit
const listener = eventBus.listen
const eventBusL2 = eventBus.get('L2')
const emitterL2 = eventBusL2.emit
const listenerL2 = eventBusL2.listen
// Will onlt get events from `emitter`
listener((data) => {
// ...Do something with data
})
// Will get events from `emitter` and `emitter -> emitterL2`
listenerL2((data) => {
// ...Do something else with data
})
emitter({ foo: 'bar' })
emitterL2({ fizz: 'bang' })
import {} from '@magic8bot/event-bus'
const eventBus = new EventBus()
// Will onlt get events from `eventBus`
eventBus.listen((data) => {
// ...Do something with data
})
// Will get events from `eventBus` and `eventBus -> L2`
eventBus.get('L2').listen((data) => {
// ...Do something else with data
})
eventBus.emit({ foo: 'bar' })
eventBus.get('L2').emit({ fizz: 'bang' })
A tree like tiered event bus.
- All
root
level events will bubble up. - Events can be granular and be sent only into a specific level.
- Each level can act as another
root
import {} from '@magic8bot/event-bus'
const eventBus = new EventBus()
// Will onlt get events from `eventBus`
eventBus.listen((data) => {
// ...Do something with data
})
// Will get events from `eventBus` and `eventBus -> L2.1`
eventBus.get('L2.1').listen((data) => {
// ...Do something else with data
})
// Will get events from `eventBus` and `eventBus -> L2.2`
eventBus.get('L2.2').listen((data) => {
// ...Do more with data
})
// Will get events from `eventBus`, `eventBus -> L2.2`, and `eventBus -> L2.2 -> L3`
eventBus
.get('L2.2')('L3')
.listen((data) => {
// ...Do even more with data
})
eventBus.emit({ foo: 'bar' })
eventBus.get('L2.1').emit({ fizz: 'bang' })
eventBus.get('L2.2').emit({ blep: 'cat' })
eventBus.get('L2.2')('L3').emit({ derp: 'dog' })