diff --git a/public/app.js b/public/app.js index 9ab01b8..74efd61 100644 --- a/public/app.js +++ b/public/app.js @@ -531,6 +531,22 @@ terminalStopBtn.addEventListener('click', () => { // --- Poll for active PTY sessions --- +// Adaptive cadence: poll fast (3s) only while PTYs are running; when idle, back +// off to 30s. Every renderer path that starts a session (launchNewSession, +// openSession, launchTerminalSession, onSessionDetected/Forked) calls +// pollActiveSessions() explicitly, which re-arms the fast cadence immediately. +// The 30s idle floor still catches sessions started outside the renderer +// (scheduler-spawned PTYs, other windows) within at most 30s. +const POLL_FAST_MS = 3000; +const POLL_IDLE_MS = 30000; +let pollTimer = null; + +function scheduleActiveSessionsPoll() { + if (pollTimer) clearTimeout(pollTimer); + const delay = activePtyIds.size > 0 ? POLL_FAST_MS : POLL_IDLE_MS; + pollTimer = setTimeout(pollActiveSessions, delay); +} + async function pollActiveSessions() { try { const ids = await window.api.getActiveSessions(); @@ -538,6 +554,7 @@ async function pollActiveSessions() { updateRunningIndicators(); updateTerminalHeader(); } catch {} + scheduleActiveSessionsPoll(); } function updateRunningIndicators() { @@ -592,10 +609,11 @@ function updatePtyTitle() { terminalHeaderPtyTitle.style.display = title ? '' : 'none'; } -setInterval(pollActiveSessions, 3000); +scheduleActiveSessionsPoll(); // Refresh sidebar timeago labels every 30s so "just now" ticks forward setInterval(() => { + if (lastActivityTime.size === 0) return; for (const [sessionId, time] of lastActivityTime) { const item = document.getElementById('si-' + sessionId); if (!item) continue;