Skip to content

Instantly share code, notes, and snippets.

@guiseek
Created June 24, 2023 03:31
Show Gist options
  • Save guiseek/18769ac3246f30a73dd53d7bb0a42fbe to your computer and use it in GitHub Desktop.
Save guiseek/18769ac3246f30a73dd53d7bb0a42fbe to your computer and use it in GitHub Desktop.
Tiny Event Emitter
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