Skip to content

Commit 9714124

Browse files
fix(reactivity): fix WATCH_ARRAY compat not triggering on mutation
1 parent b4f6786 commit 9714124

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

packages/reactivity/src/watch.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@ export function watch(
264264
: oldValue,
265265
boundCleanup,
266266
]
267+
268+
if (__COMPAT__) {
269+
for (let i = 0; i < args.length - 1; i++) {
270+
if (args[i] && args[i].WATCH_ARRAY_UNWRAP) {
271+
args[i] = args[i].WATCH_ARRAY_UNWRAP
272+
}
273+
}
274+
}
275+
267276
call
268277
? call(cb!, WatchErrorCodes.WATCH_CALLBACK, args)
269278
: // @ts-expect-error

packages/runtime-core/src/componentOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ export function createWatcher(
868868
checkCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
869869
) {
870870
traverse(val, 1)
871+
return { WATCH_ARRAY_UNWRAP: val }
871872
}
872873
return val
873874
}

packages/vue-compat/__tests__/misc.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ test('WATCH_ARRAY', async () => {
6969
expect(spy).toHaveBeenCalledTimes(1)
7070
})
7171

72+
test('WATCH_ARRAY with non-array initial value', async () => {
73+
const spy = vi.fn()
74+
const vm = new Vue({
75+
data() {
76+
return {
77+
foo: null,
78+
}
79+
},
80+
watch: {
81+
foo: spy,
82+
},
83+
}) as any
84+
expect(
85+
deprecationData[DeprecationTypes.WATCH_ARRAY].message,
86+
).not.toHaveBeenWarned()
87+
88+
expect(spy).not.toHaveBeenCalled()
89+
vm.foo = []
90+
await nextTick()
91+
expect(
92+
deprecationData[DeprecationTypes.WATCH_ARRAY].message,
93+
).toHaveBeenWarned()
94+
expect(spy).toHaveBeenCalledTimes(1)
95+
vm.foo.push(1)
96+
await nextTick()
97+
expect(spy).toHaveBeenCalledTimes(2)
98+
})
99+
72100
test('PROPS_DEFAULT_THIS', () => {
73101
let thisCtx: any
74102
const Child = {

0 commit comments

Comments
 (0)