fix(tui): drop a pending key-burst flush when an external value replaces the draft
The composer hands key bursts to the parent on a 16ms timer. When history navigation, a slash completion or a submit clears/replaces the draft while such a flush is still armed, the [value] effect reset the local buffer correctly but left the timer running, so 16ms later the stale burst was handed to the parent and overwrote the external value (second hole in #111934). Disarm the pending flush in the external branch of the [value] effect: once the parent has replaced the draft, a burst typed against the old draft can never be the newer value. Second invariant test covers it alongside the stale own-echo case.
This commit is contained in:
@@ -53,13 +53,20 @@ const tick = () => new Promise<void>(resolve => setImmediate(resolve))
|
||||
|
||||
function Harness({
|
||||
onValue,
|
||||
setValueRef,
|
||||
snapshotRef
|
||||
}: {
|
||||
onValue: (value: string) => void
|
||||
setValueRef?: React.MutableRefObject<((next: string) => void) | null>
|
||||
snapshotRef: React.RefObject<InputCursorSnapshot | null>
|
||||
}) {
|
||||
const [value, setValue] = useState('')
|
||||
|
||||
if (setValueRef) {
|
||||
// Stands in for history navigation / a slash completion replacing the draft.
|
||||
setValueRef.current = setValue
|
||||
}
|
||||
|
||||
return React.createElement(TextInput, {
|
||||
cursorSnapshotRef: snapshotRef,
|
||||
onChange: (next: string) => {
|
||||
@@ -140,6 +147,60 @@ describe('stale parent own-echo during deferred key-burst flush', () => {
|
||||
expect(values.at(-1)).toBe('abcde')
|
||||
expect(snapshotRef.current).toEqual({ cursor: 5, value: 'abcde' })
|
||||
})
|
||||
|
||||
it('drops a pending key-burst flush when an external value replaces the draft', async () => {
|
||||
vi.stubEnv('TERM_PROGRAM', 'iTerm.app')
|
||||
vi.stubEnv('TMUX', '')
|
||||
|
||||
const stdout = new FakeTty()
|
||||
const stdin = new FakeTty()
|
||||
const stderr = new FakeTty()
|
||||
const values: string[] = []
|
||||
const snapshotRef = useRefBridge()
|
||||
const setValueRef: React.MutableRefObject<((next: string) => void) | null> = { current: null }
|
||||
|
||||
const instance = renderSync(
|
||||
React.createElement(Harness, { onValue: v => values.push(v), setValueRef, snapshotRef }),
|
||||
{
|
||||
patchConsole: false,
|
||||
stderr: stderr as unknown as NodeJS.WriteStream,
|
||||
stdin: stdin as unknown as NodeJS.ReadStream,
|
||||
stdout: stdout as unknown as NodeJS.WriteStream
|
||||
}
|
||||
)
|
||||
|
||||
try {
|
||||
await tick()
|
||||
|
||||
for (const ch of ['a', 'b', 'c']) {
|
||||
stdin.send(ch)
|
||||
await tick()
|
||||
}
|
||||
|
||||
vi.advanceTimersByTime(16)
|
||||
await tick()
|
||||
await tick()
|
||||
|
||||
// "d" arms a new deferred flush; history navigation then replaces the
|
||||
// draft before that flush fires.
|
||||
stdin.send('d')
|
||||
await tick()
|
||||
setValueRef.current?.('history draft')
|
||||
await tick()
|
||||
await tick()
|
||||
|
||||
// The stale burst must not be handed to the parent 16ms later.
|
||||
vi.advanceTimersByTime(16)
|
||||
await tick()
|
||||
await tick()
|
||||
} finally {
|
||||
instance.unmount()
|
||||
instance.cleanup()
|
||||
}
|
||||
|
||||
expect(values).toEqual(['a', 'abc'])
|
||||
expect(snapshotRef.current).toEqual({ cursor: 'history draft'.length, value: 'history draft' })
|
||||
})
|
||||
})
|
||||
|
||||
// The snapshot ref is read after unmount in the assertion above; the helper
|
||||
|
||||
@@ -940,6 +940,15 @@ export function TextInput({
|
||||
return
|
||||
}
|
||||
|
||||
// An external value replaced the draft. A key burst still waiting on its
|
||||
// 16ms flush is now stale; letting it fire would hand the parent the old
|
||||
// draft on top of the value it just set.
|
||||
if (parentChangeTimer.current) {
|
||||
clearTimeout(parentChangeTimer.current)
|
||||
parentChangeTimer.current = null
|
||||
}
|
||||
|
||||
pendingParentValue.current = null
|
||||
setCur(value.length)
|
||||
setSel(null)
|
||||
curRef.current = value.length
|
||||
|
||||
Reference in New Issue
Block a user