refactor: replace var with const and fix misleading closure comment in terminal.js

This commit is contained in:
Brian Krabach
2026-03-28 01:32:45 -07:00
parent 26e2baa89c
commit cb38f11588
+7 -4
View File
@@ -15,7 +15,7 @@ function connectWebSocket(name) {
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:'; const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
const url = `${proto}//${location.host}/terminal/ws`; const url = `${proto}//${location.host}/terminal/ws`;
const reconnectOverlay = document.getElementById('reconnect-overlay'); const reconnectOverlay = document.getElementById('reconnect-overlay');
var encoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null; const encoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null;
function encodePayload(typeChar, str) { function encodePayload(typeChar, str) {
// Returns Uint8Array: [typeCharCode, ...utf8bytes] // Returns Uint8Array: [typeCharCode, ...utf8bytes]
@@ -26,7 +26,10 @@ function connectWebSocket(name) {
return payload; return payload;
} }
// Register terminal event handlers (once — _ws captured by closure reference) // Register terminal event handlers once on this _term instance.
// These handlers read the module-level _ws at call time (not a captured reference),
// so they always target the live socket. createTerminal() disposes _term before
// the next session, removing these handlers automatically.
if (_term) { if (_term) {
_term.onData(function(data) { _term.onData(function(data) {
if (_ws && _ws.readyState === WebSocket.OPEN) { if (_ws && _ws.readyState === WebSocket.OPEN) {
@@ -197,8 +200,8 @@ function openTerminal(sessionName) {
// iOS Safari defers flex layout — calling fit() synchronously here gives 0px width // iOS Safari defers flex layout — calling fit() synchronously here gives 0px width
// → 2-column terminal. The RAF and 500ms fallback fix this race condition. // → 2-column terminal. The RAF and 500ms fallback fix this race condition.
// Falls back to immediate execution in Node.js test environments where RAF is absent. // Falls back to immediate execution in Node.js test environments where RAF is absent.
var fitAddonRef = _fitAddon; const fitAddonRef = _fitAddon;
var raf = (typeof requestAnimationFrame !== 'undefined') ? requestAnimationFrame : function(fn) { fn(); }; const raf = typeof requestAnimationFrame !== 'undefined' ? requestAnimationFrame : (fn) => fn();
raf(function() { raf(function() {
try { fitAddonRef.fit(); } catch (_) {} try { fitAddonRef.fit(); } catch (_) {}
// 500ms fallback for slow mobile layout engines (e.g. first paint on low-end devices) // 500ms fallback for slow mobile layout engines (e.g. first paint on low-end devices)