π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[web] Remove retries and other knick-knacks off the HEIC conversion
Manav Rathi36e3907dfcbc
diff --git a/web/apps/photos/src/services/heic-convert.ts b/web/apps/photos/src/services/heic-convert.tsindex d2e05d9ec9..2c2c78912c 100644--- a/web/apps/photos/src/services/heic-convert.ts+++ b/web/apps/photos/src/services/heic-convert.ts@@ -1,100 +1,31 @@import { createHEICConvertComlinkWorker } from "@/media/worker/heic-convert";import type { DedicatedHEICConvertWorker } from "@/media/worker/heic-convert.worker";-import log from "@/next/log";import { ComlinkWorker } from "@/next/worker/comlink-worker";-import { CustomError } from "@ente/shared/error";-import { retryAsyncFunction } from "@ente/shared/utils";-import QueueProcessor from "@ente/shared/utils/queueProcessor";+import { wait } from "@/utils/promise";/*** Convert a HEIC image to a JPEG.*- * Behind the scenes, it uses a web worker pool to do the conversion using a- * WASM HEIC conversion package.+ * Behind the scenes, it uses a web worker to do the conversion using a WASM+ * HEIC conversion package.** @param heicBlob The HEIC blob to convert.+ ** @returns The JPEG blob.*/-export const heicToJPEG = (heicBlob: Blob) => converter.convert(heicBlob);--const WORKER_POOL_SIZE = 2;-const WAIT_TIME_BEFORE_NEXT_ATTEMPT_IN_MICROSECONDS = [100, 100];-const WAIT_TIME_IN_MICROSECONDS = 30 * 1000;-const BREATH_TIME_IN_MICROSECONDS = 1000;--class HEICConverter {- private convertProcessor = new QueueProcessor<Blob>();- private workerPool: ComlinkWorker<typeof DedicatedHEICConvertWorker>[] = [];-- private initIfNeeded() {- if (this.workerPool.length > 0) return;- this.workerPool = [];- for (let i = 0; i < WORKER_POOL_SIZE; i++)- this.workerPool.push(createHEICConvertComlinkWorker());- }-- async convert(fileBlob: Blob): Promise<Blob> {- this.initIfNeeded();-- const response = this.convertProcessor.queueUpRequest(() =>- retryAsyncFunction<Blob>(async () => {- const convertWorker = this.workerPool.shift();- const worker = await convertWorker.remote;- try {- const convertedHEIC = await new Promise<Blob>(- (resolve, reject) => {- const main = async () => {- try {- const timeout = setTimeout(() => {- reject(Error("wait time exceeded"));- }, WAIT_TIME_IN_MICROSECONDS);- const startTime = Date.now();- const convertedHEIC =- await worker.heicToJPEG(fileBlob);- const ms = Date.now() - startTime;- log.debug(() => `heic => jpeg (${ms} ms)`);- clearTimeout(timeout);- resolve(convertedHEIC);- } catch (e) {- reject(e);- }- };- main();- },- );- if (!convertedHEIC || convertedHEIC?.size === 0) {- log.error(- `Converted HEIC file is empty (original was ${fileBlob?.size} bytes)`,- );- }- await new Promise((resolve) => {- setTimeout(- () => resolve(null),- BREATH_TIME_IN_MICROSECONDS,- );- });- this.workerPool.push(convertWorker);- return convertedHEIC;- } catch (e) {- log.error("HEIC conversion failed", e);- convertWorker.terminate();- this.workerPool.push(createHEICConvertComlinkWorker());- throw e;- }- }, WAIT_TIME_BEFORE_NEXT_ATTEMPT_IN_MICROSECONDS),- );-- try {- return await response.promise;- } catch (e) {- if (e.message === CustomError.REQUEST_CANCELLED) {- // ignore- return null;- }- throw e;- }- }-}--/** The singleton instance of {@link HEICConverter}. */-const converter = new HEICConverter();+export const heicToJPEG = async (heicBlob: Blob) =>+ worker()+ .then((w) => w.heicToJPEG(heicBlob))+ // I'm told this library used to have big memory spikes, and adding pauses+ // to get GC to run helped.+ .then((res) => wait(10 /* ms */).then(() => res));++/** Cached instance of the {@link ComlinkWorker} that wraps our web worker. */+let _worker: ComlinkWorker<typeof DedicatedHEICConvertWorker> | undefined;++/** Lazily created, cached, instance of our web worker. */+const worker = async () => {+ let worker = _worker;+ if (!worker) _worker = worker = createHEICConvertComlinkWorker();+ return await worker.remote;+};