Created
June 7, 2018 10:02
-
-
Save FallenMax/7ab282db590fa57b21ac8aa685f52e40 to your computer and use it in GitHub Desktop.
This file contains 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<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