File tree Expand file tree Collapse file tree 2 files changed +32
-5
lines changed Expand file tree Collapse file tree 2 files changed +32
-5
lines changed Original file line number Diff line number Diff line change 6
6
onScopeDispose ,
7
7
computed ,
8
8
ref ,
9
- ComputedRef
9
+ ComputedRef ,
10
+ getCurrentScope
10
11
} from '../src'
11
12
12
13
describe ( 'reactivity/effect/scope' , ( ) => {
@@ -264,3 +265,29 @@ describe('reactivity/effect/scope', () => {
264
265
expect ( watchEffectSpy ) . toHaveBeenCalledTimes ( 2 )
265
266
} )
266
267
} )
268
+
269
+ it ( 'getCurrentScope() stays valid when running a detached nested EffectScope' , ( ) => {
270
+ const parentScope = new EffectScope ( )
271
+
272
+ parentScope . run ( ( ) => {
273
+ const currentScope = getCurrentScope ( )
274
+ expect ( currentScope ) . toBeDefined ( )
275
+ const detachedScope = new EffectScope ( true )
276
+ detachedScope . run ( ( ) => { } )
277
+
278
+ expect ( getCurrentScope ( ) ) . toBe ( currentScope )
279
+ } )
280
+ } )
281
+
282
+ it ( 'getCurrentScope() stays valid when offed a detached nested EffectScope' , ( ) => {
283
+ const parentScope = new EffectScope ( )
284
+
285
+ parentScope . run ( ( ) => {
286
+ const currentScope = getCurrentScope ( )
287
+ expect ( currentScope ) . toBeDefined ( )
288
+ const detachedScope = new EffectScope ( true )
289
+ detachedScope . off ( )
290
+
291
+ expect ( getCurrentScope ( ) ) . toBe ( currentScope )
292
+ } )
293
+ } )
Original file line number Diff line number Diff line change @@ -17,8 +17,8 @@ export class EffectScope {
17
17
private index : number | undefined
18
18
19
19
constructor ( detached = false ) {
20
+ this . parent = activeEffectScope
20
21
if ( ! detached && activeEffectScope ) {
21
- this . parent = activeEffectScope
22
22
this . index =
23
23
( activeEffectScope . scopes || ( activeEffectScope . scopes = [ ] ) ) . push (
24
24
this
@@ -62,12 +62,12 @@ export class EffectScope {
62
62
}
63
63
}
64
64
// nested scope, dereference from parent to avoid memory leaks
65
- if ( this . parent && ! fromParent ) {
65
+ if ( this . index !== undefined && this . parent && ! fromParent ) {
66
66
// optimized O(1) removal
67
67
const last = this . parent . scopes ! . pop ( )
68
68
if ( last && last !== this ) {
69
- this . parent . scopes ! [ this . index ! ] = last
70
- last . index = this . index !
69
+ this . parent . scopes ! [ this . index ] = last
70
+ last . index = this . index
71
71
}
72
72
}
73
73
this . active = false
You can’t perform that action at this time.
0 commit comments