File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -558,6 +558,30 @@ describe('watch', () => {
558
558
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
559
559
} )
560
560
561
+ test ( 'watching sources: ref<any[]>' , async ( ) => {
562
+ const foo = ref ( [ 1 ] )
563
+ const spy = jest . fn ( )
564
+ watch ( foo , ( ) => {
565
+ spy ( )
566
+ } )
567
+ foo . value = [ ...foo . value ]
568
+ await nextTick ( )
569
+ expect ( spy ) . toBeCalledTimes ( 1 )
570
+ } )
571
+
572
+ test ( 'watching multiple sources: computed' , async ( ) => {
573
+ let count = 0
574
+ const value = ref ( '1' )
575
+ const plus = computed ( ( ) => Boolean ( value . value ) )
576
+ watch ( [ plus ] , ( ) => {
577
+ count ++
578
+ } )
579
+ value . value = '2'
580
+ await nextTick ( )
581
+ expect ( plus . value ) . toBe ( true )
582
+ expect ( count ) . toBe ( 0 )
583
+ } )
584
+
561
585
/** Dividing line, the above tests is directly copy from vue.js **/
562
586
563
587
it ( 'warn when using old simple watch api' , async ( ) => {
Original file line number Diff line number Diff line change @@ -160,6 +160,8 @@ function doWatch(
160
160
161
161
let getter : ( ) => any
162
162
let forceTrigger = false
163
+ let isMultiSource = false
164
+
163
165
if ( isRef ( source ) ) {
164
166
getter = ( ) => ( source as Ref ) . value
165
167
// @ts -expect-error
@@ -168,6 +170,8 @@ function doWatch(
168
170
getter = ( ) => source
169
171
deep = true
170
172
} else if ( isArray ( source ) ) {
173
+ isMultiSource = true
174
+ forceTrigger = source . some ( ( s ) => isReactive ( s ) )
171
175
getter = ( ) =>
172
176
source . map ( ( s ) => {
173
177
if ( isRef ( s ) ) {
@@ -225,7 +229,7 @@ function doWatch(
225
229
}
226
230
}
227
231
228
- let oldValue = isArray ( source ) ? [ ] : INITIAL_WATCHER_VALUE
232
+ let oldValue = isMultiSource ? [ ] : INITIAL_WATCHER_VALUE
229
233
const job : SchedulerJob = ( ) => {
230
234
if ( ! runner . active ) {
231
235
return
@@ -234,7 +238,15 @@ function doWatch(
234
238
if ( cb ) {
235
239
// Watch(source, cb)
236
240
const newValue = runner ( )
237
- if ( deep || forceTrigger || hasChanged ( newValue , oldValue ) ) {
241
+ if (
242
+ deep ||
243
+ forceTrigger ||
244
+ ( isMultiSource
245
+ ? ( newValue as any [ ] ) . some ( ( v , i ) =>
246
+ hasChanged ( v , ( oldValue as any [ ] ) [ i ] )
247
+ )
248
+ : hasChanged ( newValue , oldValue ) )
249
+ ) {
238
250
// Cleanup before running cb again
239
251
if ( cleanup ) {
240
252
cleanup ( )
You can’t perform that action at this time.
0 commit comments