@air-jam/sdk 0.1.3 → 0.9.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/LICENSE +21 -0
- package/README.md +627 -57
- package/dist/agent-tooling-BSMGBix9.d.cts +123 -0
- package/dist/agent-tooling-BSMGBix9.d.ts +123 -0
- package/dist/agent-tooling.cjs +83 -0
- package/dist/agent-tooling.cjs.map +1 -0
- package/dist/agent-tooling.d.cts +2 -0
- package/dist/agent-tooling.d.ts +2 -0
- package/dist/agent-tooling.js +17 -0
- package/dist/agent-tooling.js.map +1 -0
- package/dist/arcade/bridge/controller.cjs +749 -0
- package/dist/arcade/bridge/controller.cjs.map +1 -0
- package/dist/arcade/bridge/controller.d.cts +85 -0
- package/dist/arcade/bridge/controller.d.ts +85 -0
- package/dist/arcade/bridge/controller.js +33 -0
- package/dist/arcade/bridge/controller.js.map +1 -0
- package/dist/arcade/bridge/host.cjs +421 -0
- package/dist/arcade/bridge/host.cjs.map +1 -0
- package/dist/arcade/bridge/host.d.cts +73 -0
- package/dist/arcade/bridge/host.d.ts +73 -0
- package/dist/arcade/bridge/host.js +25 -0
- package/dist/arcade/bridge/host.js.map +1 -0
- package/dist/arcade/bridge/iframe.cjs +453 -0
- package/dist/arcade/bridge/iframe.cjs.map +1 -0
- package/dist/arcade/bridge/iframe.d.cts +80 -0
- package/dist/arcade/bridge/iframe.d.ts +80 -0
- package/dist/arcade/bridge/iframe.js +28 -0
- package/dist/arcade/bridge/iframe.js.map +1 -0
- package/dist/arcade/bridge.cjs +1067 -0
- package/dist/arcade/bridge.cjs.map +1 -0
- package/dist/arcade/bridge.d.cts +11 -0
- package/dist/arcade/bridge.d.ts +11 -0
- package/dist/arcade/bridge.js +76 -0
- package/dist/arcade/bridge.js.map +1 -0
- package/dist/arcade/host.cjs +488 -0
- package/dist/arcade/host.cjs.map +1 -0
- package/dist/arcade/host.d.cts +19 -0
- package/dist/arcade/host.d.ts +19 -0
- package/dist/arcade/host.js +13 -0
- package/dist/arcade/host.js.map +1 -0
- package/dist/arcade/surface.cjs +113 -0
- package/dist/arcade/surface.cjs.map +1 -0
- package/dist/arcade/surface.d.cts +14 -0
- package/dist/arcade/surface.d.ts +14 -0
- package/dist/arcade/surface.js +27 -0
- package/dist/arcade/surface.js.map +1 -0
- package/dist/arcade/url.cjs +363 -0
- package/dist/arcade/url.cjs.map +1 -0
- package/dist/arcade/url.d.cts +68 -0
- package/dist/arcade/url.d.ts +68 -0
- package/dist/arcade/url.js +28 -0
- package/dist/arcade/url.js.map +1 -0
- package/dist/arcade-runtime-url-BW-MUAOY.d.cts +28 -0
- package/dist/arcade-runtime-url-l8qj01Pg.d.ts +28 -0
- package/dist/arcade-surface-identity-CQxDpn_w.d.cts +16 -0
- package/dist/arcade-surface-identity-CQxDpn_w.d.ts +16 -0
- package/dist/arcade.cjs +1565 -0
- package/dist/arcade.cjs.map +1 -0
- package/dist/arcade.d.cts +18 -0
- package/dist/arcade.d.ts +18 -0
- package/dist/arcade.js +122 -0
- package/dist/arcade.js.map +1 -0
- package/dist/capabilities.cjs +39 -0
- package/dist/capabilities.cjs.map +1 -0
- package/dist/capabilities.d.cts +31 -0
- package/dist/capabilities.d.ts +31 -0
- package/dist/capabilities.js +12 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/chunk-2432OL7L.js +193 -0
- package/dist/chunk-2432OL7L.js.map +1 -0
- package/dist/chunk-2J65FXEP.js +41 -0
- package/dist/chunk-2J65FXEP.js.map +1 -0
- package/dist/chunk-32NYL7XI.js +539 -0
- package/dist/chunk-32NYL7XI.js.map +1 -0
- package/dist/chunk-3USU7GY2.js +54 -0
- package/dist/chunk-3USU7GY2.js.map +1 -0
- package/dist/chunk-BE6Z4SIT.js +65 -0
- package/dist/chunk-BE6Z4SIT.js.map +1 -0
- package/dist/chunk-CQUG24WX.js +249 -0
- package/dist/chunk-CQUG24WX.js.map +1 -0
- package/dist/chunk-D3X4MN4U.js +288 -0
- package/dist/chunk-D3X4MN4U.js.map +1 -0
- package/dist/chunk-EVZJA6RN.js +140 -0
- package/dist/chunk-EVZJA6RN.js.map +1 -0
- package/dist/chunk-HFWRZVZ3.js +1438 -0
- package/dist/chunk-HFWRZVZ3.js.map +1 -0
- package/dist/chunk-IKKDONQL.js +1 -0
- package/dist/chunk-IKKDONQL.js.map +1 -0
- package/dist/chunk-JTF47B56.js +1 -0
- package/dist/chunk-JTF47B56.js.map +1 -0
- package/dist/chunk-LIXPF4P6.js +22 -0
- package/dist/chunk-LIXPF4P6.js.map +1 -0
- package/dist/chunk-MKBASO6X.js +216 -0
- package/dist/chunk-MKBASO6X.js.map +1 -0
- package/dist/chunk-MPDMJIX5.js +1 -0
- package/dist/chunk-MPDMJIX5.js.map +1 -0
- package/dist/chunk-NCL6WJCL.js +91 -0
- package/dist/chunk-NCL6WJCL.js.map +1 -0
- package/dist/chunk-NU5W6VAV.js +126 -0
- package/dist/chunk-NU5W6VAV.js.map +1 -0
- package/dist/chunk-TGRTEWYV.js +195 -0
- package/dist/chunk-TGRTEWYV.js.map +1 -0
- package/dist/chunk-VDRNNGHX.js +184 -0
- package/dist/chunk-VDRNNGHX.js.map +1 -0
- package/dist/chunk-VN5LDWVN.js +32 -0
- package/dist/chunk-VN5LDWVN.js.map +1 -0
- package/dist/chunk-W2VK36RO.js +11 -0
- package/dist/chunk-W2VK36RO.js.map +1 -0
- package/dist/chunk-X24K52DG.js +12 -0
- package/dist/chunk-X24K52DG.js.map +1 -0
- package/dist/chunk-XLUWV45V.js +25 -0
- package/dist/chunk-XLUWV45V.js.map +1 -0
- package/dist/chunk-Y4RAPKKB.js +238 -0
- package/dist/chunk-Y4RAPKKB.js.map +1 -0
- package/dist/chunk-YXHH3MQT.js +230 -0
- package/dist/chunk-YXHH3MQT.js.map +1 -0
- package/dist/chunk-ZLPL2R3R.js +183 -0
- package/dist/chunk-ZLPL2R3R.js.map +1 -0
- package/dist/chunk-ZTQ5SP4H.js +9 -0
- package/dist/chunk-ZTQ5SP4H.js.map +1 -0
- package/dist/controller-Bod8YYr1.d.cts +172 -0
- package/dist/controller-DxVchfiA.d.ts +172 -0
- package/dist/dev-log-events-CVAM29hx.d.cts +123 -0
- package/dist/dev-log-events-CVAM29hx.d.ts +123 -0
- package/dist/handshake-CC_LrK5g.d.cts +31 -0
- package/dist/handshake-CC_LrK5g.d.ts +31 -0
- package/dist/host-8hmq_Sgc.d.cts +109 -0
- package/dist/host-Cw-N6e-5.d.ts +109 -0
- package/dist/index.cjs +8512 -3518
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +676 -1070
- package/dist/index.d.ts +676 -1070
- package/dist/index.js +4892 -3188
- package/dist/index.js.map +1 -1
- package/dist/metadata.cjs +114 -0
- package/dist/metadata.cjs.map +1 -0
- package/dist/metadata.d.cts +95 -0
- package/dist/metadata.d.ts +95 -0
- package/dist/metadata.js +83 -0
- package/dist/metadata.js.map +1 -0
- package/dist/notices-CnGaaS8B.d.ts +38 -0
- package/dist/notices-NyDM5Mtz.d.cts +38 -0
- package/dist/platform-machine.cjs +425 -0
- package/dist/platform-machine.cjs.map +1 -0
- package/dist/platform-machine.d.cts +1183 -0
- package/dist/platform-machine.d.ts +1183 -0
- package/dist/platform-machine.js +309 -0
- package/dist/platform-machine.js.map +1 -0
- package/dist/platform-settings-cuor3i11.d.cts +48 -0
- package/dist/platform-settings-cuor3i11.d.ts +48 -0
- package/dist/prefabs.cjs +36 -0
- package/dist/prefabs.cjs.map +1 -0
- package/dist/prefabs.d.cts +49 -0
- package/dist/prefabs.d.ts +49 -0
- package/dist/prefabs.js +8 -0
- package/dist/prefabs.js.map +1 -0
- package/dist/preview.cjs +2609 -0
- package/dist/preview.cjs.map +1 -0
- package/dist/preview.d.cts +155 -0
- package/dist/preview.d.ts +155 -0
- package/dist/preview.js +1671 -0
- package/dist/preview.js.map +1 -0
- package/dist/protocol.cjs +621 -54
- package/dist/protocol.cjs.map +1 -1
- package/dist/protocol.d.cts +76 -240
- package/dist/protocol.d.ts +76 -240
- package/dist/protocol.js +100 -2
- package/dist/protocol.js.map +1 -1
- package/dist/realtime-client-Cu8yVJFw.d.cts +15 -0
- package/dist/realtime-client-Cu8yVJFw.d.ts +15 -0
- package/dist/release.cjs +143 -0
- package/dist/release.cjs.map +1 -0
- package/dist/release.d.cts +64 -0
- package/dist/release.d.ts +64 -0
- package/dist/release.js +43 -0
- package/dist/release.js.map +1 -0
- package/dist/standard-match-phase-BucA5dYo.d.ts +44 -0
- package/dist/standard-match-phase-U2ra8J-0.d.cts +44 -0
- package/dist/sync-B6QsNrXz.d.cts +125 -0
- package/dist/sync-B6QsNrXz.d.ts +125 -0
- package/dist/ui.cjs +2827 -0
- package/dist/ui.cjs.map +1 -0
- package/dist/ui.d.cts +320 -0
- package/dist/ui.d.ts +320 -0
- package/dist/ui.js +1718 -0
- package/dist/ui.js.map +1 -0
- package/dist/url-policy-C89zxgw9.d.cts +9 -0
- package/dist/url-policy-C89zxgw9.d.ts +9 -0
- package/package.json +154 -13
- package/src/agent/agent-action.ts +304 -0
- package/src/agent/agent-contract.ts +180 -0
- package/src/agent-tooling.ts +20 -0
- package/src/arcade/bridge/controller.ts +15 -0
- package/src/arcade/bridge/host.ts +12 -0
- package/src/arcade/bridge/iframe.ts +61 -0
- package/src/arcade/bridge.ts +3 -0
- package/src/arcade/host.ts +1 -0
- package/src/arcade/surface.ts +4 -0
- package/src/arcade/url.ts +60 -0
- package/src/arcade.ts +4 -0
- package/src/audio/audio-manager.ts +162 -171
- package/src/audio/hooks.tsx +218 -97
- package/src/audio/index.ts +1 -1
- package/src/audio/music.tsx +139 -0
- package/src/capabilities/manifest.ts +71 -0
- package/src/capabilities.ts +12 -0
- package/src/components/connection-status-pill.tsx +55 -0
- package/src/components/controller-player-name-field.tsx +119 -0
- package/src/components/controller-primary-action.tsx +58 -0
- package/src/components/host-mute-button.tsx +49 -0
- package/src/components/join-qr-overlay.tsx +133 -0
- package/src/components/join-url-action-buttons.tsx +95 -0
- package/src/components/join-url-controls.tsx +58 -0
- package/src/components/join-url-field.tsx +52 -0
- package/src/components/lifecycle-action-group.tsx +129 -0
- package/src/components/player-avatar-strip.tsx +65 -0
- package/src/components/player-avatar.tsx +42 -14
- package/src/components/room-qr-code.tsx +125 -0
- package/src/components/runtime-shell-header.tsx +37 -0
- package/src/components/shell-classes.ts +19 -0
- package/src/components/surface-orientation-layout.ts +60 -0
- package/src/components/surface-viewport-layout.ts +268 -0
- package/src/components/surface-viewport.tsx +314 -0
- package/src/components/ui/button.tsx +4 -6
- package/src/components/ui/slider.tsx +1 -1
- package/src/components/volume-controls.tsx +65 -6
- package/src/context/air-jam-context.tsx +209 -141
- package/src/context/index.ts +2 -2
- package/src/context/session-providers.tsx +77 -0
- package/src/context/session-scope.ts +64 -0
- package/src/context/socket-manager.ts +6 -25
- package/src/contracts/v2/bridge.ts +261 -0
- package/src/contracts/v2/handshake.ts +40 -0
- package/src/contracts/v2/index.ts +2 -0
- package/src/dev/browser-log-sink.ts +912 -0
- package/src/diagnostics.ts +128 -0
- package/src/events.ts +7 -34
- package/src/hooks/internal/use-controller-runtime-api.ts +875 -0
- package/src/hooks/internal/use-host-runtime-api.ts +1051 -0
- package/src/hooks/internal/use-runtime-control-contract.ts +15 -0
- package/src/hooks/internal/use-runtime-inspection-contract.ts +16 -0
- package/src/hooks/internal/use-runtime-observability-contract.ts +24 -0
- package/src/hooks/use-air-jam-controller.ts +155 -475
- package/src/hooks/use-air-jam-host.ts +156 -485
- package/src/hooks/use-connection-status.ts +8 -0
- package/src/hooks/use-controller-lifecycle-intents.ts +27 -0
- package/src/hooks/use-controller-lifecycle-permissions.ts +36 -0
- package/src/hooks/use-controller-shell-status.ts +40 -0
- package/src/hooks/use-controller-tick.ts +65 -0
- package/src/hooks/use-controller-toasts.ts +116 -0
- package/src/hooks/use-get-input.ts +8 -5
- package/src/hooks/use-host-arcade-restore.ts +30 -0
- package/src/hooks/use-host-join-controls.ts +152 -0
- package/src/hooks/use-host-tick.ts +171 -0
- package/src/hooks/use-input-writer.ts +83 -0
- package/src/hooks/use-lifecycle-action-group-model.ts +98 -0
- package/src/hooks/use-players.ts +12 -0
- package/src/hooks/use-room.ts +22 -0
- package/src/hooks/use-send-signal.ts +6 -2
- package/src/index.ts +205 -31
- package/src/internal/input-manager.ts +195 -215
- package/src/lifecycle/index.ts +9 -0
- package/src/lifecycle/standard-match-phase.ts +32 -0
- package/src/metadata/manifest.ts +152 -0
- package/src/metadata.ts +12 -0
- package/src/platform-machine.ts +475 -0
- package/src/prefabs/types.ts +82 -0
- package/src/prefabs.ts +12 -0
- package/src/preview/host-workspace.tsx +59 -0
- package/src/preview/identity.ts +66 -0
- package/src/preview/layout.ts +223 -0
- package/src/preview/manager.ts +577 -0
- package/src/preview/messages.ts +26 -0
- package/src/preview/ownership.ts +81 -0
- package/src/preview/url.ts +117 -0
- package/src/preview/window.tsx +490 -0
- package/src/preview/workspace.tsx +678 -0
- package/src/preview.ts +42 -0
- package/src/protocol/arcade-platform-actions.ts +29 -0
- package/src/protocol/controller.ts +187 -0
- package/src/protocol/core.ts +23 -0
- package/src/protocol/dev-log-events.ts +222 -0
- package/src/protocol/errors.ts +21 -0
- package/src/protocol/host-grant.ts +185 -0
- package/src/protocol/host.ts +138 -0
- package/src/protocol/notices.ts +43 -0
- package/src/protocol/signals.ts +38 -0
- package/src/protocol/socket-events.ts +154 -0
- package/src/protocol/sync.ts +121 -0
- package/src/protocol/url-policy.ts +63 -0
- package/src/protocol.ts +12 -386
- package/src/qrcode.d.ts +16 -0
- package/src/release.ts +99 -0
- package/src/runtime/air-jam-config.ts +287 -0
- package/src/runtime/air-jam-error-boundary.tsx +672 -0
- package/src/runtime/arcade-bridge-request-surface.ts +15 -0
- package/src/runtime/arcade-runtime-url.ts +95 -0
- package/src/runtime/arcade-surface-identity.ts +16 -0
- package/src/runtime/contracts/control.ts +68 -0
- package/src/runtime/contracts/inspection.ts +165 -0
- package/src/runtime/contracts/observability.ts +94 -0
- package/src/runtime/controller-bridge.ts +451 -0
- package/src/runtime/controller-identity.ts +132 -0
- package/src/runtime/controller-presentation.ts +24 -0
- package/src/runtime/controller-realtime-client.ts +591 -0
- package/src/runtime/create-air-jam-app.tsx +260 -0
- package/src/runtime/dev-runtime-events.ts +36 -0
- package/src/runtime/embedded-runtime-adapters.ts +68 -0
- package/src/runtime/host-bridge.ts +388 -0
- package/src/runtime/host-realtime-client.ts +550 -0
- package/src/runtime/iframe-bridge.ts +135 -0
- package/src/runtime/realtime-client.ts +20 -0
- package/src/runtime/router-basename.ts +25 -0
- package/src/runtime/runtime-owner-contexts.tsx +4 -0
- package/src/runtime/runtime-session-params.ts +169 -0
- package/src/runtime/sdk-version.ts +6 -0
- package/src/runtime/session-runtimes.tsx +188 -0
- package/src/runtime/validate-arcade-bridge-attach.ts +14 -0
- package/src/runtime-control.ts +10 -0
- package/src/runtime-inspection.ts +14 -0
- package/src/runtime-observability.ts +9 -0
- package/src/settings/platform-settings-bridge.ts +336 -0
- package/src/settings/platform-settings-runtime.tsx +387 -0
- package/src/settings/platform-settings.ts +288 -0
- package/src/state/connection-store.ts +80 -12
- package/src/store/air-jam-store-domain-constants.ts +4 -0
- package/src/store/create-air-jam-store.ts +1094 -227
- package/src/styles.css +4 -38
- package/src/ui.ts +29 -0
- package/src/utils/is-rpc-serializable.ts +56 -0
- package/src/utils/player-avatar-url.ts +48 -0
- package/src/utils/url-builder.ts +7 -0
- package/dist/.tsbuildinfo +0 -1
- package/dist/chunk-B5DURUWT.js +0 -80
- package/dist/chunk-B5DURUWT.js.map +0 -1
- package/src/AirJamClient.ts +0 -224
- package/src/audio/volume-store.ts +0 -124
- package/src/components/controller-shell.tsx +0 -346
- package/src/components/host-shell.tsx +0 -521
- package/src/components/qr-scanner-dialog.tsx +0 -373
- package/src/components/ui/alert.tsx +0 -66
- package/src/components/ui/avatar.tsx +0 -46
- package/src/components/ui/badge.tsx +0 -47
- package/src/components/ui/card.tsx +0 -92
- package/src/components/ui/dialog.tsx +0 -122
- package/src/components/ui/tabs.tsx +0 -64
- package/src/hooks/use-air-jam-shell.ts +0 -379
- package/src/hooks/use-fullscreen.ts +0 -108
|
@@ -1,29 +1,28 @@
|
|
|
1
|
-
/* eslint-disable react-refresh/only-export-components */
|
|
2
1
|
/**
|
|
3
2
|
* @module AirJamContext
|
|
4
3
|
* @description Core context and provider for the AirJam SDK.
|
|
5
4
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Scoped session providers build on top of this internal provider to enable
|
|
6
|
+
* AirJam functionality. It manages:
|
|
8
7
|
* - WebSocket connections to the AirJam server
|
|
9
8
|
* - Global state via Zustand store
|
|
10
|
-
* - Input validation and
|
|
11
|
-
* - Environment variable resolution for server URLs and
|
|
9
|
+
* - Input validation and behavior configuration
|
|
10
|
+
* - Environment variable resolution for server URLs and app IDs
|
|
12
11
|
*
|
|
13
12
|
* @example Basic Setup
|
|
14
13
|
* ```tsx
|
|
15
|
-
* import {
|
|
14
|
+
* import { HostSessionProvider } from "@air-jam/sdk";
|
|
16
15
|
*
|
|
17
16
|
* const App = () => (
|
|
18
|
-
* <
|
|
17
|
+
* <HostSessionProvider>
|
|
19
18
|
* <YourGame />
|
|
20
|
-
* </
|
|
19
|
+
* </HostSessionProvider>
|
|
21
20
|
* );
|
|
22
21
|
* ```
|
|
23
22
|
*
|
|
24
23
|
* @example With Input Configuration
|
|
25
24
|
* ```tsx
|
|
26
|
-
* import {
|
|
25
|
+
* import { HostSessionProvider } from "@air-jam/sdk";
|
|
27
26
|
* import { z } from "zod";
|
|
28
27
|
*
|
|
29
28
|
* const inputSchema = z.object({
|
|
@@ -32,17 +31,23 @@
|
|
|
32
31
|
* });
|
|
33
32
|
*
|
|
34
33
|
* const App = () => (
|
|
35
|
-
* <
|
|
34
|
+
* <HostSessionProvider
|
|
36
35
|
* input={{
|
|
37
36
|
* schema: inputSchema,
|
|
38
|
-
*
|
|
37
|
+
* behavior: { pulse: ["action"], latest: ["vector"] },
|
|
39
38
|
* }}
|
|
40
39
|
* >
|
|
41
40
|
* <YourGame />
|
|
42
|
-
* </
|
|
41
|
+
* </HostSessionProvider>
|
|
43
42
|
* );
|
|
44
43
|
* ```
|
|
45
44
|
*/
|
|
45
|
+
import type {
|
|
46
|
+
ResolvedAirJamRuntimeTopology,
|
|
47
|
+
RuntimeTopologyInput,
|
|
48
|
+
SurfaceRole,
|
|
49
|
+
} from "@air-jam/runtime-topology";
|
|
50
|
+
import { isLocalDevControlSurfaceTopology } from "@air-jam/runtime-topology";
|
|
46
51
|
import {
|
|
47
52
|
createContext,
|
|
48
53
|
useCallback,
|
|
@@ -54,9 +59,18 @@ import {
|
|
|
54
59
|
import type { z } from "zod";
|
|
55
60
|
import { useStore } from "zustand";
|
|
56
61
|
import { useShallow } from "zustand/react/shallow";
|
|
57
|
-
import {
|
|
62
|
+
import { ensureDevBrowserLogSink } from "../dev/browser-log-sink";
|
|
63
|
+
import { createAirJamDiagnosticError } from "../diagnostics";
|
|
58
64
|
import { InputManager, type InputConfig } from "../internal/input-manager";
|
|
59
65
|
import type { ConnectionRole } from "../protocol";
|
|
66
|
+
import { AIRJAM_DEV_LOG_EVENTS } from "../protocol";
|
|
67
|
+
import type { HostSessionKind } from "../protocol/host";
|
|
68
|
+
import type { AirJamConfig } from "../runtime/air-jam-config";
|
|
69
|
+
import { resolveAirJamConfig } from "../runtime/air-jam-config";
|
|
70
|
+
import {
|
|
71
|
+
AIRJAM_DEV_PROVIDER_MOUNTED,
|
|
72
|
+
emitAirJamDevRuntimeEvent,
|
|
73
|
+
} from "../runtime/dev-runtime-events";
|
|
60
74
|
import { createAirJamStore, type AirJamStore } from "../state/connection-store";
|
|
61
75
|
import { SocketManager, type AirJamSocket } from "./socket-manager";
|
|
62
76
|
|
|
@@ -64,21 +78,6 @@ import { SocketManager, type AirJamSocket } from "./socket-manager";
|
|
|
64
78
|
// Configuration Types
|
|
65
79
|
// ============================================================================
|
|
66
80
|
|
|
67
|
-
/**
|
|
68
|
-
* Resolved configuration for the AirJam SDK.
|
|
69
|
-
* Created from AirJamProviderProps with environment variable fallbacks applied.
|
|
70
|
-
*/
|
|
71
|
-
export interface AirJamConfig {
|
|
72
|
-
/** WebSocket server URL. Falls back to env vars or window location */
|
|
73
|
-
serverUrl?: string;
|
|
74
|
-
/** API key for production authentication */
|
|
75
|
-
apiKey?: string;
|
|
76
|
-
/** Maximum players allowed in a room */
|
|
77
|
-
maxPlayers: number;
|
|
78
|
-
/** Public host for controller URLs (optional) */
|
|
79
|
-
publicHost?: string;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
81
|
/**
|
|
83
82
|
* Props for the AirJamProvider component.
|
|
84
83
|
*
|
|
@@ -86,24 +85,25 @@ export interface AirJamConfig {
|
|
|
86
85
|
*
|
|
87
86
|
* @example Minimal configuration (uses environment variables)
|
|
88
87
|
* ```tsx
|
|
89
|
-
* <
|
|
88
|
+
* <HostSessionProvider>
|
|
90
89
|
* <App />
|
|
91
|
-
* </
|
|
90
|
+
* </HostSessionProvider>
|
|
92
91
|
* ```
|
|
93
92
|
*
|
|
94
93
|
* @example Full configuration
|
|
95
94
|
* ```tsx
|
|
96
|
-
* <
|
|
97
|
-
*
|
|
98
|
-
*
|
|
95
|
+
* <HostSessionProvider
|
|
96
|
+
* topology={myTopology}
|
|
97
|
+
* appId="your-app-id"
|
|
98
|
+
* hostGrantEndpoint="/api/airjam/host-grant"
|
|
99
99
|
* maxPlayers={4}
|
|
100
100
|
* input={{
|
|
101
101
|
* schema: myInputSchema,
|
|
102
|
-
*
|
|
102
|
+
* behavior: { pulse: ["fire"], latest: ["move"] },
|
|
103
103
|
* }}
|
|
104
104
|
* >
|
|
105
105
|
* <App />
|
|
106
|
-
* </
|
|
106
|
+
* </HostSessionProvider>
|
|
107
107
|
* ```
|
|
108
108
|
*/
|
|
109
109
|
export interface AirJamProviderProps<
|
|
@@ -111,29 +111,43 @@ export interface AirJamProviderProps<
|
|
|
111
111
|
> {
|
|
112
112
|
/** React children to render within the provider */
|
|
113
113
|
children: ReactNode;
|
|
114
|
+
/** Explicit runtime topology for this surface. Prefer this over loose endpoint props. */
|
|
115
|
+
topology?: RuntimeTopologyInput | ResolvedAirJamRuntimeTopology;
|
|
116
|
+
/** Internal surface-role hint when normalizing legacy endpoint props into topology. */
|
|
117
|
+
surfaceRole?: SurfaceRole;
|
|
114
118
|
/**
|
|
115
|
-
*
|
|
116
|
-
* Falls back to
|
|
119
|
+
* App ID for production bootstrap identity.
|
|
120
|
+
* Falls back to VITE_AIR_JAM_APP_ID or NEXT_PUBLIC_AIR_JAM_APP_ID
|
|
121
|
+
* when not provided directly.
|
|
117
122
|
*/
|
|
118
|
-
|
|
123
|
+
appId?: string;
|
|
119
124
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
125
|
+
* Optional endpoint that returns a short-lived signed host grant for stronger production bootstrap.
|
|
126
|
+
* When provided, the host runtime fetches a grant automatically before `host:bootstrap`.
|
|
122
127
|
*/
|
|
123
|
-
|
|
128
|
+
hostGrantEndpoint?: string;
|
|
124
129
|
/**
|
|
125
130
|
* Maximum number of players allowed in a room.
|
|
126
131
|
* @default 8
|
|
127
132
|
*/
|
|
128
133
|
maxPlayers?: number;
|
|
129
134
|
/**
|
|
130
|
-
*
|
|
131
|
-
*
|
|
135
|
+
* Declares whether this host provider owns a standalone game room or a system shell room.
|
|
136
|
+
* Standalone games should use `"game"`; Arcade shell hosts should use `"system"`.
|
|
137
|
+
* @default "game"
|
|
132
138
|
*/
|
|
133
|
-
|
|
139
|
+
hostSessionKind?: HostSessionKind;
|
|
134
140
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
141
|
+
* Enable environment-variable fallback resolution for config fields.
|
|
142
|
+
* Set to false to require explicit `topology` / `appId` / `hostGrantEndpoint`.
|
|
143
|
+
* @default true
|
|
144
|
+
*/
|
|
145
|
+
resolveEnv?: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* Input handling configuration including Zod schema validation and field behavior.
|
|
148
|
+
* When provided, enables typed input with tap-safe defaults:
|
|
149
|
+
* - booleans => `pulse`
|
|
150
|
+
* - vectors => `latest`
|
|
137
151
|
*
|
|
138
152
|
* @example
|
|
139
153
|
* ```ts
|
|
@@ -143,9 +157,9 @@ export interface AirJamProviderProps<
|
|
|
143
157
|
* action: z.boolean(),
|
|
144
158
|
* ability: z.boolean(),
|
|
145
159
|
* }),
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
160
|
+
* behavior: {
|
|
161
|
+
* pulse: ["action", "ability"], // consume-on-read actions
|
|
162
|
+
* latest: ["vector"], // continuous movement
|
|
149
163
|
* },
|
|
150
164
|
* }}
|
|
151
165
|
* ```
|
|
@@ -174,68 +188,10 @@ export interface AirJamContextValue {
|
|
|
174
188
|
|
|
175
189
|
const AirJamContext = createContext<AirJamContextValue | null>(null);
|
|
176
190
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
// ============================================================================
|
|
180
|
-
|
|
181
|
-
// Type-safe access to import.meta.env (Vite)
|
|
182
|
-
interface ImportMetaEnv {
|
|
183
|
-
VITE_AIR_JAM_SERVER_URL?: string;
|
|
184
|
-
VITE_AIR_JAM_API_KEY?: string;
|
|
191
|
+
interface DevProviderMountWindow extends Window {
|
|
192
|
+
__airJamDevProviderMountSent__?: boolean;
|
|
185
193
|
}
|
|
186
194
|
|
|
187
|
-
const getViteEnv = (): ImportMetaEnv | undefined => {
|
|
188
|
-
try {
|
|
189
|
-
// Access import.meta.env dynamically to avoid TS errors
|
|
190
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
191
|
-
const meta = import.meta as any;
|
|
192
|
-
if (meta && typeof meta.env === "object") {
|
|
193
|
-
return meta.env as ImportMetaEnv;
|
|
194
|
-
}
|
|
195
|
-
} catch {
|
|
196
|
-
// import.meta not available
|
|
197
|
-
}
|
|
198
|
-
return undefined;
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Attempts to read server URL from various environment variable formats
|
|
203
|
-
*/
|
|
204
|
-
const getEnvServerUrl = (): string | undefined => {
|
|
205
|
-
// Vite
|
|
206
|
-
const viteEnv = getViteEnv();
|
|
207
|
-
if (viteEnv?.VITE_AIR_JAM_SERVER_URL) {
|
|
208
|
-
return viteEnv.VITE_AIR_JAM_SERVER_URL;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// Next.js / Node
|
|
212
|
-
if (typeof process !== "undefined" && process.env) {
|
|
213
|
-
const nextUrl = process.env.NEXT_PUBLIC_AIR_JAM_SERVER_URL;
|
|
214
|
-
if (nextUrl) return nextUrl;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return undefined;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Attempts to read API key from various environment variable formats
|
|
222
|
-
*/
|
|
223
|
-
const getEnvApiKey = (): string | undefined => {
|
|
224
|
-
// Vite
|
|
225
|
-
const viteEnv = getViteEnv();
|
|
226
|
-
if (viteEnv?.VITE_AIR_JAM_API_KEY) {
|
|
227
|
-
return viteEnv.VITE_AIR_JAM_API_KEY;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Next.js / Node
|
|
231
|
-
if (typeof process !== "undefined" && process.env) {
|
|
232
|
-
const nextKey = process.env.NEXT_PUBLIC_AIR_JAM_API_KEY;
|
|
233
|
-
if (nextKey) return nextKey;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
return undefined;
|
|
237
|
-
};
|
|
238
|
-
|
|
239
195
|
// ============================================================================
|
|
240
196
|
// Provider Component
|
|
241
197
|
// ============================================================================
|
|
@@ -250,28 +206,28 @@ const getEnvApiKey = (): string | undefined => {
|
|
|
250
206
|
* **Key responsibilities:**
|
|
251
207
|
* - Creates and manages WebSocket connections to the AirJam server
|
|
252
208
|
* - Provides a Zustand store for connection state (players, game state, etc.)
|
|
253
|
-
* - Creates an InputManager for typed input
|
|
209
|
+
* - Creates an InputManager for typed input behavior processing
|
|
254
210
|
* - Resolves configuration from props and environment variables
|
|
255
211
|
*
|
|
256
212
|
* @template TSchema - Zod schema for input validation (inferred from props)
|
|
257
213
|
*
|
|
258
214
|
* @example Basic usage
|
|
259
215
|
* ```tsx
|
|
260
|
-
* import {
|
|
216
|
+
* import { HostSessionProvider } from "@air-jam/sdk";
|
|
261
217
|
*
|
|
262
218
|
* export const App = () => (
|
|
263
|
-
* <
|
|
219
|
+
* <HostSessionProvider>
|
|
264
220
|
* <Routes>
|
|
265
221
|
* <Route path="/" element={<HostView />} />
|
|
266
222
|
* <Route path="/controller" element={<ControllerView />} />
|
|
267
223
|
* </Routes>
|
|
268
|
-
* </
|
|
224
|
+
* </HostSessionProvider>
|
|
269
225
|
* );
|
|
270
226
|
* ```
|
|
271
227
|
*
|
|
272
|
-
* @example With typed input
|
|
228
|
+
* @example With typed input behavior overrides
|
|
273
229
|
* ```tsx
|
|
274
|
-
* import {
|
|
230
|
+
* import { HostSessionProvider } from "@air-jam/sdk";
|
|
275
231
|
* import { z } from "zod";
|
|
276
232
|
*
|
|
277
233
|
* const gameInputSchema = z.object({
|
|
@@ -282,37 +238,56 @@ const getEnvApiKey = (): string | undefined => {
|
|
|
282
238
|
* });
|
|
283
239
|
*
|
|
284
240
|
* export const App = () => (
|
|
285
|
-
* <
|
|
241
|
+
* <HostSessionProvider
|
|
286
242
|
* input={{
|
|
287
243
|
* schema: gameInputSchema,
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
244
|
+
* behavior: {
|
|
245
|
+
* pulse: ["action", "ability"],
|
|
246
|
+
* latest: ["vector"],
|
|
291
247
|
* },
|
|
292
248
|
* }}
|
|
293
249
|
* >
|
|
294
250
|
* <GameRoutes />
|
|
295
|
-
* </
|
|
251
|
+
* </HostSessionProvider>
|
|
296
252
|
* );
|
|
297
253
|
* ```
|
|
298
254
|
*/
|
|
299
255
|
export const AirJamProvider = <TSchema extends z.ZodSchema = z.ZodSchema>({
|
|
300
256
|
children,
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
257
|
+
topology,
|
|
258
|
+
surfaceRole,
|
|
259
|
+
appId,
|
|
260
|
+
hostGrantEndpoint,
|
|
261
|
+
maxPlayers,
|
|
262
|
+
hostSessionKind,
|
|
263
|
+
resolveEnv = true,
|
|
305
264
|
input,
|
|
306
265
|
}: AirJamProviderProps<TSchema>) => {
|
|
307
|
-
// Resolve config
|
|
266
|
+
// Resolve config via runtime config resolver.
|
|
308
267
|
const config = useMemo<AirJamConfig>(
|
|
309
|
-
() =>
|
|
310
|
-
|
|
311
|
-
|
|
268
|
+
() =>
|
|
269
|
+
resolveAirJamConfig({
|
|
270
|
+
topology,
|
|
271
|
+
surfaceRole,
|
|
272
|
+
appId,
|
|
273
|
+
hostGrantEndpoint,
|
|
274
|
+
maxPlayers,
|
|
275
|
+
hostSessionKind,
|
|
276
|
+
resolveEnv,
|
|
277
|
+
}),
|
|
278
|
+
[
|
|
279
|
+
topology,
|
|
280
|
+
surfaceRole,
|
|
281
|
+
appId,
|
|
282
|
+
hostGrantEndpoint,
|
|
312
283
|
maxPlayers,
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
284
|
+
hostSessionKind,
|
|
285
|
+
resolveEnv,
|
|
286
|
+
],
|
|
287
|
+
);
|
|
288
|
+
const localDevControlSurfacesEnabled = useMemo(
|
|
289
|
+
() => isLocalDevControlSurfaceTopology(config.topology),
|
|
290
|
+
[config.topology],
|
|
316
291
|
);
|
|
317
292
|
|
|
318
293
|
// Create InputManager from input config if provided
|
|
@@ -328,18 +303,110 @@ export const AirJamProvider = <TSchema extends z.ZodSchema = z.ZodSchema>({
|
|
|
328
303
|
|
|
329
304
|
// Create socket manager (once per provider)
|
|
330
305
|
const socketManager = useMemo(
|
|
331
|
-
() => new SocketManager(config.
|
|
332
|
-
// Only recreate if
|
|
333
|
-
|
|
334
|
-
[config.serverUrl],
|
|
306
|
+
() => new SocketManager(config.socketOrigin),
|
|
307
|
+
// Only recreate if socketOrigin changes - intentionally omitting config
|
|
308
|
+
[config.socketOrigin],
|
|
335
309
|
);
|
|
336
310
|
|
|
337
311
|
// Cleanup on unmount
|
|
338
312
|
useEffect(() => {
|
|
339
313
|
return () => {
|
|
314
|
+
const devWindow =
|
|
315
|
+
typeof window !== "undefined"
|
|
316
|
+
? (window as unknown as DevProviderMountWindow)
|
|
317
|
+
: undefined;
|
|
318
|
+
if (devWindow && localDevControlSurfacesEnabled) {
|
|
319
|
+
try {
|
|
320
|
+
emitAirJamDevRuntimeEvent({
|
|
321
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.providerUnmounted,
|
|
322
|
+
level: "info",
|
|
323
|
+
message: "Air Jam provider unmounted",
|
|
324
|
+
data: {
|
|
325
|
+
appId: config.appId,
|
|
326
|
+
backendOrigin: config.backendOrigin,
|
|
327
|
+
socketOrigin: config.socketOrigin,
|
|
328
|
+
href: devWindow.location.href,
|
|
329
|
+
origin: devWindow.location.origin,
|
|
330
|
+
pathname: devWindow.location.pathname,
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
} catch {
|
|
334
|
+
// Best effort only
|
|
335
|
+
}
|
|
336
|
+
}
|
|
340
337
|
socketManager.disconnectAll();
|
|
341
338
|
};
|
|
342
|
-
}, [
|
|
339
|
+
}, [
|
|
340
|
+
config.appId,
|
|
341
|
+
config.backendOrigin,
|
|
342
|
+
config.socketOrigin,
|
|
343
|
+
localDevControlSurfacesEnabled,
|
|
344
|
+
socketManager,
|
|
345
|
+
]);
|
|
346
|
+
|
|
347
|
+
useEffect(() => {
|
|
348
|
+
ensureDevBrowserLogSink({
|
|
349
|
+
appOrigin: config.appOrigin,
|
|
350
|
+
backendOrigin: config.backendOrigin,
|
|
351
|
+
proxyStrategy: config.proxyStrategy,
|
|
352
|
+
appId: config.appId,
|
|
353
|
+
topology: config.topology,
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
const devWindow =
|
|
357
|
+
typeof window !== "undefined"
|
|
358
|
+
? (window as unknown as DevProviderMountWindow)
|
|
359
|
+
: undefined;
|
|
360
|
+
|
|
361
|
+
if (
|
|
362
|
+
devWindow &&
|
|
363
|
+
localDevControlSurfacesEnabled &&
|
|
364
|
+
!devWindow.__airJamDevProviderMountSent__
|
|
365
|
+
) {
|
|
366
|
+
try {
|
|
367
|
+
devWindow.__airJamDevProviderMountSent__ = true;
|
|
368
|
+
emitAirJamDevRuntimeEvent({
|
|
369
|
+
event: AIRJAM_DEV_LOG_EVENTS.runtime.providerMounted,
|
|
370
|
+
level: "info",
|
|
371
|
+
message: "Air Jam provider mounted",
|
|
372
|
+
data: {
|
|
373
|
+
appId: config.appId,
|
|
374
|
+
backendOrigin: config.backendOrigin,
|
|
375
|
+
socketOrigin: config.socketOrigin,
|
|
376
|
+
href: devWindow.location.href,
|
|
377
|
+
origin: devWindow.location.origin,
|
|
378
|
+
pathname: devWindow.location.pathname,
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
if (devWindow.parent && devWindow.parent !== devWindow) {
|
|
382
|
+
devWindow.parent.postMessage(
|
|
383
|
+
{
|
|
384
|
+
type: AIRJAM_DEV_PROVIDER_MOUNTED,
|
|
385
|
+
payload: {
|
|
386
|
+
appId: config.appId,
|
|
387
|
+
backendOrigin: config.backendOrigin,
|
|
388
|
+
socketOrigin: config.socketOrigin,
|
|
389
|
+
href: devWindow.location.href,
|
|
390
|
+
origin: devWindow.location.origin,
|
|
391
|
+
pathname: devWindow.location.pathname,
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
"*",
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
} catch {
|
|
398
|
+
// Best effort only
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}, [
|
|
402
|
+
config.appId,
|
|
403
|
+
config.appOrigin,
|
|
404
|
+
config.backendOrigin,
|
|
405
|
+
config.proxyStrategy,
|
|
406
|
+
config.socketOrigin,
|
|
407
|
+
config.topology,
|
|
408
|
+
localDevControlSurfacesEnabled,
|
|
409
|
+
]);
|
|
343
410
|
|
|
344
411
|
// Stable function references - don't recreate when config changes
|
|
345
412
|
const getSocket = useCallback(
|
|
@@ -395,9 +462,9 @@ export const AirJamProvider = <TSchema extends z.ZodSchema = z.ZodSchema>({
|
|
|
395
462
|
export const useAirJamContext = (): AirJamContextValue => {
|
|
396
463
|
const context = useContext(AirJamContext);
|
|
397
464
|
if (!context) {
|
|
398
|
-
throw
|
|
399
|
-
"
|
|
400
|
-
|
|
465
|
+
throw createAirJamDiagnosticError(
|
|
466
|
+
"AJ_MISSING_SESSION_PROVIDER",
|
|
467
|
+
"useAirJamContext must be used within a session provider. Wrap your app or component tree with <HostSessionProvider> or <ControllerSessionProvider>.",
|
|
401
468
|
);
|
|
402
469
|
}
|
|
403
470
|
return context;
|
|
@@ -429,9 +496,9 @@ export const useAirJamConfig = (): AirJamConfig => {
|
|
|
429
496
|
*
|
|
430
497
|
* @example
|
|
431
498
|
* ```tsx
|
|
432
|
-
* const { players,
|
|
499
|
+
* const { players, runtimeState } = useAirJamState((state) => ({
|
|
433
500
|
* players: state.players,
|
|
434
|
-
*
|
|
501
|
+
* runtimeState: state.runtimeState,
|
|
435
502
|
* }));
|
|
436
503
|
* ```
|
|
437
504
|
*/
|
|
@@ -464,4 +531,5 @@ export const useAirJamSocket = (role: ConnectionRole): AirJamSocket => {
|
|
|
464
531
|
// Exports
|
|
465
532
|
// ============================================================================
|
|
466
533
|
|
|
534
|
+
export type { AirJamConfig } from "../runtime/air-jam-config";
|
|
467
535
|
export { AirJamContext };
|
package/src/context/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
1
|
+
export * from "./session-providers";
|
|
2
|
+
export * from "./session-scope";
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useCallback, useRef, type JSX } from "react";
|
|
2
|
+
import type { z } from "zod";
|
|
3
|
+
import { createAirJamDiagnosticError } from "../diagnostics";
|
|
4
|
+
import { AirJamProvider, type AirJamProviderProps } from "./air-jam-context";
|
|
5
|
+
import {
|
|
6
|
+
RuntimeOwnerRegistryContext,
|
|
7
|
+
SessionScopeContext,
|
|
8
|
+
type RuntimeOwnerKind,
|
|
9
|
+
type SessionScope,
|
|
10
|
+
} from "./session-scope";
|
|
11
|
+
|
|
12
|
+
type RuntimeOwnerToken = symbol;
|
|
13
|
+
|
|
14
|
+
const createScopedSessionProvider = (
|
|
15
|
+
scope: Exclude<SessionScope, "unscoped">,
|
|
16
|
+
): (<TSchema extends z.ZodSchema = z.ZodSchema>(
|
|
17
|
+
props: AirJamProviderProps<TSchema>,
|
|
18
|
+
) => JSX.Element) => {
|
|
19
|
+
const ScopedSessionProvider = <TSchema extends z.ZodSchema = z.ZodSchema>({
|
|
20
|
+
children,
|
|
21
|
+
...providerProps
|
|
22
|
+
}: AirJamProviderProps<TSchema>): JSX.Element => {
|
|
23
|
+
const runtimeOwnersRef = useRef<
|
|
24
|
+
Map<
|
|
25
|
+
RuntimeOwnerKind,
|
|
26
|
+
{
|
|
27
|
+
token: RuntimeOwnerToken;
|
|
28
|
+
hookName: string;
|
|
29
|
+
}
|
|
30
|
+
>
|
|
31
|
+
>(new Map());
|
|
32
|
+
|
|
33
|
+
const claimOwner = useCallback(
|
|
34
|
+
(kind: RuntimeOwnerKind, token: RuntimeOwnerToken, hookName: string) => {
|
|
35
|
+
const existing = runtimeOwnersRef.current.get(kind);
|
|
36
|
+
if (existing && existing.token !== token) {
|
|
37
|
+
throw createAirJamDiagnosticError(
|
|
38
|
+
"AJ_DUPLICATE_SESSION_OWNER",
|
|
39
|
+
`${hookName} mounted while another ${kind} already owns this session provider. Mount one runtime owner hook per provider tree and use useAirJamHost() or useAirJamController() in child components instead.`,
|
|
40
|
+
{
|
|
41
|
+
scope,
|
|
42
|
+
kind,
|
|
43
|
+
hookName,
|
|
44
|
+
existingHookName: existing.hookName,
|
|
45
|
+
},
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
runtimeOwnersRef.current.set(kind, { token, hookName });
|
|
50
|
+
return () => {
|
|
51
|
+
const current = runtimeOwnersRef.current.get(kind);
|
|
52
|
+
if (current?.token === token) {
|
|
53
|
+
runtimeOwnersRef.current.delete(kind);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
[],
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<SessionScopeContext.Provider value={scope}>
|
|
62
|
+
<RuntimeOwnerRegistryContext.Provider value={{ claimOwner }}>
|
|
63
|
+
<AirJamProvider<TSchema> {...providerProps} surfaceRole={scope}>
|
|
64
|
+
{children}
|
|
65
|
+
</AirJamProvider>
|
|
66
|
+
</RuntimeOwnerRegistryContext.Provider>
|
|
67
|
+
</SessionScopeContext.Provider>
|
|
68
|
+
);
|
|
69
|
+
};
|
|
70
|
+
return ScopedSessionProvider;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const HostSessionProvider = createScopedSessionProvider("host");
|
|
74
|
+
export const ControllerSessionProvider =
|
|
75
|
+
createScopedSessionProvider("controller");
|
|
76
|
+
|
|
77
|
+
export type { AirJamProviderProps };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createContext, useContext, useEffect, useRef } from "react";
|
|
2
|
+
import { createAirJamDiagnosticError } from "../diagnostics";
|
|
3
|
+
|
|
4
|
+
export type SessionScope = "unscoped" | "host" | "controller";
|
|
5
|
+
export type RuntimeOwnerKind = "host-runtime" | "controller-runtime";
|
|
6
|
+
type RuntimeOwnerToken = symbol;
|
|
7
|
+
|
|
8
|
+
export interface RuntimeOwnerRegistryValue {
|
|
9
|
+
claimOwner: (
|
|
10
|
+
kind: RuntimeOwnerKind,
|
|
11
|
+
token: RuntimeOwnerToken,
|
|
12
|
+
hookName: string,
|
|
13
|
+
) => () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const SessionScopeContext = createContext<SessionScope>("unscoped");
|
|
17
|
+
export const RuntimeOwnerRegistryContext =
|
|
18
|
+
createContext<RuntimeOwnerRegistryValue | null>(null);
|
|
19
|
+
|
|
20
|
+
export const useSessionScope = (): SessionScope => {
|
|
21
|
+
return useContext(SessionScopeContext);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const useAssertSessionScope = (
|
|
25
|
+
expectedScope: Exclude<SessionScope, "unscoped">,
|
|
26
|
+
hookName: string,
|
|
27
|
+
): void => {
|
|
28
|
+
const scope = useSessionScope();
|
|
29
|
+
if (scope === expectedScope) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const expectedProviderName =
|
|
34
|
+
expectedScope === "host"
|
|
35
|
+
? "HostSessionProvider"
|
|
36
|
+
: "ControllerSessionProvider";
|
|
37
|
+
throw createAirJamDiagnosticError(
|
|
38
|
+
"AJ_SCOPE_MISMATCH",
|
|
39
|
+
`${hookName} requires ${expectedProviderName} when using scoped providers. Received scope "${scope}".`,
|
|
40
|
+
{
|
|
41
|
+
hookName,
|
|
42
|
+
expectedScope,
|
|
43
|
+
receivedScope: scope,
|
|
44
|
+
},
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const useClaimSessionRuntimeOwner = (
|
|
49
|
+
kind: RuntimeOwnerKind,
|
|
50
|
+
hookName: string,
|
|
51
|
+
): void => {
|
|
52
|
+
const registry = useContext(RuntimeOwnerRegistryContext);
|
|
53
|
+
const tokenRef = useRef<RuntimeOwnerToken | null>(null);
|
|
54
|
+
if (!tokenRef.current) {
|
|
55
|
+
tokenRef.current = Symbol(`${kind}:${hookName}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (!registry) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
return registry.claimOwner(kind, tokenRef.current!, hookName);
|
|
63
|
+
}, [registry, kind, hookName]);
|
|
64
|
+
};
|