2025-01-12 19:08:17 +00:00
|
|
|
import { wasmBinary, wasmExecJs } from "./wasm-loader.browser.js";
|
|
|
|
|
|
|
|
|
|
export async function loadFile(path) {
|
|
|
|
|
if (path === "./d2.wasm") {
|
|
|
|
|
return wasmBinary.buffer;
|
|
|
|
|
}
|
|
|
|
|
if (path === "./wasm_exec.js") {
|
|
|
|
|
return new TextEncoder().encode(wasmExecJs).buffer;
|
|
|
|
|
}
|
|
|
|
|
throw new Error(`Unexpected file request: ${path}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createWorker() {
|
2025-01-14 03:40:25 +00:00
|
|
|
let response = await fetch(new URL("./worker.js", import.meta.url));
|
|
|
|
|
if (!response.ok)
|
2025-01-12 19:08:17 +00:00
|
|
|
throw new Error(
|
2025-01-14 03:40:25 +00:00
|
|
|
`Failed to load worker.js: ${response.status} ${response.statusText}`
|
2025-01-12 19:08:17 +00:00
|
|
|
);
|
2025-01-14 03:40:25 +00:00
|
|
|
let workerScript = await response.text();
|
2025-01-12 19:08:17 +00:00
|
|
|
|
2025-01-14 06:00:37 +00:00
|
|
|
let blob = new Blob([wasmExecJs, workerScript], {
|
|
|
|
|
type: "text/javascript;charset=utf-8",
|
|
|
|
|
});
|
2025-01-14 05:07:03 +00:00
|
|
|
|
|
|
|
|
const worker = new Worker(URL.createObjectURL(blob), {
|
2025-01-14 03:40:25 +00:00
|
|
|
type: "module",
|
2025-01-12 19:08:17 +00:00
|
|
|
});
|
2025-01-14 05:07:03 +00:00
|
|
|
return worker;
|
2025-01-12 19:08:17 +00:00
|
|
|
}
|