File tree 2 files changed +34
-2
lines changed
2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -944,4 +944,28 @@ describe('api: watch', () => {
944
944
await nextTick ( )
945
945
expect ( spy ) . toHaveBeenCalledTimes ( 2 )
946
946
} )
947
+
948
+ it ( 'watching sources: ref<any[]>' , async ( ) => {
949
+ const foo = ref ( [ 1 ] )
950
+ const spy = jest . fn ( )
951
+ watch ( foo , ( ) => {
952
+ spy ( )
953
+ } )
954
+ foo . value = foo . value . slice ( )
955
+ await nextTick ( )
956
+ expect ( spy ) . toBeCalledTimes ( 1 )
957
+ } )
958
+
959
+ it ( 'watching multiple sources: computed' , async ( ) => {
960
+ let count = 0
961
+ const value = ref ( '1' )
962
+ const plus = computed ( ( ) => ! ! value . value )
963
+ watch ( [ plus ] , ( ) => {
964
+ count ++
965
+ } )
966
+ value . value = '2'
967
+ await nextTick ( )
968
+ expect ( plus . value ) . toBe ( true )
969
+ expect ( count ) . toBe ( 0 )
970
+ } )
947
971
} )
Original file line number Diff line number Diff line change @@ -171,13 +171,17 @@ function doWatch(
171
171
172
172
let getter : ( ) => any
173
173
let forceTrigger = false
174
+ let isMultiSource = false
175
+
174
176
if ( isRef ( source ) ) {
175
177
getter = ( ) => ( source as Ref ) . value
176
178
forceTrigger = ! ! ( source as Ref ) . _shallow
177
179
} else if ( isReactive ( source ) ) {
178
180
getter = ( ) => source
179
181
deep = true
180
182
} else if ( isArray ( source ) ) {
183
+ isMultiSource = true
184
+ forceTrigger = source . some ( isReactive )
181
185
getter = ( ) =>
182
186
source . map ( s => {
183
187
if ( isRef ( s ) ) {
@@ -265,7 +269,7 @@ function doWatch(
265
269
return NOOP
266
270
}
267
271
268
- let oldValue = isArray ( source ) ? [ ] : INITIAL_WATCHER_VALUE
272
+ let oldValue = isMultiSource ? [ ] : INITIAL_WATCHER_VALUE
269
273
const job : SchedulerJob = ( ) => {
270
274
if ( ! runner . active ) {
271
275
return
@@ -276,7 +280,11 @@ function doWatch(
276
280
if (
277
281
deep ||
278
282
forceTrigger ||
279
- hasChanged ( newValue , oldValue ) ||
283
+ ( isMultiSource
284
+ ? ( newValue as any [ ] ) . some ( ( v , i ) =>
285
+ hasChanged ( v , ( oldValue as any [ ] ) [ i ] )
286
+ )
287
+ : hasChanged ( newValue , oldValue ) ) ||
280
288
( __COMPAT__ &&
281
289
isArray ( newValue ) &&
282
290
isCompatEnabled ( DeprecationTypes . WATCH_ARRAY , instance ) )
You can’t perform that action at this time.
0 commit comments