diff --git a/tools/upend_js/api.ts b/tools/upend_js/api.ts index 645a3fc..ab9d512 100644 --- a/tools/upend_js/api.ts +++ b/tools/upend_js/api.ts @@ -13,7 +13,7 @@ import type { StoreInfo, VaultInfo, } from "./types"; -import init_wasm from "upend_wasm"; +import init_wasm, { InitOutput } from "upend_wasm"; import { AddressComponents, AddressTypeConstants, @@ -27,8 +27,10 @@ export { AddressComponents }; export class UpEndApi { private instanceUrl = ""; + private wasmPath: string | undefined; private wasmInitialized = false; + private wasmPromise: Promise | undefined; private addressTypeConstants: AddressTypeConstants | undefined = undefined; private queryOnceLRU = new LRU({ max: 128 }); @@ -265,7 +267,15 @@ export class UpEndApi { "Path to WASM file not specified, cannot initialize WASM extensions." ); } - await init_wasm(this.wasmPath); + if (this.wasmPromise) { + await this.wasmPromise; + } else { + dbg("Initializing WASM..."); + this.wasmPromise = init_wasm(this.wasmPath); + await this.wasmPromise; + dbg("Wasm initialized."); + } + this.wasmInitialized = true; } }