Skip to content

Commit ddeeaa2

Browse files
yyx990803zhangzhonghe
authored andcommitted
fix(effectScope): calling off() of a detached scope should not break currentScope
1 parent e234b11 commit ddeeaa2

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/reactivity/__tests__/effectScope.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,15 @@ describe('reactivity/effect/scope', () => {
277277
expect(getCurrentScope()).toBe(currentScope)
278278
})
279279
})
280+
281+
it('calling .off() of a detached scope inside an active scope should not break currentScope', () => {
282+
const parentScope = new EffectScope()
283+
284+
parentScope.run(() => {
285+
const childScope = new EffectScope(true)
286+
childScope.on()
287+
childScope.off()
288+
expect(getCurrentScope()).toBe(parentScope)
289+
})
290+
})
280291
})

packages/reactivity/src/effectScope.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ export class EffectScope {
3434
*/
3535
private index: number | undefined
3636

37-
constructor(detached = false) {
37+
constructor(public detached = false) {
38+
this.parent = activeEffectScope
3839
if (!detached && activeEffectScope) {
39-
this.parent = activeEffectScope
4040
this.index =
4141
(activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
4242
this
@@ -89,14 +89,15 @@ export class EffectScope {
8989
}
9090
}
9191
// nested scope, dereference from parent to avoid memory leaks
92-
if (this.parent && !fromParent) {
92+
if (!this.detached && this.parent && !fromParent) {
9393
// optimized O(1) removal
9494
const last = this.parent.scopes!.pop()
9595
if (last && last !== this) {
9696
this.parent.scopes![this.index!] = last
9797
last.index = this.index!
9898
}
9999
}
100+
this.parent = undefined
100101
this.active = false
101102
}
102103
}

0 commit comments

Comments
 (0)