EventBus flexible event bus emit/pubsub weighs less than 1KB. Wildcard support. Based on Node's EventEmitter
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')
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
- EventBus
- getEventBus
- removeEventBus
- hasEventBus
EventBus flexible event bus emit/pubsub
Returns EventEmitter
Remove EventBus instance and corresponding EventEmitter
Alias for addListener(eventName, listener)
addListener
Returns EventBus
Adds a one-timelistener
function for the event named eventName
. The
next time eventName
is triggered, this listener is removed and then invoked
Returns EventBus
Alias for removeListener(eventName, listener)
removeListener
Returns EventBus
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
Returns EventBus
Removes the specified listener
from the listener array for the event namedeventName
Returns EventBus
Removes all listeners, or those of the specified eventName
Returns void
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
maxListeners
number
Returns EventBus
Returns the current max listener value for the EventEmitter
which is either
set by emitter.setMaxListeners(n)
Returns number
Returns a copy of the array of listeners for the event named eventName
Synchronously calls each of the listeners registered for the event
namedeventName
, in the order they were registered, passing the
supplied arguments to each
Returns Map<(string | Symbol), boolean>
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>
Returns a matched array listing the events for which the emitter has registered
listeners. The values in the array are strings or Symbol
s
eventName
EventName
Returns Array<EventName>
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
Returns number
Get EventBus instance
-
options
Object?
Returns EventBus
Returns boolean
Return specific EventEmitter by name
Returns EventEmitter
Remove specific EventEmitter by name
Returns void
Check if specific EventEmitter by name exists
Returns boolean