]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(effectScope): calling off() of a detached scope should not break currentScope
authorEvan You <yyx990803@gmail.com>
Fri, 14 Oct 2022 02:53:23 +0000 (10:53 +0800)
committerEvan You <yyx990803@gmail.com>
Fri, 14 Oct 2022 02:53:23 +0000 (10:53 +0800)
packages/reactivity/__tests__/effectScope.spec.ts
packages/reactivity/src/effectScope.ts

index af92c97f94c7cdfb693812d8e14ffd563e5efe7d..28579fef028327d8616c426cb91b41b00a0ed12d 100644 (file)
@@ -277,4 +277,15 @@ describe('reactivity/effect/scope', () => {
       expect(getCurrentScope()).toBe(currentScope)
     })
   })
+
+  it('calling .off() of a detached scope inside an active scope should not break currentScope', () => {
+    const parentScope = new EffectScope()
+
+    parentScope.run(() => {
+      const childScope = new EffectScope(true)
+      childScope.on()
+      childScope.off()
+      expect(getCurrentScope()).toBe(parentScope)
+    })
+  })
 })
index f4396e8f3ab07b8ed688136fb54e636ebf8b811d..557a1379492c1aa005d9ec6ab1ebf64ba6647b60 100644 (file)
@@ -34,9 +34,9 @@ export class EffectScope {
    */
   private index: number | undefined
 
-  constructor(detached = false) {
+  constructor(public detached = false) {
+    this.parent = activeEffectScope
     if (!detached && activeEffectScope) {
-      this.parent = activeEffectScope
       this.index =
         (activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
           this
@@ -89,7 +89,7 @@ export class EffectScope {
         }
       }
       // nested scope, dereference from parent to avoid memory leaks
-      if (this.parent && !fromParent) {
+      if (!this.detached && this.parent && !fromParent) {
         // optimized O(1) removal
         const last = this.parent.scopes!.pop()
         if (last && last !== this) {
@@ -97,6 +97,7 @@ export class EffectScope {
           last.index = this.index!
         }
       }
+      this.parent = undefined
       this.active = false
     }
   }