Delightful bus
Event bus with more features
Installation
$ yarn add delightful-bus
const bus =
Classic methods
.emit(evt, ...args)
- emit event
bus
.on(evt, cb)
- add event listener
bus// => bar baz (According to previous example)
.once(evt, cb)
- add self-destructable event listener
bus bus// => bar baz bus// => nothing (event was removed)
.off(evt, cb)
- remove event listener
bus bus// => nothing because listener is removed
More fun
.onMany(events)
- add many events
bus
.offAll()
- remove all listeners
bus
Extending and inheritance
.sendTo(bus)
- send events to another bus
const bus1 = const bus2 = bus1 bus1 bus2// => bar baz (bus2 got events from bus1)
.fork()
- fork bus
const bus1 = bus1 const bus2 = bus bus2// => bar (events are inherited from bus1) busbus2// => nothing (because event was send to bus1 after fork) bus2bus// => nothing (because event was send to bus2 after fork)
.merge(bus2)
- fork current bus with of events of both (bus and bus2) buses
const bus = bus const bus2 = bus const bus3 = busbus3// => bar (event from bus)bus3// => bar (event from bus2) bus3bus// => nothing (it's a fork)bus2// => nothing (it's a fork)
.injectTo(instance)
- adds .on
, .off
, .onMany
, .offMany
, .offAll
and .emit
methods of forked bus to some instance
const smth = {} const bus = bus bussmthsmth// => bar bus// => bar (inherited from bus) bussmth// => nothing (it's a fork) bus// => nothing (it's a fork)
.injectObserverTo(newInstance)
- adds only .on
and .off
methods
const smth = {} const bus = bus bussmthsmth// => bar bus// => bar (inherited from bus) bussmth// => nothing (it's a fork) bus// => nothing (it's a fork)