Skip to content

Commit 6063e26

Browse files
committed
fix: streamline WASM initialization and image processing logic in worker
1 parent b3b3411 commit 6063e26

1 file changed

Lines changed: 20 additions & 78 deletions

File tree

public/worker.js

Lines changed: 20 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,32 @@
1-
console.log("High-performance worker initialized");
1+
console.log("Worker initialized");
22

3-
let wasmModule = null;
4-
let isInitialized = false;
3+
let wasmInit;
54

6-
async function initWasm() {
7-
try {
8-
console.log("📦 Loading WASM module...");
9-
10-
const wasm = await import("./pkg/pixelbits.js");
11-
12-
await wasm.default();
13-
14-
wasmModule = wasm;
15-
isInitialized = true;
16-
17-
console.log("WASM module loaded and initialized");
18-
19-
postMessage({ type: "WORKER_READY" });
20-
} catch (err) {
21-
console.error("WASM initialization failed:", err);
22-
postMessage({
23-
type: "WORKER_ERROR",
24-
error: `Failed to initialize WASM: ${err.message}`,
25-
});
26-
}
5+
try {
6+
wasmInit = import("/pkg/pixelbits.js");
7+
console.log("WASM import started");
8+
} catch (err) {
9+
console.error("WASM import failed:", err);
2710
}
2811

29-
initWasm();
30-
3112
onmessage = async (e) => {
32-
const { type, imageData, processingType, transferId } = e.data;
13+
const { imageData, type } = e.data;
3314

3415
try {
35-
if (type === "PROCESS_IMAGE") {
36-
if (!isInitialized) {
37-
postMessage({
38-
type: "ERROR",
39-
error: "Worker not ready yet",
40-
transferId,
41-
});
42-
return;
43-
}
44-
45-
console.log(
46-
`🎨 Processing image: ${processingType}, Size: ${imageData.width}x${imageData.height}`
47-
);
48-
49-
const startTime = performance.now();
50-
51-
const pixels = new Uint8Array(imageData.data);
52-
53-
switch (processingType) {
54-
case "mangafy":
55-
wasmModule.mangafy(imageData.width, imageData.height, pixels);
56-
break;
57-
default:
58-
throw new Error(`Unknown processing type: ${processingType}`);
59-
}
60-
61-
const processedImageData = new ImageData(
62-
new Uint8ClampedArray(pixels),
63-
imageData.width,
64-
imageData.height
65-
);
66-
67-
const processingTime = performance.now() - startTime;
68-
console.log(`⚡ Image processed in ${processingTime.toFixed(2)}ms`);
16+
const wasm = await wasmInit;
17+
await wasm.default();
18+
console.log("WASM module initialized");
6919

70-
postMessage(
71-
{
72-
type: "IMAGE_PROCESSED",
73-
imageData: processedImageData,
74-
processingTime,
75-
transferId,
76-
},
77-
[processedImageData.data.buffer]
78-
);
79-
} else if (type === "PING") {
80-
postMessage({ type: "PONG", ready: isInitialized });
20+
if (type === "mangafy") {
21+
const pixels = new Uint8Array(imageData.data.buffer);
22+
wasm.mangafy(imageData.width, imageData.height, pixels);
23+
imageData.data.set(pixels);
8124
}
25+
26+
postMessage(imageData, [imageData.data.buffer]);
27+
console.log("Image processed and sent back to main thread");
8228
} catch (err) {
83-
console.error(" Error in worker:", err);
84-
postMessage({
85-
type: "ERROR",
86-
error: err.message,
87-
transferId,
88-
});
29+
console.error("Error processing image:", err);
30+
postMessage({ error: err.message });
8931
}
9032
};

0 commit comments

Comments
 (0)