Skip to content

Commit c501dcb

Browse files
committed
make sure that snarky is always defined when returning from initializebindings
1 parent 90de4c1 commit c501dcb

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/snarky.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ import { wasm, withThreadPool } from './bindings/js/node/node-backend.js';
33

44
let Snarky, Ledger, Pickles, Test_;
55
let isInitialized = false;
6+
let initializingPromise;
67

78
async function initializeBindings() {
89
if (isInitialized) return;
9-
isInitialized = true;
10+
if (initializingPromise) {
11+
await initializingPromise;
12+
return;
13+
}
1014
let snarky;
15+
let resolve;
16+
initializingPromise = new Promise((r) => (resolve = r));
1117

1218
// this dynamic import makes jest respect the import order
1319
// otherwise the cjs file gets imported before its implicit esm dependencies and fails
@@ -18,6 +24,9 @@ async function initializeBindings() {
1824
await import('./bindings/compiled/_node_bindings/o1js_node.bc.cjs')
1925
).default;
2026
({ Snarky, Ledger, Pickles, Test: Test_ } = snarky);
27+
resolve();
28+
initializingPromise = undefined;
29+
isInitialized = true;
2130
}
2231

2332
async function Test() {

0 commit comments

Comments
 (0)