Repository navigation
Expand file tree
/
Copy pathworker.js
More file actions
104 lines (91 loc) · 3.18 KB
/
Copy pathworker.js
File metadata and controls
104 lines (91 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// importScripts("./pkg/wasm_in_web_worker.js");
// import { NumberEval } from "./wasm_in_web_worker.js";
// worker.js - Handles WebAssembly initialization and thread pool setup
import init, { initThreadPool } from "./pkg/gs_rust.js";
// Listen for messages from the main thread
self.onmessage = async function (e) {
const { type, numThreads } = e.data;
if (type === "init") {
try {
// Initialize the WebAssembly module
self.postMessage({ status: "initializing_wasm" });
await init();
self.postMessage({ status: "wasm_initialized" });
// Initialize the thread pool with the specified number of threads
self.postMessage({
status: "initializing_thread_pool",
threads: numThreads,
});
await initThreadPool(numThreads);
self.postMessage({ status: "thread_pool_initialized", success: true });
} catch (error) {
self.postMessage({
status: "error",
message: error.toString(),
stack: error.stack,
});
}
} else if (type === "execute") {
// Here you would add code to execute your Rust functions
// For example: const result = yourRustFunction(e.data.params);
// self.postMessage({ status: 'result', data: result });
self.postMessage({ status: "ready_for_commands" });
}
};
// Set up error handling for uncaught errors
self.addEventListener("error", function (event) {
self.postMessage({
status: "error",
message: `Worker error: ${event.message}`,
stack: event.error ? event.error.stack : "",
});
});
// Notify the main thread that the worker is ready
self.postMessage({ status: "worker_loaded" });
console.log("Initializing worker");
// Use a try-catch block to handle potential import errors gracefully
try {
// Import the WASM module
import("./pkg/gs_rust.js")
.then((module) => {
const { testing } = module;
console.log("Worker module loaded successfully");
// Set callback to handle messages passed to the worker
self.onmessage = async (event) => {
console.log("Worker received message:", event.data);
try {
// Call the testing function from the WASM module if available
if (typeof testing === "function") {
testing(3);
}
// Send response back to be handled by callback in main thread
self.postMessage({
type: "result",
message: "Worker processed the message successfully",
});
} catch (error) {
console.error("Error in worker processing:", error);
self.postMessage({
type: "error",
message: error.toString(),
});
}
};
// Signal that the worker is ready
self.postMessage({
type: "ready",
message: "Worker initialized and ready",
});
})
.catch((error) => {
console.error("Failed to load worker module:", error);
// Notify the main thread about the error
self.postMessage({
type: "error",
message: "Failed to initialize worker: " + error.toString(),
});
});
} catch (error) {
console.error("Critical error in worker:", error);
}
console.log("Worker initialization process started");