Skip to content
Permalink
Browse files
fix(effectScope): calling off() of a detached scope should not break …
…currentScope
  • Loading branch information
yyx990803 committed Oct 14, 2022
1 parent 35dc2bb commit a71f9ac41af464fdb69220e69c50739dd3a8f365
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
@@ -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)
})
})
})
@@ -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,14 +89,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
}
}

0 comments on commit a71f9ac

Please sign in to comment.