What is component-emitter?
The component-emitter package is a simple event emitter implementation that allows objects to subscribe to events, emit events, and remove event listeners. It is often used to add observer pattern capabilities to objects, enabling event-driven programming.
What are component-emitter's main functionalities?
Event subscription
This feature allows an object to listen for specific events using the 'on' method. When the event is emitted, the callback function is called.
const Emitter = require('component-emitter');
let emitter = new Emitter();
emitter.on('event', function(message) {
console.log(message);
});
Event emission
This feature allows an object to emit an event, optionally passing arguments to the event listeners.
emitter.emit('event', 'Hello World!');
One-time event subscription
This feature allows an object to listen for an event only once. After the event is emitted and the listener is invoked, the listener is removed.
emitter.once('event', function(message) {
console.log(message);
});
Event unsubscription
This feature allows an object to stop listening for specific events using the 'off' method.
function listener(message) {
console.log(message);
}
emitter.on('event', listener);
emitter.off('event', listener);
Other packages similar to component-emitter
events
The 'events' package is Node.js's core EventEmitter class for managing and emitting events. It is similar to component-emitter but is built into Node.js and provides a more extensive API.
eventemitter3
EventEmitter3 is a high-performance event emitter. It offers a similar API to component-emitter but focuses on performance and is optimized for modern JavaScript engines.
wolfy87-eventemitter
Wolfy87's EventEmitter is an implementation of the EventEmitter module found in Node.js but can be used in the browser. It has additional features like namespacing events, but it's larger in size compared to component-emitter.
component-emitter
Simple event emitter
Install
npm install component-emitter
Usage
As an Emitter
instance:
import Emitter from 'component-emitter';
const emitter = new Emitter();
emitter.emit('🦄');
As a mixin:
import Emitter from 'component-emitter';
const user = {name: 'tobi'};
Emitter(user);
user.emit('I am a user');
As a prototype mixin:
import Emitter from 'component-emitter';
Emitter(User.prototype);
API
new Emitter()
Create a new emitter.
Emitter(object)
Use it as a mixin. For example a plain object may become an emitter, or you may extend an existing prototype.
Emitter#on(event, listener)
Register an event handler that listens to a specified event.
Emitter#once(event, listener)
Register a one-time event handler for a specified event.
Emitter#off(event, listener)
Remove a specific event handler for a specified event.
Emitter#off(event)
Remove all event handlers for a specified event.
Emitter#off()
Remove all event handlers for all events.
Emitter#emit(event, ...arguments)
Emit an event, invoking all handlers registered for it.
Emitter#listeners(event)
Retrieve the event handlers registered for a specific event.
Emitter#listenerCount(event)
Get the count of listeners for a specific event.
Emitter#listenerCount()
Get the count of all event handlers in total.
Emitter#hasListeners(event)
Check if there are any handlers registered for a specific event.
Emitter#hasListeners()
Check if there are any handlers registered for any event.
FAQ
How does it differ from the Node.js EventEmitter?
It's simpler and more lightweight, and it works in any JavaScript environment, not just Node.js. It also provides mixin functionality to add event handling to existing objects without needing inheritance.