fix(jslib): ✨ allow initialization of wasm via wasm modules
parent
11e0bfa96d
commit
1f551fc087
|
@ -19,6 +19,7 @@ import {
|
|||
AddressTypeConstants,
|
||||
addr_to_components,
|
||||
components_to_addr,
|
||||
InitInput,
|
||||
} from "upend_wasm";
|
||||
import debug from "debug";
|
||||
const dbg = debug("upend:api");
|
||||
|
@ -27,19 +28,17 @@ export { AddressComponents };
|
|||
|
||||
export class UpEndApi {
|
||||
private instanceUrl = "";
|
||||
|
||||
private wasmPath: string | undefined;
|
||||
private wasmInit: InitInput | undefined;
|
||||
private wasmInitialized = false;
|
||||
private wasmPromise: Promise<unknown> | undefined;
|
||||
private addressTypeConstants: AddressTypeConstants | undefined = undefined;
|
||||
|
||||
private queryOnceLRU = new LRU<string, UpListing>({ max: 128 });
|
||||
private inFlightRequests: { [key: string]: Promise<UpListing> | null } = {};
|
||||
|
||||
constructor(instanceUrl = "", wasmPath?: string) {
|
||||
constructor(instanceUrl = "", wasmInit?: InitInput) {
|
||||
this.setInstanceUrl(instanceUrl);
|
||||
if (wasmPath) {
|
||||
this.setWasmPath(wasmPath);
|
||||
if (wasmInit) {
|
||||
this.setWasmInit(wasmInit);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,8 +46,8 @@ export class UpEndApi {
|
|||
this.instanceUrl = apiUrl.replace(/\/+$/g, "");
|
||||
}
|
||||
|
||||
public setWasmPath(wasmPath: string) {
|
||||
this.wasmPath = wasmPath;
|
||||
public setWasmInit(wasmInit: InitInput) {
|
||||
this.wasmInit = wasmInit;
|
||||
}
|
||||
|
||||
public get apiUrl() {
|
||||
|
@ -115,13 +114,7 @@ export class UpEndApi {
|
|||
value: IValue,
|
||||
provenance?: string
|
||||
): Promise<Address> {
|
||||
dbg(
|
||||
"Putting %s = %o for %s%s",
|
||||
attribute,
|
||||
value,
|
||||
entity,
|
||||
provenance ? ` (Provenance: ${provenance})` : ""
|
||||
);
|
||||
dbg("Putting %s = %o for %s (%s)", attribute, value, entity, provenance);
|
||||
let url = `${this.apiUrl}/obj/${entity}/${attribute}`;
|
||||
if (provenance) {
|
||||
url += `?provenance=${provenance}`;
|
||||
|
@ -268,20 +261,12 @@ export class UpEndApi {
|
|||
|
||||
private async initWasm(): Promise<void> {
|
||||
if (!this.wasmInitialized) {
|
||||
if (!this.wasmPath) {
|
||||
if (!this.wasmInit) {
|
||||
throw new Error(
|
||||
"Path to WASM file not specified, cannot initialize WASM extensions."
|
||||
"WASM init not specified, cannot initialize WASM extensions."
|
||||
);
|
||||
}
|
||||
if (this.wasmPromise) {
|
||||
await this.wasmPromise;
|
||||
} else {
|
||||
dbg("Initializing WASM...");
|
||||
this.wasmPromise = init_wasm(this.wasmPath);
|
||||
await this.wasmPromise;
|
||||
dbg("Wasm initialized.");
|
||||
}
|
||||
|
||||
await init_wasm(this.wasmInit);
|
||||
this.wasmInitialized = true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue