await nextTick()
expect(spy).toHaveBeenCalledTimes(2)
})
+
+ it('watching sources: ref<any[]>', async () => {
+ const foo = ref([1])
+ const spy = jest.fn()
+ watch(foo, () => {
+ spy()
+ })
+ foo.value = foo.value.slice()
+ await nextTick()
+ expect(spy).toBeCalledTimes(1)
+ })
+
+ it('watching multiple sources: computed', async () => {
+ let count = 0
+ const value = ref('1')
+ const plus = computed(() => !!value.value)
+ watch([plus], () => {
+ count++
+ })
+ value.value = '2'
+ await nextTick()
+ expect(plus.value).toBe(true)
+ expect(count).toBe(0)
+ })
})
let getter: () => any
let forceTrigger = false
+ let isMultiSource = false
+
if (isRef(source)) {
getter = () => (source as Ref).value
forceTrigger = !!(source as Ref)._shallow
getter = () => source
deep = true
} else if (isArray(source)) {
+ isMultiSource = true
+ forceTrigger = source.some(isReactive)
getter = () =>
source.map(s => {
if (isRef(s)) {
return NOOP
}
- let oldValue = isArray(source) ? [] : INITIAL_WATCHER_VALUE
+ let oldValue = isMultiSource ? [] : INITIAL_WATCHER_VALUE
const job: SchedulerJob = () => {
if (!runner.active) {
return
if (
deep ||
forceTrigger ||
- hasChanged(newValue, oldValue) ||
+ (isMultiSource
+ ? (newValue as any[]).some((v, i) =>
+ hasChanged(v, (oldValue as any[])[i])
+ )
+ : hasChanged(newValue, oldValue)) ||
(__COMPAT__ &&
isArray(newValue) &&
isCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance))