triggerbus
v4.0.0
Published
Simple eventbus you can trigger in ~300 bytes
Downloads
102
Maintainers
Readme
🚌 triggerbus
Simple Typescript eventbus that uses trigger
to broadcast events. Works in any environment.
import triggerbus from 'triggerbus'
const bus = triggerbus()
bus.on('event', console.log)
bus.trigger('event', { foo: 42 })
// => log('event', { foo: 42 })
Installation
yarn add triggerbus
npm install triggerbus --save
Usage
import triggerbus from 'triggerbus'
const bus = triggerbus()
const off1 = bus.on('name', callback)
// => off1() unregisters callback from name
// alternatively: bus.off('name', callback)
const off2 = bus.once('name', callback)
// => off2() unregisters callback from name
// automatically unregisters after calling callback once
bus.off('name')
// => removes all listeners of name
bus.off('name', callback)
// => removes first listener that matches callback
// does *not* remove "once" listener
bus.trigger('name', { foo: 42 })
// => triggers all listeners on 'name' and '*' with ('name', { foo: 42 })