File tree 2 files changed +15
-5
lines changed
test/unit/features/v3/reactivity
2 files changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -16,9 +16,7 @@ export class EffectScope {
16
16
* @internal
17
17
*/
18
18
cleanups : ( ( ) => void ) [ ] = [ ]
19
-
20
19
/**
21
- * only assigned by undetached scope
22
20
* @internal
23
21
*/
24
22
parent : EffectScope | undefined
@@ -38,9 +36,9 @@ export class EffectScope {
38
36
*/
39
37
private index : number | undefined
40
38
41
- constructor ( detached = false ) {
39
+ constructor ( public detached = false ) {
40
+ this . parent = activeEffectScope
42
41
if ( ! detached && activeEffectScope ) {
43
- this . parent = activeEffectScope
44
42
this . index =
45
43
( activeEffectScope . scopes || ( activeEffectScope . scopes = [ ] ) ) . push (
46
44
this
@@ -93,14 +91,15 @@ export class EffectScope {
93
91
}
94
92
}
95
93
// nested scope, dereference from parent to avoid memory leaks
96
- if ( this . parent && ! fromParent ) {
94
+ if ( ! this . detached && this . parent && ! fromParent ) {
97
95
// optimized O(1) removal
98
96
const last = this . parent . scopes ! . pop ( )
99
97
if ( last && last !== this ) {
100
98
this . parent . scopes ! [ this . index ! ] = last
101
99
last . index = this . index !
102
100
}
103
101
}
102
+ this . parent = undefined
104
103
this . active = false
105
104
}
106
105
}
Original file line number Diff line number Diff line change @@ -279,4 +279,15 @@ describe('reactivity/effectScope', () => {
279
279
expect ( getCurrentScope ( ) ) . toBe ( currentScope )
280
280
} )
281
281
} )
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
+ } )
282
293
} )
You can’t perform that action at this time.
0 commit comments