diff --git a/desktop/src/features/download/DownloadManagerView.vue b/desktop/src/features/download/DownloadManagerView.vue index f6d4f1c..7615854 100644 --- a/desktop/src/features/download/DownloadManagerView.vue +++ b/desktop/src/features/download/DownloadManagerView.vue @@ -11,7 +11,9 @@ const jobs = ref([]); const maxJobs = ref(2); const tab = ref<"active" | "done">("active"); const openedId = ref(""); -let timer = 0; +let pollTimer = 0; +let stopped = false; +let lastError = ""; const activeJobs = computed(() => jobs.value.filter((job) => job.status !== "done")); const doneJobs = computed(() => jobs.value.filter((job) => job.status === "done")); @@ -29,23 +31,38 @@ const statusText: Record = { }; onMounted(() => { - void refresh(); + stopped = false; + void poll(); void loadDownloadSettings() .then((result) => { maxJobs.value = result.maxConcurrentJobs; }) .catch(() => undefined); - timer = window.setInterval(() => void refresh(), 1000); }); -onUnmounted(() => window.clearInterval(timer)); +onUnmounted(() => { + stopped = true; + window.clearTimeout(pollTimer); +}); + +async function poll() { + await refresh(); + if (stopped) return; + pollTimer = window.setTimeout(() => { + void poll(); + }, 1000); +} async function refresh() { try { const result = await loadJobs(); jobs.value = result.jobs; + lastError = ""; } catch (error) { - if (error instanceof ApiError && error.status !== 0) message.error(error.message); + if (!(error instanceof ApiError) || error.status === 0) return; + if (error.message === lastError) return; + lastError = error.message; + message.error(error.message); } } diff --git a/desktop/src/shared/http.ts b/desktop/src/shared/http.ts index 7ec2061..d734f2d 100644 --- a/desktop/src/shared/http.ts +++ b/desktop/src/shared/http.ts @@ -1,15 +1,51 @@ let apiBase = ""; let preparing: Promise | null = null; +let deepScanDone = false; + +type UnauthorizedHandler = () => void; +let unauthorizedHandler: UnauthorizedHandler | null = null; +let unauthorizedNotified = false; + +export function setUnauthorizedHandler(handler: UnauthorizedHandler | null) { + unauthorizedHandler = handler; +} + +export function clearUnauthorized() { + unauthorizedNotified = false; +} + +function notifyUnauthorized() { + if (unauthorizedNotified) return; + unauthorizedNotified = true; + unauthorizedHandler?.(); +} + +export class ApiError extends Error { + status: number; + + constructor(status: number, message: string) { + super(message); + this.name = "ApiError"; + this.status = status; + } +} export function initApi(): Promise { - preparing ??= resolveApiBase(); + if (!preparing) { + preparing = resolveApiBase().catch((error: unknown) => { + preparing = null; + throw error; + }); + } return preparing; } async function resolveApiBase(): Promise { if (!("__TAURI_INTERNALS__" in window)) return; const { invoke } = await import("@tauri-apps/api/core"); - for (let attempt = 0; attempt < 50; attempt += 1) { + const attempts = deepScanDone ? 1 : 50; + deepScanDone = true; + for (let attempt = 0; attempt < attempts; attempt += 1) { try { const port = await invoke("backend_port"); if (port > 0) { @@ -19,17 +55,11 @@ async function resolveApiBase(): Promise { } catch { // 窗口可能比端口命令更早就绪。 } - await new Promise((resolve) => window.setTimeout(resolve, 100)); - } -} - -export class ApiError extends Error { - status: number; - - constructor(status: number, message: string) { - super(message); - this.status = status; + if (attempt + 1 < attempts) { + await new Promise((resolve) => window.setTimeout(resolve, 100)); + } } + throw new ApiError(0, "连接失败"); } export async function api(path: string, init?: RequestInit): Promise { @@ -45,7 +75,16 @@ export async function api(path: string, init?: RequestInit): Promise { throw new ApiError(0, "连接失败"); } const text = await response.text(); - const data = text ? JSON.parse(text) : null; + let data: { detail?: unknown } | null = null; + if (text) { + try { + data = JSON.parse(text) as { detail?: unknown }; + } catch { + if (response.status === 401) notifyUnauthorized(); + throw new ApiError(response.status, `服务出错(${response.status})`); + } + } + if (response.status === 401) notifyUnauthorized(); if (!response.ok) { const detail = data?.detail; const message = typeof detail === "string" ? detail : "请求失败";