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
13 changes: 8 additions & 5 deletions Observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ class ObserverList {
this.observerList = [];
}
add(observer) {
// todo add observer to list
this.observerList.push(observer);
}
remove(observer) {
// todo remove observer from list
this.observerList = this.observerList.filter(item => item !== observer);
}
count() {
// return observer list size
return this.observerList.length;
}
}

Expand All @@ -25,13 +26,15 @@ class Subject {
this.observers = new ObserverList();
}
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(item => {
item.update(...args);
})
}
}

Expand Down
10 changes: 6 additions & 4 deletions PubSub.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ module.exports = class PubSub {
}

subscribe(type, fn) {
// todo subscribe
this.subscribers[type] = fn;
}

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

publish(type, ...args) {
// todo publish
if (this.subscribers[type]) {
this.subscribers[type].call(this, ...args);
}
}

}
};