ximg: start worker on demand

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/579/head
Varun Patil 2023-04-16 00:03:13 -07:00
parent 967646572c
commit e2408a5111
1 changed files with 11 additions and 3 deletions

View File

@ -10,9 +10,9 @@ let importer: ReturnType<typeof workerImporter>;
const BLOB_CACHE = new Map<string, object>() as Map<string, [number, string]>; const BLOB_CACHE = new Map<string, object>() as Map<string, [number, string]>;
const BLOB_STICKY = new Map<string, number>(); const BLOB_STICKY = new Map<string, number>();
// Configure worker on startup // Start and configure the worker
document.addEventListener("DOMContentLoaded", () => { function startWorker() {
if (globalThis.mode !== "user") return; if (worker || globalThis.mode !== "user") return;
// Start worker // Start worker
worker = new Worker(new URL("./XImgWorker.ts", import.meta.url)); worker = new Worker(new URL("./XImgWorker.ts", import.meta.url));
@ -22,6 +22,11 @@ document.addEventListener("DOMContentLoaded", () => {
importer<typeof w.configure>("configure")({ importer<typeof w.configure>("configure")({
multiUrl: API.IMAGE_MULTIPREVIEW(), multiUrl: API.IMAGE_MULTIPREVIEW(),
}); });
}
// Configure worker on startup
document.addEventListener("DOMContentLoaded", () => {
if (globalThis.mode !== "user") return;
// Periodic blob cache cleaner // Periodic blob cache cleaner
window.setInterval(() => { window.setInterval(() => {
@ -53,6 +58,9 @@ export async function sticky(url: string, delta: number) {
} }
export async function fetchImage(url: string) { export async function fetchImage(url: string) {
// Start worker
startWorker();
// Check memcache entry // Check memcache entry
if (BLOB_CACHE.has(url)) return BLOB_CACHE.get(url)[1]; if (BLOB_CACHE.has(url)) return BLOB_CACHE.get(url)[1];