File tree 2 files changed +37
-1
lines changed
2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -1205,4 +1205,39 @@ describe('api: watch', () => {
1205
1205
expect ( countWE ) . toBe ( 3 )
1206
1206
expect ( countW ) . toBe ( 2 )
1207
1207
} )
1208
+
1209
+ // #5151
1210
+ test ( 'OnCleanup also needs to be cleaned,' , async ( ) => {
1211
+ const spy1 = vi . fn ( )
1212
+ const spy2 = vi . fn ( )
1213
+ const num = ref ( 0 )
1214
+
1215
+ watch ( num , ( value , oldValue , onCleanup ) => {
1216
+ if ( value > 1 ) {
1217
+ return
1218
+ }
1219
+ spy1 ( )
1220
+ onCleanup ( ( ) => {
1221
+ // OnCleanup also needs to be cleaned
1222
+ spy2 ( )
1223
+ } )
1224
+ } )
1225
+
1226
+ num . value ++
1227
+ await nextTick ( )
1228
+ expect ( spy1 ) . toHaveBeenCalledTimes ( 1 )
1229
+ expect ( spy2 ) . toHaveBeenCalledTimes ( 0 )
1230
+
1231
+ num . value ++
1232
+ await nextTick ( )
1233
+
1234
+ expect ( spy1 ) . toHaveBeenCalledTimes ( 1 )
1235
+ expect ( spy2 ) . toHaveBeenCalledTimes ( 1 )
1236
+
1237
+ num . value ++
1238
+ await nextTick ( )
1239
+ // would not be calld when value>1
1240
+ expect ( spy1 ) . toHaveBeenCalledTimes ( 1 )
1241
+ expect ( spy2 ) . toHaveBeenCalledTimes ( 1 )
1242
+ } )
1208
1243
} )
Original file line number Diff line number Diff line change @@ -273,10 +273,11 @@ function doWatch(
273
273
getter = ( ) => traverse ( baseGetter ( ) )
274
274
}
275
275
276
- let cleanup : ( ) => void
276
+ let cleanup : ( ( ) => void ) | undefined
277
277
let onCleanup : OnCleanup = ( fn : ( ) => void ) => {
278
278
cleanup = effect . onStop = ( ) => {
279
279
callWithErrorHandling ( fn , instance , ErrorCodes . WATCH_CLEANUP )
280
+ cleanup = effect . onStop = undefined
280
281
}
281
282
}
282
283
You can’t perform that action at this time.
0 commit comments