I wrote the following EventEmitter class, and it works great so far. But today I came across a scenario where if event listeners are created and destroyed frequently, and the off function is not used to remove the listener, there will be potential performance issues. How can I optimize this problem? Can anyone give me some ideas?
class EventEmitter {
private listeners: Record<string, [Set<Function>, Set<Function>]> = {};
existsEvent(eventName: string | number) {
return !!this.listeners[eventName];
}
on(eventName: string | number, listener: Function) {
if (!this.listeners[eventName]) {
this.listeners[eventName] = [new Set(), new Set()];
}
this.listeners[eventName][0].add(listener);
}
once(eventName: string | number, listener: Function) {
if (!this.listeners[eventName]) {
this.listeners[eventName] = [new Set(), new Set()];
}
this.listeners[eventName][1].add(listener);
}
emit(eventName: string | number, ...args: any[]) {
if (!this.listeners[eventName]) {
return
}
this.listeners[eventName][0].forEach((listener: Function) => {
listener(...args);
});
this.listeners[eventName][1].forEach((listener: Function) => {
listener(...args);
});
this.listeners[eventName][1].clear();
}
off(eventName: string | number, listener: Function) {
if (!this.listeners[eventName]) {
return;
}
if (typeof listener === 'function') {
this.listeners[eventName][0].delete(listener);
this.listeners[eventName][1].delete(listener);
} else {
this.listeners[eventName][0].clear();
this.listeners[eventName][1].clear();
}
}
}
export const eventEmitter = new EventEmitter();