Created
February 25, 2017 15:09
-
-
Save patrickjuchli/372d9cfbf0a899e3a315935c27053491 to your computer and use it in GitHub Desktop.
Simple notifier for Peter
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
interface Handler<T> { | |
(payload: T); | |
} | |
export default class Notifier<T> { | |
protected handlers: Set<Handler<T>> = new Set(); | |
subscribe(handler: Handler<T>) { | |
this.handlers.add(handler); | |
} | |
unsubscribe(handler: Handler<T>) { | |
this.handlers.delete(handler); | |
} | |
unsubscribeAll() { | |
this.handlers.clear(); | |
} | |
notify(payload: T) { | |
for (const handler of this.handlers) { | |
handler(payload); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment