Skip to content

Commit 40ec346

Browse files
authored
Add optional defer to observer removal (#16526)
Adds an optional defer parameter to the observer removal as we commented in https://forum.babylonjs.com/t/remove-function-in-observer-remove-other-observers/58030
1 parent cbdd4ad commit 40ec346

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

packages/dev/core/src/Misc/observable.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class Observer<T> {
8383
* It will be set by the observable that the observer belongs to.
8484
* @internal
8585
*/
86-
public _remove: Nullable<() => void> = null;
86+
public _remove: Nullable<(defer?: boolean) => void> = null;
8787

8888
/**
8989
* Creates a new observer
@@ -109,10 +109,11 @@ export class Observer<T> {
109109
/**
110110
* Remove the observer from its observable
111111
* This can be used instead of using the observable's remove function.
112+
* @param defer if true, the removal will be deferred to avoid callback skipping (default: false)
112113
*/
113-
public remove() {
114+
public remove(defer = false) {
114115
if (this._remove) {
115-
this._remove();
116+
this._remove(defer);
116117
}
117118
}
118119
}
@@ -241,10 +242,10 @@ export class Observable<T> {
241242

242243
// attach the remove function to the observer
243244
const observableWeakRef = isWeakRefSupported ? new WeakRef(this) : { deref: () => this };
244-
observer._remove = () => {
245+
observer._remove = (defer = false) => {
245246
const observable = observableWeakRef.deref();
246247
if (observable) {
247-
observable._remove(observer);
248+
defer ? observable.remove(observer) : observable._remove(observer);
248249
}
249250
};
250251

0 commit comments

Comments
 (0)