Skip to content

Commit 7515bfd

Browse files
authored
Fixes observable implementation bug. (microsoft#183202)
1 parent 9cf00f5 commit 7515bfd

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/vs/base/common/observableImpl/derived.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,21 @@ export class Derived<T, TChangeSummary = any> extends BaseObservable<T, void> im
239239
}
240240

241241
public override addObserver(observer: IObserver): void {
242-
if (!this.observers.has(observer) && this.updateCount > 0) {
242+
const shouldCallBeginUpdate = !this.observers.has(observer) && this.updateCount > 0;
243+
super.addObserver(observer);
244+
245+
if (shouldCallBeginUpdate) {
243246
observer.beginUpdate(this);
244247
}
245-
super.addObserver(observer);
246248
}
247249

248250
public override removeObserver(observer: IObserver): void {
249-
if (this.observers.has(observer) && this.updateCount > 0) {
251+
const shouldCallEndUpdate = this.observers.has(observer) && this.updateCount > 0;
252+
super.removeObserver(observer);
253+
254+
if (shouldCallEndUpdate) {
255+
// Calling end update after removing the observer makes sure endUpdate cannot be called twice here.
250256
observer.endUpdate(this);
251257
}
252-
super.removeObserver(observer);
253258
}
254259
}

0 commit comments

Comments
 (0)