Skip to content

Instantly share code, notes, and snippets.

@meshboy
Created October 26, 2018 01:22
Show Gist options
  • Save meshboy/b55dfa5c9e717b705d2bb345aa23cf0f to your computer and use it in GitHub Desktop.
Save meshboy/b55dfa5c9e717b705d2bb345aa23cf0f to your computer and use it in GitHub Desktop.
function Observer() {
this.observerContainer = [];
}
Observer.prototype.subscribe = function (element) {
this.observerContainer.push(element);
}
/**
* removes an element from the container
*/
Observer.prototype.unsubscribe = function (element) {
const elementIndex = this.observerContainer.indexOf(element);
if (elementIndex > -1) {
this.observerContainer.splice(elementIndex, 1);
}
}
/**
* notifies all the element added to the container by calling
* each subscribed components added to the container
*/
Observer.prototype.notifyAll = function (element) {
this.observerContainer.forEach(function (observerElement) {
observerElement(element);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment