Files
esengine/thirdparty/rapier.js/rapier-compat/src2d/init.ts

61 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-12-03 16:24:08 +08:00
/**
* RAPIER initialization module with dynamic WASM loading support.
* RAPIER WASM
*/
import wasmInit from "../pkg/rapier_wasm2d";
/**
* Input types for WASM initialization.
* WASM
*/
export type InitInput =
| RequestInfo // URL string or Request object
| URL // URL object
| Response // Fetch Response object
| BufferSource // ArrayBuffer or TypedArray
| WebAssembly.Module; // Pre-compiled module
let initialized = false;
/**
* Initializes RAPIER.
* Has to be called and awaited before using any library methods.
*
* RAPIER
* 使
*
* @param input - WASM source (required). Can be URL, Response, ArrayBuffer, etc.
* WASM URLResponseArrayBuffer
*
* @example
* // Load from URL | 从 URL 加载
* await RAPIER.init('wasm/rapier_wasm2d_bg.wasm');
*
* @example
* // Load from fetch response | 从 fetch 响应加载
* const response = await fetch('wasm/rapier_wasm2d_bg.wasm');
* await RAPIER.init(response);
*
* @example
* // Load from ArrayBuffer | 从 ArrayBuffer 加载
* const buffer = await fetch('wasm/rapier_wasm2d_bg.wasm').then(r => r.arrayBuffer());
* await RAPIER.init(buffer);
*/
export async function init(input?: InitInput): Promise<void> {
if (initialized) {
return;
}
await wasmInit(input);
initialized = true;
}
/**
* Check if RAPIER is already initialized.
* RAPIER
*/
export function isInitialized(): boolean {
return initialized;
}