diff --git a/apps/app/src/library/ingest.tsx b/apps/app/src/library/ingest.tsx index 809a9b3..b26dd16 100644 --- a/apps/app/src/library/ingest.tsx +++ b/apps/app/src/library/ingest.tsx @@ -41,6 +41,8 @@ function IngestState({ api, orgId, children }: { api: ReturnType /** Several megabytes through IPC. Dozens at once freeze the webview on "A enviar…" and the send never finishes. */ const ORIGINAL_IPC_MS = 45_000; +/** A few at a time: one is slow, all of them freeze the bar. */ +const MAX_PARALLEL_ORIGINALS = 3; function withTimeout(work: Promise, ms: number): Promise { return new Promise((resolve) => { @@ -62,41 +64,42 @@ function withTimeout(work: Promise, ms: number): Promise["api"]; add: ReturnType["add"] }) { const sending = useRef(new Set()); const queue = useRef<{ sha256: string; photoId: string }[]>([]); - const busy = useRef(false); + const active = useRef(0); const pump = useCallback(() => { - if (busy.current) return; - const next = queue.current.shift(); - if (!next) return; - busy.current = true; - const release = () => { - sending.current.delete(next.sha256); - busy.current = false; - pump(); - }; - void (async () => { - const bytes = await withTimeout(localOriginal(next.sha256), ORIGINAL_IPC_MS); - if (bytes && bytes !== "timeout") { - add([new File([bytes], `${next.sha256}.jpg`, { type: "image/jpeg" })], undefined, () => release()); - return; - } - if (bytes !== "timeout") { - // The file is still here but changed since it arrived (Photo Mechanic wrote captions into it): sent as this - // photo's original, which the server checks is the same photo. - try { - const changed = await withTimeout(localOriginalChanged(next.sha256), ORIGINAL_IPC_MS); - if (changed && changed !== "timeout") await api.sendOriginal(next.photoId, new Blob([changed], { type: "image/jpeg" }), `${next.sha256}.jpg`); - } catch { - /* not the same photo, or offline: asked again later */ + while (active.current < MAX_PARALLEL_ORIGINALS) { + const next = queue.current.shift(); + if (!next) return; + active.current += 1; + const release = () => { + sending.current.delete(next.sha256); + active.current -= 1; + pump(); + }; + void (async () => { + const bytes = await withTimeout(localOriginal(next.sha256), ORIGINAL_IPC_MS); + if (bytes && bytes !== "timeout") { + add([new File([bytes], `${next.sha256}.jpg`, { type: "image/jpeg" })], undefined, () => release()); + return; } - } - release(); - })(); + if (bytes !== "timeout") { + // The file is still here but changed since it arrived (Photo Mechanic wrote captions into it): sent as this + // photo's original, which the server checks is the same photo. + try { + const changed = await withTimeout(localOriginalChanged(next.sha256), ORIGINAL_IPC_MS); + if (changed && changed !== "timeout") await api.sendOriginal(next.photoId, new Blob([changed], { type: "image/jpeg" }), `${next.sha256}.jpg`); + } catch { + /* not the same photo, or offline: asked again later */ + } + } + release(); + })(); + } }, [add, api]); const onMessage = useCallback(