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 fitRef = useRef<FitAddon | null>(null);
const lineBuffer = useRef('');
const echoQueue = useRef<string[]>([]);
const { status, send, subscribe } = useWs();
// 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) => {
for (const char of data) {
if (char === '\r' || char === '\n') {
term.write('\r\n');
sendRef.current(lineBuffer.current + '\r');
const line = lineBuffer.current + '\r';
lineBuffer.current = '';
term.write('\r\n');
echoQueue.current.push(line);
sendRef.current(line);
} else if (char === '\x7f' || char === '\b') {
if (lineBuffer.current.length > 0) {
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(() => {
return subscribe((msg) => {
const idx = echoQueue.current.indexOf(msg);
if (idx !== -1) {
echoQueue.current.splice(idx, 1);
return;
}
termRef.current?.write(msg);
});
}, [subscribe]);