fix(runtime-core): watching multiple sources: computed (#3066)
fix #3068
This commit is contained in:
parent
349eb0f0ad
commit
e7300eb479
@ -944,4 +944,28 @@ describe('api: watch', () => {
|
|||||||
await nextTick()
|
await nextTick()
|
||||||
expect(spy).toHaveBeenCalledTimes(2)
|
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -171,6 +171,8 @@ function doWatch(
|
|||||||
|
|
||||||
let getter: () => any
|
let getter: () => any
|
||||||
let forceTrigger = false
|
let forceTrigger = false
|
||||||
|
let isMultiSource = false
|
||||||
|
|
||||||
if (isRef(source)) {
|
if (isRef(source)) {
|
||||||
getter = () => (source as Ref).value
|
getter = () => (source as Ref).value
|
||||||
forceTrigger = !!(source as Ref)._shallow
|
forceTrigger = !!(source as Ref)._shallow
|
||||||
@ -178,6 +180,8 @@ function doWatch(
|
|||||||
getter = () => source
|
getter = () => source
|
||||||
deep = true
|
deep = true
|
||||||
} else if (isArray(source)) {
|
} else if (isArray(source)) {
|
||||||
|
isMultiSource = true
|
||||||
|
forceTrigger = source.some(isReactive)
|
||||||
getter = () =>
|
getter = () =>
|
||||||
source.map(s => {
|
source.map(s => {
|
||||||
if (isRef(s)) {
|
if (isRef(s)) {
|
||||||
@ -265,7 +269,7 @@ function doWatch(
|
|||||||
return NOOP
|
return NOOP
|
||||||
}
|
}
|
||||||
|
|
||||||
let oldValue = isArray(source) ? [] : INITIAL_WATCHER_VALUE
|
let oldValue = isMultiSource ? [] : INITIAL_WATCHER_VALUE
|
||||||
const job: SchedulerJob = () => {
|
const job: SchedulerJob = () => {
|
||||||
if (!runner.active) {
|
if (!runner.active) {
|
||||||
return
|
return
|
||||||
@ -276,7 +280,11 @@ function doWatch(
|
|||||||
if (
|
if (
|
||||||
deep ||
|
deep ||
|
||||||
forceTrigger ||
|
forceTrigger ||
|
||||||
hasChanged(newValue, oldValue) ||
|
(isMultiSource
|
||||||
|
? (newValue as any[]).some((v, i) =>
|
||||||
|
hasChanged(v, (oldValue as any[])[i])
|
||||||
|
)
|
||||||
|
: hasChanged(newValue, oldValue)) ||
|
||||||
(__COMPAT__ &&
|
(__COMPAT__ &&
|
||||||
isArray(newValue) &&
|
isArray(newValue) &&
|
||||||
isCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance))
|
isCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance))
|
||||||
|
Loading…
Reference in New Issue
Block a user