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): when using the deep mode of watch, watchCallBack will be triggered even if the value does not change (fix #7160) #7167

Closed
wants to merge 1 commit 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
22 changes: 22 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,4 +1150,26 @@ describe('api: watch', () => {
// own update effect
expect(instance!.scope.effects.length).toBe(1)
})

// #7160
test('when using the deep mode of watch, watchCallBack wont be triggered if the value does not change', async () => {
const msg = ref('hi')
const msgTrim = computed(() => msg.value.trim())

let changeCounter = 0
let deepChangeCounter = 0

watch(msgTrim, () => ++changeCounter)
watch(msgTrim, () => ++deepChangeCounter, { deep: true })

msg.value = 'hi!'
await nextTick()
expect(changeCounter).toBe(1)
expect(deepChangeCounter).toBe(1)

msg.value = 'hi! '
await nextTick()
expect(changeCounter).toBe(1)
expect(deepChangeCounter).toBe(1)
})
})
16 changes: 8 additions & 8 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,13 @@ function doWatch(
// watch(source, cb)
const newValue = effect.run()
if (
deep ||
forceTrigger ||
(isMultiSource
? (newValue as any[]).some((v, i) =>
hasChanged(v, (oldValue as any[])[i])
? (newValue as any[]).some(
(v, i) =>
(deep && isObject(v)) || hasChanged(v, (oldValue as any[])[i])
)
: hasChanged(newValue, oldValue)) ||
: (deep && isObject(newValue)) || hasChanged(newValue, oldValue)) ||
(__COMPAT__ &&
isArray(newValue) &&
isCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance))
Expand All @@ -329,11 +329,11 @@ function doWatch(
callWithAsyncErrorHandling(cb, instance, ErrorCodes.WATCH_CALLBACK, [
newValue,
// pass undefined as the old value when it's changed for the first time
oldValue === INITIAL_WATCHER_VALUE
oldValue === INITIAL_WATCHER_VALUE
? undefined
: (isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE)
? []
: oldValue,
: isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE
? []
: oldValue,
LittleSound marked this conversation as resolved.
Show resolved Hide resolved
onCleanup
])
oldValue = newValue
Expand Down