Skip to content

Commit 4370d11

Browse files
author
Hack.bg R&D
committed
perf(wasm): load once
1 parent 660c8af commit 4370d11

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/sdk.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type {
1111
Program as WasmProgram,
1212
} from '../pkg/fadroma_simf.d.ts';
1313

14+
/** WASM cache to download the binary only once. */
15+
let blob = null;
1416
/** Instance of SimplicityHL WASM module. */
1517
export type Wasm = InitOutput;
1618
/** Load SimplicityHL WASM module. */
@@ -19,10 +21,16 @@ export async function Wasm ({
1921
// You can replace this with w.g. `readFile` from `fs/promises`; or polyfill `globalThis.fetch`
2022
fetch = globalThis.fetch
2123
} = {}) {
22-
console.debug(`Loading Fadroma SimplicityHL WASM module from ${wasm}`);
2324
const conform = (x: string|URL) => new URL(x).toString();
2425
const wrap = await import('../pkg/fadroma_simf.js');
25-
if ((typeof wasm === 'string')||(wasm instanceof URL)) wasm = await fetch(conform(wasm));
26+
if (!blob) {
27+
if ((typeof wasm === 'string')||(wasm instanceof URL)) {
28+
console.debug(`Loading Fadroma SimplicityHL WASM module from ${wasm}`);
29+
blob = await fetch(conform(wasm));
30+
} else {
31+
throw new Error(`Invalid WASM URL, need string or URL, got: ${wasm}`)
32+
}
33+
}
2634
await wrap.default(wasm);
2735
return wrap;
2836
}

0 commit comments

Comments
 (0)