What is ev-emitter?
The ev-emitter package is a simple event emitter library for JavaScript. It allows you to add event-driven architecture to your applications by providing methods to emit events and listen for them.
What are ev-emitter's main functionalities?
Add Event Listener
This feature allows you to add an event listener for a specific event. In the code sample, an event listener is added for the 'event' event, and when the event is emitted, the callback function `onEvent` is executed.
const EvEmitter = require('ev-emitter');
const emitter = new EvEmitter();
function onEvent() {
console.log('Event triggered!');
}
emitter.on('event', onEvent);
emitter.emitEvent('event');
Remove Event Listener
This feature allows you to remove an event listener for a specific event. In the code sample, the event listener for the 'event' event is removed before the event is emitted, so the callback function `onEvent` is not executed.
const EvEmitter = require('ev-emitter');
const emitter = new EvEmitter();
function onEvent() {
console.log('Event triggered!');
}
emitter.on('event', onEvent);
emitter.off('event', onEvent);
emitter.emitEvent('event');
Emit Event
This feature allows you to emit an event, triggering all the listeners that are registered for that event. In the code sample, the 'event' event is emitted, and the callback function `onEvent` is executed.
const EvEmitter = require('ev-emitter');
const emitter = new EvEmitter();
function onEvent() {
console.log('Event triggered!');
}
emitter.on('event', onEvent);
emitter.emitEvent('event');
Other packages similar to ev-emitter
events
The 'events' package is the built-in Node.js event emitter module. It provides a similar event-driven architecture and is widely used in Node.js applications. Compared to ev-emitter, 'events' is more feature-rich and is part of the Node.js core modules.
eventemitter3
The 'eventemitter3' package is a high-performance event emitter for both Node.js and the browser. It offers a similar API to ev-emitter but is optimized for performance and has a smaller footprint. It is a good alternative if you need a lightweight and fast event emitter.
mitt
The 'mitt' package is a tiny functional event emitter. It provides a minimalistic API for event handling and is suitable for use in both Node.js and browser environments. Compared to ev-emitter, 'mitt' is even more lightweight and has a simpler API.
EvEmitter
Lil' event emitter — add a little pub/sub
EvEmitter adds publish/subscribe pattern to a browser class. It's a smaller version of Olical/EventEmitter. That EventEmitter is full featured, widely used, and great. This EvEmitter has just the base event functionality to power the event API in libraries like Isotope, Flickity, Masonry, and imagesLoaded.
API
class MyClass extends EvEmitter {}
Object.assign( MyClass.prototype, EvEmitter.prototype );
let emitter = new EvEmitter();
on
Add an event listener.
emitter.on( eventName, listener )
eventName
- String - name of the eventlistener
- Function
off
Remove an event listener.
emitter.off( eventName, listener )
once
Add an event listener to be triggered only once.
emitter.once( eventName, listener )
emitEvent
Trigger an event.
emitter.emitEvent( eventName, args )
eventName
- String - name of the eventargs
- Array - arguments passed to listeners
allOff
Removes all event listeners.
emitter.allOff()
Code example
var emitter = new EvEmitter();
function hey( a, b, c ) {
console.log( 'Hey', a, b, c )
}
function ho( a, b, c ) {
console.log( 'Ho', a, b, c )
}
function letsGo( a, b, c ) {
console.log( 'Lets go', a, b, c )
}
emitter.on( 'rock', hey )
emitter.on( 'rock', ho )
emitter.once( 'rock', letsGo )
emitter.emitEvent( 'rock', [ 1, 2, 3 ] )
emitter.off( 'rock', ho )
emitter.emitEvent( 'rock', [ 4, 5, 6 ] )
Browser support
EvEmitter v2 uses ES6 features like for...of loops and class definition as such it supports Chrome 49+, Firefox 45+, Safari 9+, and Edge 13+.
For older browser support, use EvEmitter v1.
License
EvEmitter is released under the MIT License. Have at it.