Now I understand the issue better. Looking at the playground URL from the issue, the reproduction is:
<script setup>
const model = defineModel({
set(value) {
// Only allow "a"
return value === 'a' ? 'a' : model.value
}
})
</script>
<template>
<input v-model="model" />
</template>
When the user types anything other than "a" (or any combination), the setter should reject it and return the previous value. The issue is that on Firefox, this doesn't work properly.
The problem is in how vModelText's beforeUpdate hook handles updating the input value when the element is focused. Let me re-examine the code:
beforeUpdate(
el,
{ value, oldValue, modifiers: { lazy, trim, number } },
vnode,
) {
el[assignKey] = getModelAssigner(vnode)
// avoid clearing unresolved text. #2302
if ((el as any).composing) return
const elValue =
(number || el.type === 'number') && !/^0\d/.test(el.value)
? looseToNumber(el.value)
: el.value
const newValue = value == null ? '' : value
if (elValue === newValue) {
return
}
const rootNode = el.getRootNode()
if (
(rootNode instanceof Document || rootNode instanceof ShadowRoot) &&
rootNode.activeElement === el &&
el.type !== 'range'
) {
// #8546
if (lazy && value === oldValue) {
return
}
if (trim && el.value.trim() === newValue) {
return
}
}
el.value = newValue
},
The issue is in this beforeUpdate hook. When the input element is focused, Vue skips updating the el.value to avoid cursor jumping issues. However, when using defineModel with a setter that rejects certain values, this behavior causes a problem on Firefox.
Here's what happens:
- User focuses the input and types "b"
- The
input event fires, which triggers the setter
- The setter sees "ab", compares it with the previous value "a", and returns "a" (rejects the change)
- The component re-renders because the parent's prop changes
- In
beforeUpdate, elValue is "ab" (the current DOM value) and newValue is "a" (the prop value after rejection)
- Since
elValue !== newValue ("ab" !== "a"), we don't return early
- But since the element is the active element (focused), and
lazy is false and trim doesn't apply, we should proceed to update el.value = newValue
Wait, actually the code should work correctly then. Let me re-read the issue.
The issue says "It should only keep 'a' and nothing else should 'make it through'" on Firefox, but "it won't on firefox." So on Chrome, the input correctly reverts to "a" when the user types something else, but on Firefox, the invalid character stays in the input.
Hmm, the key might be in how the input event is handled. Let me look at the input handler again:
addEventListener(el, lazy ? 'change' : 'input', e => {
if ((e.target as any).composing) return
el[assignKey](castValue(el.value, trim, castToNumber))
})
The input handler gets the el.value at the time the event fires and passes it to the assigner. The assigner is getModelAssigner(vnode) which is onUpdate:modelValue.
So when the user types "b" in the input:
- The
input event fires
el.value is "ab" (the browser has already updated the DOM)
el[assignKey]("ab") is called, which triggers the setter
- The setter returns "a" (rejects the change)
- The parent's prop is updated to "a" (because the setter returned "a")
Wait, no! Let me re-read the setter:
set(value) {
return value === 'a' ? 'a' : model.value
}
So when the user types "b", value is "ab", the setter returns model.value which is "a", and the setter returns "a". But what gets emitted?
Actually, looking at useModel.ts again:
set(value) {
const emittedValue = options.set ? options.set(value) : value
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
...
i.emit(`update:${name}`, emittedValue)
...
}
So emittedValue is options.set(value) which is the return value of the setter. If the setter returns "a", then emittedValue is "a", and i.emit("update:modelValue", "a") is called.
Then the parent's onUpdate:modelValue handler sets the prop to "a". But wait, the prop was already "a", so the parent doesn't re-render... or does it?
Actually, the issue is more subtle. Let me trace through more carefully:
- Initial state:
model.value is "a", parent's prop modelValue is "a"
- User types "b" in the input
input event fires, el.value is "ab"
el[assignKey]("ab") is called
- In
useModel's setter, value is "ab", options.set("ab") returns "a" (the setter rejects the change)
emittedValue is "a"
hasChanged("a", "a") is false (localValue is "a"), but prevSetValue is EMPTY_OBJ, so prevSetValue !== EMPTY_OBJ is false
- So
!hasChanged(emittedValue, localValue) is !false = true, and !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) is !false = true
- Wait, both conditions are
true, so the && is true, and if (true) returns early!
Wait, that's the problem! If both conditions are true, we return early without emitting anything. Let me re-read:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
hasChanged(emittedValue, localValue) = hasChanged("a", "a") = false
- So
!hasChanged(emittedValue, localValue) = true
prevSetValue is EMPTY_OBJ (initially), so prevSetValue !== EMPTY_OBJ = false
- So
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) = !false = true
- Both conditions are
true, so return is executed without emitting anything!
But wait, after the first set, prevSetValue is updated to the value. Let me trace through again with a second key press:
After the first key press that's rejected:
prevSetValue = "ab" (set at the end of the setter)
Second key press (e.g., user types "c" when the input shows "a"):
- User types "c",
el.value becomes "ac"
el[assignKey]("ac") is called
- In
useModel's setter, value is "ac", options.set("ac") returns "a" (the setter rejects the change)
emittedValue is "a"
hasChanged("a", "a") = false (localValue is "a")
- So
!hasChanged(emittedValue, localValue) = true
prevSetValue is "ab" from the previous set, so prevSetValue !== EMPTY_OBJ = true
hasChanged("ac", "ab") = true
- So
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) = !true = false
true && false = false, so we don't return early
- We proceed to emit:
i.emit("update:modelValue", "a")
- The parent receives the emission and sets its prop to "a"
- But wait, the parent's prop was already "a", so does it re-render?
Hmm, but the parent doesn't know that the value was rejected. The parent just receives onUpdate:modelValue with "a" and sets modelValue.value = "a". If modelValue is already "a", it might not trigger a re-render.
Actually, wait. Let me think about this differently. The issue is about the input event handling and how the value gets reverted.
Actually, I think the issue might be more about the interaction between Vue's reactivity and the browser's handling of the input event. Let me think about this more carefully.
Actually, looking at the issue again, I think the problem is more fundamental. The setter in defineModel with a custom set option doesn't actually prevent the value from being "accepted" - it just transforms it. But the input event has already fired with the new value, and the browser has already updated the DOM.
When the setter rejects a value (returns the previous value instead), the parent's prop doesn't change (because the emitted value is the same as before), so there's no re-render triggered. This means the input's value is not updated to show the "rejected" value.
But wait, that doesn't explain why it works on Chrome but not Firefox. Both browsers should have the same behavior...
Let me look at the issue description again:
Seems like either a race condition or firefox not updating at the same time as Chrome? no clue.
This suggests that there's a timing difference between Chrome and Firefox in how they handle the input event. Maybe Firefox fires the input event before the DOM is fully updated with the new value, while Chrome fires it after.
Actually, I think I might be overcomplicating this. Let me look at the playground URL more carefully.
The playground URL is:
https://play.vuejs.org/#eNqFUslOwzAQ/RXLlxSppAc4lRaxqIcisQgQJ1+idBJcnLHlJVSq8u+MnbYUynKy573xm+eZWfNLY/I2AB/ziSutNJ458MEwVWA9Fdw7wc8FysZo69maWag6VlndsIxeZTvmWjdmg+ejGETR7EygwFKj86xodEDPplFhkGVHAiejviDJU+ChMarwQBFjkyTXHjd6AYpc9I8FHxE7Ge2l8iE5pAKVrPOl00jfWEcBwUtSkArsvfGSDAg+ZomJXKGUfr9JmLcBhlu8fIXy7Qd86VYRE/zBggPbguA7zhe2BrIW6dnTHazoviPJf1CU/Qf5CE6rED32aVcBF2R7Ly+5nac2S6yf3WzlAd32U9FozOxSvuDU9ti8377+afckP03vBHbUxe3I/tmDfpZpLDTKBVQS4TZGAxqowCpgGWsyjXM0wQ+gBfRjNovHUe9CQRKQlYTFS6ECkFBW0CaxXjdvN+CXJHL5bWMOdkbGintLk07B2UUiCNiYIuhwj7oPe2AHrg==
Let me try to decode this.
Actually, I think I need to focus on the actual issue. The problem is that when using defineModel with a custom set function that rejects certain values, the rejection doesn't work properly on Firefox. The typical use case is:
const model = defineModel({
set(value) {
// Only allow 'a', reject everything else
return value === 'a' ? 'a' : model.value
}
})
The issue is that on Firefox, when the user types a character that should be rejected, the character still appears in the input field.
Looking at the code flow:
- User types "b" in the input
input event fires
el[assignKey]("ab") is called
- In
useModel's setter, value is "ab", the setter returns "a"
- The setter checks if the emitted value has changed...
Actually, I think the issue is in the beforeUpdate hook of vModelText. Let me look at it again:
beforeUpdate(
el,
{ value, oldValue, modifiers: { lazy, trim, number } },
vnode,
) {
...
const elValue = el.value
const newValue = value == null ? '' : value
if (elValue === newValue) {
return
}
const rootNode = el.getRootNode()
if (
(rootNode instanceof Document || rootNode instanceof ShadowRoot) &&
rootNode.activeElement === el &&
el.type !== 'range'
) {
// #8546
if (lazy && value === oldValue) {
return
}
if (trim && el.value.trim() === newValue) {
return
}
}
el.value = newValue
},
The issue is that when the user types "b" and the setter rejects it:
elValue is "ab" (the browser has already updated the input)
newValue is "a" (the value after rejection)
elValue !== newValue, so we don't return early
- The element is focused (active element), so we enter the
if block
lazy is false, so lazy && value === oldValue is false
trim is not set (assuming), so trim && el.value.trim() === newValue is false
- We should then execute
el.value = newValue
This should work correctly! The input value should be updated to "a".
Unless... there's something special about Firefox that causes the value not to be updated.
Wait, actually, I think I see the issue now! The problem might be that when el[assignKey]("ab") is called, it triggers the setter synchronously, which emits to the parent, which sets the model, which triggers a re-render, which calls beforeUpdate. But all of this happens synchronously within the input event handler.
In the input event handler, we're in the middle of the event dispatch. When we update el.value in beforeUpdate, we're changing the value of the input element while the input event is still being processed. On some browsers, this might cause the event to re-fire or might cause unexpected behavior.
Actually, that doesn't make sense either, because the input event has already finished dispatching by the time beforeUpdate runs. The event handler has returned, and Vue's reactivity system is updating the DOM asynchronously (via the scheduler).
Wait, actually Vue's scheduler might not be async. Let me think about this again.
When the user types "b" in the input:
- The
input event fires
- The event handler calls
el[assignKey]("ab")
el[assignKey] is getModelAssigner(vnode) which is vnode.props['onUpdate:modelValue']
- This calls the parent's
onUpdate:modelValue handler, which sets the model's value
- Setting the model's value triggers Vue's reactivity system
- The reactivity system schedules a re-render
But wait, setting a ref in Vue triggers the watchers synchronously (not via the scheduler). So when the parent sets model.value = 'a', the child's watchSyncEffect that watches props.modelValue will immediately update localValue to 'a'.
Hmm, but this should still work correctly. Let me think about this more carefully.
Actually, I think the key issue might be in how the setter handles the prevSetValue and prevEmittedValue tracking. Let me look at the setter again:
set(value) {
const emittedValue = options.set ? options.set(value) : value
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
...
i.emit(`update:${name}`, emittedValue)
...
prevSetValue = value
prevEmittedValue = emittedValue
},
Let me trace through the sequence of events when the user types "b":
Initial state:
localValue = "a" (from the prop)
prevSetValue = EMPTY_OBJ
prevEmittedValue = undefined
User types "b":
input event fires
el.value is "ab"
el[assignKey]("ab") is called
- In the setter,
value = "ab"
options.set("ab") returns "a" (the setter rejects the change)
emittedValue = "a"
- Check:
!hasChanged("a", "a") = !false = true
- Check:
!(EMPTY_OBJ !== EMPTY_OBJ && hasChanged("ab", EMPTY_OBJ)) = !false = true
- Both conditions are
true, so return is executed without emitting!
But wait, this is the first key press. What about subsequent key presses?
After the first key press (rejected):
prevSetValue is still EMPTY_OBJ (we returned early)
prevEmittedValue is still undefined
Second key press (e.g., user types "c"):
- User types "c" when the input shows "a"
el.value becomes "ac"
el[assignKey]("ac") is called
- In the setter,
value = "ac"
options.set("ac") returns "a"
emittedValue = "a"
- Check:
!hasChanged("a", "a") = !false = true
- Check:
!(EMPTY_OBJ !== EMPTY_OBJ && hasChanged("ac", EMPTY_OBJ)) = !false = true
- Both conditions are
true, so return is executed!
So the setter never emits anything because prevSetValue is never updated (we always return early).
This means the parent never receives any update events, so the parent never re-renders the child. The child's localValue stays as "a", but the input's DOM value stays as whatever the user typed.
But wait, this should be the same behavior on Chrome and Firefox. Why would it work on Chrome but not Firefox?
Unless the issue is about how the input event handler gets the value. Let me think about this...
Actually, I think the issue is about the timing of when the input event fires relative to when the DOM is updated. In some browsers, the input event might fire before the DOM value is updated, while in other browsers, it might fire after.
Actually, I just realized something. Looking at the input handler:
addEventListener(el, lazy ? 'change' : 'input', e => {
if ((e.target as any).composing) return
el[assignKey](castValue(el.value, trim, castToNumber))
})
The handler gets el.value at the time the event fires. In some browsers, el.value might be the value before the new character is inserted, while in other browsers, it might be the value after.
But wait, the input event is supposed to fire after the DOM is updated with the new value. So el.value should always be the new value after the input event fires.
Let me think about this differently. The issue says "It should only keep 'a' and nothing else should 'make it through'". So the setter is supposed to reject any input that's not exactly "a". But on Firefox, the input accepts the character even though the setter rejects it.
I think the issue might be that when the setter rejects the value (returns the old value), the input event handler has already read the new value from the DOM, so the input's DOM value is not updated to reflect the rejection.
But this should be the same on Chrome and Firefox. Unless there's some browser-specific behavior I'm missing.
Actually, let me re-read the useModel setter code one more time:
set(value) {
const emittedValue = options.set ? options.set(value) : value
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
const rawProps = i.vnode!.props
if (
!(
rawProps &&
// check if parent has passed v-model
(name in rawProps ||
camelizedName in rawProps ||
hyphenatedName in rawProps) &&
(`onUpdate:${name}` in rawProps ||
`onUpdate:${camelizedName}` in rawProps ||
`onUpdate:${hyphenatedName}` in rawProps)
)
) {
// no v-model, local update
localValue = value
trigger()
}
i.emit(`update:${name}`, emittedValue)
// #10279: if the local value is converted via a setter but the value
// emitted to parent was the same, the parent will not trigger any
// 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 (
hasChanged(value, emittedValue) &&
hasChanged(value, prevSetValue) &&
!hasChanged(emittedValue, prevEmittedValue)
) {
trigger()
}
prevSetValue = value
prevEmittedValue = emittedValue
},
I see the issue now! When prevSetValue is EMPTY_OBJ, the condition prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue) is false, so !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) is true. This means if both conditions are true (which they are on the first key press), we return early without emitting.
But after the first key press, prevSetValue should be updated to the value that was set. But we returned early, so prevSetValue is never updated!
This is the bug. The prevSetValue = value line is after the return, so when we return early, prevSetValue is never updated. This means on subsequent key presses, prevSetValue is still EMPTY_OBJ, and we always return early.
But wait, why would this work on Chrome but not Firefox? Maybe on Chrome, the input event handler reads the old value (before the new character is inserted), so value is "a" and the setter returns "a" (no change), so no re-render is needed. On Firefox, the input event handler reads the new value ("ab"), so value is "ab" and the setter returns "a" (rejects the change), and the rejection doesn't work because we return early.
But that doesn't make sense. The input event should always fire after the DOM is updated. Both browsers should read the new value.
Let me look at this from a different angle. Maybe the issue is not in the useModel setter but in the vModelText directive. Let me look at the beforeUpdate hook again:
beforeUpdate(
el,
{ value, oldValue, modifiers: { lazy, trim, number } },
vnode,
) {
el[assignKey] = getModelAssigner(vnode)
// avoid clearing unresolved text. #2302
if ((el as any).composing) return
const elValue =
(number || el.type === 'number') && !/^0\d/.test(el.value)
? looseToNumber(el.value)
: el.value
const newValue = value == null ? '' : value
if (elValue === newValue) {
return
}
const rootNode = el.getRootNode()
if (
(rootNode instanceof Document || rootNode instanceof ShadowRoot) &&
rootNode.activeElement === el &&
el.type !== 'range'
) {
// #8546
if (lazy && value === oldValue) {
return
}
if (trim && el.value.trim() === newValue) {
return
}
}
el.value = newValue
},
The issue might be in how the beforeUpdate hook determines whether to update the input value. Let me think about this...
Actually, I think I finally understand the issue! The problem is that when the setter rejects the value, the condition !hasChanged(emittedValue, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) evaluates to true on the first key press because prevSetValue is EMPTY_OBJ. This causes an early return without emitting to the parent.
On the second key press (and subsequent ones), prevSetValue is still EMPTY_OBJ (because we never update it), so the same thing happens.
But wait, I just realized that on the first key press, when we return early, the input's DOM value is still "ab". The beforeUpdate hook should catch this and update the input value. But beforeUpdate only runs when there's a re-render triggered by a prop change. Since we returned early without emitting, the parent's prop doesn't change, and no re-render is triggered.
So the issue is:
- User types "b"
input event fires, el.value is "ab"
- Setter returns "a" (rejects), but we return early because
prevSetValue is EMPTY_OBJ
- No emit to parent, no prop change, no re-render
- The input's DOM value stays as "ab"
But wait, if the setter returns early without emitting, then the parent's prop doesn't change, and the child doesn't re-render. The beforeUpdate hook never runs. So the input's value stays as "ab".
This explains why the rejection doesn't work on any browser. So why does the issue say it works on Chrome?
Maybe on Chrome, the input event handler reads the old value? Let me think...
Actually, wait. Let me re-read the setter condition:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
The condition !hasChanged(emittedValue, localValue) is true when the emitted value equals the local value. In our case, emittedValue is "a" and localValue is "a", so this is true.
The condition !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) is true when prevSetValue is EMPTY_OBJ or when the current value equals the previous set value. In our case, prevSetValue is EMPTY_OBJ, so this is true.
Both conditions are true, so we return early. This is the intended behavior for when the user types the same value again (no change needed). But it's not the intended behavior for when the user types a new value that gets rejected by the setter.
The issue is that prevSetValue is supposed to track the last value that was set on the model ref. When the user types "b", the model ref is set to "ab". Even though the setter rejects this and emits "a", the model ref's local value should be updated to track "ab" as the last set value. But since we return early, prevSetValue is never updated.
Actually wait, I need to think about this more carefully. The model ref is a customRef with a getter and setter. When we call model.value = "ab", the setter is called. In the setter, prevSetValue = value is supposed to update prevSetValue to the value that was set on the ref, regardless of what the setter function returns.
But the setter returns early before reaching prevSetValue = value. So prevSetValue is never updated.
This seems like a bug in the condition. The condition should not return early when the value has changed but was rejected by the setter.
Actually, let me re-read the condition:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
This condition is designed to avoid emitting redundant updates. The idea is:
- If the emitted value hasn't changed from the local value, and
- The current value is the same as the previous set value,
- Then don't emit (because the value hasn't changed).
But the problem is that when prevSetValue is EMPTY_OBJ, the second condition is always true, so we return early even when the value has changed.
Actually, let me think about this again. The condition is:
!hasChanged(emittedValue, localValue) - the emitted value is the same as the local value
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) - the current value is the same as the previous set value, OR prevSetValue is EMPTY_OBJ
The second condition is true when:
prevSetValue is EMPTY_OBJ (initial state)
prevSetValue is not EMPTY_OBJ and hasChanged(value, prevSetValue) is false (same value as before)
So the entire condition is true when:
- The emitted value equals the local value, AND
prevSetValue is EMPTY_OBJ OR the current value equals the previous set value
In our case:
emittedValue is "a" and localValue is "a", so !hasChanged(emittedValue, localValue) is true
prevSetValue is EMPTY_OBJ, so !(prevSetValue !== EMPTY_OBJ && ...) is true
- The entire condition is
true && true = true, so we return early
This is the bug. When prevSetValue is EMPTY_OBJ, we always return early if the emitted value equals the local value. But this is wrong when the user has just typed a new value that was rejected.
The fix should be to not return early when the value has changed. Let me think about the correct fix...
Actually, I think the issue is more subtle. The condition hasChanged(value, prevSetValue) is supposed to detect when the user has set a different value. But when prevSetValue is EMPTY_OBJ, this check is bypassed.
The fix should be to track whether this is the first time the setter is being called, or to use a different mechanism to detect when the value has changed.
Actually, I think the simplest fix is to check if the value has changed from the local value before returning early. If the value has changed from the local value but the emitted value is the same as the local value (i.e., the setter rejected the change), we should still emit to the parent so that the child can re-render and update the input value.
Let me look at the condition again:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
This condition returns early when:
- The emitted value equals the local value, AND
- The current value is the same as the previous set value (or
prevSetValue is EMPTY_OBJ)
The problem is the second condition. When prevSetValue is EMPTY_OBJ, we return early even if the current value has changed.
I think the fix is to change the condition to:
if (
!hasChanged(emittedValue, localValue) &&
prevSetValue !== EMPTY_OBJ &&
hasChanged(value, prevSetValue)
) {
return
}
Wait, that's not right either. Let me think about this more carefully.
The original condition is:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
Using De Morgan's law, this is equivalent to:
if (
!hasChanged(emittedValue, localValue) &&
(prevSetValue === EMPTY_OBJ || !hasChanged(value, prevSetValue))
) {
return
}
This returns early when the emitted value equals the local value, and either prevSetValue is EMPTY_OBJ or the current value equals the previous set value.
The fix should be to only return early when prevSetValue is not EMPTY_OBJ and the current value equals the previous set value. So the fix is:
if (
!hasChanged(emittedValue, localValue) &&
prevSetValue !== EMPTY_OBJ &&
!hasChanged(value, prevSetValue)
) {
return
}
Wait, but this would change the behavior. Let me think about what the original condition is trying to achieve.
The original condition is trying to avoid redundant emissions. If the emitted value hasn't changed from the local value, and the user hasn't set a new value, then we don't need to emit.
But the issue is that when prevSetValue is EMPTY_OBJ, we can't tell if the user has set a new value. So we should only skip the emission when we know the user hasn't set a new value (i.e., prevSetValue is not EMPTY_OBJ and hasChanged(value, prevSetValue) is false).
So the fix is:
if (
!hasChanged(emittedValue, localValue) &&
prevSetValue !== EMPTY_OBJ &&
!hasChanged(value, prevSetValue)
) {
return
}
This returns early only when:
- The emitted value equals the local value, AND
prevSetValue is not EMPTY_OBJ, AND
- The current value equals the previous set value
This is more restrictive than the original condition. The original condition returns early when the emitted value equals the local value and the current value equals the previous set value OR prevSetValue is EMPTY_OBJ. The fixed condition returns early only when all three conditions are true.
But wait, is this the right fix? Let me think about the scenarios:
- User types "b" when the input shows "a":
value = "ab", emittedValue = "a" (setter rejects)
localValue = "a"
prevSetValue = EMPTY_OBJ
- With the original condition: returns early (bug!)
- With the fixed condition:
prevSetValue === EMPTY_OBJ is true, so the second condition prevSetValue !== EMPTY_OBJ is false, and the entire condition is false. So we don't return early and emit to the parent.
- Parent receives "a" (the rejected value), but the parent's prop was already "a", so no re-render...
Hmm, actually this still doesn't solve the problem. When the parent receives the emission and sets its prop to "a", the prop doesn't change (it was already "a"), so no re-render is triggered.
Let me re-read the issue description again...
OK so the issue is specifically about Firefox. On Chrome, the rejection works, but on Firefox, it doesn't. This suggests that there's a browser-specific difference in how the input event is handled.
Actually, I think the issue might be about how the input event is dispatched. In some browsers, the input event might be dispatched synchronously, while in others, it might be dispatched asynchronously.
Actually, I think I finally understand the issue. The problem is in the vModelText directive's beforeUpdate hook. Let me look at it again:
beforeUpdate(
el,
{ value, oldValue, modifiers: { lazy, trim, number } },
vnode,
) {
...
if (
(rootNode instanceof Document || rootNode instanceof ShadowRoot) &&
rootNode.activeElement === el &&
el.type !== 'range'
) {
// #8546
if (lazy && value === oldValue) {
return
}
if (trim && el.value.trim() === newValue) {
return
}
}
el.value = newValue
},
The issue is that when the input is focused, Vue only updates el.value if certain conditions are met. Specifically, it skips updating if:
lazy is true and value === oldValue
trim is true and el.value.trim() === newValue
But these conditions don't account for the case where the value has been rejected by the setter and needs to be updated to show the rejected value.
Actually, wait. Let me re-read the issue description one more time...
The issue says:
- Use firefox
- Enter anything in the field. It should be blocked and only show "a", but it won't on firefox.
So on Firefox, when the user types something, the input field shows the typed character instead of being blocked and showing only "a".
The setter in the playground URL should reject any input that's not exactly "a". So when the user types "b", the setter should return "a" (reject the change).
Let me think about this from the perspective of the DOM. When the user types "b" in the input:
- The browser updates
el.value to "ab"
- The browser dispatches the
input event
- The
input event handler calls el[assignKey]("ab")
- This triggers the setter, which returns "a"
- The setter emits "a" to the parent
- The parent receives the emission and sets its prop to "a"
- The child's
watchSyncEffect detects the prop change and updates localValue to "a"
- The child's
customRef's getter now returns "a"
- The parent's prop is "a", so the child re-renders
- The child's
beforeUpdate hook runs
- In
beforeUpdate, elValue is "ab" and newValue is "a"
elValue !== newValue, so we don't return early
- The element is focused, so we enter the
if block
lazy is false, so lazy && value === oldValue is false
trim is not set, so trim && el.value.trim() === newValue is false
- We execute
el.value = newValue, which sets el.value to "a"
This should work correctly! The input value should be updated to "a".
Unless... there's something special about Firefox that causes the value not to be updated.
Let me look at the issue from a different angle. Maybe the issue is not about the beforeUpdate hook but about the input event handler.
Actually, I just thought of something. The issue might be about how the input event handler reads el.value. In some browsers, el.value might be the value before the event, while in others, it might be the value after.
But wait, the input event is supposed to fire after the DOM is updated. So el.value should always be the new value after the input event fires.
Unless... in Firefox, the input event fires before the DOM is updated?
Let me check if there's any known Firefox bug related to this.
Actually, I think I need to look at the actual playground URL to understand the reproduction. Let me try to decode it.
Actually, let me just search for any existing issues or fixes related to this problem.
Wait, let me re-read the issue description one more time:
defineModel on Firefox does not work as expected
The key is "defineModel on Firefox". The issue is specifically about defineModel, not about v-model in general.
When using defineModel, the customRef returned by useModel has a setter that emits to the parent. When the setter rejects the value, the parent's prop doesn't change, so no re-render is triggered. This means the input's value is not updated to reflect the rejection.
But why does this work on Chrome but not Firefox?
I think the key might be in how the input event is handled. On Chrome, the input event might be dispatched after the DOM is fully updated, while on Firefox, it might be dispatched before.
Actually, I think I finally understand the issue. Let me look at the input event handler again:
addEventListener(el, lazy ? 'change' : 'input', e => {
if ((e.target as any).composing) return
el[assignKey](castValue(el.value, trim, castToNumber))
})
The handler gets el.value at the time the event fires. The issue might be that on Firefox, the input event fires before the DOM is updated with the new character, so el.value is still the old value.
But that doesn't make sense. The input event is supposed to fire after the DOM is updated.
Actually, I think the issue might be about how the input event handler's callback is invoked. In some browsers, the callback might be invoked synchronously, while in others, it might be invoked asynchronously.
OK let me take a step back and think about this problem differently.
The issue is that on Firefox, when the user types a character that should be rejected by the setter, the character appears in the input field. This means the input's value is not being updated to reflect the rejection.
The only place where the input's value is updated is in the beforeUpdate hook of vModelText. If the beforeUpdate hook is not running, or if it's running but not updating the input value, then the input will show the rejected value.
The beforeUpdate hook runs when the component re-renders. The component re-renders when its props change. The props change when the parent emits an update:modelValue event.
So the issue might be:
- The setter is not emitting the rejected value to the parent, OR
- The parent is not updating its prop, OR
- The
beforeUpdate hook is not updating the input value
Let me check each possibility:
-
The setter is not emitting the rejected value to the parent:
- This is possible if the
useModel setter returns early without emitting.
-
The parent is not updating its prop:
- This is possible if the parent's prop is already the rejected value (e.g., "a"), so setting it to the same value doesn't trigger a re-render.
-
The beforeUpdate hook is not updating the input value:
- This is possible if the
beforeUpdate hook returns early without updating the input value.
Let me check possibility 1 first. Looking at the useModel setter:
set(value) {
const emittedValue = options.set ? options.set(value) : value
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
...
}
The setter returns early when the emitted value equals the local value and the current value equals the previous set value (or prevSetValue is EMPTY_OBJ). In our case, the emitted value is "a" and the local value is "a", so !hasChanged(emittedValue, localValue) is true. And prevSetValue is EMPTY_OBJ, so !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) is true. So the setter returns early without emitting.
This is the bug! The setter should emit the rejected value to the parent so that the parent can update its prop and trigger a re-render.
Wait, but the emitted value is "a" and the parent's prop is already "a". So even if the setter emits "a" to the parent, the parent's prop doesn't change, and no re-render is triggered.
Hmm, so the issue is that the parent's prop doesn't change when the setter rejects the value. This is expected behavior - if the setter rejects the value and emits the old value, the parent's prop doesn't change.
But then how does the input's value get updated to show the rejected value?
The input's value is updated in the beforeUpdate hook. The beforeUpdate hook runs when the component re-renders. The component re-renders when the customRef's getter returns a different value than before.
Wait, but the customRef's getter returns localValue, which is updated by the watchSyncEffect when the prop changes. If the prop doesn't change, localValue doesn't change, and the getter returns the same value as before. So no re-render is triggered.
So the issue is that when the setter rejects the value, the parent's prop doesn't change, the watchSyncEffect doesn't run, localValue doesn't change, and the component doesn't re-render. This means the beforeUpdate hook doesn't run, and the input's value is not updated.
But wait, the useModel setter has this code:
// #10279: if the local value is converted via a setter but the value
// emitted to parent was the same, the parent will not trigger any
// 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 (
hasChanged(value, emittedValue) &&
hasChanged(value, prevSetValue) &&
!hasChanged(emittedValue, prevEmittedValue)
) {
trigger()
}
This is supposed to handle exactly this case! If the value has changed but the emitted value is the same as before (the setter rejected the change), we need to force an update.
But this code only runs if the setter doesn't return early. And we've established that the setter returns early because prevSetValue is EMPTY_OBJ.
So the fix is to make sure the setter doesn't return early when the value has changed but was rejected by the setter.
Let me think about how to fix this.
The issue is in the condition:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
This condition returns early when the emitted value equals the local value and the current value equals the previous set value (or prevSetValue is EMPTY_OBJ). But we need to not return early when the current value has changed from the local value (i.e., the user has typed a new value that was rejected).
I think the fix is to add an additional check: if the current value has changed from the local value, we should not return early.
if (
!hasChanged(emittedValue, localValue) &&
!hasChanged(value, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
Wait, but this would change the behavior. Let me think about this more carefully.
Actually, I think the simpler fix is to just remove the prevSetValue !== EMPTY_OBJ check:
if (
!hasChanged(emittedValue, localValue) &&
!hasChanged(value, prevSetValue)
) {
return
}
But wait, this doesn't handle the case where prevSetValue is EMPTY_OBJ. We can't call hasChanged(value, EMPTY_OBJ) because EMPTY_OBJ is not the actual previous value.
OK let me think about this differently. The condition !hasChanged(emittedValue, localValue) && !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) is designed to avoid redundant emissions. The idea is:
- If the emitted value equals the local value, AND
- The current value equals the previous set value,
- Then don't emit (because the value hasn't changed).
But when prevSetValue is EMPTY_OBJ, we can't tell if the current value equals the previous set value. So we should not return early.
The fix should be to check if prevSetValue is not EMPTY_OBJ before comparing:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
Wait, this is the original condition! The problem is that when prevSetValue is EMPTY_OBJ, the condition !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)) is true, so we return early.
The fix should be to change the condition so that we don't return early when prevSetValue is EMPTY_OBJ and the current value has changed from the local value.
Actually, I think the fix is to add a check for whether the current value has changed from the local value:
if (
!hasChanged(emittedValue, localValue) &&
!hasChanged(value, localValue)
) {
return
}
This returns early only when both the emitted value and the current value equal the local value. If either has changed, we don't return early.
But wait, this might cause redundant emissions. Let me think about the scenarios:
-
User types "b" when the input shows "a":
value = "ab", emittedValue = "a" (setter rejects), localValue = "a"
!hasChanged("a", "a") = true, but !hasChanged("ab", "a") = false
- The condition is
true && false = false, so we don't return early
- We emit "a" to the parent
- Parent's prop doesn't change, so no re-render
- But wait, this is what we want - we need to emit so that the force update code can run
-
User types "c" when the input shows "a":
- Same as above, we don't return early
-
User types "b" when the input shows "ab" (if the input somehow shows "ab"):
value = "abc", emittedValue = "a" (setter rejects), localValue = "a"
- Same as above
-
User clicks the input again without typing anything:
- Hmm, this doesn't trigger the
input event, so it's not relevant
Actually, I think the fix is correct. We should not return early when the current value has changed from the local value, even if the emitted value equals the local value.
But wait, I need to make sure this doesn't cause any issues. Let me think about the scenarios where the condition !hasChanged(emittedValue, localValue) && !hasChanged(value, localValue) is true:
emittedValue equals localValue, AND
value equals localValue
This happens when the user sets the model ref to the same value as the current local value. For example, if the model ref's value is "a" and the user sets it to "a" again, emittedValue = "a" and value = "a" and localValue = "a". In this case, we should return early because there's no change.
So the fix is correct. We should not return early when either the emitted value or the current value has changed from the local value.
Let me implement this fix.
Actually, wait. I just realized that the condition !hasChanged(emittedValue, localValue) && !hasChanged(value, localValue) would return early when both the emitted value and the current value equal the local value. But what if the current value is the same as the local value but the emitted value is different? For example, if the user sets the model ref to "a" and the setter transforms it to "b"? In this case, value = "a", emittedValue = "b", and localValue = "a". The condition !hasChanged("b", "a") = false, so we don't return early, which is correct.
OK so I think the fix is correct. Let me implement it.
Actually, let me re-read the original condition one more time:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
The original condition returns early when:
- The emitted value equals the local value, AND
prevSetValue is EMPTY_OBJ OR the current value equals the previous set value
The issue is the second part. When prevSetValue is EMPTY_OBJ, we return early even if the current value has changed.
The fix should be to change the second part to also check if the current value equals the local value:
if (
!hasChanged(emittedValue, localValue) &&
(prevSetValue === EMPTY_OBJ || !hasChanged(value, prevSetValue)) &&
!hasChanged(value, localValue)
) {
return
}
Hmm, this is getting complicated. Let me think about this more carefully.
Actually, I think the simplest fix is to just change the condition to:
if (
!hasChanged(emittedValue, localValue) &&
(prevSetValue === EMPTY_OBJ || value === prevSetValue)
) {
return
}
Wait, but this is equivalent to the original condition when prevSetValue is not EMPTY_OBJ. When prevSetValue is EMPTY_OBJ, we return early if !hasChanged(emittedValue, localValue) is true, which is the same as before.
OK I think I need to think about this problem from a different angle.
The issue is that when the setter rejects the value, the parent's prop doesn't change, so no re-render is triggered. This means the input's value is not updated.
The useModel setter has a mechanism to force an update in this case:
if (
hasChanged(value, emittedValue) &&
hasChanged(value, prevSetValue) &&
!hasChanged(emittedValue, prevEmittedValue)
) {
trigger()
}
But this code only runs if the setter doesn't return early. So the fix should be to make sure the setter doesn't return early when the value has changed but was rejected by the setter.
Let me think about when we should return early:
- We should return early if the emitted value equals the local value AND the current value equals the previous set value (or
prevSetValue is EMPTY_OBJ).
- But we should NOT return early if the current value has changed from the local value (i.e., the user has typed a new value that was rejected).
The issue is that the original condition doesn't account for the case where the current value has changed from the local value. So the fix should be to add a check for this case.
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
// Additional check: if the value has changed from the local value, don't return early
if (hasChanged(value, localValue)) {
// Fall through to emit
} else {
return
}
}
Hmm, this is getting complicated. Let me think about a cleaner fix.
Actually, I think the simplest fix is to check if the value has changed from the local value before returning early:
if (
!hasChanged(emittedValue, localValue) &&
!hasChanged(value, localValue)
) {
return
}
Wait, but this would change the behavior for the case where the value hasn't changed but prevSetValue has been updated. For example, if the user sets the model ref to "a" twice in a row, the first time prevSetValue is EMPTY_OBJ, so we don't return early. The second time prevSetValue is "a", so hasChanged(value, prevSetValue) is false, and we return early.
But with the new condition !hasChanged(emittedValue, localValue) && !hasChanged(value, localValue), if the user sets the model ref to "a" twice in a row, the first time emittedValue = "a", value = "a", and localValue = "a", so we return early. The second time, same thing, we return early.
Wait, but this is not the correct behavior. We should not return early on the first time because prevSetValue is EMPTY_OBJ. The issue is that the original condition is designed to handle this case: when prevSetValue is EMPTY_OBJ, we should not return early because we can't tell if the value has changed.
Hmm, I think I'm overcomplicating this. Let me just look at the fix from a practical perspective.
The issue is that when the setter rejects the value, the input's value is not updated. This is because the setter returns early without emitting, so the parent's prop doesn't change, so no re-render is triggered.
The fix should be to make sure the setter emits the rejected value to the parent, so that the parent can update its prop and trigger a re-render.
But the parent's prop is already the rejected value (e.g., "a"), so setting it to the same value doesn't trigger a re-render. So even if the setter emits the rejected value, no re-render is triggered.
So the fix should be to force an update when the setter rejects the value. The useModel setter already has code to do this:
if (
hasChanged(value, emittedValue) &&
hasChanged(value, prevSetValue) &&
!hasChanged(emittedValue, prevEmittedValue)
) {
trigger()
}
But this code only runs if the setter doesn't return early. So the fix should be to make sure the setter doesn't return early when the value has changed but was rejected.
Let me look at the condition one more time:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
The issue is that when prevSetValue is EMPTY_OBJ, we return early even if the current value has changed. The fix should be to not return early when the current value has changed from the local value.
I think the fix is to change the condition to:
if (
!hasChanged(emittedValue, localValue) &&
!hasChanged(value, localValue)
) {
return
}
But wait, this would change the behavior for the case where the user sets the model ref to the same value multiple times. Let me think about this...
Actually, I think the fix should be to change the condition to:
if (
!hasChanged(emittedValue, localValue) &&
prevSetValue !== EMPTY_OBJ &&
!hasChanged(value, prevSetValue)
) {
return
}
This returns early only when:
- The emitted value equals the local value, AND
prevSetValue is not EMPTY_OBJ, AND
- The current value equals the previous set value
This is more restrictive than the original condition. The original condition returns early when the emitted value equals the local value and the current value equals the previous set value OR prevSetValue is EMPTY_OBJ. The fixed condition returns early only when all three conditions are true.
But wait, this means on the first set, we never return early. Is that a problem? Let me think...
On the first set:
prevSetValue is EMPTY_OBJ
- The fixed condition
prevSetValue !== EMPTY_OBJ is false
- So the entire condition is
false, and we don't return early
- We proceed to emit and update
prevSetValue
On subsequent sets:
- If the value hasn't changed,
hasChanged(value, prevSetValue) is false, so we return early
- If the value has changed but the setter rejects it,
hasChanged(value, emittedValue) is true, so the force update code runs
- If the value has changed and the setter accepts it,
hasChanged(value, localValue) is true, so we emit and update prevSetValue
I think this fix is correct. Let me implement it.
Wait, but there's a subtle issue. On the first set, if the user sets the model ref to "a" (same as the current local value), we don't return early because prevSetValue is EMPTY_OBJ. But the setter might return early based on the options.set function. Let me think about this...
Actually, the options.set function is the setter function that the user provides. In the playground URL, the setter function is:
set(value) {
return value === 'a' ? 'a' : model.value
}
So when the user sets the model ref to "a", the setter returns "a". When the user sets the model ref to "ab", the setter returns "a" (rejects the change).
In the useModel setter, emittedValue = options.set(value). So if the user sets the model ref to "a", emittedValue is "a". If the user sets the model ref to "ab", emittedValue is "a" (rejected).
So the fix should work correctly. On the first set, we don't return early. On subsequent sets, we return early only if the value hasn't changed.
Actually, wait. I just realized that the condition prevSetValue !== EMPTY_OBJ && !hasChanged(value, prevSetValue) doesn't check if the value has changed from the local value. It only checks if the value has changed from the previous set value.
So if the user sets the model ref to "a" (same as the previous set value), we return early. But if the user sets the model ref to "a" when the local value is "b", we don't return early even though the value is the same as the previous set value.
Hmm, this might be intentional. The prevSetValue is supposed to track the last value that was set on the model ref, regardless of whether it was accepted or rejected. So if the user sets the model ref to "ab" (rejected), prevSetValue is "ab". If the user sets the model ref to "ab" again, hasChanged(value, prevSetValue) is false, so we return early.
But what if the user sets the model ref to "a" after it was rejected? hasChanged("a", "ab") is true, so we don't return early, and we emit "a" to the parent. But the parent's prop is already "a", so no re-render is triggered. Then the force update code runs:
if (
hasChanged(value, emittedValue) &&
hasChanged(value, prevSetValue) &&
!hasChanged(emittedValue, prevEmittedValue)
) {
trigger()
}
hasChanged("a", "a") = false (value equals emittedValue)
- So the force update code doesn't run
Wait, but hasChanged(value, emittedValue) is false because both are "a". So the force update code doesn't run. But do we need it to run? The user is setting the model ref to "a", which is the same as the local value. So there's no need to force an update.
OK so I think the fix is correct. Let me implement it.
Actually wait, I need to re-check the original condition logic. Let me trace through the scenarios:
Original condition:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
Scenario 1: User types "b" when the input shows "a"
value = "ab", emittedValue = "a" (rejected), localValue = "a", prevSetValue = EMPTY_OBJ
!hasChanged("a", "a") = true
!(EMPTY_OBJ !== EMPTY_OBJ && hasChanged("ab", EMPTY_OBJ)) = !false = true
- Condition is
true && true = true, so we return early
This is the bug! We should not return early because the current value "ab" has changed from the local value "a".
Scenario 2: User types "b" when the input shows "a" (second time)
- Same as Scenario 1 because
prevSetValue was never updated
Fixed condition:
if (
!hasChanged(emittedValue, localValue) &&
prevSetValue !== EMPTY_OBJ &&
!hasChanged(value, prevSetValue)
) {
return
}
Scenario 1: User types "b" when the input shows "a"
value = "ab", emittedValue = "a" (rejected), localValue = "a", prevSetValue = EMPTY_OBJ
!hasChanged("a", "a") = true
prevSetValue !== EMPTY_OBJ = false
- Condition is
true && false && ... = false, so we don't return early
- We proceed to emit "a" to the parent
- Parent receives "a" (the rejected value), but the parent's prop is already "a", so no re-render is triggered
- But wait, we need to trigger an update so that the input value is updated
Actually, I think the issue is more fundamental. Even if we don't return early and emit "a" to the parent, the parent's prop doesn't change, so no re-render is triggered. The force update code:
if (
hasChanged(value, emittedValue) &&
hasChanged(value, prevSetValue) &&
!hasChanged(emittedValue, prevEmittedValue)
) {
trigger()
}
hasChanged("ab", "a") = true (value has changed from emittedValue)
hasChanged("ab", EMPTY_OBJ) = true (prevSetValue is EMPTY_OBJ)
!hasChanged("a", undefined) = true (prevEmittedValue is undefined)
- Condition is
true && true && true = true, so we trigger()
So the force update code runs, and the input value is updated. But wait, this only happens if the setter doesn't return early. So the fix is to make sure the setter doesn't return early when the value has changed but was rejected.
OK so the fix is correct. Let me implement it.
Wait, but I need to make sure the fix doesn't break anything else. Let me think about the scenarios where the original condition returns early:
Scenario: User sets the model ref to the same value as the local value
value = "a", emittedValue = "a", localValue = "a", prevSetValue = "a" (from a previous set)
!hasChanged("a", "a") = true
!("a" !== EMPTY_OBJ && hasChanged("a", "a")) = !false = true
- Condition is
true && true = true, so we return early
This is the intended behavior - we don't emit if the value hasn't changed.
Fixed condition:
!hasChanged("a", "a") = true
prevSetValue !== EMPTY_OBJ = true
!hasChanged("a", "a") = true
- Condition is
true && true && true = true, so we return early
OK so the fix works correctly for this scenario.
Scenario: User sets the model ref to a new value, and the setter accepts it
value = "b", emittedValue = "b", localValue = "a", prevSetValue = "a"
!hasChanged("b", "a") = false
- Condition is
false && ... = false, so we don't return early
This is correct - we should emit the new value.
Scenario: User sets the model ref to a new value, and the setter rejects it
value = "ab", emittedValue = "a" (rejected), localValue = "a", prevSetValue = "ab" (from a previous set)
!hasChanged("a", "a") = true
prevSetValue !== EMPTY_OBJ = true
!hasChanged("ab", "ab") = true
- Condition is
true && true && true = true, so we return early
Wait, this is wrong! We should not return early because the current value "ab" is different from the local value "a". The force update code should run to update the input value.
Hmm, but with the fixed condition, we return early when:
- The emitted value equals the local value, AND
prevSetValue is not EMPTY_OBJ, AND
- The current value equals the previous set value
In this scenario, prevSetValue is "ab" (from a previous set), and the current value is also "ab", so we return early. But this is wrong because the current value "ab" is different from the local value "a".
Wait, but if prevSetValue is "ab" and the current value is "ab", then the user set the model ref to "ab" again. The setter rejected it last time, and the local value is still "a". So the input value should be "a". When the user sets the model ref to "ab" again, the setter rejects it again, and the input value should still be "a".
In this case, do we need to emit to the parent? The parent's prop is already "a", and the setter returns "a" again, so no change. Do we need to trigger an update? The local value is already "a", and the prop is "a", so the input value should already be "a". So no update is needed.
Hmm, but wait. The force update code:
if (
hasChanged(value, emittedValue) &&
hasChanged(value, prevSetValue) &&
!hasChanged(emittedValue, prevEmittedValue)
) {
trigger()
}
hasChanged("ab", "a") = true (value has changed from emittedValue)
hasChanged("ab", "ab") = false (prevSetValue equals value)
- Condition is
true && false && ... = false, so we don't trigger()
So even if we don't return early, the force update code doesn't run because hasChanged(value, prevSetValue) is false. This means the input value is not updated.
But wait, if prevSetValue is "ab" and value is "ab", and the force update code doesn't run, then the input value is not updated. But the input value should already be "a" (from the previous rejection), so no update is needed.
Hmm, but what if the input value is not "a"? For example, if the user typed "ab" in the input and the setter rejected it, but the input value is still "ab" (because the beforeUpdate hook didn't run). Then when the user types "ab" again, the setter rejects it again, and the input value should be "a". But the force update code doesn't run because hasChanged(value, prevSetValue) is false.
Wait, but if the setter doesn't return early (because prevSetValue is "ab" and hasChanged("ab", "ab") is false), then the force update code runs:
Actually no, the force update code checks hasChanged(value, prevSetValue), which is false. So the force update code doesn't run.
Hmm, so even if the setter doesn't return early, the input value is not updated if the user sets the model ref to the same value as the previous set value.
But wait, this is the intended behavior. If the user sets the model ref to the same value as the previous set value, there's no need to trigger an update because the input value should already be correct.
OK so I think the fix is correct. Let me re-read the original condition and the fixed condition:
Original condition:
if (
!hasChanged(emittedValue, localValue) &&
!(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue))
) {
return
}
Fixed condition:
if (
!hasChanged(emittedValue, localValue) &&
prevSetValue !== EMPTY_OBJ &&
!hasChanged(value, prevSetValue)
) {
return
}
The difference is:
- Original: return early if the emitted value equals the local value AND (prevSetValue is EMPTY_OBJ OR the current value equals the previous set value)
- Fixed: return early only if the emitted value equals the local value AND prevSetValue is not EMPTY_OBJ AND the current value equals the previous set value
The fix prevents returning early when prevSetValue is EMPTY_OBJ, which is the bug.
OK let me implement this fix now. I'll modify the useModel.ts file to change the condition.
Actually, wait. Let me re-read the fixed condition more carefully:
if (
!hasChanged(emittedValue, localValue) &&
prevSetValue !== EMPTY_OBJ &&
!hasChanged(value, prevSetValue)
) {
return
}
This returns early when:
- The emitted value equals the local value, AND
prevSetValue is not EMPTY_OBJ, AND
- The current value equals the previous set value
This is correct because:
- If the emitted value equals the local value, there's no need to emit to the parent (the parent's prop won't change)
- If
prevSetValue is not EMPTY_OBJ, we have a valid previous set value to compare against
- If the current value equals the previous set value, the user hasn't set a new value
So we return early only when the user hasn't set a new value and the emitted value equals the local value.
If the user has set a new value (hasChanged(value, prevSetValue) is true), we don't return early. Even if the emitted value equals the local value (the setter rejected the change), we need to emit to the parent so that the force update code can run.
But wait, the force update code:
if (
hasChanged(value, emittedValue) &&
hasChanged(value, prevSetValue) &&
!hasChanged(emittedValue, prevEmittedValue)
) {
trigger()
}
This runs when:
- The value has changed from the emitted value (i.e., the setter rejected the change)
- The value has changed from the previous set value
- The emitted value hasn't changed from the previous emitted value
In our scenario:
value = "ab", emittedValue = "a" (rejected), prevSetValue = EMPTY_OBJ, prevEmittedValue = undefined
hasChanged("ab", "a") = true
hasChanged("ab", EMPTY_OBJ) = true
!hasChanged("a", undefined) = true
- Condition is
true && true && true = true, so we trigger()
So the force update code runs, and the input value is updated. This is correct.
OK so the fix is correct. Let me implement it now.
Wait, I just want to make sure I understand the fix correctly. The original condition uses !(prevSetValue !== EMPTY_OBJ && hasChanged(value, prevSetValue)), which is prevSetValue === EMPTY_OBJ || !hasChanged(value, prevSetValue). So the original condition returns early when:
- The emitted value equals the local value, AND
prevSetValue is EMPTY_OBJ OR the current value equals the previous set value
The fixed condition uses prevSetValue !== EMPTY_OBJ && !hasChanged(value, prevSetValue), so the fixed condition returns early only when:
- The emitted value equals the local value, AND
prevSetValue is not EMPTY_OBJ, AND
- The current value equals the previous set value
The difference is that the fixed condition requires prevSetValue to not be EMPTY_OBJ, which prevents returning early on the first set.
OK I'm confident the fix is correct. Let me implement it.
Actually, let me double-check by looking at the git blame for this file to see when this condition was added and why.
<tool_call>
<function=bash_terminal>
<parameter=command>
cd /core && git blame packages/runtime-core/src/helpers/useModel.ts | head -70
</tool_call>