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 :star:!
once concept with manyPlatform: 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
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
});
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).
once concept.emitter.many('foo', 4, function() {
console.log('hello');
});
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
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
waitFor(event: event | eventNS, filter?: WaitForFilter): CancelablePromise
waitFor(event: event | eventNS, options?: WaitForOptions): CancelablePromise
listenTo(target: GeneralEventEmitter, event: event | eventNS, options?: ListenToOptions): this
listenTo(target: GeneralEventEmitter, events: (event | eventNS)[], options?: ListenToOptions): this
listenTo(target: GeneralEventEmitter, events: Object, 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 the nextTick 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 using emitAsync method.
This option will be activated by default if its value is undefined
and the listener function is an asynchronous function (whose constructor name is AsyncFunction).
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 instance
- event: event|eventNS - subscription event
- listener: Function - reference to the listener
- off(): 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:
```javascript 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('
$ claude mcp add EventEmitter2 \
-- python -m otcore.mcp_server <graph>