Install EventEmitter3 via npm
masterInstall the eventemitter3 package using npm to use it in your Node.js or browser projects.
$ npm install --save eventemitter3repository·master·Indexed 26 days ago
https://github.com/primus/eventemitter3A high-performance, micro-optimized EventEmitter for Node.js and browsers. It serves as a faster, drop-in replacement for the standard Node.js EventEmitter API, featuring support for custom context bindings to avoid fn.bind overhead and TypeScript support for typed events. Version 5.0.4.
Install the eventemitter3 package using npm to use it in your Node.js or browser projects.
$ npm install --save eventemitter3You can include EventEmitter3 directly in the browser using the following unpkg CDN link:
https://unpkg.com/eventemitter3@latest/dist/eventemitter3.umd.min.jsEventEmitter instances. The API is compatible with the standard Node.js EventEmitter API.The on, once, and removeListener methods accept an optional extra argument to serve as the context (the this value) for the listener. This avoids the performance overhead of using fn.bind.
var EE = new EventEmitter()
, context = { foo: 'bar' };
function emitted() {
console.log(this === context); // true
}
EE.once('event-name', emitted, context);
EE.on('another-event', emitted, context);
EE.removeListener('another-event', emitted, context);While eventemitter3 is a high-performance drop-in replacement for Node.js EventEmitter, note the following differences:
Domain support, newListener and removeListener events, setMaxListeners, getMaxListeners, prependListener, and prependOnceListener methods.throw an error when an error event is emitted and no listeners are registered.removeListener method removes all matching listeners, not just the first one.Use the following methods to inspect the state of the emitter:
eventNames(): Returns an array of all event names that have registered listeners.listeners(event): Returns an array of the listener functions registered for a specific event.listenerCount(event): Returns the number of listeners currently registered for a specific event.EventEmitter class provides a high-performance event handling interface. It supports typed events by passing an interface defining event names and their argument types. You can also specify a Context type to be used for the this binding in listeners.EventEmitter class from the eventemitter3 package using either a named import or a default import. This class is the primary interface for managing event listeners and emitting events.To get full TypeScript support for event names and arguments, pass an object to the EventEmitter generic. The object should map event names to either an array of argument types or a function signature.
interface MyEvents {
'event-with-parameters': any[]; // Using array for arguments
'event-with-example-handler': (...args: string[]) => void; // Using function signature
}off() or removeListener() to remove listeners. You can remove a specific function, or if fn is omitted, remove all listeners for that event. You can also specify a context and a once flag to target specific listener instances.emit() method synchronously calls each of the listeners registered for the specified event, passing the provided arguments.The emit(event, ...args) method calls all listeners registered for the specified event. It supports passing up to 5 arguments directly for performance optimization, and handles additional arguments via an array.
true if the event had listeners, otherwise false.emit are passed to the listener functions.