diff --git a/Observable.js b/Observable.js index 03545cd..529cd2f 100644 --- a/Observable.js +++ b/Observable.js @@ -11,12 +11,20 @@ class ObserverList { } add(observer) { // todo add observer to list + this.observerList.push(observer); + } remove(observer) { // todo remove observer from list + let index = this.observerList.indexOf(observer); + if(index > -1){ + this.observerList.splice(index,1); + } } count() { // return observer list size + return this.observerList.length; + } } @@ -26,12 +34,17 @@ class Subject { } addObserver(observer) { // todo add observer + this.observers.add(observer); } removeObserver(observer) { // todo remove observer + this.observers.remove(observer); } notify(...args) { // todo notify + this.observers.observerList.forEach((ob) => { + ob.update(...args); + }) } } diff --git a/PubSub.js b/PubSub.js index 0c7999e..bc65e08 100644 --- a/PubSub.js +++ b/PubSub.js @@ -13,14 +13,25 @@ module.exports = class PubSub { subscribe(type, fn) { // todo subscribe + if(this.subscribers[type] == undefined){ + this.subscribers[type] = []; + } + this.subscribers[type].push(fn); + } unsubscribe(type, fn) { // todo unsubscribe + delete this.subscribers[type]; } publish(type, ...args) { // todo publish + if(this.subscribers[type]){ + this.subscribers[type].forEach(fn => { + fn(...args); + }); + } } }