Skip to content

Instantly share code, notes, and snippets.

@FallenMax
Created June 7, 2018 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FallenMax/7ab282db590fa57b21ac8aa685f52e40 to your computer and use it in GitHub Desktop.
Save FallenMax/7ab282db590fa57b21ac8aa685f52e40 to your computer and use it in GitHub Desktop.
export class EventEmitter<T> {
handlers = [] as ((payload: T) => void)[]
addHandler(cb: (payload: T) => void) {
if (this.handlers.indexOf(cb) === -1) {
this.handlers.push(cb)
}
}
removeHandler(cb: (payload: T) => void) {
const index = this.handlers.indexOf(cb)
if (index !== -1) {
this.handlers.splice(index, 1)
}
}
emit(payload: T) {
this.handlers.forEach(handler => handler(payload))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment