evt-listener
What is it?
A class encapsulating an event and it's listener together.
Why would you use it?
Whenever you create an event listener and need to eventually turn it off, you would pass the same function passed to emitter.on to emitter.removeListener to do so.
Creating an EventListener (the class behind evt-listener) allows you to just call listener.off (where listener would be an instance of EventListener) without having to worry about saving and passing the event name and function. See below for a comparison.
Prerequisites
Install it using npm, saving it as a dependency.
npm i evt-listener --save
Both examples share the same event emitter boilerplate code.
var EventEmitter = EventEmitter;var emitter = ;
Comparison
Without EventListener
//we have to save the event name and the listenervar eventName = 'eventName';var { console;}; emitter; //sometime later...emitter;
With EventListener
var EventListener = ; //create a listenervar eventNameListener = emitter 'eventName'; eventNameListener; //sometime later...eventNameListener;
Tips
1. Name your listeners appropriately!
2. You can chain your listener function after you create an EventListener instance.
//create a listener, giving it a handlervar eventNameListener = emitter 'eventName'; //same goes for oncevar eventNameListener = emitter 'eventName';
3. Don't like using the new keyword in javascript? No worries.
var EventListener = ;var listener = EventListeneremitter 'eventName';
4. Prefer the revealing module pattern?
var EventListener = EventListener;
PS
Check out evt-emitter which adds a createListener factory method on the EventEmitter prototype. This allows you to create instances of EventListener without having to pass an emitter.