refactor: centralize oauth identity and route refresh workflow

This commit is contained in:
Cita
2026-03-23 11:53:59 +08:00
parent 328b492ab3
commit 3877f3a3f1
22 changed files with 361 additions and 78 deletions
+11 -2
View File
@@ -40,6 +40,7 @@ const TASK_CLEANUP_INTERVAL_MS = 60 * 1000;
const tasks = new Map<string, BackgroundTask>();
const dedupeTaskIds = new Map<string, string>();
let cleanupTimer: ReturnType<typeof setInterval> | null = null;
function nowIso() {
return new Date().toISOString();
@@ -157,13 +158,17 @@ function cleanupExpiredTasks() {
}
}
const cleanupTimer = setInterval(cleanupExpiredTasks, TASK_CLEANUP_INTERVAL_MS);
cleanupTimer.unref?.();
function ensureCleanupTimer() {
if (cleanupTimer) return;
cleanupTimer = setInterval(cleanupExpiredTasks, TASK_CLEANUP_INTERVAL_MS);
cleanupTimer.unref?.();
}
export function startBackgroundTask(
options: BackgroundTaskStartOptions,
runner: () => Promise<unknown>,
): { task: BackgroundTask; reused: boolean } {
ensureCleanupTimer();
const dedupeKey = options.dedupeKey?.trim() || '';
if (dedupeKey) {
const existingTaskId = dedupeTaskIds.get(dedupeKey);
@@ -241,4 +246,8 @@ export function summarizeCheckinResults(results: Array<{ result?: any }>): { tot
export function __resetBackgroundTasksForTests() {
tasks.clear();
dedupeTaskIds.clear();
if (cleanupTimer) {
clearInterval(cleanupTimer);
cleanupTimer = null;
}
}