Skip to content

Instantly share code, notes, and snippets.

@alexnoz
Created July 17, 2017 15:36
Show Gist options
  • Save alexnoz/b76bb4a785b4414066d2fe9c073054f7 to your computer and use it in GitHub Desktop.
Save alexnoz/b76bb4a785b4414066d2fe9c073054f7 to your computer and use it in GitHub Desktop.
The "Observer" pattern
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