Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Jan 14, 2025
1 parent 0b7b6cf commit 4576d15
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 41 deletions.
2 changes: 1 addition & 1 deletion d2js/js/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 6 additions & 5 deletions d2js/js/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion d2js/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions d2js/js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}

Expand Down
28 changes: 3 additions & 25 deletions d2js/js/src/platform.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion d2js/js/src/worker.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ async function initWasmBrowser(wasmBinary) {
return self.d2;
}

setupMessageHandler(self, initWasmBrowser);
setupMessageHandler(false, self, initWasmBrowser);
2 changes: 1 addition & 1 deletion d2js/js/src/worker.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./worker.node.js";
// Replaced at build time
2 changes: 1 addition & 1 deletion d2js/js/src/worker.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ async function initWasmNode(wasmBinary) {
return global.d2;
}

setupMessageHandler(parentPort, initWasmNode);
setupMessageHandler(true, parentPort, initWasmNode);
7 changes: 5 additions & 2 deletions d2js/js/src/worker.shared.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4576d15

Please sign in to comment.