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
// deno-lint-ignore-file
import init, { _thread_entry_point } from "../../index.js";
/**
*
* @param {WebAssembly.Memory} memory
* @param {WebAssembly.Module} module
* @returns {Worker}
*/
export function createThreadWorker(module, memory) {
const w = new Worker(import.meta.url, {
type: "module",
});
w.postMessage([module, memory]);
return w;
}
self.onmessage = (event) => {
const [module, memory] = event.data;
const initialised = init(module, memory).catch((err) => {
// Propagate to main `onerror`:
setTimeout(() => {
{
throw err;
}
});
// Rethrow to keep promise rejected and prevent execution of further commands:
throw err;
});
self.onmessage = async (event) => {
// This will queue further commands up until the module is fully initialised:
const mod = await initialised;
_thread_entry_point(event.data);
};
};