fix(webui): stop idle polling and release background timers
This commit is contained in:
1 parent
652b9c4eda
commit
0f358b2ac2
8 files changed
+257
-131
No files matched your search
@@ -7,6 +7,7 @@ import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { SkeletonSwap } from '@/components/interior/skeleton-swap';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { useApi } from '@/lib/api';
|
||||
import { startPolling } from '@/lib/polling';
|
||||
import { useTheme } from '@/contexts/ThemeContext';
|
||||
import type { NotificationDeliveryRecord } from '@/types';
|
||||
|
||||
@@ -20,54 +21,27 @@ export function DeliveriesWidget() {
|
||||
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
let inFlight = false;
|
||||
const stop = startPolling(async () => {
|
||||
try {
|
||||
const records = await api.notifications.recent(50);
|
||||
|
||||
const pull = () => {
|
||||
// Skip while the tab is backgrounded, or if a request is still in flight
|
||||
// (a slow endpoint must not stack overlapping polls). #185.
|
||||
if (inFlight || document.hidden) {
|
||||
return;
|
||||
if (active) {
|
||||
setLoadError(null);
|
||||
setRecords(records);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('load recent notification deliveries failed', error);
|
||||
|
||||
if (active) {
|
||||
setLoadError(error instanceof Error ? error.message : '加载最近投递失败');
|
||||
setRecords((prev) => prev ?? []);
|
||||
}
|
||||
}
|
||||
|
||||
inFlight = true;
|
||||
api.notifications
|
||||
.recent(50)
|
||||
.then((r) => {
|
||||
if (active) {
|
||||
setLoadError(null);
|
||||
setRecords(r);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('load recent notification deliveries failed', error);
|
||||
|
||||
if (active) {
|
||||
setLoadError(error instanceof Error ? error.message : '加载最近投递失败');
|
||||
setRecords((prev) => prev ?? []);
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
inFlight = false;
|
||||
});
|
||||
};
|
||||
|
||||
void pull();
|
||||
const id = window.setInterval(() => void pull(), POLL_MS);
|
||||
|
||||
// The interval keeps firing but no-ops while hidden, so refresh immediately
|
||||
// when the tab becomes visible again to shed any staleness.
|
||||
const onVisible = () => {
|
||||
if (!document.hidden) {
|
||||
pull();
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('visibilitychange', onVisible);
|
||||
}, POLL_MS);
|
||||
|
||||
return () => {
|
||||
active = false;
|
||||
window.clearInterval(id);
|
||||
document.removeEventListener('visibilitychange', onVisible);
|
||||
stop();
|
||||
};
|
||||
}, [api]);
|
||||
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
/** Run immediately, then at the requested cadence without overlapping requests. */
|
||||
/** Wait between requests; keep no timer while hidden or awaiting a response. */
|
||||
export function startPolling(
|
||||
refresh: () => Promise<unknown>,
|
||||
intervalMs: number,
|
||||
pauseWhenHidden = true,
|
||||
): () => void {
|
||||
let stopped = false;
|
||||
let running = false;
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
const paused = () => pauseWhenHidden && document.hidden;
|
||||
|
||||
const clearTimer = () => {
|
||||
if (timer !== null) {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
};
|
||||
|
||||
const tick = async () => {
|
||||
if (running || (pauseWhenHidden && document.hidden)) {
|
||||
if (stopped || running || paused()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -15,31 +25,40 @@ export function startPolling(
|
||||
|
||||
try {
|
||||
await refresh();
|
||||
} catch (error) {
|
||||
console.error('polling failed', error);
|
||||
} finally {
|
||||
running = false;
|
||||
|
||||
if (!stopped && !paused() && intervalMs > 0) {
|
||||
timer = setTimeout(() => {
|
||||
timer = null;
|
||||
void tick();
|
||||
}, intervalMs);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void tick();
|
||||
const interval = intervalMs > 0 ? setInterval(tick, intervalMs) : null;
|
||||
|
||||
const onVisible = () => {
|
||||
const onVisibilityChange = () => {
|
||||
clearTimer();
|
||||
|
||||
if (!document.hidden) {
|
||||
void tick();
|
||||
}
|
||||
};
|
||||
|
||||
if (pauseWhenHidden) {
|
||||
document.addEventListener('visibilitychange', onVisible);
|
||||
document.addEventListener('visibilitychange', onVisibilityChange);
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (interval !== null) {
|
||||
clearInterval(interval);
|
||||
}
|
||||
stopped = true;
|
||||
clearTimer();
|
||||
|
||||
if (pauseWhenHidden) {
|
||||
document.removeEventListener('visibilitychange', onVisible);
|
||||
document.removeEventListener('visibilitychange', onVisibilityChange);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user