Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 53 additions & 14 deletions src/app/service/service_worker/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,17 +772,51 @@ export class ScriptService {
setTimeout(resolve, Math.round(MIN_DELAY + ((++i / n + Math.random()) / 2) * (MAX_DELAY - MIN_DELAY)))
);

return Promise.all(
(uuids as string[]).map(async (uuid, _idx) => {
const script = scripts[_idx];
const res =
!script || script.uuid !== uuid || !checkScripts.includes(script)
? false
: await this._checkUpdateAvailable(script, delayFn);
if (!res) return false;
return res;
})
);
const CHECK_UPDATE_TIMEOUT_MS = 300_000; // 5 分钟超时

const results = new Map<
string,
| false
| {
updateAvailable: true;
code: string;
metadata: Partial<Record<string, string[]>>;
}
>();

// 预初始化 Map 确保顺序
for (const uuid of uuids as string[]) {
results.set(uuid, false);
}

const abortController = new AbortController();
let timeoutId: ReturnType<typeof setTimeout>;

const timeoutPromise = new Promise<void>((resolve) => {
timeoutId = setTimeout(() => {
abortController.abort();
resolve();
}, CHECK_UPDATE_TIMEOUT_MS);
});

await Promise.race([
timeoutPromise,
Promise.allSettled(
(uuids as string[]).map(async (uuid, _idx) => {
const script = scripts[_idx];
const res =
!script || script.uuid !== uuid || !checkScripts.includes(script)
? false
: await this._checkUpdateAvailable(script, delayFn, abortController.signal);
if (!res) return false;
results.set(uuid, res);
return res;
})
).finally(() => {
clearTimeout(timeoutId);
}),
]);
Comment on lines +802 to +818
return [...results.values()];
Comment on lines +802 to +819
}

async _checkUpdateAvailable(
Expand All @@ -792,7 +826,8 @@ export class ScriptService {
checkUpdateUrl?: string;
metadata: Partial<Record<string, any>>;
},
delayFn?: () => Promise<any>
delayFn?: () => Promise<any>,
signal?: AbortSignal
): Promise<false | { updateAvailable: true; code: string; metadata: SCMetadata }> {
const { uuid, name, checkUpdateUrl } = script;

Expand All @@ -804,8 +839,12 @@ export class ScriptService {
name,
});
try {
if (delayFn) await delayFn();
const code = await fetchScriptBody(checkUpdateUrl);
if (delayFn) {
if (signal?.aborted) return false;
await delayFn();
}
if (signal?.aborted) return false;
const code = await fetchScriptBody(checkUpdateUrl, signal);
const metadata = parseMetadata(code);
if (!metadata) {
logger.error("parse metadata failed");
Expand Down
3 changes: 2 additions & 1 deletion src/pkg/utils/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export function parseMetadata(code: string): SCMetadata | null {
}

// 从网址取得脚本代码
export async function fetchScriptBody(url: string): Promise<string> {
export async function fetchScriptBody(url: string, signal?: AbortSignal): Promise<string> {
const resp = await fetch(url, {
signal,
headers: {
"Cache-Control": "no-cache",
},
Expand Down
Loading