Skip to content

Commit

Permalink
Store a promise when loading the manifold module to prevent multiple …
Browse files Browse the repository at this point in the history
…instances from being created (#16200)

Co-authored-by: Kevin Libertowski <[email protected]>
  • Loading branch information
klibertowski and Kevin Libertowski authored Feb 20, 2025
1 parent ca0ed76 commit 82ff9b4
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/dev/core/src/Meshes/csg2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import { Vector3 } from "core/Maths/math.vector";
// eslint-disable-next-line @typescript-eslint/naming-convention
let Manifold: any;

/**
* Promise to wait for the manifold library to be ready
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
let ManifoldPromise: Promise<{ Manifold: any; Mesh: any }>;

/**
* Manifold mesh
*/
Expand Down Expand Up @@ -463,11 +469,16 @@ export async function InitializeCSG2Async(options?: Partial<ICSG2Options>) {
return; // Already initialized
}

if (ManifoldPromise) {
await ManifoldPromise;
return;
}

if (localOptions.manifoldInstance) {
Manifold = localOptions.manifoldInstance;
ManifoldMesh = localOptions.manifoldMeshInstance;
} else {
const result = await _LoadScriptModuleAsync(
ManifoldPromise = _LoadScriptModuleAsync(
`
import Module from '${localOptions.manifoldUrl}/manifold.js';
const wasm = await Module();
Expand All @@ -477,6 +488,7 @@ export async function InitializeCSG2Async(options?: Partial<ICSG2Options>) {
`
);

const result = await ManifoldPromise;
Manifold = result.Manifold;
ManifoldMesh = result.Mesh;
}
Expand Down

0 comments on commit 82ff9b4

Please sign in to comment.