From 5e3b7fdbe478041988444d90c0d70e40c8105b7e Mon Sep 17 00:00:00 2001 From: jean-baptiste Date: Wed, 3 Jun 2026 00:24:48 +0200 Subject: [PATCH] perf(app): poll active sessions adaptively (3s active, 30s idle) --- public/app.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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;