Created
June 24, 2023 03:31
-
-
Save guiseek/18769ac3246f30a73dd53d7bb0a42fbe to your computer and use it in GitHub Desktop.
Tiny Event Emitter
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
type Callback<T> = (value: T) => void | |
export class EventEmitter<T> { | |
#on = new Set<Callback<T>>([]) | |
set on(cb: Callback<T>) { | |
this.#on.add(cb) | |
} | |
set off(cb: Callback<T>) { | |
if (this.#on.has(cb)) { | |
this.#on.delete(cb) | |
} | |
} | |
emit(value: T) { | |
for (const cb of this.#on) cb(value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment