File tree Expand file tree Collapse file tree 2 files changed +30
-8
lines changed Expand file tree Collapse file tree 2 files changed +30
-8
lines changed Original file line number Diff line number Diff line change @@ -1150,4 +1150,26 @@ describe('api: watch', () => {
1150
1150
// own update effect
1151
1151
expect ( instance ! . scope . effects . length ) . toBe ( 1 )
1152
1152
} )
1153
+
1154
+ // #7160
1155
+ test ( 'when using the deep mode of watch, watchCallBack wont be triggered if the value does not change' , async ( ) => {
1156
+ const msg = ref ( 'hi' )
1157
+ const msgTrim = computed ( ( ) => msg . value . trim ( ) )
1158
+
1159
+ let changeCounter = 0
1160
+ let deepChangeCounter = 0
1161
+
1162
+ watch ( msgTrim , ( ) => ++ changeCounter )
1163
+ watch ( msgTrim , ( ) => ++ deepChangeCounter , { deep : true } )
1164
+
1165
+ msg . value = 'hi!'
1166
+ await nextTick ( )
1167
+ expect ( changeCounter ) . toBe ( 1 )
1168
+ expect ( deepChangeCounter ) . toBe ( 1 )
1169
+
1170
+ msg . value = 'hi! '
1171
+ await nextTick ( )
1172
+ expect ( changeCounter ) . toBe ( 1 )
1173
+ expect ( deepChangeCounter ) . toBe ( 1 )
1174
+ } )
1153
1175
} )
Original file line number Diff line number Diff line change @@ -311,13 +311,13 @@ function doWatch(
311
311
// watch(source, cb)
312
312
const newValue = effect . run ( )
313
313
if (
314
- deep ||
315
314
forceTrigger ||
316
315
( isMultiSource
317
- ? ( newValue as any [ ] ) . some ( ( v , i ) =>
318
- hasChanged ( v , ( oldValue as any [ ] ) [ i ] )
316
+ ? ( newValue as any [ ] ) . some (
317
+ ( v , i ) =>
318
+ ( deep && isObject ( v ) ) || hasChanged ( v , ( oldValue as any [ ] ) [ i ] )
319
319
)
320
- : hasChanged ( newValue , oldValue ) ) ||
320
+ : ( deep && isObject ( newValue ) ) || hasChanged ( newValue , oldValue ) ) ||
321
321
( __COMPAT__ &&
322
322
isArray ( newValue ) &&
323
323
isCompatEnabled ( DeprecationTypes . WATCH_ARRAY , instance ) )
@@ -329,11 +329,11 @@ function doWatch(
329
329
callWithAsyncErrorHandling ( cb , instance , ErrorCodes . WATCH_CALLBACK , [
330
330
newValue ,
331
331
// pass undefined as the old value when it's changed for the first time
332
- oldValue === INITIAL_WATCHER_VALUE
332
+ oldValue === INITIAL_WATCHER_VALUE
333
333
? undefined
334
- : ( isMultiSource && oldValue [ 0 ] === INITIAL_WATCHER_VALUE )
335
- ? [ ]
336
- : oldValue ,
334
+ : isMultiSource && oldValue [ 0 ] === INITIAL_WATCHER_VALUE
335
+ ? [ ]
336
+ : oldValue ,
337
337
onCleanup
338
338
] )
339
339
oldValue = newValue
You can’t perform that action at this time.
0 commit comments