Last active
April 6, 2020 19:22
-
-
Save jucian0/6c899b1fd82786a3f65e8d4d0d6ee4ac 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
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