Skip to content

Commit

Permalink
fix lazy loaders (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljttl3q04t authored Apr 5, 2024
1 parent 94a7fda commit 7af6dc8
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion packages/translucent/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,79 @@ async function loadModule() {
}
}

class CModuleLoader {
private _wasm: CModule | null = null;

get get(): CModule {
if (this._wasm === null) {
throw new Error("C has not been loaded");
}
return this._wasm;
}

async load(): Promise<void> {
if (this._wasm !== null) {
return;
}

if (process.browser) {
this._wasm = await import("@dcspark/cardano-multiplatform-lib-browser");
} else {
this._wasm = await import("@dcspark/cardano-multiplatform-lib-nodejs");
}
}
}

class UModuleLoader {
private _wasm: UModule | null = null;

get get(): UModule {
if (this._wasm === null) {
throw new Error("U has not been loaded");
}
return this._wasm;
}

async load(): Promise<void> {
if (this._wasm !== null) {
return;
}

if (process.browser) {
this._wasm = await import("uplc-web");
} else {
this._wasm = await import("uplc-node");
}
}
}

class MModuleLoader {
private _wasm: MModule | null = null;

get get(): MModule {
if (this._wasm === null) {
throw new Error("M has not been loaded");
}
return this._wasm;
}

async load(): Promise<void> {
if (this._wasm !== null) {
return;
}

if (process.browser) {
this._wasm = await import("@emurgo/cardano-message-signing-browser");
} else {
this._wasm = await import("@emurgo/cardano-message-signing-nodejs");
}
}
}

export const cModuleLoader: CModuleLoader = new CModuleLoader();
export const uModuleLoader: UModuleLoader = new UModuleLoader();
export const mModuleLoader: MModuleLoader = new MModuleLoader();

export type {
Address as CAddress,
BigNum as CBigNum,
Expand Down Expand Up @@ -46,7 +119,15 @@ export type {
WithdrawalBuilderResult as CWithdrawalBuilderResult,
} from "@dcspark/cardano-multiplatform-lib-nodejs";

export { C, U, M, loadModule };
export {
C,
U,
M,
loadModule,
cModuleLoader as CModuleLoader,
uModuleLoader as UModuleLoader,
mModuleLoader as MModuleLoader,
};

declare global {
namespace NodeJS {
Expand Down

0 comments on commit 7af6dc8

Please sign in to comment.