-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.ts
More file actions
80 lines (70 loc) · 2.36 KB
/
Copy pathtimer.ts
File metadata and controls
80 lines (70 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type { ParsedQuota } from "./types";
export interface RefreshContext {
getApiKey: () => string;
fetchQuota: (apiKey: string) => Promise<ParsedQuota>;
formatQuotaStatus: (quota: ParsedQuota) => string;
formatErrorState: (error: unknown) => string | undefined;
setStatus: (id: string, value: string | undefined) => void;
lastKnownQuota: { value: ParsedQuota | null };
refreshActiveStatus?: () => Promise<void>;
}
export interface PeriodicRefreshController {
start: (intervalMs?: number) => void;
stop: () => void;
isRunning: () => boolean;
}
const DEFAULT_INTERVAL_MS = 60_000;
const STATUS_BAR_ID = "pi-usage";
export function createPeriodicRefresh(deps: RefreshContext): PeriodicRefreshController {
let intervalId: ReturnType<typeof setInterval> | null = null;
// Guard flag to prevent concurrent tick executions.
// If a tick takes longer than the interval, this ensures only one
// tick runs at a time, avoiding race conditions where multiple
// fetches could update state concurrently.
let tickInProgress = false;
async function tick(): Promise<void> {
// Skip this tick if one is already in progress
if (tickInProgress) {
return;
}
tickInProgress = true;
try {
if (deps.refreshActiveStatus) {
await deps.refreshActiveStatus();
return;
}
const apiKey = deps.getApiKey();
const quota = await deps.fetchQuota(apiKey);
deps.lastKnownQuota.value = quota;
const formatted = deps.formatQuotaStatus(quota);
deps.setStatus(STATUS_BAR_ID, formatted);
} catch (error) {
if (deps.lastKnownQuota.value) {
const formatted = deps.formatQuotaStatus(deps.lastKnownQuota.value);
deps.setStatus(STATUS_BAR_ID, formatted);
} else {
const errorDisplay = deps.formatErrorState(error);
deps.setStatus(STATUS_BAR_ID, errorDisplay);
}
} finally {
tickInProgress = false;
}
}
return {
start(intervalMs: number = DEFAULT_INTERVAL_MS): void {
if (intervalId !== null) return; // already running
tick(); // immediate first fetch
intervalId = setInterval(tick, intervalMs);
},
stop(): void {
if (intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
tickInProgress = false;
}
},
isRunning(): boolean {
return intervalId !== null;
},
};
}