Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): free the turn of an archived preview that gave up
Yudhi Armyndharis4bf46db1f6ff
diff --git a/dashboard/src/utils/archivedMediaLoad.test.ts b/dashboard/src/utils/archivedMediaLoad.test.tsindex 2629cb091..4d322e8c4 100644--- a/dashboard/src/utils/archivedMediaLoad.test.ts+++ b/dashboard/src/utils/archivedMediaLoad.test.ts@@ -44,6 +44,30 @@ test('a load aborted while it waits for its turn never starts', async () => {assert.equal(ran, false);});+test('loads that gave up their turn leave no gap for the loads behind them', async () => {+ // Four previews from the chat the operator just left: the first starts at once, the rest are still+ // waiting when it unmounts. The next chat's first preview must not sit out their turns.+ const left = Array.from({ length: 6 }, () => new AbortController());+ const started: number[] = [];+ const first = loadArchivedMedia(left[0].signal, async () => {+ started.push(Date.now());+ return 'x';+ });+ const abandoned = left+ .slice(1)+ .map(controller => assert.rejects(loadArchivedMedia(controller.signal, async () => 'x')));+ for (const controller of left.slice(1)) controller.abort();+ await first;+ await Promise.all(abandoned);+ const t0 = Date.now();+ await loadArchivedMedia(new AbortController().signal, async () => {+ started.push(Date.now());+ return 'y';+ });+ assert.ok(started[1] - started[0] < 300, `the next chat waited ${started[1] - started[0]} ms behind abandoned turns`);+ assert.ok(Date.now() - t0 < 300);+});+test('a throttled load is retried and then resolves', async () => {let calls = 0;const value = await loadArchivedMedia(diff --git a/dashboard/src/utils/archivedMediaLoad.ts b/dashboard/src/utils/archivedMediaLoad.tsindex 87aeae994..f5603074c 100644--- a/dashboard/src/utils/archivedMediaLoad.ts+++ b/dashboard/src/utils/archivedMediaLoad.ts@@ -6,24 +6,43 @@ import { retryThrottled } from './fetchAllPages.ts';// otherwise the newest previews are the ones refused and stay download buttons for good. A 429 that// still arrives (a lower configured limit, another tab) is retried like fetchAllPages retries a page.const START_GAP_MS = 125;-let nextStart = 0;+let lastStart = 0;+let timer: ReturnType<typeof setTimeout> | undefined;+const waiting: Array<() => void> = [];++// One timer hands out the turns, a gap apart. A load that gives up its turn (its preview unmounted) leaves+// the queue, so it costs the loads behind it nothing.+function pump(): void {+ if (timer !== undefined || waiting.length === 0) return;+ timer = setTimeout(+ () => {+ timer = undefined;+ const next = waiting.shift();+ if (next) {+ lastStart = Date.now();+ next();+ }+ pump();+ },+ Math.max(0, lastStart + START_GAP_MS - Date.now()),+ );+}function waitForTurn(signal: AbortSignal): Promise<void> {if (signal.aborted) return Promise.reject(signal.reason);- const now = Date.now();- const startAt = Math.max(now, nextStart);- nextStart = startAt + START_GAP_MS;- if (startAt === now) return Promise.resolve();return new Promise((resolve, reject) => {+ const turn = () => {+ signal.removeEventListener('abort', onAbort);+ resolve();+ };const onAbort = () => {- clearTimeout(timer);+ const at = waiting.indexOf(turn);+ if (at >= 0) waiting.splice(at, 1);reject(signal.reason);};- const timer = setTimeout(() => {- signal.removeEventListener('abort', onAbort);- resolve();- }, startAt - now);+ waiting.push(turn);signal.addEventListener('abort', onAbort, { once: true });+ pump();});}