From e2408a511101b780b8ad5ab092fbb23b2b652c70 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sun, 16 Apr 2023 00:03:13 -0700 Subject: [PATCH] ximg: start worker on demand Signed-off-by: Varun Patil --- src/components/frame/XImgCache.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/components/frame/XImgCache.ts b/src/components/frame/XImgCache.ts index 718f5e1f..9b0abd2c 100644 --- a/src/components/frame/XImgCache.ts +++ b/src/components/frame/XImgCache.ts @@ -10,9 +10,9 @@ let importer: ReturnType; const BLOB_CACHE = new Map() as Map; const BLOB_STICKY = new Map(); -// Configure worker on startup -document.addEventListener("DOMContentLoaded", () => { - if (globalThis.mode !== "user") return; +// Start and configure the worker +function startWorker() { + if (worker || globalThis.mode !== "user") return; // Start worker worker = new Worker(new URL("./XImgWorker.ts", import.meta.url)); @@ -22,6 +22,11 @@ document.addEventListener("DOMContentLoaded", () => { importer("configure")({ multiUrl: API.IMAGE_MULTIPREVIEW(), }); +} + +// Configure worker on startup +document.addEventListener("DOMContentLoaded", () => { + if (globalThis.mode !== "user") return; // Periodic blob cache cleaner window.setInterval(() => { @@ -53,6 +58,9 @@ export async function sticky(url: string, delta: number) { } export async function fetchImage(url: string) { + // Start worker + startWorker(); + // Check memcache entry if (BLOB_CACHE.has(url)) return BLOB_CACHE.get(url)[1];