From c0d86d3da5cd48ee6842a560a539507d9affaf2a Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 2 Jan 2024 15:11:26 +0800 Subject: [PATCH] Fix timeout not works --- client.ts | 6 +++--- tasks/download.ts | 17 +++++++++++++++-- utils/progress_readable.ts | 7 ++++--- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/client.ts b/client.ts index 707b68c..0724b45 100644 --- a/client.ts +++ b/client.ts @@ -52,7 +52,7 @@ export class Client { return t ? t : undefined; } else return undefined; } - request( + async request( url: string | Request | URL, method: string | undefined = undefined, options: RequestInit | undefined = undefined, @@ -81,7 +81,7 @@ export class Client { url.headers.set(v[0], v[1]); } try { - return fetch(url); + return await fetch(url); } catch (e) { if (e instanceof TypeError) { throw new RecoverableError(e.message, { cause: e.cause }); @@ -112,7 +112,7 @@ export class Client { }); d.signal = abortController.signal; try { - return fetch(url, d); + return await fetch(url, d); } catch (e) { if (e instanceof TypeError) { throw new RecoverableError(e.message, { cause: e.cause }); diff --git a/tasks/download.ts b/tasks/download.ts index 91549ae..5a835ff 100644 --- a/tasks/download.ts +++ b/tasks/download.ts @@ -280,7 +280,10 @@ export async function download_task( if (load_times >= max_retry_count) reject(errors); i.load().then(resolve).catch((e) => { if (force_abort.aborted) { - throw Error("aborted."); + console.log("Aborted when loading image:", i); + errors.push(e); + reject(errors); + return; } errors.push(e); load_times += 1; @@ -392,7 +395,13 @@ export async function download_task( } download().then(resolve).catch((e) => { if (force_abort.aborted) { - throw Error("aborted."); + console.log( + "Aborted when downloading image:", + i, + ); + errors.push(e); + reject(errors); + return; } if (e instanceof DOMException) { if (e.name == "AbortError") { @@ -422,6 +431,10 @@ export async function download_task( function try_() { load().then(() => { deal_with_img().then(resolve).catch((e) => { + if (force_abort.aborted) { + reject(e); + return; + } console.log("Failed to download, retry: ", e); retry += 1; if (retry >= max_retry_count) { diff --git a/utils/progress_readable.ts b/utils/progress_readable.ts index 978de01..c2bd20a 100644 --- a/utils/progress_readable.ts +++ b/utils/progress_readable.ts @@ -46,10 +46,11 @@ export class ProgressReadable extends EventTarget { this.#clearInterval(); return; } else { - this.readed += v.value.byteLength; + const len = v.value.byteLength; + this.readed += len; this.dispatchEvent("progress", this.readed); c.enqueue(v.value); - if (v.value.byteLength != 0) { + if (len != 0) { this.#last_readed = Date.now(); } } @@ -83,7 +84,7 @@ export class ProgressReadable extends EventTarget { } } #setInterval() { - this.#timeout = setTimeout(() => { + this.#timeout = setInterval(() => { const now = Date.now(); if (now - this.#last_readed > this.timeout) { this.#is_timeout = true;