d2/d2js/js/src/platform.browser.js

27 lines
828 B
JavaScript
Raw Normal View History

import { wasmBinary, wasmExecJs } from "./wasm-loader.browser.js";
2025-01-14 18:31:57 +00:00
import workerScript from "./worker.js" with { type: "text" };
2025-01-14 18:55:48 +00:00
import elkScript from "./elk.js" with { type: "text" };
2025-01-14 18:55:48 +00:00
// For the browser version, we build the wasm files into a file (wasm-loader.browser.js)
// and loading a file just reads the text, so there's no external dependency calls
export async function loadFile(path) {
if (path === "./d2.wasm") {
return wasmBinary.buffer;
}
if (path === "./wasm_exec.js") {
return new TextEncoder().encode(wasmExecJs).buffer;
}
2025-01-14 18:55:48 +00:00
return null;
}
export async function createWorker() {
2025-01-14 18:55:48 +00:00
let blob = new Blob([wasmExecJs, elkScript, workerScript], {
2025-01-14 06:00:37 +00:00
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-14 05:07:03 +00:00
return worker;
}