Skip to content

Commit

Permalink
chore: Merge branch 'main' into minor
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 24, 2024
2 parents cd28172 + 9111252 commit fa6c5d8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [3.4.34](https://github.com/vuejs/core/compare/v3.4.33...v3.4.34) (2024-07-24)

* **defineModel:** correct update with multiple changes in same tick ([#11430](https://github.com/vuejs/core/issues/11430)) ([a18f1ec](https://github.com/vuejs/core/commit/a18f1ecf05842337f1eb39a6871adb8cb4024093)), closes [#11429](https://github.com/vuejs/core/issues/11429)



# [3.5.0-alpha.3](https://github.com/vuejs/core/compare/v3.4.33...v3.5.0-alpha.3) (2024-07-19)


Expand Down
25 changes: 15 additions & 10 deletions packages/runtime-core/__tests__/helpers/useModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,32 +614,31 @@ describe('useModel', () => {
})

test('set no change value', async () => {
let changeChildMsg: (() => void) | null = null
let changeChildMsg!: (val: string) => void

const compRender = vi.fn()
const setValue = vi.fn()
const Comp = defineComponent({
props: ['msg'],
emits: ['update:msg'],
setup(props) {
const childMsg = useModel(props, 'msg')
changeChildMsg = () => {
childMsg.value = childMsg.value
}
changeChildMsg = (val: string) => (childMsg.value = val)
return () => {
return childMsg.value
}
},
})

const msg = ref('HI')
const defaultVal = 'defaultVal'
const msg = ref(defaultVal)
const Parent = defineComponent({
setup() {
return () =>
h(Comp, {
msg: msg.value,
'onUpdate:msg': val => {
msg.value = val
compRender()
setValue()
},
})
},
Expand All @@ -648,8 +647,14 @@ describe('useModel', () => {
const root = nodeOps.createElement('div')
render(h(Parent), root)

expect(compRender).toBeCalledTimes(0)
changeChildMsg!()
expect(compRender).toBeCalledTimes(0)
expect(setValue).toBeCalledTimes(0)

changeChildMsg(defaultVal)
expect(setValue).toBeCalledTimes(0)

changeChildMsg('changed')
changeChildMsg(defaultVal)
expect(setValue).toBeCalledTimes(2)
expect(msg.value).toBe(defaultVal)
})
})
13 changes: 8 additions & 5 deletions packages/runtime-core/src/helpers/useModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function useModel(

const res = customRef((track, trigger) => {
let localValue: any
let prevSetValue: any
let prevSetValue: any = EMPTY_OBJ
let prevEmittedValue: any

watchSyncEffect(() => {
Expand All @@ -51,7 +51,10 @@ export function useModel(
},

set(value) {
if (!hasChanged(value, localValue)) {
if (
!hasChanged(value, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
const rawProps = i.vnode!.props
Expand All @@ -78,9 +81,9 @@ export function useModel(
// updates and there will be no prop sync. However the local input state
// may be out of sync, so we need to force an update here.
if (
value !== emittedValue &&
value !== prevSetValue &&
emittedValue === prevEmittedValue
hasChanged(value, emittedValue) &&
hasChanged(value, prevSetValue) &&
!hasChanged(emittedValue, prevEmittedValue)
) {
trigger()
}
Expand Down

0 comments on commit fa6c5d8

Please sign in to comment.