Skip to content

Instantly share code, notes, and snippets.

@patrickjuchli
Created February 25, 2017 15:09
Show Gist options
  • Save patrickjuchli/372d9cfbf0a899e3a315935c27053491 to your computer and use it in GitHub Desktop.
Save patrickjuchli/372d9cfbf0a899e3a315935c27053491 to your computer and use it in GitHub Desktop.
Simple notifier for Peter
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