File tree 2 files changed +23
-2
lines changed
2 files changed +23
-2
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' , ( ) => {
@@ -263,4 +264,17 @@ describe('reactivity/effect/scope', () => {
263
264
expect ( watchSpy ) . toHaveBeenCalledTimes ( 1 )
264
265
expect ( watchEffectSpy ) . toHaveBeenCalledTimes ( 2 )
265
266
} )
267
+
268
+ it ( 'getCurrentScope() stays valid when running a detached nested EffectScope' , ( ) => {
269
+ const parentScope = new EffectScope ( )
270
+
271
+ parentScope . run ( ( ) => {
272
+ const currentScope = getCurrentScope ( )
273
+ expect ( currentScope ) . toBeDefined ( )
274
+ const detachedScope = new EffectScope ( true )
275
+ detachedScope . run ( ( ) => { } )
276
+
277
+ expect ( getCurrentScope ( ) ) . toBe ( currentScope )
278
+ } )
279
+ } )
266
280
} )
Original file line number Diff line number Diff line change @@ -8,7 +8,13 @@ export class EffectScope {
8
8
effects : ReactiveEffect [ ] = [ ]
9
9
cleanups : ( ( ) => void ) [ ] = [ ]
10
10
11
+ /**
12
+ * only assinged by undetached scope
13
+ */
11
14
parent : EffectScope | undefined
15
+ /**
16
+ * record undetached scopes
17
+ */
12
18
scopes : EffectScope [ ] | undefined
13
19
/**
14
20
* track a child scope's index in its parent's scopes array for optimized
@@ -28,11 +34,12 @@ export class EffectScope {
28
34
29
35
run < T > ( fn : ( ) => T ) : T | undefined {
30
36
if ( this . active ) {
37
+ const currentEffectScope = activeEffectScope
31
38
try {
32
39
activeEffectScope = this
33
40
return fn ( )
34
41
} finally {
35
- activeEffectScope = this . parent
42
+ activeEffectScope = currentEffectScope
36
43
}
37
44
} else if ( __DEV__ ) {
38
45
warn ( `cannot run an inactive effect scope.` )
You can’t perform that action at this time.
0 commit comments