refactor: Simplify terminal rendering logic

- Remove nested requestAnimationFrame calls
- Simplify handleReplay: write + refresh + scrollToBottom
- Simplify onBecameVisible: fit + refresh + focus
- Remove excessive console.log statements
- Convert async functions to sync where appropriate
This commit is contained in:
2026-02-14 12:33:03 -06:00
parent 303755437d
commit 2151255239
3 changed files with 37 additions and 139 deletions

View File

@@ -357,7 +357,6 @@ async function connect() {
const connectionTimeout = window.setTimeout(() => {
if (connecting.value && !connected.value) {
console.log('[Terminal] Connection timeout, retrying...')
connecting.value = false
socket?.close()
socket = null
@@ -384,24 +383,19 @@ async function connect() {
}
}
socket.onmessage = async (event) => {
socket.onmessage = (event) => {
const msg = JSON.parse(event.data)
if (msg.type === 'connected') {
sessionId.value = msg.sessionId
console.log('[Terminal] Connected, hasHistory:', msg.hasHistory, 'bufferSize:', msg.bufferSize)
// Request replay if there's history and terminal is visible
if (msg.hasHistory && isOpen.value) {
console.log('[Terminal] Requesting replay after connect...')
setTimeout(() => requestReplay(), 50)
} else if (!msg.isNew) {
renderer.writeln('\x1b[36m[Session restored]\x1b[0m')
}
} else if (msg.type === 'replay') {
console.log('[Terminal] Received replay, length:', msg.data?.length)
// USE THE COMPOSABLE'S handleReplay!
await renderer.handleReplay(msg.data || '')
renderer.handleReplay(msg.data || '')
} else if (msg.type === 'output') {
// Token detection
if (waitingForToken.value) {
@@ -415,7 +409,6 @@ async function connect() {
try {
const decoded = atob(match[0])
JSON.parse(decoded)
console.log('[Terminal] WebMCP token detected')
waitingForToken.value = false
tokenBuffer = ''
stopTokenPolling()
@@ -465,7 +458,6 @@ function scheduleReconnect() {
reconnectAttempts++
const delay = RECONNECT_DELAY_MS * Math.min(reconnectAttempts, 5)
console.log(`[Terminal] Reconnecting in ${delay}ms (attempt ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS})`)
reconnectTimeout = window.setTimeout(() => {
if (isOpen.value && !connected.value && !connecting.value) {
@@ -550,7 +542,6 @@ function scrollTerminal(direction: 'up' | 'down' | 'end') {
function requestReplay(tailOnly = false) {
if (socket?.readyState === WebSocket.OPEN) {
console.log('[Terminal] Requesting replay, tailOnly:', tailOnly)
socket.send(JSON.stringify({ type: 'request-replay', tailOnly, chunks: 500 }))
}
}
@@ -571,29 +562,22 @@ function refreshTerminal(fullReplay = false) {
// WATCHERS
// ============================================================================
watch(isOpen, async (open) => {
watch(isOpen, (open) => {
if (open) {
console.log('[Terminal] Opening terminal...')
await nextTick()
nextTick(() => {
renderer.init()
// Initialize terminal (only works when visible!)
renderer.init()
// Wait for CSS transition then setup
setTimeout(() => {
renderer.onBecameVisible()
// Wait for CSS transition, then connect
setTimeout(async () => {
console.log('[Terminal] Transition done, preparing terminal...')
// Wait for terminal to be fully visible
await renderer.onBecameVisible()
console.log('[Terminal] Terminal visible, connecting...')
// Now connect (or request replay if already connected)
if (!connected.value && !connecting.value) {
connect()
} else if (connected.value) {
requestReplay()
}
}, 180)
if (!connected.value && !connecting.value) {
connect()
} else if (connected.value) {
requestReplay()
}
}, 150)
})
} else {
renderer.blur()
waitingForToken.value = false
@@ -618,9 +602,6 @@ onMounted(() => {
checkMobile()
window.addEventListener('resize', checkMobile)
setupKeyboardDetection()
// DON'T initialize terminal here - wait for isOpen!
console.log('[Terminal] Mounted, waiting for terminal to open...')
})
onBeforeUnmount(() => {