feat(SerialConsolePage): enhance echo handling by implementing an echo queue to suppress self-echoes

This commit is contained in:
Jaime Idolpx 2026-06-11 00:22:11 -04:00
parent 0fce95e9b8
commit 3705e72b6d

View File

@ -12,6 +12,7 @@ export default function SerialConsolePage({ onBack }: Props) {
const termRef = useRef<Terminal | null>(null); const termRef = useRef<Terminal | null>(null);
const fitRef = useRef<FitAddon | null>(null); const fitRef = useRef<FitAddon | null>(null);
const lineBuffer = useRef(''); const lineBuffer = useRef('');
const echoQueue = useRef<string[]>([]);
const { status, send, subscribe } = useWs(); const { status, send, subscribe } = useWs();
// Keep a stable ref to `send` so the xterm onData closure never goes stale // Keep a stable ref to `send` so the xterm onData closure never goes stale
@ -50,9 +51,11 @@ export default function SerialConsolePage({ onBack }: Props) {
term.onData((data) => { term.onData((data) => {
for (const char of data) { for (const char of data) {
if (char === '\r' || char === '\n') { if (char === '\r' || char === '\n') {
term.write('\r\n'); const line = lineBuffer.current + '\r';
sendRef.current(lineBuffer.current + '\r');
lineBuffer.current = ''; lineBuffer.current = '';
term.write('\r\n');
echoQueue.current.push(line);
sendRef.current(line);
} else if (char === '\x7f' || char === '\b') { } else if (char === '\x7f' || char === '\b') {
if (lineBuffer.current.length > 0) { if (lineBuffer.current.length > 0) {
lineBuffer.current = lineBuffer.current.slice(0, -1); lineBuffer.current = lineBuffer.current.slice(0, -1);
@ -79,9 +82,14 @@ export default function SerialConsolePage({ onBack }: Props) {
}; };
}, []); }, []);
// Forward all incoming WS messages to the terminal // Forward all incoming WS messages to the terminal, suppressing our own echoes
useEffect(() => { useEffect(() => {
return subscribe((msg) => { return subscribe((msg) => {
const idx = echoQueue.current.indexOf(msg);
if (idx !== -1) {
echoQueue.current.splice(idx, 1);
return;
}
termRef.current?.write(msg); termRef.current?.write(msg);
}); });
}, [subscribe]); }, [subscribe]);