vector-im-hydrogen-web/src/observable/BaseObservableCollection.js

33 lines
679 B
JavaScript
Raw Normal View History

2019-02-21 23:08:23 +01:00
export default class BaseObservableCollection {
constructor() {
this._handlers = new Set();
}
onSubscribeFirst() {
}
onUnsubscribeLast() {
}
subscribe(handler) {
this._handlers.add(handler);
if (this._handlers.length === 1) {
this.onSubscribeFirst();
}
return () => {
if (handler) {
this._handlers.delete(this._handler);
if (this._handlers.length === 0) {
this.onUnsubscribeLast();
}
handler = null;
}
return null;
};
}
// Add iterator over handlers here
2019-02-21 23:08:23 +01:00
}