Created
October 4, 2024 15:22
-
-
Save PuruVJ/57c7c404fbb884eb600a65e105cf8ba2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class EventEmitter { | |
#events: Map<string, Set<Function>> = new Map(); | |
on(event: string, callback: Function): void { | |
let callbacks = this.#events.get(event); | |
if (!callbacks) { | |
callbacks = new Set(); | |
this.#events.set(event, callbacks); | |
} | |
callbacks.add(callback); | |
} | |
off(event: string, callback: Function): void { | |
let callbacks = this.#events.get(event); | |
if (!callbacks) { | |
return; | |
} | |
callbacks.delete(callback); | |
} | |
emit(event: string, ...args: any[]): void { | |
let callbacks = this.#events.get(event); | |
if (!callbacks) { | |
return; | |
} | |
for (let callback of callbacks) { | |
callback(...args); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment