Skip to content

Commit 800207c

Browse files
committed
fix(effectScope): calling off() of a detached scope should not break currentScope
fix #12825
1 parent 5960f05 commit 800207c

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/v3/reactivity/effectScope.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ export class EffectScope {
1616
* @internal
1717
*/
1818
cleanups: (() => void)[] = []
19-
2019
/**
21-
* only assigned by undetached scope
2220
* @internal
2321
*/
2422
parent: EffectScope | undefined
@@ -38,9 +36,9 @@ export class EffectScope {
3836
*/
3937
private index: number | undefined
4038

41-
constructor(detached = false) {
39+
constructor(public detached = false) {
40+
this.parent = activeEffectScope
4241
if (!detached && activeEffectScope) {
43-
this.parent = activeEffectScope
4442
this.index =
4543
(activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
4644
this
@@ -93,14 +91,15 @@ export class EffectScope {
9391
}
9492
}
9593
// nested scope, dereference from parent to avoid memory leaks
96-
if (this.parent && !fromParent) {
94+
if (!this.detached && this.parent && !fromParent) {
9795
// optimized O(1) removal
9896
const last = this.parent.scopes!.pop()
9997
if (last && last !== this) {
10098
this.parent.scopes![this.index!] = last
10199
last.index = this.index!
102100
}
103101
}
102+
this.parent = undefined
104103
this.active = false
105104
}
106105
}

test/unit/features/v3/reactivity/effectScope.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,15 @@ describe('reactivity/effectScope', () => {
279279
expect(getCurrentScope()).toBe(currentScope)
280280
})
281281
})
282+
283+
it('calling .off() of a detached scope inside an active scope should not break currentScope', () => {
284+
const parentScope = new EffectScope()
285+
286+
parentScope.run(() => {
287+
const childScope = new EffectScope(true)
288+
childScope.on()
289+
childScope.off()
290+
expect(getCurrentScope()).toBe(parentScope)
291+
})
292+
})
282293
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy