Skip to content

Commit 1aa920e

Browse files
fix: pause polling when hidden and backoff server checks (#428)
1 parent 8c9c747 commit 1aa920e

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

packages/app/src/app/app.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ export default function App() {
294294
setOpenworkServerSettings(readOpenworkServerSettings());
295295
});
296296

297+
createEffect(() => {
298+
if (typeof document === "undefined") return;
299+
const update = () => setDocumentVisible(document.visibilityState !== "hidden");
300+
update();
301+
document.addEventListener("visibilitychange", update);
302+
onCleanup(() => document.removeEventListener("visibilitychange", update));
303+
});
304+
297305
createEffect(() => {
298306
const pref = startupPreference();
299307
const info = openworkServerHostInfo();
@@ -339,6 +347,7 @@ export default function App() {
339347

340348
createEffect(() => {
341349
if (typeof window === "undefined") return;
350+
if (!documentVisible()) return;
342351
const url = openworkServerBaseUrl().trim();
343352
const auth = openworkServerAuth();
344353
const token = auth.token;
@@ -353,6 +362,13 @@ export default function App() {
353362

354363
let active = true;
355364
let busy = false;
365+
let timeoutId: number | undefined;
366+
let delayMs = 10_000;
367+
368+
const scheduleNext = () => {
369+
if (!active) return;
370+
timeoutId = window.setTimeout(run, delayMs);
371+
};
356372

357373
const run = async () => {
358374
if (busy) return;
@@ -362,23 +378,30 @@ export default function App() {
362378
if (!active) return;
363379
setOpenworkServerStatus(result.status);
364380
setOpenworkServerCapabilities(result.capabilities);
381+
delayMs =
382+
result.status === "connected" || result.status === "limited"
383+
? 10_000
384+
: Math.min(delayMs * 2, 60_000);
385+
} catch {
386+
delayMs = Math.min(delayMs * 2, 60_000);
365387
} finally {
366388
if (!active) return;
367389
setOpenworkServerCheckedAt(Date.now());
368390
busy = false;
391+
scheduleNext();
369392
}
370393
};
371394

372395
run();
373-
const interval = window.setInterval(run, 10_000);
374396
onCleanup(() => {
375397
active = false;
376-
window.clearInterval(interval);
398+
if (timeoutId) window.clearTimeout(timeoutId);
377399
});
378400
});
379401

380402
createEffect(() => {
381403
if (!isTauriRuntime()) return;
404+
if (!documentVisible()) return;
382405
let active = true;
383406

384407
const run = async () => {
@@ -400,6 +423,7 @@ export default function App() {
400423

401424
createEffect(() => {
402425
if (typeof window === "undefined") return;
426+
if (!documentVisible()) return;
403427
if (!developerMode()) {
404428
setOpenworkServerDiagnostics(null);
405429
return;
@@ -438,6 +462,7 @@ export default function App() {
438462
createEffect(() => {
439463
if (!isTauriRuntime()) return;
440464
if (!developerMode()) return;
465+
if (!documentVisible()) return;
441466

442467
let busy = false;
443468

@@ -464,6 +489,7 @@ export default function App() {
464489
setOwpenbotInfoState(null);
465490
return;
466491
}
492+
if (!documentVisible()) return;
467493

468494
let active = true;
469495

@@ -490,6 +516,7 @@ export default function App() {
490516
setOpenwrkStatusState(null);
491517
return;
492518
}
519+
if (!documentVisible()) return;
493520

494521
let active = true;
495522

@@ -525,6 +552,7 @@ export default function App() {
525552
const mountTime = Date.now();
526553
const [lastKnownConfigSnapshot, setLastKnownConfigSnapshot] = createSignal("");
527554
const [developerMode, setDeveloperMode] = createSignal(false);
555+
const [documentVisible, setDocumentVisible] = createSignal(true);
528556
let markReloadRequiredRef: (reason: ReloadReason, trigger?: ReloadTrigger) => void = () => {};
529557
let setReloadLastFinishedAtRef: (value: number) => void = () => {};
530558

@@ -1392,6 +1420,7 @@ export default function App() {
13921420
setDevtoolsWorkspaceId(null);
13931421
return;
13941422
}
1423+
if (!documentVisible()) return;
13951424

13961425
const client = devtoolsOpenworkClient();
13971426
if (!client) {
@@ -1430,6 +1459,7 @@ export default function App() {
14301459
setOpenworkAuditError(null);
14311460
return;
14321461
}
1462+
if (!documentVisible()) return;
14331463

14341464
const client = devtoolsOpenworkClient();
14351465
const workspaceId = devtoolsWorkspaceId();
@@ -2100,6 +2130,7 @@ export default function App() {
21002130

21012131
createEffect(() => {
21022132
if (typeof window === "undefined") return;
2133+
if (!documentVisible()) return;
21032134
if (openworkReloadUnsupported()) return;
21042135
const client = openworkServerClient();
21052136
const workspaceId = openworkServerWorkspaceId();

packages/app/src/app/components/status-bar.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type StatusBarProps = {
2222

2323
export default function StatusBar(props: StatusBarProps) {
2424
const [owpenbotStatus, setOwpenbotStatus] = createSignal<OwpenbotStatus | null>(null);
25+
const [documentVisible, setDocumentVisible] = createSignal(true);
2526

2627
const opencodeStatusMeta = createMemo(() => ({
2728
dot: props.clientConnected ? "bg-green-9" : "bg-gray-6",
@@ -162,12 +163,24 @@ export default function StatusBar(props: StatusBarProps) {
162163
setOwpenbotStatus(next);
163164
};
164165

165-
onMount(() => {
166+
createEffect(() => {
167+
if (typeof document === "undefined") return;
168+
const update = () => setDocumentVisible(document.visibilityState !== "hidden");
169+
update();
170+
document.addEventListener("visibilitychange", update);
171+
onCleanup(() => document.removeEventListener("visibilitychange", update));
172+
});
173+
174+
createEffect(() => {
175+
if (!documentVisible()) return;
166176
refreshOwpenbot();
167177
const interval = window.setInterval(refreshOwpenbot, 15_000);
178+
onCleanup(() => window.clearInterval(interval));
179+
});
180+
181+
onMount(() => {
168182
scheduleTips(6_000);
169183
onCleanup(() => {
170-
window.clearInterval(interval);
171184
if (tipTimer) window.clearTimeout(tipTimer);
172185
if (tipHideTimer) window.clearTimeout(tipHideTimer);
173186
});

0 commit comments

Comments
 (0)