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
19 changes: 19 additions & 0 deletions Observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ class ObserverList {
}
add(observer) {
// todo add observer to list
return this.observerList.push(observer);
}
remove(observer) {
// todo remove observer from list
const index = this.observerList.indexOf(observer);
if (index !== -1) {
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 @@ -26,12 +37,20 @@ class Subject {
}
addObserver(observer) {
// todo add observer
this.observers.add(observer);
}
removeObserver(observer) {
// todo remove observer
this.observers.remove(observer);
}
notify(...args) {
// todo notify
if(this.observers.count() > 0) {
const length = this.observers.count();
for (let i = 0; i < length; i++) {
this.observers.get(i).update(...args);
}
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions PubSub.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ module.exports = class PubSub {

subscribe(type, fn) {
// todo subscribe
let topic = this.subscribers[type];
if (!topic) {
this.subscribers[type] = [];
}
this.subscribers[type].push(fn);
}

unsubscribe(type, fn) {
// todo unsubscribe
delete this.subscribers[type];
}

publish(type, ...args) {
// todo publish
this.subscribers[type] && this.subscribers[type].forEach(fn => fn(...args));
}

}