cleanup
This commit is contained in:
parent
0b7b6cf79c
commit
4576d15bea
9 changed files with 20 additions and 41 deletions
|
|
@ -21,4 +21,4 @@ node_modules:
|
|||
|
||||
.PHONY: cleanup
|
||||
cleanup: test
|
||||
prefix "$@" git checkout -- src/platform.js
|
||||
prefix "$@" git checkout -- src/platform.js src/worker.js
|
||||
|
|
|
|||
|
|
@ -36,13 +36,14 @@ async function buildDynamicFiles(platform) {
|
|||
const platformPath = join(SRC_DIR, "platform.js");
|
||||
await writeFile(platformPath, platformContent);
|
||||
|
||||
const workerContent =
|
||||
const workerSource =
|
||||
platform === "node"
|
||||
? `export * from "./worker.node.js";`
|
||||
: `export * from "./worker.browser.js";`;
|
||||
? join(SRC_DIR, "worker.node.js")
|
||||
: join(SRC_DIR, "worker.browser.js");
|
||||
|
||||
const workerPath = join(SRC_DIR, "worker.js");
|
||||
await writeFile(workerPath, workerContent);
|
||||
const workerTarget = join(SRC_DIR, "worker.js");
|
||||
const workerContent = await readFile(workerSource, "utf8");
|
||||
await writeFile(workerTarget, workerContent);
|
||||
}
|
||||
|
||||
async function buildAndCopy(buildType) {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
"name": "@terrastruct/d2",
|
||||
"author": "Terrastruct, Inc.",
|
||||
"description": "D2.js is a wrapper around the WASM build of D2, the modern text-to-diagram language.",
|
||||
"version": "0.1.17",
|
||||
"version": "0.1.19",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/terrastruct/d2.git",
|
||||
|
|
|
|||
|
|
@ -54,10 +54,7 @@ export class D2 {
|
|||
});
|
||||
} else {
|
||||
this.worker.onerror = (error) => {
|
||||
console.error("Worker detailed error:", error);
|
||||
console.error("Error message:", error.message);
|
||||
console.error("Error filename:", error.filename);
|
||||
console.error("Error lineno:", error.lineno);
|
||||
console.error("Worker encountered an error:", error.message || error);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,34 +18,12 @@ export async function createWorker() {
|
|||
);
|
||||
let workerScript = await response.text();
|
||||
|
||||
// Create global Go without IIFE in module context
|
||||
let blob = new Blob(
|
||||
[
|
||||
// First establish Go in global scope
|
||||
wasmExecJs,
|
||||
// Then the module code
|
||||
workerScript,
|
||||
],
|
||||
{
|
||||
type: "text/javascript;charset=utf-8",
|
||||
}
|
||||
);
|
||||
let blob = new Blob([wasmExecJs, workerScript], {
|
||||
type: "text/javascript;charset=utf-8",
|
||||
});
|
||||
|
||||
console.log("about to create worker");
|
||||
const worker = new Worker(URL.createObjectURL(blob), {
|
||||
type: "module",
|
||||
});
|
||||
console.log("worker", worker);
|
||||
|
||||
// Add error handler to see initialization errors
|
||||
worker.onerror = (error) => {
|
||||
console.error("Worker initialization error:", {
|
||||
message: error.message,
|
||||
filename: error.filename,
|
||||
lineno: error.lineno,
|
||||
error: error.error,
|
||||
});
|
||||
};
|
||||
|
||||
return worker;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,4 +7,4 @@ async function initWasmBrowser(wasmBinary) {
|
|||
return self.d2;
|
||||
}
|
||||
|
||||
setupMessageHandler(self, initWasmBrowser);
|
||||
setupMessageHandler(false, self, initWasmBrowser);
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
export * from "./worker.node.js";
|
||||
// Replaced at build time
|
||||
|
|
|
|||
|
|
@ -8,4 +8,4 @@ async function initWasmNode(wasmBinary) {
|
|||
return global.d2;
|
||||
}
|
||||
|
||||
setupMessageHandler(parentPort, initWasmNode);
|
||||
setupMessageHandler(true, parentPort, initWasmNode);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
let currentPort;
|
||||
let d2;
|
||||
|
||||
export function setupMessageHandler(port, initWasm) {
|
||||
export function setupMessageHandler(isNode, port, initWasm) {
|
||||
currentPort = port;
|
||||
|
||||
const handleMessage = async (e) => {
|
||||
|
|
@ -10,6 +10,9 @@ export function setupMessageHandler(port, initWasm) {
|
|||
switch (type) {
|
||||
case "init":
|
||||
try {
|
||||
if (isNode) {
|
||||
eval(data.wasmExecContent);
|
||||
}
|
||||
d2 = await initWasm(data.wasm);
|
||||
currentPort.postMessage({ type: "ready" });
|
||||
} catch (err) {
|
||||
|
|
@ -41,7 +44,7 @@ export function setupMessageHandler(port, initWasm) {
|
|||
}
|
||||
};
|
||||
|
||||
if (typeof process !== "undefined" && process.release?.name === "node") {
|
||||
if (isNode) {
|
||||
port.on("message", handleMessage);
|
||||
} else {
|
||||
port.onmessage = (e) => handleMessage(e.data);
|
||||
|
|
|
|||
Loading…
Reference in a new issue