μBus
Install
$ npm i --save ubus
What is this?
μBus (micro bus) is a micro implementation of a message bus/event emitter in javascript. It was created to be light, fast and intuitive - it's less than 500 bytes
when gzipped (es2015 module).
The API is minimal and straight forward. There's one
class and four
methods - nothing more, nothing less.
You can use it anywhere javascript is being used, both server and client side.
Also, given there are a lot of different projects out there, μBus
has five
different module loaders
for you to play with:
es2015
;commonjs
;systemjs
;amd
;umd
.
Pick the one that fits your project and have fun!
API
on
bus.ontoken: string, callback: Function:void
const Bus = ; let bus = ; bus; bus;
once
bus.oncetoken: string, callback: Function:void
const Bus = ; let bus = ; bus; bus;
emit
bus.emittoken: string, info?: any:void
const Bus = ; let bus = ; bus; bus; bus; bus;
off
bus.offtoken: string | string:void
const Bus = ; let bus = ; bus; bus; bus; // will trigger the event bus; // will trigger the event bus; bus; /* or bus.off([ 'my-event', 'my-other-event' ]); */ bus; // won't trigger the event, nobody listening bus; // won't trigger the event, nobody listening
destroy function
const Bus = ; let bus = ; let _destroyMyEvent = bus; let _destroyMyOtherEvent = bus; bus; // triggers the event bus; // triggers the event ; // destroys 'my-event' ; // destroys 'my-other-event' bus; // triggers nothing, no one listening bus; // triggers nothing, no one listening
Wiki
For more information on how to integrate with existing projects, Benchmarks, FAQ, Troubleshooting and other stuff, take a look at the wiki.
Inspired by
- Node.js's EventEmitter.
- socket.io's simple API;
- angular (1.x)'s implementation of
$emit/$broadcast
and$on
; - minibus's compact API.
LICENSE
MIT