Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<this.observerList.length) {
return this.ObserverList[index];
}
}
}

Expand All @@ -25,13 +37,21 @@ class Subject {
this.observers = new ObserverList();
}
addObserver(observer) {
// todo add observer
this.observer.add(observer);// todo add observer
}
removeObserver(observer) {
// todo remove observer
this.observer.remove(observer);
}
notify(...args) {
// todo notify
var observerCont=this.observers.count();
// for (let index = 0; index < observerCont.length; index++) {
// this.observers.get[i].update(args);
// }
this.observers.ObserverList.forEach(observe => {
observe.update(...args);
});
}
}

Expand Down
13 changes: 12 additions & 1 deletion PubSub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}


}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"homepage": "https://github.com/FE-star/exercise17#readme",
"devDependencies": {
"mocha": "^5.0.0"
},
"dependencies": {
"should": "^13.2.3"
}
}