Selaa lähdekoodia

Merge pull request #6488 from thscott/observablesFix

Fix issue when adding and removing observers
David Catuhe 6 vuotta sitten
vanhempi
commit
612ab5f07f
2 muutettua tiedostoa jossa 7 lisäystä ja 2 poistoa
  1. 1 0
      dist/preview release/what's new.md
  2. 6 2
      src/Misc/observable.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -70,5 +70,6 @@
 - Context loss should not cause PBR materials to render black or instances to stop rendering ([TrevorDev](https://github.com/TrevorDev))
 - Only cast pointer ray input when pointer is locked in webVR ([TrevorDev](https://github.com/TrevorDev))
 - Avoid using default utility layer in gizmo manager to support multiple scenes ([TrevorDev](https://github.com/TrevorDev))
+- Fix bug when adding and removing observers in quick succession ([sable](https://github.com/thscott))
 
 ## Breaking changes

+ 6 - 2
src/Misc/observable.ts

@@ -233,8 +233,12 @@ export class Observable<T> {
     public removeCallback(callback: (eventData: T, eventState: EventState) => void, scope?: any): boolean {
 
         for (var index = 0; index < this._observers.length; index++) {
-            if (this._observers[index].callback === callback && (!scope || scope === this._observers[index].scope)) {
-                this._deferUnregister(this._observers[index]);
+            const observer = this._observers[index];
+            if (observer._willBeUnregistered) {
+                continue;
+            }
+            if (observer.callback === callback && (!scope || scope === observer.scope)) {
+                this._deferUnregister(observer);
                 return true;
             }
         }