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
- getEventBus
- removeEventBus
- hasEventBus
EventBus
EventBus flexible event bus emit/pubsub
getBus
Returns EventEmitter
getBusName
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, EventEmitter
s 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
-
maxListeners
number
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
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 Symbol
s
Returns Array<EventName>
matchedEventNames
Returns a matched array listing the events for which the emitter has registered
listeners. The values in the array are strings or Symbol
s
Parameters
-
eventName
EventName
Returns Array<EventName>
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
-
options
Object?
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