Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
emitter-component
Advanced tools
The emitter-component npm package is a simple event emitter implementation. It allows you to create objects that can emit events and have listeners that respond to those events. This is useful for decoupling different parts of an application and enabling communication between them.
Event Emission
This feature allows you to emit events and have listeners respond to those events. In the code sample, an event listener is defined for the 'event' event, and then the event is emitted with a message.
const Emitter = require('emitter-component');
const emitter = new Emitter();
// Define an event listener
emitter.on('event', function(message) {
console.log('Event received:', message);
});
// Emit an event
emitter.emit('event', 'Hello, World!');
Removing Event Listeners
This feature allows you to remove event listeners. In the code sample, an event listener is defined and then removed before the event is emitted, so no listeners respond to the event.
const Emitter = require('emitter-component');
const emitter = new Emitter();
function onEvent(message) {
console.log('Event received:', message);
}
// Define an event listener
emitter.on('event', onEvent);
// Remove the event listener
emitter.off('event', onEvent);
// Emit an event (no listeners should respond)
emitter.emit('event', 'Hello, World!');
Once Event Listeners
This feature allows you to define event listeners that will be called only once. In the code sample, the event listener is defined with the 'once' method and is called only the first time the event is emitted.
const Emitter = require('emitter-component');
const emitter = new Emitter();
// Define an event listener that will be called only once
emitter.once('event', function(message) {
console.log('Event received:', message);
});
// Emit the event twice
emitter.emit('event', 'Hello, World!');
emitter.emit('event', 'Hello again!');
The 'events' package is the built-in Node.js event emitter module. It provides a similar API to emitter-component but is more feature-rich and is maintained as part of the Node.js core. It is widely used and well-documented.
The 'eventemitter3' package is a high-performance event emitter for Node.js and the browser. It is similar to emitter-component but offers better performance and additional features like wildcard event listeners.
The 'mitt' package is a tiny (~200 bytes) functional event emitter. It is similar to emitter-component but is designed to be extremely lightweight and simple, making it suitable for use in performance-critical applications.
Event emitter component.
$ npm install emitter-component
Register an event
handler fn
.
Register a single-shot event
handler fn
,
removed immediately after it is invoked the
first time.
Remove event
handler fn
, or pass only the event
name to remove all handlers for event
.
Emit an event
with variable option args.
Return an array of callbacks, or an empty array.
Check if this emitter has event
handlers.
FAQs
Event emitter
The npm package emitter-component receives a total of 317,651 weekly downloads. As such, emitter-component popularity was classified as popular.
We found that emitter-component demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 22 open source maintainers 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
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.