Skip to content

Instantly share code, notes, and snippets.

@qmzik
Last active April 3, 2022 07:03
Show Gist options
  • Save qmzik/d8f4751c4cebfc0aa994fbcc9e5ccc35 to your computer and use it in GitHub Desktop.
Save qmzik/d8f4751c4cebfc0aa994fbcc9e5ccc35 to your computer and use it in GitHub Desktop.
type SubscribedEvent = (...args) => any;
class EventObserver {
public events: SubscribedEvent[] = [];
subscribe(fn: SubscribedEvent) {
this.events.push(fn)
}
unsubscribe(fn: SubscribedEvent) {
this.events = this.events.filter(subscriber => subscriber !== fn)
}
emit(data: any) {
this.events.forEach(subscriber => subscriber(data))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment