@plasius/gpu-xr 0.1.0

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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## 0.1.0 - 2026-02-10
6
+
7
+ - Initial scaffold for `@plasius/gpu-xr`.
8
+ - Added framework-agnostic WebXR support probing and session lifecycle APIs.
9
+ - Added unit tests, demo, and ADR documentation.
package/LICENSE ADDED
@@ -0,0 +1,203 @@
1
+ Copyright 2025 Plasius LTD
2
+
3
+ Apache License
4
+ Version 2.0, January 2004
5
+ http://www.apache.org/licenses/
6
+
7
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ 1. Definitions.
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction,
12
+ and distribution as defined by Sections 1 through 9 of this document.
13
+
14
+ "Licensor" shall mean the copyright owner or entity authorized by
15
+ the copyright owner that is granting the License.
16
+
17
+ "Legal Entity" shall mean the union of the acting entity and all
18
+ other entities that control, are controlled by, or are under common
19
+ control with that entity. For the purposes of this definition,
20
+ "control" means (i) the power, direct or indirect, to cause the
21
+ direction or management of such entity, whether by contract or
22
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
23
+ outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ "You" (or "Your") shall mean an individual or Legal Entity
26
+ exercising permissions granted by this License.
27
+
28
+ "Source" form shall mean the preferred form for making modifications,
29
+ including but not limited to software source code, documentation
30
+ source, and configuration files.
31
+
32
+ "Object" form shall mean any form resulting from mechanical
33
+ transformation or translation of a Source form, including but
34
+ not limited to compiled object code, generated documentation,
35
+ and conversions to other media types.
36
+
37
+ "Work" shall mean the work of authorship, whether in Source or
38
+ Object form, made available under the License, as indicated by a
39
+ copyright notice that is included in or attached to the work
40
+ (an example is provided in the Appendix below).
41
+
42
+ "Derivative Works" shall mean any work, whether in Source or Object
43
+ form, that is based on (or derived from) the Work and for which the
44
+ editorial revisions, annotations, elaborations, or other modifications
45
+ represent, as a whole, an original work of authorship. For the purposes
46
+ of this License, Derivative Works shall not include works that remain
47
+ separable from, or merely link (or bind by name) to the interfaces of,
48
+ the Work and Derivative Works thereof.
49
+
50
+ "Contribution" shall mean any work of authorship, including
51
+ the original version of the Work and any modifications or additions
52
+ to that Work or Derivative Works thereof, that is intentionally
53
+ submitted to Licensor for inclusion in the Work by the copyright owner
54
+ or by an individual or Legal Entity authorized to submit on behalf of
55
+ the copyright owner. For the purposes of this definition, "submitted"
56
+ means any form of electronic, verbal, or written communication sent
57
+ to the Licensor or its representatives, including but not limited to
58
+ communication on electronic mailing lists, source code control systems,
59
+ and issue tracking systems that are managed by, or on behalf of, the
60
+ Licensor for the purpose of discussing and improving the Work, but
61
+ excluding communication that is conspicuously marked or otherwise
62
+ designated in writing by the copyright owner as "Not a Contribution."
63
+
64
+ "Contributor" shall mean Licensor and any individual or Legal Entity
65
+ on behalf of whom a Contribution has been received by Licensor and
66
+ subsequently incorporated within the Work.
67
+
68
+ 2. Grant of Copyright License. Subject to the terms and conditions of
69
+ this License, each Contributor hereby grants to You a perpetual,
70
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
71
+ copyright license to reproduce, prepare Derivative Works of,
72
+ publicly display, publicly perform, sublicense, and distribute the
73
+ Work and such Derivative Works in Source or Object form.
74
+
75
+ 3. Grant of Patent License. Subject to the terms and conditions of
76
+ this License, each Contributor hereby grants to You a perpetual,
77
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
78
+ (except as stated in this section) patent license to make, have made,
79
+ use, offer to sell, sell, import, and otherwise transfer the Work,
80
+ where such license applies only to those patent claims licensable
81
+ by such Contributor that are necessarily infringed by their
82
+ Contribution(s) alone or by combination of their Contribution(s)
83
+ with the Work to which such Contribution(s) was submitted. If You
84
+ institute patent litigation against any entity (including a
85
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
86
+ or a Contribution incorporated within the Work constitutes direct
87
+ or contributory patent infringement, then any patent licenses
88
+ granted to You under this License for that Work shall terminate
89
+ as of the date such litigation is filed.
90
+
91
+ 4. Redistribution. You may reproduce and distribute copies of the
92
+ Work or Derivative Works thereof in any medium, with or without
93
+ modifications, and in Source or Object form, provided that You
94
+ meet the following conditions:
95
+
96
+ (a) You must give any other recipients of the Work or
97
+ Derivative Works a copy of this License; and
98
+
99
+ (b) You must cause any modified files to carry prominent notices
100
+ stating that You changed the files; and
101
+
102
+ (c) You must retain, in the Source form of any Derivative Works
103
+ that You distribute, all copyright, patent, trademark, and
104
+ attribution notices from the Source form of the Work,
105
+ excluding those notices that do not pertain to any part of
106
+ the Derivative Works; and
107
+
108
+ (d) If the Work includes a "NOTICE" text file as part of its
109
+ distribution, then any Derivative Works that You distribute must
110
+ include a readable copy of the attribution notices contained
111
+ within such NOTICE file, excluding those notices that do not
112
+ pertain to any part of the Derivative Works, in at least one
113
+ of the following places: within a NOTICE text file distributed
114
+ as part of the Derivative Works; within the Source form or
115
+ documentation, if provided along with the Derivative Works; or,
116
+ within a display generated by the Derivative Works, if and
117
+ wherever such third-party notices normally appear. The contents
118
+ of the NOTICE file are for informational purposes only and
119
+ do not modify the License. You may add Your own attribution
120
+ notices within Derivative Works that You distribute, alongside
121
+ or as an addendum to the NOTICE text from the Work, provided
122
+ that such additional attribution notices cannot be construed
123
+ as modifying the License.
124
+
125
+ You may add Your own copyright statement to Your modifications and
126
+ may provide additional or different license terms and conditions
127
+ for use, reproduction, or distribution of Your modifications, or
128
+ for any such Derivative Works as a whole, provided Your use,
129
+ reproduction, and distribution of the Work otherwise complies with
130
+ the conditions stated in this License.
131
+
132
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
133
+ any Contribution intentionally submitted for inclusion in the Work
134
+ by You to the Licensor shall be under the terms and conditions of
135
+ this License, without any additional terms or conditions.
136
+ Notwithstanding the above, nothing herein shall supersede or modify
137
+ the terms of any separate license agreement you may have executed
138
+ with Licensor regarding such Contributions.
139
+
140
+ 6. Trademarks. This License does not grant permission to use the trade
141
+ names, trademarks, service marks, or product names of the Licensor,
142
+ except as required for reasonable and customary use in describing the
143
+ origin of the Work and reproducing the content of the NOTICE file.
144
+
145
+ 7. Disclaimer of Warranty. Unless required by applicable law or
146
+ agreed to in writing, Licensor provides the Work (and each
147
+ Contributor provides its Contributions) on an "AS IS" BASIS,
148
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
149
+ implied, including, without limitation, any warranties or conditions
150
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
151
+ PARTICULAR PURPOSE. You are solely responsible for determining the
152
+ appropriateness of using or redistributing the Work and assume any
153
+ risks associated with Your exercise of permissions under this License.
154
+
155
+ 8. Limitation of Liability. In no event and under no legal theory,
156
+ whether in tort (including negligence), contract, or otherwise,
157
+ unless required by applicable law (such as deliberate and grossly
158
+ negligent acts) or agreed to in writing, shall any Contributor be
159
+ liable to You for damages, including any direct, indirect, special,
160
+ incidental, or consequential damages of any character arising as a
161
+ result of this License or out of the use or inability to use the
162
+ Work (including but not limited to damages for loss of goodwill,
163
+ work stoppage, computer failure or malfunction, or any and all
164
+ other commercial damages or losses), even if such Contributor
165
+ has been advised of the possibility of such damages.
166
+
167
+ 9. Accepting Warranty or Additional Liability. While redistributing
168
+ the Work or Derivative Works thereof, You may choose to offer,
169
+ and charge a fee for, acceptance of support, warranty, indemnity,
170
+ or other liability obligations and/or rights consistent with this
171
+ License. However, in accepting such obligations, You may act only
172
+ on Your own behalf and on Your sole responsibility, not on behalf
173
+ of any other Contributor, and only if You agree to indemnify,
174
+ defend, and hold each Contributor harmless for any liability
175
+ incurred by, or claims asserted against, such Contributor by reason
176
+ of your accepting any such warranty or additional liability.
177
+
178
+ END OF TERMS AND CONDITIONS
179
+
180
+ APPENDIX: How to apply the Apache License to your work.
181
+
182
+ To apply the Apache License to your work, attach the following
183
+ boilerplate notice, with the fields enclosed by brackets "[]"
184
+ replaced with your own identifying information. (Don't include
185
+ the brackets!) The text should be enclosed in the appropriate
186
+ comment syntax for the file format. We also recommend that a
187
+ file or class name and description of purpose be included on the
188
+ same "printed page" as the copyright notice for easier
189
+ identification within third-party archives.
190
+
191
+ Copyright [yyyy] [name of copyright owner]
192
+
193
+ Licensed under the Apache License, Version 2.0 (the "License");
194
+ you may not use this file except in compliance with the License.
195
+ You may obtain a copy of the License at
196
+
197
+ http://www.apache.org/licenses/LICENSE-2.0
198
+
199
+ Unless required by applicable law or agreed to in writing, software
200
+ distributed under the License is distributed on an "AS IS" BASIS,
201
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202
+ See the License for the specific language governing permissions and
203
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # @plasius/gpu-xr
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@plasius/gpu-xr)](https://www.npmjs.com/package/@plasius/gpu-xr)
4
+ [![license](https://img.shields.io/github/license/Plasius-LTD/gpu-xr)](./LICENSE)
5
+
6
+ Framework-agnostic WebXR session management for Plasius GPU rendering projects.
7
+ This package isolates VR session lifecycle and capability probing so app layers can
8
+ replace Three.js and still keep immersive workflows.
9
+
10
+ Apache-2.0. ESM + CJS builds.
11
+
12
+ ## Install
13
+
14
+ ```sh
15
+ npm install @plasius/gpu-xr
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```js
21
+ import { createXrManager } from "@plasius/gpu-xr";
22
+
23
+ const xr = createXrManager();
24
+ await xr.probeSupport(["immersive-vr"]);
25
+
26
+ const session = await xr.enterVr({
27
+ optionalFeatures: ["depth-sensing", "layers"],
28
+ });
29
+
30
+ // ... bind WebGPU render state to the session ...
31
+
32
+ await xr.exitSession();
33
+ ```
34
+
35
+ ## API
36
+
37
+ - `isXrModeSupported(mode, options)`
38
+ - `requestXrSession(options)`
39
+ - `createXrStore(initialState)`
40
+ - `createXrManager(options)`
41
+ - `mergeXrSessionInit(base, override)`
42
+ - `defaultVrSessionInit`
43
+
44
+ ## Demo
45
+
46
+ Run the demo server from the repo root:
47
+
48
+ ```sh
49
+ cd gpu-xr
50
+ npm run demo
51
+ ```
52
+
53
+ Then open `http://localhost:8000/gpu-xr/demo/`.
54
+
55
+ ## Files
56
+
57
+ - `src/index.js`: XR runtime/session manager and store.
58
+ - `tests/package.test.js`: Unit tests for support probing and lifecycle handling.
59
+ - `docs/adrs/*`: XR architecture decisions.
package/dist/index.cjs ADDED
@@ -0,0 +1,312 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.js
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ createXrManager: () => createXrManager,
23
+ createXrStore: () => createXrStore,
24
+ defaultVrSessionInit: () => defaultVrSessionInit,
25
+ isXrModeSupported: () => isXrModeSupported,
26
+ mergeXrSessionInit: () => mergeXrSessionInit,
27
+ requestXrSession: () => requestXrSession,
28
+ xrReferenceSpaceTypes: () => xrReferenceSpaceTypes,
29
+ xrSessionModes: () => xrSessionModes
30
+ });
31
+ module.exports = __toCommonJS(index_exports);
32
+ var DEFAULT_VR_SESSION_INIT = Object.freeze({
33
+ requiredFeatures: ["local-floor"],
34
+ optionalFeatures: ["bounded-floor", "hand-tracking", "layers"]
35
+ });
36
+ var xrSessionModes = Object.freeze([
37
+ "inline",
38
+ "immersive-vr",
39
+ "immersive-ar"
40
+ ]);
41
+ var xrReferenceSpaceTypes = Object.freeze([
42
+ "viewer",
43
+ "local",
44
+ "local-floor",
45
+ "bounded-floor",
46
+ "unbounded"
47
+ ]);
48
+ function toStringArray(values) {
49
+ if (!Array.isArray(values)) {
50
+ return [];
51
+ }
52
+ return values.filter((value) => typeof value === "string").map((value) => value.trim()).filter(Boolean);
53
+ }
54
+ function dedupeStrings(values) {
55
+ return [...new Set(toStringArray(values))];
56
+ }
57
+ function readNavigator(navigatorOverride) {
58
+ const currentNavigator = navigatorOverride ?? globalThis.navigator;
59
+ if (!currentNavigator || typeof currentNavigator !== "object") {
60
+ throw new Error(
61
+ "WebXR navigator unavailable. Provide a browser navigator with navigator.xr."
62
+ );
63
+ }
64
+ return currentNavigator;
65
+ }
66
+ function readXrSystem(navigatorOverride) {
67
+ const currentNavigator = readNavigator(navigatorOverride);
68
+ const xr = currentNavigator.xr;
69
+ if (!xr || typeof xr !== "object") {
70
+ throw new Error(
71
+ "WebXR runtime unavailable. navigator.xr is missing in this environment."
72
+ );
73
+ }
74
+ return xr;
75
+ }
76
+ function assertSessionMode(mode) {
77
+ if (!xrSessionModes.includes(mode)) {
78
+ const available = xrSessionModes.join(", ");
79
+ throw new Error(
80
+ `Unknown XR session mode "${mode}". Available modes: ${available}.`
81
+ );
82
+ }
83
+ }
84
+ function mergeXrSessionInit(base = {}, override = {}) {
85
+ const requiredFeatures = dedupeStrings([
86
+ ...toStringArray(base.requiredFeatures),
87
+ ...toStringArray(override.requiredFeatures)
88
+ ]);
89
+ const optionalFeatures = dedupeStrings([
90
+ ...toStringArray(base.optionalFeatures),
91
+ ...toStringArray(override.optionalFeatures)
92
+ ]);
93
+ const merged = {
94
+ ...base,
95
+ ...override,
96
+ requiredFeatures,
97
+ optionalFeatures
98
+ };
99
+ if (requiredFeatures.length === 0) {
100
+ delete merged.requiredFeatures;
101
+ }
102
+ if (optionalFeatures.length === 0) {
103
+ delete merged.optionalFeatures;
104
+ }
105
+ return merged;
106
+ }
107
+ async function isXrModeSupported(mode = "immersive-vr", options = {}) {
108
+ assertSessionMode(mode);
109
+ const { navigator: navigatorOverride } = options;
110
+ let xr;
111
+ try {
112
+ xr = readXrSystem(navigatorOverride);
113
+ } catch {
114
+ return false;
115
+ }
116
+ if (typeof xr.isSessionSupported !== "function") {
117
+ return false;
118
+ }
119
+ try {
120
+ return Boolean(await xr.isSessionSupported(mode));
121
+ } catch {
122
+ return false;
123
+ }
124
+ }
125
+ async function requestXrSession(options = {}) {
126
+ const {
127
+ mode = "immersive-vr",
128
+ sessionInit = {},
129
+ baseSessionInit = DEFAULT_VR_SESSION_INIT,
130
+ navigator: navigatorOverride
131
+ } = options;
132
+ assertSessionMode(mode);
133
+ const xr = readXrSystem(navigatorOverride);
134
+ if (typeof xr.requestSession !== "function") {
135
+ throw new Error("WebXR requestSession API unavailable.");
136
+ }
137
+ const init = mergeXrSessionInit(baseSessionInit, sessionInit);
138
+ return xr.requestSession(mode, init);
139
+ }
140
+ function createXrStore(initialState = {}) {
141
+ const listeners = /* @__PURE__ */ new Set();
142
+ let state = {
143
+ activeSession: null,
144
+ mode: null,
145
+ isEntering: false,
146
+ lastError: null,
147
+ supportedModes: {},
148
+ ...initialState
149
+ };
150
+ const notify = () => {
151
+ for (const listener of listeners) {
152
+ listener(state);
153
+ }
154
+ };
155
+ return {
156
+ getSnapshot() {
157
+ return state;
158
+ },
159
+ subscribe(listener) {
160
+ listeners.add(listener);
161
+ return () => {
162
+ listeners.delete(listener);
163
+ };
164
+ },
165
+ set(partialState) {
166
+ state = {
167
+ ...state,
168
+ ...partialState
169
+ };
170
+ notify();
171
+ },
172
+ reset() {
173
+ state = {
174
+ activeSession: null,
175
+ mode: null,
176
+ isEntering: false,
177
+ lastError: null,
178
+ supportedModes: {},
179
+ ...initialState
180
+ };
181
+ notify();
182
+ }
183
+ };
184
+ }
185
+ function createXrManager(options = {}) {
186
+ const {
187
+ navigator: navigatorOverride,
188
+ defaultMode = "immersive-vr",
189
+ baseSessionInit = DEFAULT_VR_SESSION_INIT,
190
+ onSessionStart,
191
+ onSessionEnd
192
+ } = options;
193
+ assertSessionMode(defaultMode);
194
+ const store = createXrStore();
195
+ let activeSessionEndHandler = null;
196
+ const detachSessionEndHandler = () => {
197
+ const { activeSession } = store.getSnapshot();
198
+ if (activeSession && activeSessionEndHandler && typeof activeSession.removeEventListener === "function") {
199
+ activeSession.removeEventListener("end", activeSessionEndHandler);
200
+ }
201
+ activeSessionEndHandler = null;
202
+ };
203
+ const handleSessionEnded = () => {
204
+ detachSessionEndHandler();
205
+ store.set({
206
+ activeSession: null,
207
+ mode: null,
208
+ isEntering: false
209
+ });
210
+ if (typeof onSessionEnd === "function") {
211
+ onSessionEnd();
212
+ }
213
+ };
214
+ const attachSessionEndHandler = (session) => {
215
+ if (!session || typeof session.addEventListener !== "function") {
216
+ return;
217
+ }
218
+ activeSessionEndHandler = handleSessionEnded;
219
+ session.addEventListener("end", activeSessionEndHandler);
220
+ };
221
+ const getState = () => store.getSnapshot();
222
+ const subscribe = (listener) => store.subscribe(listener);
223
+ const probeSupport = async (modes = [defaultMode]) => {
224
+ const supportedModes = {};
225
+ for (const mode of modes) {
226
+ assertSessionMode(mode);
227
+ supportedModes[mode] = await isXrModeSupported(mode, {
228
+ navigator: navigatorOverride
229
+ });
230
+ }
231
+ store.set({ supportedModes });
232
+ return supportedModes;
233
+ };
234
+ const enterSession = async (mode = defaultMode, sessionInit = {}) => {
235
+ assertSessionMode(mode);
236
+ const existing = store.getSnapshot().activeSession;
237
+ if (existing) {
238
+ return existing;
239
+ }
240
+ store.set({ isEntering: true, lastError: null });
241
+ try {
242
+ const session = await requestXrSession({
243
+ mode,
244
+ sessionInit,
245
+ baseSessionInit,
246
+ navigator: navigatorOverride
247
+ });
248
+ attachSessionEndHandler(session);
249
+ store.set({
250
+ activeSession: session,
251
+ mode,
252
+ isEntering: false,
253
+ lastError: null
254
+ });
255
+ if (typeof onSessionStart === "function") {
256
+ onSessionStart(session, mode);
257
+ }
258
+ return session;
259
+ } catch (error) {
260
+ const message = error instanceof Error ? error.message : String(error ?? "Unknown XR error");
261
+ store.set({
262
+ isEntering: false,
263
+ lastError: message
264
+ });
265
+ throw error;
266
+ }
267
+ };
268
+ const enterVr = async (sessionInit = {}) => {
269
+ return enterSession("immersive-vr", sessionInit);
270
+ };
271
+ const exitSession = async () => {
272
+ const { activeSession } = store.getSnapshot();
273
+ if (!activeSession) {
274
+ return false;
275
+ }
276
+ if (typeof activeSession.end === "function") {
277
+ await activeSession.end();
278
+ }
279
+ if (store.getSnapshot().activeSession) {
280
+ handleSessionEnded();
281
+ }
282
+ return true;
283
+ };
284
+ const dispose = async () => {
285
+ await exitSession();
286
+ detachSessionEndHandler();
287
+ store.reset();
288
+ };
289
+ return {
290
+ store,
291
+ getState,
292
+ subscribe,
293
+ probeSupport,
294
+ enterSession,
295
+ enterVr,
296
+ exitSession,
297
+ dispose
298
+ };
299
+ }
300
+ var defaultVrSessionInit = DEFAULT_VR_SESSION_INIT;
301
+ // Annotate the CommonJS export names for ESM import in node:
302
+ 0 && (module.exports = {
303
+ createXrManager,
304
+ createXrStore,
305
+ defaultVrSessionInit,
306
+ isXrModeSupported,
307
+ mergeXrSessionInit,
308
+ requestXrSession,
309
+ xrReferenceSpaceTypes,
310
+ xrSessionModes
311
+ });
312
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.js"],"sourcesContent":["const DEFAULT_VR_SESSION_INIT = Object.freeze({\n requiredFeatures: [\"local-floor\"],\n optionalFeatures: [\"bounded-floor\", \"hand-tracking\", \"layers\"],\n});\n\nexport const xrSessionModes = Object.freeze([\n \"inline\",\n \"immersive-vr\",\n \"immersive-ar\",\n]);\n\nexport const xrReferenceSpaceTypes = Object.freeze([\n \"viewer\",\n \"local\",\n \"local-floor\",\n \"bounded-floor\",\n \"unbounded\",\n]);\n\nfunction toStringArray(values) {\n if (!Array.isArray(values)) {\n return [];\n }\n return values\n .filter((value) => typeof value === \"string\")\n .map((value) => value.trim())\n .filter(Boolean);\n}\n\nfunction dedupeStrings(values) {\n return [...new Set(toStringArray(values))];\n}\n\nfunction readNavigator(navigatorOverride) {\n const currentNavigator = navigatorOverride ?? globalThis.navigator;\n if (!currentNavigator || typeof currentNavigator !== \"object\") {\n throw new Error(\n \"WebXR navigator unavailable. Provide a browser navigator with navigator.xr.\"\n );\n }\n return currentNavigator;\n}\n\nfunction readXrSystem(navigatorOverride) {\n const currentNavigator = readNavigator(navigatorOverride);\n const xr = currentNavigator.xr;\n if (!xr || typeof xr !== \"object\") {\n throw new Error(\n \"WebXR runtime unavailable. navigator.xr is missing in this environment.\"\n );\n }\n return xr;\n}\n\nfunction assertSessionMode(mode) {\n if (!xrSessionModes.includes(mode)) {\n const available = xrSessionModes.join(\", \");\n throw new Error(\n `Unknown XR session mode \\\"${mode}\\\". Available modes: ${available}.`\n );\n }\n}\n\nexport function mergeXrSessionInit(base = {}, override = {}) {\n const requiredFeatures = dedupeStrings([\n ...toStringArray(base.requiredFeatures),\n ...toStringArray(override.requiredFeatures),\n ]);\n\n const optionalFeatures = dedupeStrings([\n ...toStringArray(base.optionalFeatures),\n ...toStringArray(override.optionalFeatures),\n ]);\n\n const merged = {\n ...base,\n ...override,\n requiredFeatures,\n optionalFeatures,\n };\n\n if (requiredFeatures.length === 0) {\n delete merged.requiredFeatures;\n }\n\n if (optionalFeatures.length === 0) {\n delete merged.optionalFeatures;\n }\n\n return merged;\n}\n\nexport async function isXrModeSupported(\n mode = \"immersive-vr\",\n options = {}\n) {\n assertSessionMode(mode);\n\n const { navigator: navigatorOverride } = options;\n let xr;\n try {\n xr = readXrSystem(navigatorOverride);\n } catch {\n return false;\n }\n\n if (typeof xr.isSessionSupported !== \"function\") {\n return false;\n }\n\n try {\n return Boolean(await xr.isSessionSupported(mode));\n } catch {\n return false;\n }\n}\n\nexport async function requestXrSession(options = {}) {\n const {\n mode = \"immersive-vr\",\n sessionInit = {},\n baseSessionInit = DEFAULT_VR_SESSION_INIT,\n navigator: navigatorOverride,\n } = options;\n\n assertSessionMode(mode);\n\n const xr = readXrSystem(navigatorOverride);\n\n if (typeof xr.requestSession !== \"function\") {\n throw new Error(\"WebXR requestSession API unavailable.\");\n }\n\n const init = mergeXrSessionInit(baseSessionInit, sessionInit);\n return xr.requestSession(mode, init);\n}\n\nexport function createXrStore(initialState = {}) {\n const listeners = new Set();\n let state = {\n activeSession: null,\n mode: null,\n isEntering: false,\n lastError: null,\n supportedModes: {},\n ...initialState,\n };\n\n const notify = () => {\n for (const listener of listeners) {\n listener(state);\n }\n };\n\n return {\n getSnapshot() {\n return state;\n },\n subscribe(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n set(partialState) {\n state = {\n ...state,\n ...partialState,\n };\n notify();\n },\n reset() {\n state = {\n activeSession: null,\n mode: null,\n isEntering: false,\n lastError: null,\n supportedModes: {},\n ...initialState,\n };\n notify();\n },\n };\n}\n\nexport function createXrManager(options = {}) {\n const {\n navigator: navigatorOverride,\n defaultMode = \"immersive-vr\",\n baseSessionInit = DEFAULT_VR_SESSION_INIT,\n onSessionStart,\n onSessionEnd,\n } = options;\n\n assertSessionMode(defaultMode);\n\n const store = createXrStore();\n let activeSessionEndHandler = null;\n\n const detachSessionEndHandler = () => {\n const { activeSession } = store.getSnapshot();\n if (\n activeSession &&\n activeSessionEndHandler &&\n typeof activeSession.removeEventListener === \"function\"\n ) {\n activeSession.removeEventListener(\"end\", activeSessionEndHandler);\n }\n activeSessionEndHandler = null;\n };\n\n const handleSessionEnded = () => {\n detachSessionEndHandler();\n store.set({\n activeSession: null,\n mode: null,\n isEntering: false,\n });\n if (typeof onSessionEnd === \"function\") {\n onSessionEnd();\n }\n };\n\n const attachSessionEndHandler = (session) => {\n if (!session || typeof session.addEventListener !== \"function\") {\n return;\n }\n activeSessionEndHandler = handleSessionEnded;\n session.addEventListener(\"end\", activeSessionEndHandler);\n };\n\n const getState = () => store.getSnapshot();\n\n const subscribe = (listener) => store.subscribe(listener);\n\n const probeSupport = async (modes = [defaultMode]) => {\n const supportedModes = {};\n for (const mode of modes) {\n assertSessionMode(mode);\n supportedModes[mode] = await isXrModeSupported(mode, {\n navigator: navigatorOverride,\n });\n }\n store.set({ supportedModes });\n return supportedModes;\n };\n\n const enterSession = async (mode = defaultMode, sessionInit = {}) => {\n assertSessionMode(mode);\n\n const existing = store.getSnapshot().activeSession;\n if (existing) {\n return existing;\n }\n\n store.set({ isEntering: true, lastError: null });\n\n try {\n const session = await requestXrSession({\n mode,\n sessionInit,\n baseSessionInit,\n navigator: navigatorOverride,\n });\n\n attachSessionEndHandler(session);\n\n store.set({\n activeSession: session,\n mode,\n isEntering: false,\n lastError: null,\n });\n\n if (typeof onSessionStart === \"function\") {\n onSessionStart(session, mode);\n }\n\n return session;\n } catch (error) {\n const message =\n error instanceof Error ? error.message : String(error ?? \"Unknown XR error\");\n store.set({\n isEntering: false,\n lastError: message,\n });\n throw error;\n }\n };\n\n const enterVr = async (sessionInit = {}) => {\n return enterSession(\"immersive-vr\", sessionInit);\n };\n\n const exitSession = async () => {\n const { activeSession } = store.getSnapshot();\n if (!activeSession) {\n return false;\n }\n\n if (typeof activeSession.end === \"function\") {\n await activeSession.end();\n }\n\n // Fallback for test fakes or runtimes that do not emit an end event.\n if (store.getSnapshot().activeSession) {\n handleSessionEnded();\n }\n\n return true;\n };\n\n const dispose = async () => {\n await exitSession();\n detachSessionEndHandler();\n store.reset();\n };\n\n return {\n store,\n getState,\n subscribe,\n probeSupport,\n enterSession,\n enterVr,\n exitSession,\n dispose,\n };\n}\n\nexport const defaultVrSessionInit = DEFAULT_VR_SESSION_INIT;\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAM,0BAA0B,OAAO,OAAO;AAAA,EAC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