Fix timeout not works

This commit is contained in:
2024-01-02 15:11:26 +08:00
parent f6b5d25d9a
commit c0d86d3da5
3 changed files with 22 additions and 8 deletions

View File

@@ -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 });

View File

@@ -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) {

View File

@@ -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;