Skip to content

Commit 288b4ea

Browse files
authored
fix(watch): fix watching reactive array (#1656)
fixes #1655
1 parent d39c037 commit 288b4ea

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ describe('api: watch', () => {
6969
expect(dummy).toMatchObject([1, 0])
7070
})
7171

72+
it('watching single source: array', async () => {
73+
const array = reactive([] as number[])
74+
const spy = jest.fn()
75+
watch(array, spy)
76+
array.push(1)
77+
await nextTick()
78+
expect(spy).toBeCalledTimes(1)
79+
expect(spy).toBeCalledWith([1], expect.anything(), expect.anything())
80+
})
81+
7282
it('watching single source: computed ref', async () => {
7383
const count = ref(0)
7484
const plus = computed(() => count.value + 1)

packages/runtime-core/src/apiWatch.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ function doWatch(
159159
}
160160

161161
let getter: () => any
162-
if (isArray(source)) {
162+
if (isRef(source)) {
163+
getter = () => source.value
164+
} else if (isReactive(source)) {
165+
getter = () => source
166+
deep = true
167+
} else if (isArray(source)) {
163168
getter = () =>
164169
source.map(s => {
165170
if (isRef(s)) {
@@ -172,11 +177,6 @@ function doWatch(
172177
__DEV__ && warnInvalidSource(s)
173178
}
174179
})
175-
} else if (isRef(source)) {
176-
getter = () => source.value
177-
} else if (isReactive(source)) {
178-
getter = () => source
179-
deep = true
180180
} else if (isFunction(source)) {
181181
if (cb) {
182182
// getter with cb

0 commit comments

Comments
 (0)