
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
listener-tracker
Advanced tools
Lightweight module to support tracking added event listeners for easy removal later.
npm install listener-tracker --save
The native Node.js events module makes it difficult to remove listeners since you must keep a reference to the listener function in order to remove it later. For example, to properly remove a listener you must do the following:
function fooListener() {
/* ... */
}
// Add a listener:
eventEmitter.on('foo', fooListener);
// Now remove it:
eventEmitter.removeListener('foo', fooListener);
This is a problem because often times it is more convenient to add listeners with an anonymous function. For example:
// Add a listener:
eventEmitter.on('foo', function fooListener() {
/* ... */
});
// How can we remove it???
Also, what if an object is being destroyed and needs to remove all listeners that were added to other EventEmitter
instances? This module solves these types of problems by keeping tracking of all listeners that are attached by proxying all of the methods that are used to add listeners (i.e. on
, once
and addListener
).
To prevent a memory leak resulting from keeping references to all of the listener functions that were attached, this module will automatically do cleanup if a target EventEmitter
emits a destroy
event. You can also manually remove all listeners, of course.
EventEmitter
instance for easy removalvar EventEmitter = require('events').EventEmitter;
var myEventEmitter = EventEmitter();
var wrapped = require('listener-tracker').wrap(myEventEmitter);
wrapped
.on('foo', function() { /* ... */ })
.on('bar', function() { /* ... */ });
// Remove all of the listenters that were tracked by this instance:
wrapped.removeAllListeners();
// Remove the listenters that were tracked by this instance for a specific event:
wrapped.removeAllListeners('foo');
EventEmitter
instances for easy removalvar EventEmitter = require('events').EventEmitter;
var listenerTracker = require('listener-tracker').createTracker();
var eventEmitter1 = EventEmitter();
var eventEmitter2 = EventEmitter();
listenerTracker.subscribeTo(eventEmitter1)
.on('foo', function() { /* ... */ })
.on('bar', function() { /* ... */ });
listenerTracker.subscribeTo(eventEmitter2)
.on('foo', function() { /* ... */ })
.on('bar', function() { /* ... */ });
// Remove all listeners across all EventEmitters that were subscribed to:
listenerTracker.removeAllListeners();
// It's also possible to remove just listeners from one of the event emitters:
listenerTracker.removeAllListeners(eventEmitter1);
// Finally, it's also possible to remove just listeners from one of the event
// emitters for a specific event type:
listenerTracker.removeAllListeners(eventEmitter1, 'foo');
Pull Requests welcome. Please submit Github issues for any feature enhancements, bugs or documentation problems.
Apache License v2.0
FAQs
Allows event listeners to be tracked for easy removal
We found that listener-tracker 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
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.