Skip to content

Commit

Permalink
fix(watch): fix watching reactive array (#1656)
Browse files Browse the repository at this point in the history
fixes #1655
  • Loading branch information
tanhauhau authored Jul 20, 2020
1 parent d39c037 commit 288b4ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
Expand Down

0 comments on commit 288b4ea

Please sign in to comment.