diff --git a/Observable.js b/Observable.js index 03545cd..673a7a5 100644 --- a/Observable.js +++ b/Observable.js @@ -11,12 +11,15 @@ class ObserverList { } add(observer) { // todo add observer to list + this.observerList.push(observer); } remove(observer) { // todo remove observer from list + this.observerList = this.observerList.filter(ob => ob != observer); } count() { // return observer list size + return this.observerList.length; } } @@ -26,12 +29,15 @@ 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.map(ob => ob.update.call(ob, ...args)) } } diff --git a/PubSub.js b/PubSub.js index 0c7999e..e889053 100644 --- a/PubSub.js +++ b/PubSub.js @@ -13,14 +13,23 @@ module.exports = class PubSub { subscribe(type, fn) { // todo subscribe + if (!this.subscribers[type]) { + this.subscribers[type] = [fn]; + } else { + this.subscribers[type] = this.subscribers[type].concat(fn); + } } unsubscribe(type, fn) { // todo unsubscribe + if (!this.subscribers[type]) return; + this.subscribers[type] = this.subscribers[type].filter(func => func != fn); } publish(type, ...args) { // todo publish + if (!this.subscribers[type]) return; + this.subscribers[type].map(func => func.call(this, ...args)); } }