From b3d6866c7c13899738881c82eb6f139d9bb28a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Sun, 27 Aug 2023 11:48:44 +0200 Subject: [PATCH] fix: duplicate wasm initialization --- tools/upend_js/api.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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; } }