What is emitter-component?
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.
What are emitter-component's main functionalities?
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!');
Other packages similar to emitter-component
events
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.
eventemitter3
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.
mitt
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.
Emitter
Event emitter component.
Installation
$ npm install emitter-component
API
Emitter#on(event, fn)
Register an event
handler fn
.
Emitter#once(event, fn)
Register a single-shot event
handler fn
,
removed immediately after it is invoked the
first time.
Emitter#off(event, fn)
Remove event
handler fn
, or pass only the event
name to remove all handlers for event
.
Emitter#emit(event, ...)
Emit an event
with variable option args.