Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
ev-emitter
Advanced tools
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.
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');
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.
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.
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.
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.
// class inheritence
class MyClass extends EvEmitter {}
// mixin prototype
Object.assign( MyClass.prototype, EvEmitter.prototype );
// single instance
let emitter = new EvEmitter();
Add an event listener.
emitter.on( eventName, listener )
eventName
- String - name of the eventlistener
- FunctionRemove an event listener.
emitter.off( eventName, listener )
Add an event listener to be triggered only once.
emitter.once( eventName, listener )
Trigger an event.
emitter.emitEvent( eventName, args )
eventName
- String - name of the eventargs
- Array - arguments passed to listenersRemoves all event listeners.
emitter.allOff()
// create event emitter
var emitter = new EvEmitter();
// listeners
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 )
}
// bind listeners
emitter.on( 'rock', hey )
emitter.on( 'rock', ho )
// trigger letsGo once
emitter.once( 'rock', letsGo )
// emit event
emitter.emitEvent( 'rock', [ 1, 2, 3 ] )
// => 'Hey', 1, 2, 3
// => 'Ho', 1, 2, 3
// => 'Lets go', 1, 2, 3
// unbind
emitter.off( 'rock', ho )
emitter.emitEvent( 'rock', [ 4, 5, 6 ] )
// => 'Hey' 4, 5, 6
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.
EvEmitter is released under the MIT License. Have at it.
FAQs
lil' event emitter
The npm package ev-emitter receives a total of 362,298 weekly downloads. As such, ev-emitter popularity was classified as popular.
We found that ev-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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.