You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
480 B
24 lines
480 B
/**
|
|
* Create a wrapper around an event emitter with a `remove` method to remove
|
|
* all events that were added using the wrapped emitter.
|
|
*/
|
|
export default class EventTracker {
|
|
#emitter
|
|
|
|
#events = []
|
|
|
|
constructor (emitter) {
|
|
this.#emitter = emitter
|
|
}
|
|
|
|
on (event, fn) {
|
|
this.#events.push([event, fn])
|
|
return this.#emitter.on(event, fn)
|
|
}
|
|
|
|
remove () {
|
|
for (const [event, fn] of this.#events.splice(0)) {
|
|
this.#emitter.off(event, fn)
|
|
}
|
|
}
|
|
}
|
|
|