fix: 修复请求解析失败、端口重试和重复报错
This commit is contained in:
@@ -11,7 +11,9 @@ const jobs = ref<DownloadJob[]>([]);
|
|||||||
const maxJobs = ref(2);
|
const maxJobs = ref(2);
|
||||||
const tab = ref<"active" | "done">("active");
|
const tab = ref<"active" | "done">("active");
|
||||||
const openedId = ref("");
|
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 activeJobs = computed(() => jobs.value.filter((job) => job.status !== "done"));
|
||||||
const doneJobs = 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<string, string> = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
void refresh();
|
stopped = false;
|
||||||
|
void poll();
|
||||||
void loadDownloadSettings()
|
void loadDownloadSettings()
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
maxJobs.value = result.maxConcurrentJobs;
|
maxJobs.value = result.maxConcurrentJobs;
|
||||||
})
|
})
|
||||||
.catch(() => undefined);
|
.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() {
|
async function refresh() {
|
||||||
try {
|
try {
|
||||||
const result = await loadJobs();
|
const result = await loadJobs();
|
||||||
jobs.value = result.jobs;
|
jobs.value = result.jobs;
|
||||||
|
lastError = "";
|
||||||
} catch (error) {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+52
-13
@@ -1,15 +1,51 @@
|
|||||||
let apiBase = "";
|
let apiBase = "";
|
||||||
let preparing: Promise<void> | null = null;
|
let preparing: Promise<void> | 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<void> {
|
export function initApi(): Promise<void> {
|
||||||
preparing ??= resolveApiBase();
|
if (!preparing) {
|
||||||
|
preparing = resolveApiBase().catch((error: unknown) => {
|
||||||
|
preparing = null;
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
}
|
||||||
return preparing;
|
return preparing;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolveApiBase(): Promise<void> {
|
async function resolveApiBase(): Promise<void> {
|
||||||
if (!("__TAURI_INTERNALS__" in window)) return;
|
if (!("__TAURI_INTERNALS__" in window)) return;
|
||||||
const { invoke } = await import("@tauri-apps/api/core");
|
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 {
|
try {
|
||||||
const port = await invoke<number>("backend_port");
|
const port = await invoke<number>("backend_port");
|
||||||
if (port > 0) {
|
if (port > 0) {
|
||||||
@@ -19,17 +55,11 @@ async function resolveApiBase(): Promise<void> {
|
|||||||
} catch {
|
} catch {
|
||||||
// 窗口可能比端口命令更早就绪。
|
// 窗口可能比端口命令更早就绪。
|
||||||
}
|
}
|
||||||
await new Promise((resolve) => window.setTimeout(resolve, 100));
|
if (attempt + 1 < attempts) {
|
||||||
}
|
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;
|
|
||||||
}
|
}
|
||||||
|
throw new ApiError(0, "连接失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
export async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
||||||
@@ -45,7 +75,16 @@ export async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
|||||||
throw new ApiError(0, "连接失败");
|
throw new ApiError(0, "连接失败");
|
||||||
}
|
}
|
||||||
const text = await response.text();
|
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) {
|
if (!response.ok) {
|
||||||
const detail = data?.detail;
|
const detail = data?.detail;
|
||||||
const message = typeof detail === "string" ? detail : "请求失败";
|
const message = typeof detail === "string" ? detail : "请求失败";
|
||||||
|
|||||||
Reference in New Issue
Block a user