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(runtime-core): incorrect callback parameters when reactive array ref passes to watch #9417

Closed
wants to merge 2 commits into from
Closed
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
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 @@ -94,6 +94,16 @@ describe('api: watch', () => {
expect(spy).toBeCalledWith([1], expect.anything(), expect.anything())
})

it('watching single source: reactive with array ref', async () => {
const foo = reactive([ref(0)])
const callback = vi.fn()
watch(foo, callback)
foo[0].value++
await nextTick()
expect(callback).toBeCalledTimes(1)
expect(callback).toBeCalledWith([1], expect.anything(), expect.anything())
})

it('should not fire if watched getter result did not change', async () => {
const spy = vi.fn()
const n = ref(0)
Expand Down
7 changes: 5 additions & 2 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ function doWatch(
getCurrentScope() === currentInstance?.scope ? currentInstance : null
// const instance = currentInstance
let getter: () => any
let sourceIsReactive = false
let forceTrigger = false
let isMultiSource = false

if (isRef(source)) {
getter = () => source.value
forceTrigger = isShallow(source)
} else if (isReactive(source)) {
} else if ((sourceIsReactive = isReactive(source)) && !isArray(source)) {
getter = () => source
deep = true
} else if (isArray(source)) {
Expand All @@ -222,6 +222,9 @@ function doWatch(
return traverse(s)
} else if (isFunction(s)) {
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
} else if (sourceIsReactive) {
deep = true
return s
} else {
__DEV__ && warnInvalidSource(s)
}
Expand Down