File tree Expand file tree Collapse file tree 2 files changed +45
-2
lines changed Expand file tree Collapse file tree 2 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -5,10 +5,12 @@ import {
5
5
type WatchOptions ,
6
6
type WatchScheduler ,
7
7
computed ,
8
+ effectScope ,
8
9
onWatcherCleanup ,
9
10
ref ,
10
11
watch ,
11
12
} from '../src'
13
+ import { expect } from 'vitest'
12
14
13
15
const queue : ( ( ) => void ) [ ] = [ ]
14
16
@@ -277,4 +279,44 @@ describe('watch', () => {
277
279
278
280
expect ( dummy ) . toEqual ( [ 1 , 2 , 3 ] )
279
281
} )
282
+
283
+ test ( 'removing a watcher while stopping its effectScope' , async ( ) => {
284
+ const count = ref ( 0 )
285
+ const scope = effectScope ( )
286
+ let watcherCalls = 0
287
+ let cleanupCalls = 0
288
+
289
+ scope . run ( ( ) => {
290
+ const stop1 = watch ( count , ( ) => {
291
+ watcherCalls ++
292
+ } )
293
+ watch ( count , ( val , old , onCleanup ) => {
294
+ watcherCalls ++
295
+ onCleanup ( ( ) => {
296
+ cleanupCalls ++
297
+ stop1 ( )
298
+ } )
299
+ } )
300
+ watch ( count , ( ) => {
301
+ watcherCalls ++
302
+ } )
303
+ } )
304
+
305
+ expect ( watcherCalls ) . toBe ( 0 )
306
+ expect ( cleanupCalls ) . toBe ( 0 )
307
+
308
+ count . value ++
309
+ await nextTick ( )
310
+ expect ( watcherCalls ) . toBe ( 3 )
311
+ expect ( cleanupCalls ) . toBe ( 0 )
312
+
313
+ scope . stop ( )
314
+ count . value ++
315
+ await nextTick ( )
316
+ expect ( watcherCalls ) . toBe ( 3 )
317
+ expect ( cleanupCalls ) . toBe ( 1 )
318
+
319
+ expect ( scope . effects . length ) . toBe ( 0 )
320
+ expect ( scope . cleanups . length ) . toBe ( 0 )
321
+ } )
280
322
} )
Original file line number Diff line number Diff line change @@ -118,8 +118,9 @@ export class EffectScope {
118
118
stop ( fromParent ?: boolean ) : void {
119
119
if ( this . _active ) {
120
120
let i , l
121
- for ( i = 0 , l = this . effects . length ; i < l ; i ++ ) {
122
- this . effects [ i ] . stop ( )
121
+ const effects = this . effects . slice ( )
122
+ for ( i = 0 , l = effects . length ; i < l ; i ++ ) {
123
+ effects [ i ] . stop ( )
123
124
}
124
125
const cleanups = this . cleanups . slice ( )
125
126
for ( i = 0 , l = cleanups . length ; i < l ; i ++ ) {
You can’t perform that action at this time.
0 commit comments