Open source CapCut alternative.
TypeScript
fix(site): land links a batch reported as pending
David Le859c36ee582a
diff --git a/site/src/cut/lib/urlBatch.test.ts b/site/src/cut/lib/urlBatch.test.tsindex d4913732..7cb5a2cd 100644--- a/site/src/cut/lib/urlBatch.test.ts+++ b/site/src/cut/lib/urlBatch.test.ts@@ -78,4 +78,25 @@ describe("importEach", () => {);expect(out).toEqual([{ url: "a", notStarted: true }]);});++ test("a link reported as still downloading lands even when its slot answer comes after the budget", async () => {+ let tries = 0;+ const landed: string[] = [];+ const out = await importEach(+ ["a"],+ async (u) => {+ // The first ask is in flight across the deadline and comes back busy.+ if (tries++ === 0) {+ await new Promise((r) => setTimeout(r, 60));+ throw new Error(URL_BUSY_MESSAGE);+ }+ landed.push(u);+ return u;+ },+ { busyWaitMs: 5, budgetMs: 30 }+ );+ expect(out).toEqual([{ url: "a", pending: true }]);+ await new Promise((r) => setTimeout(r, 100));+ expect(landed).toEqual(["a"]);+ });});diff --git a/site/src/cut/lib/urlBatch.ts b/site/src/cut/lib/urlBatch.tsindex 7bafdf8a..3c57bd65 100644--- a/site/src/cut/lib/urlBatch.ts+++ b/site/src/cut/lib/urlBatch.ts@@ -49,15 +49,18 @@ export async function importEach<T>(const wait = opts.busyWaitMs ?? BUSY_WAIT_MS;const deadline = Date.now() + (opts.budgetMs ?? URL_BATCH_BUDGET_MS);const rows: (UrlRow<T> | undefined)[] = new Array(list.length);+ let answered = false;// A link turned away as busy (a download started elsewhere holds a slot)- // waits and asks again rather than failing, until the budget runs out.+ // waits and asks again, until the budget runs out. One whose ask was in+ // flight when the batch answered was reported as still downloading, so it+ // keeps asking until it starts and lands on its own.const attempt = async (url: string): Promise<T> => {for (;;) {try {return await importOne(url);} catch (e) {if (messageOf(e) !== URL_BUSY_MESSAGE) throw e;- if (Date.now() + wait >= deadline) throw OUT_OF_TIME;+ if (!answered && Date.now() + wait >= deadline) throw OUT_OF_TIME;await new Promise((r) => setTimeout(r, wait));}}@@ -79,6 +82,7 @@ export async function importEach<T>(const outOfTime = new Promise<void>((r) => (timer = setTimeout(r, Math.max(0, deadline - Date.now()))));await Promise.race([lanes, outOfTime]);clearTimeout(timer);+ answered = true;// A link a lane took up is still downloading; one no lane reached never// started.return list.map((url, i) => rows[i] ?? (i < next ? { url, pending: true as const } : { url, notStarted: true as const }));