Keeps track of EventEmitter listeners and automatically removes them upon selected events. Useful when you want to avoid EventEmitter memory leaks caused by listeners not being removed.
const EventRegistry = require('event-registry')
const eventRegistry = new EventRegistry()
const emitter = getSomeEventEmitter()
const otherEmitter = getSomeOtherEventEmitter()
// Attach a listener using EventEmitter.prototype.on and remember it.
eventRegistry.on(emitter, 'data', someDataListener)
// Attach a one-off listener using EventEmitter.prototype.on and remember it.
eventRegistry.once(emitter, 'end', someEndListener)
// A single registry can track any number of event emitters.
eventRegistry.on(otherEmitter, 'progress', someProgressListener)
// When the 'end' event is emitted, remove all remembered listeners.
// Note that this will also remove the progress listener from otherEmitter.
eventRegistry.fin(emitter, 'end')
An EventRegistry
proxies the following functions to the emitter
:
addListener(emitter, event, listener)
AKAon(emitter, event, listener)
once(emitter, event, listener)
removeListener(emitter, event, listener)
removeAllListeners(emitter, [event])
Additionally, it keeps track of every listener
passed. When a final event
occurs (see below), it will remove all of these listeners.
Marks the given event
on the emitter
as final. When that event occurs, all
listeners will be removed.
Undoes a call to fin
. Marks the event
on the emitter
as non-final.
Shorthand for once(emitter, event, listener)
followed by fin(emitter, event)
.
Saves you a statement in this common scenario.
Removes all attached listeners and marks all final events as non-final. Basically what happens when a final event is emitted.
MIT