File tree Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ export {
53
53
enableTracking ,
54
54
pauseTracking ,
55
55
resetTracking ,
56
+ onEffectCleanup ,
56
57
ReactiveEffect ,
57
58
EffectFlags ,
58
59
type ReactiveEffectRunner ,
Original file line number Diff line number Diff line change 5
5
defineComponent ,
6
6
getCurrentInstance ,
7
7
nextTick ,
8
+ onWatcherCleanup ,
8
9
reactive ,
9
10
ref ,
10
11
watch ,
@@ -394,6 +395,35 @@ describe('api: watch', () => {
394
395
expect ( cleanup ) . toHaveBeenCalledTimes ( 2 )
395
396
} )
396
397
398
+ it ( 'onWatcherCleanup' , async ( ) => {
399
+ const count = ref ( 0 )
400
+ const cleanupEffect = vi . fn ( )
401
+ const cleanupWatch = vi . fn ( )
402
+
403
+ const stopEffect = watchEffect ( ( ) => {
404
+ onWatcherCleanup ( cleanupEffect )
405
+ count . value
406
+ } )
407
+ const stopWatch = watch ( count , ( ) => {
408
+ onWatcherCleanup ( cleanupWatch )
409
+ } )
410
+
411
+ count . value ++
412
+ await nextTick ( )
413
+ expect ( cleanupEffect ) . toHaveBeenCalledTimes ( 1 )
414
+ expect ( cleanupWatch ) . toHaveBeenCalledTimes ( 0 )
415
+
416
+ count . value ++
417
+ await nextTick ( )
418
+ expect ( cleanupEffect ) . toHaveBeenCalledTimes ( 2 )
419
+ expect ( cleanupWatch ) . toHaveBeenCalledTimes ( 1 )
420
+
421
+ stopEffect ( )
422
+ expect ( cleanupEffect ) . toHaveBeenCalledTimes ( 3 )
423
+ stopWatch ( )
424
+ expect ( cleanupWatch ) . toHaveBeenCalledTimes ( 2 )
425
+ } )
426
+
397
427
it ( 'flush timing: pre (default)' , async ( ) => {
398
428
const count = ref ( 0 )
399
429
const count2 = ref ( 0 )
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ export {
28
28
// effect
29
29
effect ,
30
30
stop ,
31
+ onWatcherCleanup ,
31
32
ReactiveEffect ,
32
33
// effect scope
33
34
effectScope ,
You can’t perform that action at this time.
0 commit comments