@@ -2,13 +2,15 @@ import { nextTick, watch, watchEffect } from '@vue/runtime-core'
2
2
import {
3
3
type ComputedRef ,
4
4
EffectScope ,
5
+ ReactiveEffect ,
5
6
computed ,
6
7
effect ,
7
8
effectScope ,
8
9
getCurrentScope ,
9
10
onScopeDispose ,
10
11
reactive ,
11
12
ref ,
13
+ setCurrentScope ,
12
14
} from '../src'
13
15
14
16
describe ( 'reactivity/effect/scope' , ( ) => {
@@ -20,7 +22,7 @@ describe('reactivity/effect/scope', () => {
20
22
21
23
it ( 'should accept zero argument' , ( ) => {
22
24
const scope = effectScope ( )
23
- expect ( scope . effects . length ) . toBe ( 0 )
25
+ expect ( getEffectsCount ( scope ) ) . toBe ( 0 )
24
26
} )
25
27
26
28
it ( 'should return run value' , ( ) => {
@@ -29,7 +31,8 @@ describe('reactivity/effect/scope', () => {
29
31
30
32
it ( 'should work w/ active property' , ( ) => {
31
33
const scope = effectScope ( )
32
- scope . run ( ( ) => 1 )
34
+ const src = computed ( ( ) => 1 )
35
+ scope . run ( ( ) => src . value )
33
36
expect ( scope . active ) . toBe ( true )
34
37
scope . stop ( )
35
38
expect ( scope . active ) . toBe ( false )
@@ -47,7 +50,7 @@ describe('reactivity/effect/scope', () => {
47
50
expect ( dummy ) . toBe ( 7 )
48
51
} )
49
52
50
- expect ( scope . effects . length ) . toBe ( 1 )
53
+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
51
54
} )
52
55
53
56
it ( 'stop' , ( ) => {
@@ -60,7 +63,7 @@ describe('reactivity/effect/scope', () => {
60
63
effect ( ( ) => ( doubled = counter . num * 2 ) )
61
64
} )
62
65
63
- expect ( scope . effects . length ) . toBe ( 2 )
66
+ expect ( getEffectsCount ( scope ) ) . toBe ( 2 )
64
67
65
68
expect ( dummy ) . toBe ( 0 )
66
69
counter . num = 7
@@ -87,9 +90,8 @@ describe('reactivity/effect/scope', () => {
87
90
} )
88
91
} )
89
92
90
- expect ( scope . effects . length ) . toBe ( 1 )
91
- expect ( scope . scopes ! . length ) . toBe ( 1 )
92
- expect ( scope . scopes ! [ 0 ] ) . toBeInstanceOf ( EffectScope )
93
+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
94
+ expect ( scope . deps ?. nextDep ?. dep ) . toBeInstanceOf ( EffectScope )
93
95
94
96
expect ( dummy ) . toBe ( 0 )
95
97
counter . num = 7
@@ -117,7 +119,7 @@ describe('reactivity/effect/scope', () => {
117
119
} )
118
120
} )
119
121
120
- expect ( scope . effects . length ) . toBe ( 1 )
122
+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
121
123
122
124
expect ( dummy ) . toBe ( 0 )
123
125
counter . num = 7
@@ -142,13 +144,13 @@ describe('reactivity/effect/scope', () => {
142
144
effect ( ( ) => ( dummy = counter . num ) )
143
145
} )
144
146
145
- expect ( scope . effects . length ) . toBe ( 1 )
147
+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
146
148
147
149
scope . run ( ( ) => {
148
150
effect ( ( ) => ( doubled = counter . num * 2 ) )
149
151
} )
150
152
151
- expect ( scope . effects . length ) . toBe ( 2 )
153
+ expect ( getEffectsCount ( scope ) ) . toBe ( 2 )
152
154
153
155
counter . num = 7
154
156
expect ( dummy ) . toBe ( 7 )
@@ -166,21 +168,21 @@ describe('reactivity/effect/scope', () => {
166
168
effect ( ( ) => ( dummy = counter . num ) )
167
169
} )
168
170
169
- expect ( scope . effects . length ) . toBe ( 1 )
171
+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
170
172
171
173
scope . stop ( )
172
174
175
+ expect ( getEffectsCount ( scope ) ) . toBe ( 0 )
176
+
173
177
scope . run ( ( ) => {
174
178
effect ( ( ) => ( doubled = counter . num * 2 ) )
175
179
} )
176
180
177
- expect ( '[Vue warn] cannot run an inactive effect scope.' ) . toHaveBeenWarned ( )
178
-
179
- expect ( scope . effects . length ) . toBe ( 0 )
181
+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
180
182
181
183
counter . num = 7
182
184
expect ( dummy ) . toBe ( 0 )
183
- expect ( doubled ) . toBe ( undefined )
185
+ expect ( doubled ) . toBe ( 14 )
184
186
} )
185
187
186
188
it ( 'should fire onScopeDispose hook' , ( ) => {
@@ -224,9 +226,9 @@ describe('reactivity/effect/scope', () => {
224
226
it ( 'should dereference child scope from parent scope after stopping child scope (no memleaks)' , ( ) => {
225
227
const parent = effectScope ( )
226
228
const child = parent . run ( ( ) => effectScope ( ) ) !
227
- expect ( parent . scopes ! . includes ( child ) ) . toBe ( true )
229
+ expect ( parent . deps ?. dep ) . toBe ( child )
228
230
child . stop ( )
229
- expect ( parent . scopes ! . includes ( child ) ) . toBe ( false )
231
+ expect ( parent . deps ) . toBeUndefined ( )
230
232
} )
231
233
232
234
it ( 'test with higher level APIs' , async ( ) => {
@@ -290,21 +292,7 @@ describe('reactivity/effect/scope', () => {
290
292
291
293
parentScope . run ( ( ) => {
292
294
const childScope = effectScope ( true )
293
- childScope . on ( )
294
- childScope . off ( )
295
- expect ( getCurrentScope ( ) ) . toBe ( parentScope )
296
- } )
297
- } )
298
-
299
- it ( 'calling on() and off() multiple times inside an active scope should not break currentScope' , ( ) => {
300
- const parentScope = effectScope ( )
301
- parentScope . run ( ( ) => {
302
- const childScope = effectScope ( true )
303
- childScope . on ( )
304
- childScope . on ( )
305
- childScope . off ( )
306
- childScope . off ( )
307
- childScope . off ( )
295
+ setCurrentScope ( setCurrentScope ( childScope ) )
308
296
expect ( getCurrentScope ( ) ) . toBe ( parentScope )
309
297
} )
310
298
} )
@@ -372,7 +360,17 @@ describe('reactivity/effect/scope', () => {
372
360
expect ( watcherCalls ) . toBe ( 3 )
373
361
expect ( cleanupCalls ) . toBe ( 1 )
374
362
375
- expect ( scope . effects . length ) . toBe ( 0 )
376
- expect ( scope . cleanups . length ) . toBe ( 0 )
363
+ expect ( getEffectsCount ( scope ) ) . toBe ( 0 )
364
+ expect ( scope . cleanupsLength ) . toBe ( 0 )
377
365
} )
378
366
} )
367
+
368
+ function getEffectsCount ( scope : EffectScope ) : number {
369
+ let n = 0
370
+ for ( let dep = scope . deps ; dep !== undefined ; dep = dep . nextDep ) {
371
+ if ( dep . dep instanceof ReactiveEffect ) {
372
+ n ++
373
+ }
374
+ }
375
+ return n
376
+ }
0 commit comments