Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added checks to init() in rapier3d-compat module to catch multiple calls #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion rapier-compat/src2d/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,37 @@ import wasmBase64 from "../pkg2d/rapier_wasm2d_bg.wasm";
import wasmInit from "../pkg2d/rapier_wasm2d";
import base64 from "base64-js";

/**
* Flag to check if RAPIER has already been initialized
* or is currently being initialized.
*/
let initialized = false;

/**
* If init has already been called before but initialization
* is not done yet, the unresolved promise is returned.
*/
let initPromise: ReturnType<typeof wasmInit> | undefined = undefined;

/**
* Initializes RAPIER.
* Has to be called and awaited before using any library methods.
*/
export async function init() {
await wasmInit(base64.toByteArray(wasmBase64 as unknown as string).buffer);
// return if RAPIER has been initialized
if (initialized) return;

// return the unresolve promise if RAPIER is currently initializing
if (initPromise) return initPromise;

// init and assign promise
initPromise = wasmInit(
base64.toByteArray(wasmBase64 as unknown as string).buffer,
);

// await initialization
await initPromise;

// set initialized flag
initialized = true;
}
29 changes: 28 additions & 1 deletion rapier-compat/src3d/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,37 @@ import wasmBase64 from "../pkg3d/rapier_wasm3d_bg.wasm";
import wasmInit from "../pkg3d/rapier_wasm3d";
import base64 from "base64-js";

/**
* Flag to check if RAPIER has already been initialized
* or is currently being initialized.
*/
let initialized = false;

/**
* If init has already been called before but initialization
* is not done yet, the unresolved promise is returned.
*/
let initPromise: ReturnType<typeof wasmInit> | undefined = undefined;

/**
* Initializes RAPIER.
* Has to be called and awaited before using any library methods.
*/
export async function init() {
await wasmInit(base64.toByteArray(wasmBase64 as unknown as string).buffer);
// return if RAPIER has been initialized
if (initialized) return;

// return the unresolve promise if RAPIER is currently initializing
if (initPromise) return initPromise;

// init and assign promise
initPromise = wasmInit(
base64.toByteArray(wasmBase64 as unknown as string).buffer,
);

// await initialization
await initPromise;

// set initialized flag
initialized = true;
}