EventEmitter2 is an implementation of the EventEmitter module found in Node.js. In addition to having a better benchmark performance than EventEmitter and being browser-compatible, it also extends the interface of EventEmitter with many additional non-breaking features.
If you like this project please show your support with a GitHub ⭐!
- ES5 compatible UMD module, that supports node.js, browser and workers of any kind
- Namespaces/Wildcards
- Any listeners
- Times To Listen (TTL), extends the
once
concept withmany
- Async listeners (using setImmediate|setTimeout|nextTick) with promise|async function support
- The emitAsync method to return the results of the listeners via Promise.all
- Subscription methods (on, once, many, ...) can return a listener object that makes it easy to remove the subscription when needed - just call the listener.off() method.
- Feature-rich waitFor method to wait for events using promises
- listenTo & stopListeningTo methods for listening to an external event emitter of any kind and propagate its events through itself using optional reducers/filters
- Extended version of the events.once method from the node events API
- Browser & Workers environment compatibility
- Demonstrates good performance in benchmarks
Platform: win32, x64, 15267MB
Node version: v13.11.0
CPU: 4 x AMD Ryzen 3 2200U with Radeon Vega Mobile Gfx @ 2495MHz
----------------------------------------------------------------
EventEmitterHeatUp x 2,897,056 ops/sec ±3.86% (67 runs sampled)
EventEmitter x 3,232,934 ops/sec ±3.50% (65 runs sampled)
EventEmitter2 x 12,261,042 ops/sec ±4.72% (59 runs sampled)
EventEmitter2 (wild) x 242,751 ops/sec ±5.15% (68 runs sampled)
EventEmitter2 (wild) using plain events x 358,916 ops/sec ±2.58% (78 runs sampled)
EventEmitter2 (wild) emitting ns x 1,837,323 ops/sec ±3.50% (72 runs sampled)
EventEmitter2 (wild) emitting a plain event x 2,743,707 ops/sec ±4.08% (65 runs sampled)
EventEmitter3 x 10,380,258 ops/sec ±3.93% (67 runs sampled)
Fastest is EventEmitter2
To find out what's new see the project CHANGELOG
- The EventEmitter2 constructor takes an optional configuration object with the following default values:
var EventEmitter2 = require('eventemitter2');
var emitter = new EventEmitter2({
// set this to `true` to use wildcards
wildcard: false,
// the delimiter used to segment namespaces
delimiter: '.',
// set this to `true` if you want to emit the newListener event
newListener: false,
// set this to `true` if you want to emit the removeListener event
removeListener: false,
// the maximum amount of listeners that can be assigned to an event
maxListeners: 10,
// show event name in memory leak message when more than maximum amount of listeners is assigned
verboseMemoryLeak: false,
// disable throwing uncaughtException if an error event is emitted and it has no listeners
ignoreErrors: false
});
- Getting the actual event that fired.
emitter.on('foo.*', function(value1, value2) {
console.log(this.event, value1, value2);
});
emitter.emit('foo.bar', 1, 2); // 'foo.bar' 1 2
emitter.emit(['foo', 'bar'], 3, 4); // 'foo.bar' 3 4
emitter.emit(Symbol(), 5, 6); // Symbol() 5 6
emitter.emit(['foo', Symbol()], 7, 8); // ['foo', Symbol()] 7 8
Note: Generally this.event is normalized to a string ('event', 'event.test'), except the cases when event is a symbol or namespace contains a symbol. In these cases this.event remains as is (symbol and array).
- Fire an event N times and then remove it, an extension of the
once
concept.
emitter.many('foo', 4, function() {
console.log('hello');
});
- Pass in a namespaced event as an array rather than a delimited string.
emitter.many(['foo', 'bar', 'bazz'], 4, function() {
console.log('hello');
});
$ npm install eventemitter2
Or you can use unpkg.com CDN to import this module as a script directly from the browser
Event
: string | symbolEventNS
: string | Event []
-
emitAsync(event: event | eventNS, ...values: any[]): Promise<any[]>
-
addListener(event: event | eventNS, listener: ListenerFn, boolean|options?: object): this|Listener
-
on(event: event | eventNS, listener: ListenerFn, boolean|options?: object): this|Listener
-
once(event: event | eventNS, listener: ListenerFn, boolean|options?: object): this|Listener
-
removeListener(event: event | eventNS, listener: ListenerFn): this
-
waitFor(event: event | eventNS, timeout?: number): CancelablePromise<any[]>
-
waitFor(event: event | eventNS, filter?: WaitForFilter): CancelablePromise<any[]>
-
waitFor(event: event | eventNS, options?: WaitForOptions): CancelablePromise<any[]>
-
listenTo(target: GeneralEventEmitter, event: event | eventNS, options?: ListenToOptions): this
-
listenTo(target: GeneralEventEmitter, events: (event | eventNS)[], options?: ListenToOptions): this
-
stopListeningTo(target?: GeneralEventEmitter, event?: event | eventNS): Boolean
The event
argument specified in the API declaration can be a string or symbol for a simple event emitter
and a string|symbol|Array(string|symbol) in a case of a wildcard emitter;
When an EventEmitter
instance experiences an error, the typical action is
to emit an error
event. Error events are treated as a special case.
If there is no listener for it, then the default action is to print a stack
trace and exit the program.
All EventEmitters emit the event newListener
when new listeners are
added. EventEmitters also emit the event removeListener
when listeners are
removed, and removeListenerAny
when listeners added through onAny
are
removed.
Namespaces with Wildcards
To use namespaces/wildcards, pass the wildcard
option into the EventEmitter
constructor. When namespaces/wildcards are enabled, events can either be
strings (foo.bar
) separated by a delimiter or arrays (['foo', 'bar']
). The
delimiter is also configurable as a constructor option.
An event name passed to any event emitter method can contain a wild card (the
*
character). If the event name is a string, a wildcard may appear as foo.*
.
If the event name is an array, the wildcard may appear as ['foo', '*']
.
If either of the above described events were passed to the on
method,
subsequent emits such as the following would be observed...
emitter.emit(Symbol());
emitter.emit('foo');
emitter.emit('foo.bazz');
emitter.emit(['foo', 'bar']);
emitter.emit(['foo', Symbol()]);
NOTE: An event name may use more than one wildcard. For example,
foo.*.bar.*
is a valid event name, and would match events such as
foo.x.bar.y
, or ['foo', 'bazz', 'bar', 'test']
A double wildcard (the string **
) matches any number of levels (zero or more) of events. So if for example 'foo.**'
is passed to the on
method, the following events would be observed:
emitter.emit('foo');
emitter.emit('foo.bar');
emitter.emit('foo.bar.baz');
emitter.emit(['foo', Symbol(), 'baz']);
On the other hand, if the single-wildcard event name was passed to the on method, the callback would only observe the second of these events.
Adds a listener to the end of the listeners array for the specified event.
emitter.on('data', function(value1, value2, value3, ...) {
console.log('The event was raised!');
});
emitter.on('data', function(value) {
console.log('The event was raised!');
});
Options:
-
async:boolean= false
- invoke the listener in async mode using setImmediate (fallback to setTimeout if not available) or process.nextTick depending on thenextTick
option. -
nextTick:boolean= false
- use process.nextTick instead of setImmediate to invoke the listener asynchronously. -
promisify:boolean= false
- additionally wraps the listener to a Promise for later invocation usingemitAsync
method. This option will be activated by default if its value isundefined
and the listener function is anasynchronous function
(whose constructor name isAsyncFunction
). -
objectify:boolean= false
- activates returning a listener object instead of 'this' by the subscription method.
The listener object has the following properties:
emitter: EventEmitter2
- reference to the event emitter instanceevent: event|eventNS
- subscription eventlistener: Function
- reference to the listeneroff(): Function
- removes the listener (voids the subscription)
var listener= emitter.on('event', function(){
console.log('hello!');
}, {objectify: true});
emitter.emit('event');
listener.off();
Note: If the options argument is true
it will be considered as {promisify: true}
Note: If the options argument is false
it will be considered as {async: true}
var EventEmitter2= require('eventemitter2');
var emitter= new EventEmitter2();
emitter.on('event', function(){
console.log('The event was raised!');
}, {async: true});
emitter.emit('event');
console.log('emitted');
Since the async
option was set the output from the code above is as follows:
emitted
The event was raised!
If the listener is an async function or function which returns a promise, use the promisify
option as follows:
var EventEmitter2= require('eventemitter2');
var emitter= new EventEmitter2();
emitter.on('event', function(){
console.log('The event was raised!');
return new Promise(function(resolve){
console.log('listener resolved');
setTimeout(resolve, 1000);
});
}, {promisify: true});
emitter.emitAsync('event').then(function(){
console.log('all listeners were resolved!');
});
console.log('emitted');
Output:
emitted
The event was raised!
listener resolved
all listeners were resolved!
If the promisify
option is false (default value) the output of the same code is as follows:
The event was raised!
listener resolved
emitted
all listeners were resolved!
Adds a listener to the beginning of the listeners array for the specified event.
emitter.prependListener('data', function(value1, value2, value3, ...) {
console.log('The event was raised!');
});
options:
options?
: See the addListener options
Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the callback.
emitter.onAny(function(event, value) {
console.log('All events trigger this.');
});
Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the callback. The listener is added to the beginning of the listeners array
emitter.prependAny(function(event, value) {
console.log('All events trigger this.');
});
Removes the listener that will be fired when any event is emitted.
emitter.offAny(function(value) {
console.log('The event was raised!');
});
Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed.
emitter.once('get', function (value) {
console.log('Ah, we have our first value!');
});
options:
options?
: See the addListener options
Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed. The listener is added to the beginning of the listeners array
emitter.prependOnceListener('get', function (value) {
console.log('Ah, we have our first value!');
});
options:
options?
: See the addListener options
Adds a listener that will execute n times for the event before being removed. The listener is invoked only the first n times the event is fired, after which it is removed.
emitter.many('get', 4, function (value) {
console.log('This event will be listened to exactly four times.');
});
options:
options?
: See the addListener options
Adds a listener that will execute n times for the event before being removed. The listener is invoked only the first n times the event is fired, after which it is removed. The listener is added to the beginning of the listeners array.
emitter.many('get', 4, function (value) {
console.log('This event will be listened to exactly four times.');
});
options:
options?
: See the addListener options
Remove a listener from the listener array for the specified event. Caution: Calling this method changes the array indices in the listener array behind the listener.
var callback = function(value) {
console.log('someone connected!');
};
emitter.on('get', callback);
// ...
emitter.removeListener('get', callback);
Removes all listeners, or those of the specified event.
By default EventEmitters will print a warning if more than 10 listeners are added to it. This is a useful default which helps finding memory leaks. Obviously not all Emitters should be limited to 10. This function allows that to be increased. Set to zero for unlimited.
Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to EventEmitter2.defaultMaxListeners
Returns an array of listeners for the specified event. This array can be manipulated, e.g. to remove listeners.
emitter.on('get', function(value) {
console.log('someone connected!');
});
console.log(emitter.listeners('get')); // [ [Function] ]
Returns an array of listeners that are listening for any event that is specified. This array can be manipulated, e.g. to remove listeners.
emitter.onAny(function(value) {
console.log('someone connected!');
});
console.log(emitter.listenersAny()[0]); // [ [Function] ]
Execute each of the listeners that may be listening for the specified event name in order with the list of arguments.
Return the results of the listeners via Promise.all. Only this method doesn't work IE.
emitter.on('get',function(i) {
return new Promise(function(resolve){
setTimeout(function(){
resolve(i+3);
},50);
});
});
emitter.on('get',function(i) {
return new Promise(function(resolve){
resolve(i+2)
});
});
emitter.on('get',function(i) {
return Promise.resolve(i+1);
});
emitter.on('get',function(i) {
return i+0;
});
emitter.on('get',function(i) {
// noop
});
emitter.emitAsync('get',0)
.then(function(results){
console.log(results); // [3,2,1,0,undefined]
});
Returns a thenable object (promise interface) that resolves when a specific event occurs
emitter.waitFor('event').then(function (data) {
console.log(data); // ['bar']
});
emitter.emit('event', 'bar');
emitter.waitFor('event', {
// handle first event data argument as an error (err, ...data)
handleError: false,
// the timeout for resolving the promise before it is rejected with an error (Error: timeout).
timeout: 0,
//filter function to determine acceptable values for resolving the promise.
filter: function(arg0, arg1){
return arg0==='foo' && arg1==='bar'
},
Promise: Promise, // Promise constructor to use,
overload: false // overload cancellation api in a case of external Promise class
}).then(function(data){
console.log(data); // ['foo', 'bar']
});
emitter.emit('event', 'foo', 'bar')
var promise= emitter.waitFor('event');
promise.then(null, function(error){
console.log(error); //Error: canceled
});
promise.cancel(); //stop listening the event and reject the promise
emitter.waitFor('event', {
handleError: true
}).then(null, function(error){
console.log(error); //Error: custom error
});
emitter.emit('event', new Error('custom error')); // reject the promise
Returns an array listing the events for which the emitter has registered listeners.
var emitter= new EventEmitter2();
emitter.on('foo', () => {});
emitter.on('bar', () => {});
emitter.on(Symbol('test'), () => {});
emitter.on(['foo', Symbol('test2')], () => {});
console.log(emitter.eventNames());
// Prints: [ 'bar', 'foo', [ 'foo', Symbol(test2) ], [ 'foo', Symbol(test2) ] ]
Note: Listeners order not guaranteed
Listens to the events emitted by an external emitter and propagate them through itself.
The target object could be of any type that implements methods for subscribing and unsubscribing to its events.
By default this method attempts to use addListener
/removeListener
, on
/off
and addEventListener
/removeEventListener
pairs,
but you able to define own hooks on(event, handler)
and off(event, handler)
in the options object to use
custom subscription API. In these hooks this
refers to the target object.
The options object has the following interface:
on(event, handler): void
off(event, handler): void
reducer: (Function) | (Object<Function>): Boolean
In case you selected the newListener
and removeListener
options when creating the emitter,
the subscription to the events of the target object will be conditional,
depending on whether there are listeners in the emitter that could listen them.
var EventEmitter2 = require('EventEmitter2');
var http = require('http');
var server = http.createServer(function(request, response){
console.log(request.url);
response.end('Hello Node.js Server!')
}).listen(3000);
server.on('connection', function(req, socket, head){
console.log('connect');
});
// activate the ability to attach listeners on demand
var emitter= new EventEmitter2({
newListener: true,
removeListener: true
});
emitter.listenTo(server, {
'connection': 'localConnection',
'close': 'close'
}, {
reducers: {
connection: function(event){
console.log('event name:' + event.name); //'localConnection'
console.log('original event name:' + event.original); //'connection'
return event.data[0].remoteAddress==='::1';
}
}
});
emitter.on('localConnection', function(socket){
console.log('local connection', socket.remoteAddress);
});
setTimeout(function(){
emitter.stopListeningTo(server);
}, 30000);
An example of using a wildcard emitter in a browser:
const ee= new EventEmitter2({
wildcard: true
});
ee.listenTo(document.querySelector('#test'), {
'click': 'div.click',
'mouseup': 'div.mouseup',
'mousedown': 'div.mousedown'
});
ee.on('div.*', function(evt){
console.log('listenTo: '+ evt.type);
});
setTimeout(function(){
ee.stopListeningTo(document.querySelector('#test'));
}, 30000);
Stops listening the targets. Returns true if some listener was removed.
Checks whether emitter has any listeners.
Returns the array of listeners for the event named eventName. In wildcard mode this method returns namespaces as strings:
var emitter= new EventEmitter2({
wildcard: true
});
emitter.on('a.b.c', function(){});
emitter.on(['z', 'x', 'c'], function(){});
console.log(emitter.eventNames()) // [ 'z.x.c', 'a.b.c' ]
If some namespace contains a Symbol member or the nsAsArray
option is set the method will return namespace as an array of its members;
var emitter= new EventEmitter2({
wildcard: true
});
emitter.on('a.b.c', function(){});
emitter.on(['z', 'x', Symbol()], function(){});
console.log(emitter.eventNames()) // [ [ 'z', 'x', Symbol() ], 'a.b.c' ]
Creates a cancellable Promise that is fulfilled when the EventEmitter emits the given event or that is rejected when the EventEmitter emits 'error'. The Promise will resolve with an array of all the arguments emitted to the given event. This method is intentionally generic and works with the web platform EventTarget interface, which has no special 'error' event semantics and does not listen to the 'error' event.
Basic example:
var emitter= new EventEmitter2();
EventEmitter2.once(emitter, 'event', {
timeout: 0,
Promise: Promise, // a custom Promise constructor
overload: false // overload promise cancellation api if exists with library implementation
}).then(function(data){
console.log(data); // [1, 2, 3]
});
emitter.emit('event', 1, 2, 3);
With timeout option:
EventEmitter2.once(emitter, 'event', {
timeout: 1000
}).then(null, function(err){
console.log(err); // Error: timeout
});
The library promise cancellation API:
promise= EventEmitter2.once(emitter, 'event');
// notice: the cancel method exists only in the first promise chain
promise.then(null, function(err){
console.log(err); // Error: canceled
});
promise.cancel();
Using the custom Promise class (bluebird.js):
var BBPromise = require("bluebird");
EventEmitter2.once(emitter, 'event', {
Promise: BBPromise
}).then(function(data){
console.log(data); // [4, 5, 6]
});
emitter.emit('event', 4, 5, 6);
var BBPromise = require("bluebird");
BBPromise.config({
// if false or options.overload enabled, the library cancellation API will be used
cancellation: true
});
var promise= EventEmitter2.once(emitter, 'event', {
Promise: BBPromise,
overload: false // use bluebird cancellation API
}).then(function(data){
// notice: never executed due to BlueBird cancellation logic
}, function(err){
// notice: never executed due to BlueBird cancellation logic
});
promise.cancel();
emitter.emit('event', 'never handled');
Sets default max listeners count globally for all instances, including those created before the change is made.