Skip to content

Commit a18f1ec

Browse files
authoredJul 24, 2024··
fix(defineModel): correct update with multiple changes in same tick (#11430)
close #11429
1 parent 422ef34 commit a18f1ec

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed
 

‎packages/runtime-core/__tests__/helpers/useModel.spec.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -614,32 +614,31 @@ describe('useModel', () => {
614614
})
615615

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

619-
const compRender = vi.fn()
619+
const setValue = vi.fn()
620620
const Comp = defineComponent({
621621
props: ['msg'],
622622
emits: ['update:msg'],
623623
setup(props) {
624624
const childMsg = useModel(props, 'msg')
625-
changeChildMsg = () => {
626-
childMsg.value = childMsg.value
627-
}
625+
changeChildMsg = (val: string) => (childMsg.value = val)
628626
return () => {
629627
return childMsg.value
630628
}
631629
},
632630
})
633631

634-
const msg = ref('HI')
632+
const defaultVal = 'defaultVal'
633+
const msg = ref(defaultVal)
635634
const Parent = defineComponent({
636635
setup() {
637636
return () =>
638637
h(Comp, {
639638
msg: msg.value,
640639
'onUpdate:msg': val => {
641640
msg.value = val
642-
compRender()
641+
setValue()
643642
},
644643
})
645644
},
@@ -648,8 +647,14 @@ describe('useModel', () => {
648647
const root = nodeOps.createElement('div')
649648
render(h(Parent), root)
650649

651-
expect(compRender).toBeCalledTimes(0)
652-
changeChildMsg!()
653-
expect(compRender).toBeCalledTimes(0)
650+
expect(setValue).toBeCalledTimes(0)
651+
652+
changeChildMsg(defaultVal)
653+
expect(setValue).toBeCalledTimes(0)
654+
655+
changeChildMsg('changed')
656+
changeChildMsg(defaultVal)
657+
expect(setValue).toBeCalledTimes(2)
658+
expect(msg.value).toBe(defaultVal)
654659
})
655660
})

‎packages/runtime-core/src/helpers/useModel.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function useModel(
3333

3434
const res = customRef((track, trigger) => {
3535
let localValue: any
36-
let prevSetValue: any
36+
let prevSetValue: any = EMPTY_OBJ
3737
let prevEmittedValue: any
3838

3939
watchSyncEffect(() => {
@@ -51,7 +51,10 @@ export function useModel(
5151
},
5252

5353
set(value) {
54-
if (!hasChanged(value, localValue)) {
54+
if (
55+
!hasChanged(value, localValue) &&
56+
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
57+
) {
5558
return
5659
}
5760
const rawProps = i.vnode!.props

0 commit comments

Comments
 (0)
Please sign in to comment.