@forgeax/engine-physics-rapier2d 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ // @forgeax/engine-physics-rapier2d — WASM loader for Rapier 2D compat variant.
2
+ //
3
+ // Dynamic import of @dimforge/rapier2d-compat (plan-strategy D-4: compat variant,
4
+ // zero Vite configuration).
5
+
6
+ import type { PhysicsErrorCode } from '@forgeax/engine-types';
7
+ import { PhysicsError } from '@forgeax/engine-types';
8
+
9
+ /**
10
+ * The RAPIER module namespace — all constructors, types, and helpers exposed
11
+ * by @dimforge/rapier2d-compat after init(). This is the shape of the default
12
+ * export of the compat package.
13
+ */
14
+ // biome-ignore lint/suspicious/noExplicitAny: Rapier compat namespace type
15
+ export type Rapier2DModule = any;
16
+
17
+ let rapierInstance: Rapier2DModule | null = null;
18
+ let loadingPromise: Promise<Rapier2DModule | PhysicsError> | null = null;
19
+
20
+ export async function loadRapier2D(): Promise<Rapier2DModule | PhysicsError> {
21
+ if (rapierInstance !== null) return rapierInstance;
22
+ if (loadingPromise !== null) return loadingPromise;
23
+
24
+ loadingPromise = _doLoad();
25
+ return loadingPromise;
26
+ }
27
+
28
+ async function _doLoad(): Promise<Rapier2DModule | PhysicsError> {
29
+ try {
30
+ const RAPIER = await import('@dimforge/rapier2d-compat');
31
+ await RAPIER.default.init();
32
+ rapierInstance = RAPIER.default;
33
+ loadingPromise = null;
34
+ return RAPIER.default;
35
+ } catch (cause) {
36
+ const reason = cause instanceof Error ? cause.message : String(cause);
37
+ loadingPromise = null;
38
+ return new PhysicsError({
39
+ code: 'wasm-load-failed' as PhysicsErrorCode,
40
+ expected: 'successful dynamic import and init of @dimforge/rapier2d-compat',
41
+ hint: `dynamic import or init() failed: ${reason}. Check network, file path, and that @dimforge/rapier2d-compat is installed.`,
42
+ detail: { code: 'wasm-load-failed', reason },
43
+ });
44
+ }
45
+ }