Skip to content

Commit 82ff9b4

Browse files
klibertowskiKevin Libertowski
andauthored
Store a promise when loading the manifold module to prevent multiple instances from being created (#16200)
Co-authored-by: Kevin Libertowski <[email protected]>
1 parent ca0ed76 commit 82ff9b4

File tree

1 file changed

+13
-1
lines changed
  • packages/dev/core/src/Meshes

1 file changed

+13
-1
lines changed

packages/dev/core/src/Meshes/csg2.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import { Vector3 } from "core/Maths/math.vector";
1818
// eslint-disable-next-line @typescript-eslint/naming-convention
1919
let Manifold: any;
2020

21+
/**
22+
* Promise to wait for the manifold library to be ready
23+
*/
24+
// eslint-disable-next-line @typescript-eslint/naming-convention
25+
let ManifoldPromise: Promise<{ Manifold: any; Mesh: any }>;
26+
2127
/**
2228
* Manifold mesh
2329
*/
@@ -463,11 +469,16 @@ export async function InitializeCSG2Async(options?: Partial<ICSG2Options>) {
463469
return; // Already initialized
464470
}
465471

472+
if (ManifoldPromise) {
473+
await ManifoldPromise;
474+
return;
475+
}
476+
466477
if (localOptions.manifoldInstance) {
467478
Manifold = localOptions.manifoldInstance;
468479
ManifoldMesh = localOptions.manifoldMeshInstance;
469480
} else {
470-
const result = await _LoadScriptModuleAsync(
481+
ManifoldPromise = _LoadScriptModuleAsync(
471482
`
472483
import Module from '${localOptions.manifoldUrl}/manifold.js';
473484
const wasm = await Module();
@@ -477,6 +488,7 @@ export async function InitializeCSG2Async(options?: Partial<ICSG2Options>) {
477488
`
478489
);
479490

491+
const result = await ManifoldPromise;
480492
Manifold = result.Manifold;
481493
ManifoldMesh = result.Mesh;
482494
}

0 commit comments

Comments
 (0)