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

[pull] main from vuejs:main #12

Merged
merged 1 commit into from
Dec 31, 2023
Merged
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
18 changes: 18 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ describe('api: watch', () => {
expect(cb).toBeCalledTimes(1)
})

it('should still respect deep: true on shallowReactive source', async () => {
const obj = reactive({ a: 1 })
const arr = shallowReactive([obj])

let dummy
watch(
arr,
() => {
dummy = arr[0].a
},
{ deep: true },
)

obj.a++
await nextTick()
expect(dummy).toBe(2)
})

it('watching multiple sources', async () => {
const state = reactive({ count: 1 })
const count = ref(1)
Expand Down
14 changes: 8 additions & 6 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ function doWatch(

const instance =
getCurrentScope() === currentInstance?.scope ? currentInstance : null
// const instance = currentInstance
const reactiveGetter = (source: object) =>
deep === true
? source // traverse will happen in wrapped getter below
: // for shallow or deep: false, only traverse root-level properties
traverse(source, isShallow(source) || deep === false ? 1 : undefined)

let getter: () => any
let forceTrigger = false
let isMultiSource = false
Expand All @@ -231,10 +236,7 @@ function doWatch(
getter = () => source.value
forceTrigger = isShallow(source)
} else if (isReactive(source)) {
getter =
isShallow(source) || deep === false
? () => traverse(source, 1)
: () => traverse(source)
getter = () => reactiveGetter(source)
forceTrigger = true
} else if (isArray(source)) {
isMultiSource = true
Expand All @@ -244,7 +246,7 @@ function doWatch(
if (isRef(s)) {
return s.value
} else if (isReactive(s)) {
return traverse(s, isShallow(s) || deep === false ? 1 : undefined)
return reactiveGetter(s)
} else if (isFunction(s)) {
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
} else {
Expand Down
Loading