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 {
5353 enableTracking ,
5454 pauseTracking ,
5555 resetTracking ,
56+ onEffectCleanup ,
5657 ReactiveEffect ,
5758 EffectFlags ,
5859 type ReactiveEffectRunner ,
Original file line number Diff line number Diff line change 55 defineComponent ,
66 getCurrentInstance ,
77 nextTick ,
8+ onWatcherCleanup ,
89 reactive ,
910 ref ,
1011 watch ,
@@ -394,6 +395,35 @@ describe('api: watch', () => {
394395 expect ( cleanup ) . toHaveBeenCalledTimes ( 2 )
395396 } )
396397
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+
397427 it ( 'flush timing: pre (default)' , async ( ) => {
398428 const count = ref ( 0 )
399429 const count2 = ref ( 0 )
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ export {
2828 // effect
2929 effect ,
3030 stop ,
31+ onWatcherCleanup ,
3132 ReactiveEffect ,
3233 // effect scope
3334 effectScope ,
You can’t perform that action at this time.
0 commit comments