ubus
TypeScript icon, indicating that this package has built-in type declarations

1.0.13 • Public • Published

μBus

Build Status npm version

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.on(tokenstring, callbackFunction): () => void
  const {Bus} = require('ubus');
  let bus = new Bus();
 
  bus.on('my-event', () => {
    console.log('yo!'); // logs 'yo!' every time this Bus instance emits 'my-event'
  });
 
  bus.on('my-other-event', (info) => {
    console.log(info); // logs info every time this Bus instance emits 'my-other-event'
  });

once

  bus.once(tokenstring, callbackFunction):void
  const {Bus} = require('ubus');
  let bus = new Bus();
 
  bus.once('my-event', () => {
    console.log('yo!'); // logs 'yo!' only once per bus instance
  });
 
  bus.once('my-other-event', (info) => {
    console.log(info); // logs 'yo!' only once per bus instance
  });

emit

  bus.emit(tokenstring, info?: any):void
  const {Bus} = require('ubus');
  let bus = new Bus();
 
  bus.on('my-event', () => {
    console.log('yo!'); // logs 'yo!' when 'my-event' is called
  });
 
  bus.on('my-other-event', (info) => {
    console.log(info); // logs { yo: true } when 'my-other-event' is called
  });
 
  bus.emit('my-event');
  bus.emit('my-other-event', { yo: true });

off

  bus.off(tokenstring | string[]):void
  const {Bus} = require('ubus');
  let bus = new Bus();
 
  bus.on('my-event', () => {
    console.log('yo!'); // logs 'yo!' when 'my-event' is emitted
  });
 
  bus.on('my-other-event', (info) => {
    console.log(info); // logs { yo: true } when 'my-other-event' is emitted
  });
 
 
  bus.emit('my-event'); // will trigger the event
  bus.emit('my-other-event', { yo: true }); // will trigger the event
 
  bus.off('my-event');
  bus.off('my-other-event');
 
  /* or
  bus.off([
    'my-event',
    'my-other-event'
  ]);
  */
 
  bus.emit('my-event'); // won't trigger the event, nobody listening
  bus.emit('my-other-event', { yo: true }); // won't trigger the event, nobody listening

destroy function

  let destroyFn = bus.on(tokenstring, cbFunction): () => void
  const {Bus} = require('ubus');
  let bus = new Bus();
 
  let _destroyMyEvent = bus.on('my-event', () => {
    console.log('yo!'); // logs 'yo!' when 'my-event' is called
  });
 
  let _destroyMyOtherEvent = bus.on('my-other-event', (info) => {
    console.log(info); // logs { yo: true } when 'my-other-event' is called
  });
 
  bus.emit('my-event'); // triggers the event
  bus.emit('my-other-event', { yo: true }); // triggers the event
 
  _destroyMyEvent(); // destroys 'my-event'
  _destroyMyOtherEvent(); // destroys 'my-other-event'
 
  bus.emit('my-event'); // triggers nothing, no one listening
  bus.emit('my-other-event', { yo: true }); // 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

LICENSE

MIT

Package Sidebar

Install

npm i ubus

Weekly Downloads

5

Version

1.0.13

License

MIT

Unpacked Size

19.4 kB

Total Files

13

Last publish

Collaborators

  • ericmdantas