A helpful, efficient and lightweight util - event emitter.
npm i util-eventemitter
or
npm i util-eventemitter --save
var { Emitter } = require('util-eventemitter');
var em = new Emitter();
var callbackFn = function () {};
//listener
em.on('foo', callbackFn);
em.addEventListener('foo', callbackFn);
//one time listener
em.once('bar', callbackFn);
em.addOneTimeListener('bar', callbackFn);
//emit
em.emit('foo', ...arg);
//remove listener
em.off('bar', callbackFn);
em.removeListener('bar', callbackFn);
//clear all listeners
em.removeAll();
em.removeAllListeners();
//check number of existing events
em.count();
//check number of listeners under the specific event
em.count('event name');
Alternatively, you can using emitter
directly as imported module without initialization, as it is initialized.
var { emitter } = require('util-eventemitter');
//no need to initialize, use it directly same as above
event
- the name of the eventfn
- a callback function to trigger when event is emitted(listener)
event
- the name of the eventfn
- a callback function to trigger when event is emitted
This listener only trigger listener only once
event
- the name of the event...payload
[optional] - a series of arguements pass to the listener
event
- the name of the eventfn
- a listener
event
[optional] - the name of the event
If event is skipped, returns number of events, otherwise it returns number of existing listeners for specific event
npm install
npm test
npm install
npm run build