Skip to content

Commit 7f76378

Browse files
authored
fix(runtime-core): force model update when reverted before sync (#14897)
close #13524
1 parent c11f6ee commit 7f76378

2 files changed

Lines changed: 92 additions & 14 deletions

File tree

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,76 @@ describe('useModel', () => {
613613
expect(compRender).toHaveBeenCalledTimes(3)
614614
})
615615

616+
// #13524
617+
test('force local update when an intermediate model value is reverted before parent update', async () => {
618+
let childMsg: Ref<string>
619+
620+
const compRender = vi.fn()
621+
const parentRender = vi.fn()
622+
623+
const Comp = defineComponent({
624+
props: ['msg'],
625+
emits: ['update:msg'],
626+
setup(props) {
627+
childMsg = useModel(props, 'msg')
628+
return () => {
629+
compRender()
630+
return h('input', {
631+
// simulate how v-model works
632+
onVnodeBeforeMount(vnode) {
633+
;(vnode.el as TestElement).props.value = childMsg.value
634+
},
635+
onVnodeBeforeUpdate(vnode) {
636+
;(vnode.el as TestElement).props.value = childMsg.value
637+
},
638+
onModelInput(value: any) {
639+
childMsg.value = value
640+
},
641+
onInput() {
642+
childMsg.value = 'a'
643+
},
644+
})
645+
}
646+
},
647+
})
648+
649+
const msg = ref('a')
650+
const Parent = defineComponent({
651+
setup() {
652+
return () => {
653+
parentRender()
654+
return h(Comp, {
655+
msg: msg.value,
656+
'onUpdate:msg': val => {
657+
msg.value = val
658+
},
659+
})
660+
}
661+
},
662+
})
663+
664+
const root = nodeOps.createElement('div')
665+
render(h(Parent), root)
666+
667+
expect(parentRender).toHaveBeenCalledTimes(1)
668+
expect(compRender).toHaveBeenCalledTimes(1)
669+
expect(serializeInner(root)).toBe('<input value="a"></input>')
670+
671+
const input = root.children[0] as TestElement
672+
673+
// simulate a browser that does not flush microtasks between event listeners
674+
;['ab', 'ac', 'ad'].forEach(value => {
675+
input.props.onModelInput((input.props.value = value))
676+
input.props.onInput()
677+
})
678+
await nextTick()
679+
680+
expect(msg.value).toBe('a')
681+
expect(parentRender).toHaveBeenCalledTimes(2)
682+
expect(compRender).toHaveBeenCalledTimes(2)
683+
expect(serializeInner(root)).toBe('<input value="a"></input>')
684+
})
685+
616686
test('set no change value', async () => {
617687
let changeChildMsg!: (val: string) => void
618688

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,17 @@ export function useModel(
6565
return
6666
}
6767
const rawProps = i.vnode!.props
68-
if (
69-
!(
70-
rawProps &&
71-
// check if parent has passed v-model
72-
(name in rawProps ||
73-
camelizedName in rawProps ||
74-
hyphenatedName in rawProps) &&
75-
(`onUpdate:${name}` in rawProps ||
76-
`onUpdate:${camelizedName}` in rawProps ||
77-
`onUpdate:${hyphenatedName}` in rawProps)
78-
)
79-
) {
68+
const hasVModel = !!(
69+
rawProps &&
70+
// check if parent has passed v-model
71+
(name in rawProps ||
72+
camelizedName in rawProps ||
73+
hyphenatedName in rawProps) &&
74+
(`onUpdate:${name}` in rawProps ||
75+
`onUpdate:${camelizedName}` in rawProps ||
76+
`onUpdate:${hyphenatedName}` in rawProps)
77+
)
78+
if (!hasVModel) {
8079
// no v-model, local update
8180
localValue = value
8281
trigger()
@@ -88,9 +87,18 @@ export function useModel(
8887
// updates and there will be no prop sync. However the local input state
8988
// may be out of sync, so we need to force an update here.
9089
if (
91-
hasChanged(value, emittedValue) &&
9290
hasChanged(value, prevSetValue) &&
93-
!hasChanged(emittedValue, prevEmittedValue)
91+
((hasChanged(value, emittedValue) &&
92+
!hasChanged(emittedValue, prevEmittedValue)) ||
93+
// #13524: browsers differ in when they flush microtasks between
94+
// event listeners. If a v-model listener emits an intermediate value
95+
// and a following listener restores the model to its previous prop
96+
// value before parent updates are flushed, the parent render can be
97+
// deduped as having no prop change. Force a local update so DOM state
98+
// such as an input's value is synchronized back to the current model.
99+
(hasVModel &&
100+
prevSetValue !== EMPTY_OBJ &&
101+
!hasChanged(emittedValue, localValue)))
94102
) {
95103
trigger()
96104
}

0 commit comments

Comments
 (0)