Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): fix WATCH_ARRAY v2 compat deep watching elements (fix #11872) #12236

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/reactivity/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ export function watch(
: oldValue,
boundCleanup,
]

if (__COMPAT__) {
for (let i = 0; i < args.length - 1; i++) {
if (args[i] && args[i].WATCH_ARRAY_UNWRAP) {
args[i] = args[i].WATCH_ARRAY_UNWRAP
}
}
}

call
? call(cb!, WatchErrorCodes.WATCH_CALLBACK, args)
: // @ts-expect-error
Expand Down
11 changes: 2 additions & 9 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,22 +860,15 @@ export function createWatcher(
? currentInstance
: null

const newValue = getter()
if (
isArray(newValue) &&
isCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
) {
options.deep = true
thecodewarrior marked this conversation as resolved.
Show resolved Hide resolved
}

const baseGetter = getter
getter = () => {
const val = baseGetter()
if (
isArray(val) &&
checkCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
) {
traverse(val)
traverse(val, 1)
return { WATCH_ARRAY_UNWRAP: val }
}
return val
}
Expand Down
28 changes: 28 additions & 0 deletions packages/vue-compat/__tests__/misc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,34 @@ test('WATCH_ARRAY', async () => {
expect(spy).toHaveBeenCalledTimes(1)
})

test('WATCH_ARRAY with non-array initial value', async () => {
const spy = vi.fn()
const vm = new Vue({
data() {
return {
foo: null,
}
},
watch: {
foo: spy,
},
}) as any
expect(
deprecationData[DeprecationTypes.WATCH_ARRAY].message,
).not.toHaveBeenWarned()

expect(spy).not.toHaveBeenCalled()
vm.foo = []
await nextTick()
expect(
deprecationData[DeprecationTypes.WATCH_ARRAY].message,
).toHaveBeenWarned()
expect(spy).toHaveBeenCalledTimes(1)
vm.foo.push(1)
await nextTick()
expect(spy).toHaveBeenCalledTimes(2)
})

test('PROPS_DEFAULT_THIS', () => {
let thisCtx: any
const Child = {
Expand Down