Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
event-emitter
Advanced tools
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.
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);
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.
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.
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.
Basic event emitter for Node.js and Browsers
Node & npm:
$ npm install event-emitter
var ee = require('event-emitter');
var MyCostructor = function () {};
ee(MyConstructor.prototype);
var myObj = new MyConstructor();
// Register listener:
var listener;
myObj.on('name', listener = function (args) {
// ... whatever
});
// Register listener that would be removed after first emit:
myObj.once('name', function (args) {
// ... whatever
});
// Remove registered listener
myObj.off('name', listener);
// Emit event
myObj.emit('name', arg1/*, arg2, arg3*/);
FAQs
Environment agnostic event emitter
The npm package event-emitter receives a total of 5,565,922 weekly downloads. As such, event-emitter popularity was classified as popular.
We found that event-emitter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.