Skip to content

Commit

Permalink
refactor: early return for instance
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Aug 11, 2023
1 parent 54905c5 commit bd4aafe
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,37 @@ const go = new Go();
let inst;

export default async function init(wasm_url) {
if (!inst) {
if (!wasm_url) {
wasm_url = new URL("lib.wasm", import.meta.url);
}
if (inst) {
return await inst;
}

if (typeof wasm_url === "string") {
wasm_url = new URL(wasm_url);
}
if (!wasm_url) {
wasm_url = new URL("lib.wasm", import.meta.url);
}

if (
typeof __webpack_require__ !== "function" &&
wasm_url.protocol === "file:"
) {
inst = import("node:fs/promises")
.then((fs) => fs.readFile(wasm_url))
.then((bytes) =>
WebAssembly.instantiate(bytes, go.importObject)
);
} else if ("instantiateStreaming" in WebAssembly) {
inst = WebAssembly.instantiateStreaming(
fetch(wasm_url),
go.importObject
);
} else {
inst = fetch(wasm_url)
.then((response) => response.arrayBuffer())
.then((bytes) =>
WebAssembly.instantiate(bytes, go.importObject)
);
}
inst = (await inst).instance;
go.run(inst);
if (typeof wasm_url === "string") {
wasm_url = new URL(wasm_url);
}

await inst;
if (
typeof __webpack_require__ !== "function" &&
wasm_url.protocol === "file:"
) {
inst = import("node:fs/promises")
.then((fs) => fs.readFile(wasm_url))
.then((bytes) => WebAssembly.instantiate(bytes, go.importObject));
} else if ("instantiateStreaming" in WebAssembly) {
inst = WebAssembly.instantiateStreaming(
fetch(wasm_url),
go.importObject
);
} else {
inst = fetch(wasm_url)
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, go.importObject));
}
inst = (await inst).instance;
go.run(inst);
}

export function format(input) {
Expand Down

0 comments on commit bd4aafe

Please sign in to comment.