Created
July 17, 2017 15:36
-
-
Save alexnoz/b76bb4a785b4414066d2fe9c073054f7 to your computer and use it in GitHub Desktop.
The "Observer" pattern
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
const Subject = function () { | |
const observersList = []; | |
const addObserver = observer => { | |
observersList.push(observer); | |
} | |
const removeObserver = toRemove => { | |
const i = observersList.findIndex(observer => observer == toRemove); | |
observersList.splice(i, 1); | |
} | |
const notify = context => { | |
observersList.forEach(observer => observer.update(context)); | |
} | |
this.addObserver = addObserver; | |
this.removeObserver = removeObserver; | |
this.notify = notify; | |
}; | |
const Observer = function () { | |
// ... | |
} | |
Observer.prototype.update = function (context) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment