UNPKG

backsplash-app

Version:
55 lines (49 loc) 1.84 kB
import { parentPort } from "worker_threads"; import { GenerationService } from "./generationService"; import { FilesystemStorageService } from "./filesystemStorageService"; import { StoreService } from "./storeService"; import log from "@/logger"; if (!parentPort) { throw new Error("This module must be run as a worker thread"); } // Initialize services const storeService = new StoreService(); const fileSystem = new FilesystemStorageService(); const generationService = new GenerationService( storeService, fileSystem, () => { const styleMap = storeService.get("wallpaper-style-map"); return styleMap && typeof styleMap === "object" && !Array.isArray(styleMap) ? (styleMap as Record<string, string>) : {}; }, (map) => storeService.set("wallpaper-style-map", map as Record<string, string>), ); // Handle messages from the main thread parentPort.on("message", async (message) => { try { switch (message.type) { case "upscale": { const { objectKey } = message; const result = await generationService.upscaleWallpaper(objectKey); parentPort?.postMessage(result); break; } default: parentPort?.postMessage({ error: "Unknown message type" }); } } catch (error) { log.error("[WallpaperWorker] Error processing message:", error); parentPort?.postMessage({ error: error instanceof Error ? error.message : "Unknown error" }); } }); // Handle worker errors process.on("uncaughtException", (error) => { log.error("[WallpaperWorker] Uncaught exception:", error); parentPort?.postMessage({ error: error.message }); }); process.on("unhandledRejection", (reason) => { log.error("[WallpaperWorker] Unhandled rejection:", reason); parentPort?.postMessage({ error: reason instanceof Error ? reason.message : "Unknown error" }); });