fix(watch): fix watching reactive array (#1656)

fixes #1655
This commit is contained in:
Tan Li Hau 2020-07-21 00:39:22 +08:00 committed by GitHub
parent d39c03771b
commit 288b4eab9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -69,6 +69,16 @@ describe('api: watch', () => {
expect(dummy).toMatchObject([1, 0])
})
it('watching single source: array', async () => {
const array = reactive([] as number[])
const spy = jest.fn()
watch(array, spy)
array.push(1)
await nextTick()
expect(spy).toBeCalledTimes(1)
expect(spy).toBeCalledWith([1], expect.anything(), expect.anything())
})
it('watching single source: computed ref', async () => {
const count = ref(0)
const plus = computed(() => count.value + 1)

View File

@ -159,7 +159,12 @@ function doWatch(
}
let getter: () => any
if (isArray(source)) {
if (isRef(source)) {
getter = () => source.value
} else if (isReactive(source)) {
getter = () => source
deep = true
} else if (isArray(source)) {
getter = () =>
source.map(s => {
if (isRef(s)) {
@ -172,11 +177,6 @@ function doWatch(
__DEV__ && warnInvalidSource(s)
}
})
} else if (isRef(source)) {
getter = () => source.value
} else if (isReactive(source)) {
getter = () => source
deep = true
} else if (isFunction(source)) {
if (cb) {
// getter with cb