diff --git a/Observable.js b/Observable.js index 03545cd..3758743 100644 --- a/Observable.js +++ b/Observable.js @@ -10,13 +10,25 @@ class ObserverList { this.observerList = []; } add(observer) { - // todo add observer to list + return this.observerList.push(observer);// todo add observer to list } remove(observer) { // todo remove observer from list + for (let index = 0; index < this.observerList.length; index++) { + if(this.observerLis[index]==observer){ + this.observerList.splice(index,1); + } + + } } count() { // return observer list size + return this.observerList.length; + } + get(index){ + if (index<-1 && index { + observe.update(...args); + }); } } diff --git a/PubSub.js b/PubSub.js index 0c7999e..0de117a 100644 --- a/PubSub.js +++ b/PubSub.js @@ -13,14 +13,25 @@ module.exports = class PubSub { subscribe(type, fn) { // todo subscribe + if (!this.subscribers[type]) { + this.subscribers[type]=[]; + } + this.subscribers[type].push(fn); } unsubscribe(type, fn) { + if(this.subscribers[type]==fn){ + delete this.subscribers[type] + } // todo unsubscribe } publish(type, ...args) { // todo publish - } + this.subscribers[type].forEach(fn => { + fn(...args); + }); + } + } diff --git a/package.json b/package.json index a7933a0..1e857b3 100644 --- a/package.json +++ b/package.json @@ -18,5 +18,8 @@ "homepage": "https://github.com/FE-star/exercise17#readme", "devDependencies": { "mocha": "^5.0.0" + }, + "dependencies": { + "should": "^13.2.3" } }