1.0.0 • Published 7 months ago

@rsol/ebus v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

EventBus

EventBus flexible event bus emit/pubsub weighs less than 1KB. Wildcard support. Based on Node's EventEmitter

Table of Contents

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

$ npm install --save @rsol/ebus

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import EventBus, { getEventBus, removeEventBus, hasEventBus } from '@rsol/ebus'

// using CommonJS modules
const EventBus = require('@rsol/ebus')

Usage

const global = EventBus.getInstance();

const bus = EventBus.getInstance('Bus', {
  wildcard: true,
  maxListeners: 5
});

let i = 1;
const addF = (add: number) => i += add;
const subF = (sub: number) => i -= sub;
const mulF = (mul: number) => i *= mul;
const divF = (div: number) => i /= div;

bus
  .on('wildcard.add', addF)
  .on('wildcard.sub', subF)
  .on('wildcard.mul', mulF)
  .on('wildcard.div', divF)
  .emit('wildcard.*', 10);
console.log(i); // 1

bus.emit('wildcard.add', 2);
console.log(i); // 3

const result = bus
  .removeListener('wildcard.div', divF)
  .emit('wildcard.*', 10);
console.log(i); // 30

console.log(result);
/**
 * Map(3) {
 'wildcard.add' => true,
 'wildcard.sub' => true,
 'wildcard.mul' => true
 }
 */

console.log(bus.listenerCount('wildcard.*')); //3 

bus.removeAllListeners('wildcard.\[div|mul\]')

console.log(wildcardEb.eventNames());//['wildcard.add', 'wildcard.sub']

To find more examples see test folder. For more methods see API

API

Table of Contents

EventBus

EventBus flexible event bus emit/pubsub

getBus

Returns EventEmitter

getBusName

Returns (string | Symbol)

close

Remove EventBus instance and corresponding EventEmitter

on

Alias for addListener(eventName, listener) addListener

Parameters

Returns EventBus

once

Adds a one-timelistener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked

Parameters

Returns EventBus

off

Alias for removeListener(eventName, listener) removeListener

Parameters

Returns EventBus

addListener

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times

Parameters

Returns EventBus

removeListener

Removes the specified listener from the listener array for the event namedeventName

Parameters

Returns EventBus

removeAllListeners

Removes all listeners, or those of the specified eventName

Parameters

Returns void

setMaxListeners

By default, EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps to find memory leaks. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance

Parameters

Returns EventBus

getMaxListeners

Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n)

Returns number

listeners

Returns a copy of the array of listeners for the event named eventName

Parameters

Returns Array[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)

emit

Synchronously calls each of the listeners registered for the event namedeventName, in the order they were registered, passing the supplied arguments to each

Parameters

Returns Map<(string | Symbol), boolean>

eventNames

Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols

Returns Array\

matchedEventNames

Returns a matched array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols

Parameters
  • eventName EventName

Returns Array\

listenerCount

Returns the number of listeners listening for the event named eventName. If listener is provided, it will return how many times the listener is found in the list of the listeners of the event

Parameters

Returns number

getInstance

Get EventBus instance

Parameters
  • name (string | Symbol) (optional, default 'GLOBAL')
  • options Object?

    • options.wildcard boolean Is use wildcard for emit, listenerCount and removeAllListeners methods (optional, default false)
    • options.maxListeners number Max number of listeners (optional, default 50)

Returns EventBus

hasBus

Parameters

Returns boolean

getEventBus

Return specific EventEmitter by name

Parameters

Returns EventEmitter

removeEventBus

Remove specific EventEmitter by name

Parameters

Returns void

hasEventBus

Check if specific EventEmitter by name exists

Parameters

Returns boolean

License

MIT License © Jason Miller