8000 fix(reactivity): fix WATCH_ARRAY v2 compat deep watching elements (fix #11872) by thecodewarrior · Pull Request #12236 · vuejs/core · GitHub
[go: up one dir, main page]

Skip to content

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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix(reactivity): fix WATCH_ARRAY compat not triggering on mutation
  • Loading branch information
thecodewarrior committed Oct 23, 2024
commit 971412445d8c3e7a1fed0118f6ca6048d7d3c660
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
1 change: 1 addition & 0 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ export function createWatcher(
checkCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
) {
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
0