Skip to content

Commit 7af6dc8

Browse files
authored
fix lazy loaders (#2)
1 parent 94a7fda commit 7af6dc8

File tree

1 file changed

+82
-1
lines changed
  • packages/translucent/src/core

1 file changed

+82
-1
lines changed

packages/translucent/src/core/core.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,79 @@ async function loadModule() {
1818
}
1919
}
2020

21+
class CModuleLoader {
22+
private _wasm: CModule | null = null;
23+
24+
get get(): CModule {
25+
if (this._wasm === null) {
26+
throw new Error("C has not been loaded");
27+
}
28+
return this._wasm;
29+
}
30+
31+
async load(): Promise<void> {
32+
if (this._wasm !== null) {
33+
return;
34+
}
35+
36+
if (process.browser) {
37+
this._wasm = await import("@dcspark/cardano-multiplatform-lib-browser");
38+
} else {
39+
this._wasm = await import("@dcspark/cardano-multiplatform-lib-nodejs");
40+
}
41+
}
42+
}
43+
44+
class UModuleLoader {
45+
private _wasm: UModule | null = null;
46+
47+
get get(): UModule {
48+
if (this._wasm === null) {
49+
throw new Error("U has not been loaded");
50+
}
51+
return this._wasm;
52+
}
53+
54+
async load(): Promise<void> {
55+
if (this._wasm !== null) {
56+
return;
57+
}
58+
59+
if (process.browser) {
60+
this._wasm = await import("uplc-web");
61+
} else {
62+
this._wasm = await import("uplc-node");
63+
}
64+
}
65+
}
66+
67+
class MModuleLoader {
68+
private _wasm: MModule | null = null;
69+
70+
get get(): MModule {
71+
if (this._wasm === null) {
72+
throw new Error("M has not been loaded");
73+
}
74+
return this._wasm;
75+
}
76+
77+
async load(): Promise<void> {
78+
if (this._wasm !== null) {
79+
return;
80+
}
81+
82+
if (process.browser) {
83+
this._wasm = await import("@emurgo/cardano-message-signing-browser");
84+
} else {
85+
this._wasm = await import("@emurgo/cardano-message-signing-nodejs");
86+
}
87+
}
88+
}
89+
90+
export const cModuleLoader: CModuleLoader = new CModuleLoader();
91+
export const uModuleLoader: UModuleLoader = new UModuleLoader();
92+
export const mModuleLoader: MModuleLoader = new MModuleLoader();
93+
2194
export type {
2295
Address as CAddress,
2396
BigNum as CBigNum,
@@ -46,7 +119,15 @@ export type {
46119
WithdrawalBuilderResult as CWithdrawalBuilderResult,
47120
} from "@dcspark/cardano-multiplatform-lib-nodejs";
48121

49-
export { C, U, M, loadModule };
122+
export {
123+
C,
124+
U,
125+
M,
126+
loadModule,
127+
cModuleLoader as CModuleLoader,
128+
uModuleLoader as UModuleLoader,
129+
mModuleLoader as MModuleLoader,
130+
};
50131

51132
declare global {
52133
namespace NodeJS {

0 commit comments

Comments
 (0)