Created
November 27, 2018 15:54
-
-
Save nishanbajracharya/d56878bf9d5bf63d111677cebb11cef9 to your computer and use it in GitHub Desktop.
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
class Subject { | |
constructor(state) { | |
this.state = state; | |
this.observers = []; | |
} | |
getState() { | |
return this.state; | |
} | |
setState(state) { | |
this.state = state; | |
this.notifyObservers(this.state); | |
} | |
registerObserver(observer) { | |
this.observers.push(observer); | |
} | |
unregisterObserver(observer) { | |
this.observers = this.observers.filter(obs => obs !== observer); | |
} | |
notifyObservers(data) { | |
this.observers.forEach(observer => observer.notify(data)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment