Fix unsubscribing from observed events containing null

This commit is contained in:
Danila Fedorin 2021-08-04 11:08:35 -07:00
parent 2375bf061c
commit 960e3ec469

View File

@ -25,7 +25,7 @@ export class ObservedEventMap {
observe(eventId, eventEntry = null) { observe(eventId, eventEntry = null) {
let observable = this._map.get(eventId); let observable = this._map.get(eventId);
if (!observable) { if (!observable) {
observable = new ObservedEvent(this, eventEntry); observable = new ObservedEvent(this, eventEntry, eventId);
this._map.set(eventId, observable); this._map.set(eventId, observable);
} }
return observable; return observable;
@ -39,8 +39,8 @@ export class ObservedEventMap {
} }
} }
_remove(observable) { _remove(id) {
this._map.delete(observable.get().id); this._map.delete(id);
if (this._map.size === 0) { if (this._map.size === 0) {
this._notifyEmpty(); this._notifyEmpty();
} }
@ -48,16 +48,17 @@ export class ObservedEventMap {
} }
class ObservedEvent extends BaseObservableValue { class ObservedEvent extends BaseObservableValue {
constructor(eventMap, entry) { constructor(eventMap, entry, id) {
super(); super();
this._eventMap = eventMap; this._eventMap = eventMap;
this._entry = entry; this._entry = entry;
this._id = id;
// remove subscription in microtask after creating it // remove subscription in microtask after creating it
// otherwise ObservedEvents would easily never get // otherwise ObservedEvents would easily never get
// removed if you never subscribe // removed if you never subscribe
Promise.resolve().then(() => { Promise.resolve().then(() => {
if (!this.hasSubscriptions) { if (!this.hasSubscriptions) {
this._eventMap.remove(this); this._eventMap._remove(this._id);
this._eventMap = null; this._eventMap = null;
} }
}); });
@ -71,7 +72,7 @@ class ObservedEvent extends BaseObservableValue {
} }
onUnsubscribeLast() { onUnsubscribeLast() {
this._eventMap._remove(this); this._eventMap._remove(this._id);
this._eventMap = null; this._eventMap = null;
super.onUnsubscribeLast(); super.onUnsubscribeLast();
} }