1.0.13 • Published 6 years ago

ubus v1.0.13

Weekly downloads
50
License
MIT
Repository
github
Last release
6 years ago

μ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(token: string, callback: Function): () => 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(token: string, callback: Function):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(token: string, 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(token: string | 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(token: string, cb: Function): () => 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

1.0.13

6 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.4

9 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.14

10 years ago

0.0.13

10 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago