Skip to content

Commit

Permalink
fix(effectScope): calling off() of a detached scope should not break …
Browse files Browse the repository at this point in the history
…currentScope

fix #12825
  • Loading branch information
yyx990803 committed Oct 14, 2022
1 parent 5960f05 commit 800207c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/v3/reactivity/effectScope.ts
Expand Up @@ -16,9 +16,7 @@ export class EffectScope {
* @internal
*/
cleanups: (() => void)[] = []

/**
* only assigned by undetached scope
* @internal
*/
parent: EffectScope | undefined
Expand All @@ -38,9 +36,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
Expand Down Expand Up @@ -93,14 +91,15 @@ 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) {
this.parent.scopes![this.index!] = last
last.index = this.index!
}
}
this.parent = undefined
this.active = false
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/features/v3/reactivity/effectScope.spec.ts
Expand Up @@ -279,4 +279,15 @@ describe('reactivity/effectScope', () => {
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)
})
})
})

0 comments on commit 800207c

Please sign in to comment.