What is event-emitter?
The event-emitter npm package is a lightweight and flexible library for managing events in JavaScript. It allows you to create, listen to, and emit events, making it easier to handle asynchronous operations and decouple different parts of your application.
What are event-emitter's main functionalities?
Creating an Event Emitter
This feature allows you to create a new event emitter instance. The instance can be used to manage events within your application.
const EventEmitter = require('event-emitter');
const emitter = EventEmitter();
Listening to Events
This feature allows you to listen for specific events. When the event is emitted, the provided callback function is executed.
emitter.on('event', function(data) {
console.log('Event received:', data);
});
Emitting Events
This feature allows you to emit events. When an event is emitted, all listeners for that event are called with the provided data.
emitter.emit('event', { key: 'value' });
Removing Event Listeners
This feature allows you to remove specific event listeners. This is useful for cleaning up and preventing memory leaks.
const callback = function(data) {
console.log('Event received:', data);
};
emitter.on('event', callback);
emitter.off('event', callback);
Other packages similar to event-emitter
events
The 'events' package is a core Node.js module that provides a similar event-driven architecture. It is more robust and feature-rich compared to event-emitter, offering additional functionalities like once listeners and error handling.
eventemitter3
The 'eventemitter3' package is a high-performance event emitter for Node.js and the browser. It is similar to event-emitter but is optimized for speed and has a smaller footprint.
mitt
The 'mitt' package is a tiny, functional event emitter. It is simpler and more lightweight compared to event-emitter, making it ideal for small projects or performance-critical applications.
EventEmitter – Cross-environment event emitter solution for JavaScript
Installation
NPM
In your project path:
$ npm install event-emitter
Browser
Browser bundle can be easily created with help of modules-webmake. Mind that it relies on some EcmaScript5 features, so for older browsers you need as well es5-shim.
Usage
var ee = require('event-emitter');
var emitter = ee({}), listener;
emitter.on('test', listener = function (args) {
});
emitter.once('test', function (args) {
});
emitter.emit('test', arg1, arg2);
emitter.emit('test', arg1, arg2);
emitter.off('test', listener);
emitter.emit('test', arg1, arg2);
Additional functionalities (provided as separate modules)
allOff(obj)
Remove all listeners
var eeAllOff = require('event-emitter/lib/all-off');
eeAllOff(emitter);
pipe(emitter1, emitter2)
Pipe events from one emitter to other
var eePipe = require('event-emitter/lib/pipe');
var emitter1 = ee(), listener1;
var emitter2 = ee(), listener2;
emitter1.on('test', listener1 = function () { });
emitter2.on('test', listener2 = function () { });
emitter1.emit('test');
emitter2.emit('test');
var pipe = eePipe(emitter1, emitter2);
emitter1.emit('test');
emitter2.emit('test');
pipe.close();
emitter1.emit('test');
emitter2.emit('test');
hasListeners(obj[, type])
Whether given object have registered listeners
var emitter = ee();
var hasListeners = require('event-emitter/lib/has-listeners');
var listner = function () {};
hasListeners(emitter);
emitter.on('foo', listener);
hasListeners(emitter);
hasListeners(emitter, 'foo');
hasListeners(emitter, 'bar');
emitter.off('foo', listener);
hasListeners(emitter, 'foo');
Tests
$ npm test