Created
October 26, 2018 01:22
-
-
Save meshboy/b55dfa5c9e717b705d2bb345aa23cf0f 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
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