Skip to content

Instantly share code, notes, and snippets.

@jucian0
Last active April 6, 2020 19:22
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 jucian0/6c899b1fd82786a3f65e8d4d0d6ee4ac to your computer and use it in GitHub Desktop.
Save jucian0/6c899b1fd82786a3f65e8d4d0d6ee4ac to your computer and use it in GitHub Desktop.
class Observable {
constructor() {
this.observers = [];
}
subscribe(fn) {
this.observers = [...this.observers, fn];
return () => {
this.unsubscribe(fn);
};
}
unsubscribe(fn) {
this.observers = this.observers.filter(observer => observer !== fn);
}
notify(data) {
this.observers.forEach(observer => {
observer(data);
});
}
}
export default new Observable();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment