Library: send up to three originals at a time from the desktop app #5
@@ -41,6 +41,8 @@ function IngestState({ api, orgId, children }: { api: ReturnType<typeof useAuth>
|
||||
|
||||
/** 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<T>(work: Promise<T>, ms: number): Promise<T | "timeout" | null> {
|
||||
return new Promise((resolve) => {
|
||||
@@ -62,41 +64,42 @@ function withTimeout<T>(work: Promise<T>, ms: number): Promise<T | "timeout" | n
|
||||
/**
|
||||
* Desktop app: when someone needs the full original of a photo this computer keeps (100% zoom on the web, an export
|
||||
* made on the server, delivery to ZeroZero), the server asks over the live channel and this sends it.
|
||||
* One original at a time, so the webview stays responsive and the next ask can run.
|
||||
* Up to three at a time, so the webview stays responsive and the next ask can run.
|
||||
*/
|
||||
function OriginalResponder({ api, add }: { api: ReturnType<typeof useAuth>["api"]; add: ReturnType<typeof useUploadQueue>["add"] }) {
|
||||
const sending = useRef(new Set<string>());
|
||||
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(
|
||||
|
||||
Reference in new issue
Block a user