MCPcopy Index your code
hub / github.com/EventEmitter2/EventEmitter2

github.com/EventEmitter2/EventEmitter2 @v6.4.9

Chat with this repo
repository ↗ · DeepWiki ↗ · release v6.4.9 ↗ · + Follow
87 symbols 166 edges 33 files 0 documented · 0% 27 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Build Status Coverage Status NPM version Dependency Status npm

SYNOPSIS

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:!

DESCRIPTION

FEATURES

  • 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 with many
  • 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

What's new

To find out what's new see the project CHANGELOG

Differences (Non-breaking, compatible with existing EventEmitter)

  • 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');
});

Installing

$ npm install eventemitter2

Or you can use unpkg.com CDN to import this module as a script directly from the browser

API

Types definition

  • Event: string | symbol
  • EventNS: string | Event []

Class EventEmitter2

instance:

static:

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']

Multi-level Wildcards

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.

emitter.addListener(event, listener, options?: object|boolean)

emitter.on(event, listener, options?: object|boolean)

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.

listener

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('

Extension points exported contracts — how you extend this code

ConstructorOptions (Interface)
(no doc)
eventemitter2.d.ts
ListenerFn (Interface)
(no doc)
eventemitter2.d.ts
EventAndListener (Interface)
(no doc)
eventemitter2.d.ts
WaitForFilter (Interface)
(no doc)
eventemitter2.d.ts
WaitForOptions (Interface)
(no doc)
eventemitter2.d.ts

Core symbols most depended-on inside this repo

on
called by 251
eventemitter2.d.ts
removeListener
called by 25
eventemitter2.d.ts
off
called by 16
eventemitter2.d.ts
f
called by 12
test/wildcardEvents/addListener.js
reject
called by 9
lib/eventemitter2.js
setupRemoveListenerTest
called by 8
test/simple/removeListener.js
cancel
called by 5
eventemitter2.d.ts
resolveOptions
called by 4
lib/eventemitter2.js

Shape

Function 66
Interface 11
Method 8
Class 2

Languages

TypeScript100%

Modules by API surface

lib/eventemitter2.js28 symbols
eventemitter2.d.ts21 symbols
test/loader.js5 symbols
test/wildcardEvents/ttl.js4 symbols
test/wildcardEvents/removeListener.js4 symbols
test/simple/ttl.js4 symbols
test/simple/removeListener.js4 symbols
test/wildcardEvents/prependListener.js2 symbols
test/wildcardEvents/all.js2 symbols
test/simple/prepend.js2 symbols
test/simple/emit.js2 symbols
test/simple/addListener.js2 symbols

For agents

$ claude mcp add EventEmitter2 \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact