@air-jam/sdk 0.9.2 → 1.0.0-beta.1

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.
Files changed (590) hide show
  1. package/README.md +185 -242
  2. package/dist/actions-D4eh6Xaa.d.cts +72 -0
  3. package/dist/actions-D4eh6Xaa.d.ts +72 -0
  4. package/dist/{agent-tooling-BSMGBix9.d.cts → agent-contract-DfLm4Mf-.d.ts} +3 -19
  5. package/dist/{agent-tooling-BSMGBix9.d.ts → agent-contract-TINzp-5Y.d.cts} +3 -19
  6. package/dist/agent-tooling.cjs +270 -42
  7. package/dist/agent-tooling.cjs.map +1 -1
  8. package/dist/agent-tooling.d.cts +117 -2
  9. package/dist/agent-tooling.d.ts +117 -2
  10. package/dist/agent-tooling.js +150 -11
  11. package/dist/agent-tooling.js.map +1 -1
  12. package/dist/air-jam-context-BCVCWiaT.d.ts +410 -0
  13. package/dist/air-jam-context-DavligpM.d.cts +410 -0
  14. package/dist/arcade/bridge/controller.cjs +872 -582
  15. package/dist/arcade/bridge/controller.cjs.map +1 -1
  16. package/dist/arcade/bridge/controller.d.cts +28 -76
  17. package/dist/arcade/bridge/controller.d.ts +28 -76
  18. package/dist/arcade/bridge/controller.js +27 -26
  19. package/dist/arcade/bridge/controller.js.map +1 -1
  20. package/dist/arcade/bridge/host.cjs +1017 -252
  21. package/dist/arcade/bridge/host.cjs.map +1 -1
  22. package/dist/arcade/bridge/host.d.cts +6 -72
  23. package/dist/arcade/bridge/host.d.ts +6 -72
  24. package/dist/arcade/bridge/host.js +19 -21
  25. package/dist/arcade/bridge/host.js.map +1 -1
  26. package/dist/arcade/bridge/iframe.cjs +3 -31
  27. package/dist/arcade/bridge/iframe.cjs.map +1 -1
  28. package/dist/arcade/bridge/iframe.d.cts +4 -46
  29. package/dist/arcade/bridge/iframe.d.ts +4 -46
  30. package/dist/arcade/bridge/iframe.js +6 -18
  31. package/dist/arcade/bridge/iframe.js.map +1 -1
  32. package/dist/arcade/host.cjs +0 -479
  33. package/dist/arcade/host.cjs.map +1 -1
  34. package/dist/arcade/host.d.cts +1 -18
  35. package/dist/arcade/host.d.ts +1 -18
  36. package/dist/arcade/host.js +0 -13
  37. package/dist/arcade/host.js.map +1 -1
  38. package/dist/arcade/runtime.cjs +12163 -0
  39. package/dist/arcade/runtime.cjs.map +1 -0
  40. package/dist/arcade/runtime.d.cts +54 -0
  41. package/dist/arcade/runtime.d.ts +54 -0
  42. package/dist/arcade/runtime.js +45 -0
  43. package/dist/arcade/runtime.js.map +1 -0
  44. package/dist/arcade/surface.cjs +12 -35
  45. package/dist/arcade/surface.cjs.map +1 -1
  46. package/dist/arcade/surface.d.cts +19 -2
  47. package/dist/arcade/surface.d.ts +19 -2
  48. package/dist/arcade/surface.js +12 -14
  49. package/dist/arcade/surface.js.map +1 -1
  50. package/dist/arcade/url.cjs +45 -283
  51. package/dist/arcade/url.cjs.map +1 -1
  52. package/dist/arcade/url.d.cts +3 -56
  53. package/dist/arcade/url.d.ts +3 -56
  54. package/dist/arcade/url.js +33 -16
  55. package/dist/arcade/url.js.map +1 -1
  56. package/dist/chunk-24NZZOIO.js +700 -0
  57. package/dist/chunk-24NZZOIO.js.map +1 -0
  58. package/dist/chunk-27QLKLZL.js +469 -0
  59. package/dist/chunk-27QLKLZL.js.map +1 -0
  60. package/dist/{chunk-UWJOIVON.js → chunk-2H2FGF3H.js} +3 -6
  61. package/dist/chunk-2H2FGF3H.js.map +1 -0
  62. package/dist/chunk-2QZADUGA.js +188 -0
  63. package/dist/chunk-2QZADUGA.js.map +1 -0
  64. package/dist/chunk-3LDQCQL2.js +307 -0
  65. package/dist/chunk-3LDQCQL2.js.map +1 -0
  66. package/dist/chunk-3M7656SU.js +389 -0
  67. package/dist/chunk-3M7656SU.js.map +1 -0
  68. package/dist/{chunk-ZLPL2R3R.js → chunk-5CRZFIN6.js} +2 -82
  69. package/dist/chunk-5CRZFIN6.js.map +1 -0
  70. package/dist/chunk-5TVVPBQI.js +7 -0
  71. package/dist/chunk-5TVVPBQI.js.map +1 -0
  72. package/dist/chunk-6Y3F6Y2K.js +343 -0
  73. package/dist/chunk-6Y3F6Y2K.js.map +1 -0
  74. package/dist/{chunk-EVZJA6RN.js → chunk-6ZJUA63D.js} +183 -2
  75. package/dist/chunk-6ZJUA63D.js.map +1 -0
  76. package/dist/chunk-A6EOGRRG.js +49 -0
  77. package/dist/chunk-A6EOGRRG.js.map +1 -0
  78. package/dist/chunk-BEM77SVQ.js +57 -0
  79. package/dist/chunk-BEM77SVQ.js.map +1 -0
  80. package/dist/chunk-C5722GTG.js +176 -0
  81. package/dist/chunk-C5722GTG.js.map +1 -0
  82. package/dist/{chunk-DY67S3LR.js → chunk-C5GS2GMK.js} +99 -105
  83. package/dist/chunk-C5GS2GMK.js.map +1 -0
  84. package/dist/chunk-CDL7LYZH.js +19 -0
  85. package/dist/chunk-CDL7LYZH.js.map +1 -0
  86. package/dist/chunk-DA3TE4OP.js +16 -0
  87. package/dist/chunk-DA3TE4OP.js.map +1 -0
  88. package/dist/chunk-DBF7NJ3S.js +72 -0
  89. package/dist/chunk-DBF7NJ3S.js.map +1 -0
  90. package/dist/chunk-DHFKL3DI.js +2477 -0
  91. package/dist/chunk-DHFKL3DI.js.map +1 -0
  92. package/dist/chunk-DRBPVG47.js +89 -0
  93. package/dist/chunk-DRBPVG47.js.map +1 -0
  94. package/dist/{chunk-7OTQTT6H.js → chunk-EG25EXSA.js} +24 -3
  95. package/dist/chunk-EG25EXSA.js.map +1 -0
  96. package/dist/chunk-FCG42U7D.js +131 -0
  97. package/dist/chunk-FCG42U7D.js.map +1 -0
  98. package/dist/chunk-FJ4J2BEC.js +26 -0
  99. package/dist/chunk-FJ4J2BEC.js.map +1 -0
  100. package/dist/chunk-H4DEVMTO.js +221 -0
  101. package/dist/chunk-H4DEVMTO.js.map +1 -0
  102. package/dist/chunk-I6TSHJF7.js +63 -0
  103. package/dist/chunk-I6TSHJF7.js.map +1 -0
  104. package/dist/{chunk-NCL6WJCL.js → chunk-JCGTL77W.js} +32 -1
  105. package/dist/chunk-JCGTL77W.js.map +1 -0
  106. package/dist/chunk-JJVCBUYM.js +98 -0
  107. package/dist/chunk-JJVCBUYM.js.map +1 -0
  108. package/dist/chunk-K6BJJJJM.js +138 -0
  109. package/dist/chunk-K6BJJJJM.js.map +1 -0
  110. package/dist/chunk-KSYN4I5Y.js +50 -0
  111. package/dist/chunk-KSYN4I5Y.js.map +1 -0
  112. package/dist/chunk-KUGROY5S.js +265 -0
  113. package/dist/chunk-KUGROY5S.js.map +1 -0
  114. package/dist/chunk-LX6CTPDW.js +16 -0
  115. package/dist/chunk-LX6CTPDW.js.map +1 -0
  116. package/dist/{chunk-BE6Z4SIT.js → chunk-M5553I2G.js} +2 -11
  117. package/dist/chunk-M5553I2G.js.map +1 -0
  118. package/dist/{chunk-X24K52DG.js → chunk-MD25536W.js} +1 -1
  119. package/dist/chunk-MD25536W.js.map +1 -0
  120. package/dist/{chunk-3USU7GY2.js → chunk-MJAWAHLT.js} +1 -1
  121. package/dist/chunk-MJAWAHLT.js.map +1 -0
  122. package/dist/chunk-OAXCFPZ7.js +14 -0
  123. package/dist/chunk-OAXCFPZ7.js.map +1 -0
  124. package/dist/chunk-QHBRHYMV.js +81 -0
  125. package/dist/chunk-QHBRHYMV.js.map +1 -0
  126. package/dist/chunk-QTNYGAZ7.js +45 -0
  127. package/dist/chunk-QTNYGAZ7.js.map +1 -0
  128. package/dist/chunk-S3TN7Y7L.js +921 -0
  129. package/dist/chunk-S3TN7Y7L.js.map +1 -0
  130. package/dist/chunk-SMKD4JY3.js +129 -0
  131. package/dist/chunk-SMKD4JY3.js.map +1 -0
  132. package/dist/{chunk-NUJBMV3O.js → chunk-TFW4NJW2.js} +619 -646
  133. package/dist/chunk-TFW4NJW2.js.map +1 -0
  134. package/dist/chunk-UBBP2UY3.js +83 -0
  135. package/dist/chunk-UBBP2UY3.js.map +1 -0
  136. package/dist/chunk-URVBMQCK.js +802 -0
  137. package/dist/chunk-URVBMQCK.js.map +1 -0
  138. package/dist/chunk-VGX2274F.js +1995 -0
  139. package/dist/chunk-VGX2274F.js.map +1 -0
  140. package/dist/chunk-WAFGYJZN.js +1513 -0
  141. package/dist/chunk-WAFGYJZN.js.map +1 -0
  142. package/dist/chunk-WGHNGQUS.js +78 -0
  143. package/dist/chunk-WGHNGQUS.js.map +1 -0
  144. package/dist/chunk-XLY44PXZ.js +46 -0
  145. package/dist/chunk-XLY44PXZ.js.map +1 -0
  146. package/dist/connection-store-BYpQ50W2.d.ts +41 -0
  147. package/dist/connection-store-D6QqzXU1.d.cts +41 -0
  148. package/dist/control-Bv8dWjyp.d.ts +1400 -0
  149. package/dist/control-CNzybWMd.d.cts +1400 -0
  150. package/dist/controller-0CVO1t_3.d.cts +26 -0
  151. package/dist/controller-agent-vOiuK7T6.d.cts +130 -0
  152. package/dist/controller-agent-vOiuK7T6.d.ts +130 -0
  153. package/dist/controller-bridge.cjs +992 -0
  154. package/dist/controller-bridge.cjs.map +1 -0
  155. package/dist/controller-bridge.d.cts +52 -0
  156. package/dist/controller-bridge.d.ts +52 -0
  157. package/dist/controller-bridge.js +23 -0
  158. package/dist/controller-bridge.js.map +1 -0
  159. package/dist/controller-capture-2GKKGHJ2.js +8 -0
  160. package/dist/controller-capture-2GKKGHJ2.js.map +1 -0
  161. package/dist/controller-h9M2Nngd.d.ts +26 -0
  162. package/dist/controller-press-EWWIMVXP.js +231 -0
  163. package/dist/controller-press-EWWIMVXP.js.map +1 -0
  164. package/dist/core-BAO0pHrJ.d.cts +15 -0
  165. package/dist/core-BAO0pHrJ.d.ts +15 -0
  166. package/dist/core.cjs +5931 -0
  167. package/dist/core.cjs.map +1 -0
  168. package/dist/core.d.cts +232 -0
  169. package/dist/core.d.ts +232 -0
  170. package/dist/core.js +906 -0
  171. package/dist/core.js.map +1 -0
  172. package/dist/dev-events-BffobOfb.d.ts +31 -0
  173. package/dist/dev-events-Ct7QEgKb.d.cts +31 -0
  174. package/dist/dev-log-events-B1_B3guZ.d.cts +47 -0
  175. package/dist/dev-log-events-B1_B3guZ.d.ts +47 -0
  176. package/dist/frames-D-xKuODS.d.cts +686 -0
  177. package/dist/frames-D-xKuODS.d.ts +686 -0
  178. package/dist/game-Db58JNk3.d.cts +77 -0
  179. package/dist/game-bySEy-QO.d.ts +77 -0
  180. package/dist/host-runtime-BuLhGQx5.d.ts +399 -0
  181. package/dist/host-runtime-_06jpW5v.d.cts +399 -0
  182. package/dist/index.cjs +12406 -8020
  183. package/dist/index.cjs.map +1 -1
  184. package/dist/index.d.cts +106 -973
  185. package/dist/index.d.ts +106 -973
  186. package/dist/index.js +451 -4637
  187. package/dist/index.js.map +1 -1
  188. package/dist/input-Xvs80gqG.d.cts +92 -0
  189. package/dist/input-Xvs80gqG.d.ts +92 -0
  190. package/dist/input-bindings-BaEFrgRb.d.cts +129 -0
  191. package/dist/input-bindings-BgsJRLPm.d.ts +129 -0
  192. package/dist/leased-control-BEg4JuXA.d.cts +158 -0
  193. package/dist/leased-control-BEg4JuXA.d.ts +158 -0
  194. package/dist/metadata.cjs +3 -9
  195. package/dist/metadata.cjs.map +1 -1
  196. package/dist/metadata.d.cts +6 -17
  197. package/dist/metadata.d.ts +6 -17
  198. package/dist/metadata.js +8 -76
  199. package/dist/metadata.js.map +1 -1
  200. package/dist/phone-room-session-Ca6rlxJr.d.ts +439 -0
  201. package/dist/phone-room-session-DkPR-K6e.d.cts +439 -0
  202. package/dist/platform-machine.cjs +248 -18
  203. package/dist/platform-machine.cjs.map +1 -1
  204. package/dist/platform-machine.d.cts +1867 -96
  205. package/dist/platform-machine.d.ts +1867 -96
  206. package/dist/platform-machine.js +206 -18
  207. package/dist/platform-machine.js.map +1 -1
  208. package/dist/{platform-settings-cuor3i11.d.cts → platform-settings-Yb0fQ1dl.d.cts} +1 -3
  209. package/dist/{platform-settings-cuor3i11.d.ts → platform-settings-Yb0fQ1dl.d.ts} +1 -3
  210. package/dist/prefabs.cjs +134 -3
  211. package/dist/prefabs.cjs.map +1 -1
  212. package/dist/prefabs.d.cts +23 -47
  213. package/dist/prefabs.d.ts +23 -47
  214. package/dist/prefabs.js +23 -4
  215. package/dist/prefabs.js.map +1 -1
  216. package/dist/preview.cjs +2269 -774
  217. package/dist/preview.cjs.map +1 -1
  218. package/dist/preview.d.cts +195 -38
  219. package/dist/preview.d.ts +195 -38
  220. package/dist/preview.js +494 -260
  221. package/dist/preview.js.map +1 -1
  222. package/dist/protocol.cjs +705 -396
  223. package/dist/protocol.cjs.map +1 -1
  224. package/dist/protocol.d.cts +49 -100
  225. package/dist/protocol.d.ts +49 -100
  226. package/dist/protocol.js +56 -75
  227. package/dist/protocol.js.map +1 -1
  228. package/dist/release.cjs +37 -0
  229. package/dist/release.cjs.map +1 -1
  230. package/dist/release.d.cts +29 -3
  231. package/dist/release.d.ts +29 -3
  232. package/dist/release.js +13 -1
  233. package/dist/release.js.map +1 -1
  234. package/dist/replay-input-log-BvX4As2f.d.ts +31 -0
  235. package/dist/replay-input-log-Vcl0VDIy.d.cts +31 -0
  236. package/dist/room-protocol.cjs +794 -0
  237. package/dist/room-protocol.cjs.map +1 -0
  238. package/dist/room-protocol.d.cts +63 -0
  239. package/dist/room-protocol.d.ts +63 -0
  240. package/dist/room-protocol.js +143 -0
  241. package/dist/room-protocol.js.map +1 -0
  242. package/dist/room-session.cjs +2336 -0
  243. package/dist/room-session.cjs.map +1 -0
  244. package/dist/room-session.d.cts +77 -0
  245. package/dist/room-session.d.ts +77 -0
  246. package/dist/room-session.js +103 -0
  247. package/dist/room-session.js.map +1 -0
  248. package/dist/runtime/prefab-inspection-bridge.cjs +207 -0
  249. package/dist/runtime/prefab-inspection-bridge.cjs.map +1 -0
  250. package/dist/runtime/prefab-inspection-bridge.d.cts +156 -0
  251. package/dist/runtime/prefab-inspection-bridge.d.ts +156 -0
  252. package/dist/runtime/prefab-inspection-bridge.js +22 -0
  253. package/dist/runtime/prefab-inspection-bridge.js.map +1 -0
  254. package/dist/runtime-control.cjs +2150 -0
  255. package/dist/runtime-control.cjs.map +1 -0
  256. package/dist/runtime-control.d.cts +985 -0
  257. package/dist/runtime-control.d.ts +985 -0
  258. package/dist/runtime-control.js +268 -0
  259. package/dist/runtime-control.js.map +1 -0
  260. package/dist/runtime-inspection.cjs +1319 -0
  261. package/dist/runtime-inspection.cjs.map +1 -0
  262. package/dist/runtime-inspection.d.cts +555 -0
  263. package/dist/runtime-inspection.d.ts +555 -0
  264. package/dist/runtime-inspection.js +29 -0
  265. package/dist/runtime-inspection.js.map +1 -0
  266. package/dist/runtime-reporting-K_ZXpubu.d.cts +43 -0
  267. package/dist/runtime-reporting-K_ZXpubu.d.ts +43 -0
  268. package/dist/runtime-topology.cjs +2 -5
  269. package/dist/runtime-topology.cjs.map +1 -1
  270. package/dist/runtime-topology.d.cts +1 -1
  271. package/dist/runtime-topology.d.ts +1 -1
  272. package/dist/runtime-topology.js +1 -1
  273. package/dist/runtime-topology.js.map +1 -1
  274. package/dist/scoped-connector-CiiWu06D.d.ts +66 -0
  275. package/dist/scoped-connector-D7252pn0.d.cts +66 -0
  276. package/dist/sdk-channels-S6j6AH8v.d.cts +103 -0
  277. package/dist/sdk-channels-S6j6AH8v.d.ts +103 -0
  278. package/dist/simulation-BfRnC4rg.d.ts +782 -0
  279. package/dist/simulation-BwZx2pgt.d.cts +782 -0
  280. package/dist/{standard-match-phase-BucA5dYo.d.ts → standard-match-phase-D4N-AU0d.d.cts} +3 -13
  281. package/dist/{standard-match-phase-U2ra8J-0.d.cts → standard-match-phase-abt8ZjCh.d.ts} +3 -13
  282. package/dist/transport-CWF29jU6.d.cts +173 -0
  283. package/dist/transport-CWF29jU6.d.ts +173 -0
  284. package/dist/types-p28maMEo.d.cts +102 -0
  285. package/dist/types-p28maMEo.d.ts +102 -0
  286. package/dist/ui.cjs +2324 -1051
  287. package/dist/ui.cjs.map +1 -1
  288. package/dist/ui.d.cts +75 -17
  289. package/dist/ui.d.ts +75 -17
  290. package/dist/ui.js +555 -389
  291. package/dist/ui.js.map +1 -1
  292. package/package.json +88 -37
  293. package/src/agent/agent-action.ts +0 -39
  294. package/src/agent/agent-contract-conformance.ts +208 -0
  295. package/src/agent/agent-contract.ts +2 -4
  296. package/src/agent/agent-evaluator.ts +95 -0
  297. package/src/agent/capture-canvas.ts +79 -0
  298. package/src/agent/host-canvas-capture.ts +189 -0
  299. package/src/agent-tooling.ts +8 -18
  300. package/src/arcade/bridge/controller.ts +8 -11
  301. package/src/arcade/bridge/host.ts +6 -11
  302. package/src/arcade/bridge/iframe.ts +1 -50
  303. package/src/arcade/host.ts +0 -1
  304. package/src/arcade/runtime.ts +6 -0
  305. package/src/arcade/surface.ts +6 -1
  306. package/src/arcade/url.ts +2 -9
  307. package/src/audio/audio-manager.ts +23 -47
  308. package/src/audio/hooks.tsx +59 -72
  309. package/src/components/controller-player-name-field.tsx +2 -2
  310. package/src/components/controller-primary-action.tsx +1 -1
  311. package/src/components/controls.tsx +398 -0
  312. package/src/components/gamepad-layout.tsx +69 -0
  313. package/src/components/join-url-controls.tsx +5 -1
  314. package/src/components/lifecycle-action-group.tsx +1 -1
  315. package/src/components/player-avatar.tsx +50 -3
  316. package/src/components/room-qr-code.tsx +36 -77
  317. package/src/components/stage-capture-surface.tsx +91 -0
  318. package/src/components/surface-viewport.tsx +16 -3
  319. package/src/components/ui/button.tsx +1 -1
  320. package/src/constants.ts +0 -15
  321. package/src/context/air-jam-context.tsx +64 -195
  322. package/src/context/session-providers.tsx +13 -10
  323. package/src/controller-bridge.ts +7 -0
  324. package/src/core/assets.ts +48 -0
  325. package/src/core/config.ts +70 -0
  326. package/src/core/connection-store.ts +77 -0
  327. package/src/core/controller-input.ts +147 -0
  328. package/src/core/controller-runtime.ts +427 -0
  329. package/src/core/dev-events.ts +18 -0
  330. package/src/core/environment.ts +132 -0
  331. package/src/core/game-description.ts +44 -0
  332. package/src/core/game.ts +130 -0
  333. package/src/core/host-input.ts +207 -0
  334. package/src/core/host-runtime.ts +988 -0
  335. package/src/core/hosted-rooms.ts +26 -0
  336. package/src/core/input-bindings.ts +180 -0
  337. package/src/core/input-log.ts +144 -0
  338. package/src/core/input.ts +228 -0
  339. package/src/core/local-input.ts +319 -0
  340. package/src/core/replay-input-log.ts +298 -0
  341. package/src/core/replicated-store.ts +1178 -0
  342. package/src/core/room-action-ack.ts +43 -0
  343. package/src/core/settings.ts +202 -0
  344. package/src/core/simulation.ts +307 -0
  345. package/src/core/theme.ts +33 -0
  346. package/src/core.ts +162 -0
  347. package/src/dev/browser-log-sink.ts +47 -27
  348. package/src/diagnostics.ts +5 -6
  349. package/src/hooks/internal/use-controller-runtime-api.ts +58 -836
  350. package/src/hooks/internal/use-host-runtime-api.ts +98 -1013
  351. package/src/hooks/internal/use-runtime-inspection-contract.ts +14 -2
  352. package/src/hooks/use-air-jam-controller.ts +41 -62
  353. package/src/hooks/use-air-jam-host.ts +66 -107
  354. package/src/hooks/use-air-jam-settings.ts +27 -0
  355. package/src/hooks/use-controller-toasts.ts +16 -19
  356. package/src/hooks/use-host-agent-capture.ts +31 -0
  357. package/src/hooks/use-lifecycle-action-group-model.ts +1 -1
  358. package/src/hooks/use-send-signal.ts +23 -156
  359. package/src/hooks/use-simulation.ts +83 -0
  360. package/src/index.ts +26 -106
  361. package/src/lifecycle/index.ts +0 -4
  362. package/src/lifecycle/standard-match-phase.ts +1 -22
  363. package/src/metadata/manifest.ts +8 -20
  364. package/src/metadata.ts +1 -1
  365. package/src/platform-machine.ts +236 -15
  366. package/src/prefabs/types.ts +91 -20
  367. package/src/prefabs/view.ts +82 -0
  368. package/src/prefabs.ts +19 -0
  369. package/src/preview/host-workspace.tsx +4 -9
  370. package/src/preview/identity.ts +31 -23
  371. package/src/preview/layout.ts +5 -1
  372. package/src/preview/manager.ts +205 -78
  373. package/src/preview/messages.ts +47 -0
  374. package/src/preview/ownership.ts +6 -1
  375. package/src/preview/url.ts +46 -23
  376. package/src/preview/window.tsx +26 -69
  377. package/src/preview/workspace-local-seats.ts +166 -0
  378. package/src/preview/workspace.tsx +137 -66
  379. package/src/preview.ts +27 -15
  380. package/src/protocol/actions.ts +58 -0
  381. package/src/protocol/arcade-platform-actions.ts +20 -16
  382. package/src/protocol/controller.ts +15 -169
  383. package/src/protocol/core.ts +8 -15
  384. package/src/protocol/dev-log-events.ts +5 -84
  385. package/src/protocol/leased-control.ts +177 -0
  386. package/src/protocol/room/addressing.ts +35 -0
  387. package/src/protocol/room/close-codes.ts +33 -0
  388. package/src/protocol/room/frames.ts +343 -0
  389. package/src/protocol/room/input-codec.ts +182 -0
  390. package/src/protocol/room/limits.ts +58 -0
  391. package/src/protocol/room/patch.ts +135 -0
  392. package/src/protocol/room/sdk-channels.ts +81 -0
  393. package/src/protocol/room/seat-id.ts +31 -0
  394. package/src/protocol/room/transport.ts +63 -0
  395. package/src/protocol/room/weight.ts +58 -0
  396. package/src/protocol/runtime-reporting.ts +78 -0
  397. package/src/protocol/signed-envelope.ts +176 -0
  398. package/src/protocol.ts +4 -7
  399. package/src/qrcode.d.ts +3 -13
  400. package/src/release.ts +32 -0
  401. package/src/room-protocol.ts +10 -0
  402. package/src/room-session/browser-wake-events.ts +14 -0
  403. package/src/room-session/channel-state.ts +293 -0
  404. package/src/room-session/clock.ts +24 -0
  405. package/src/room-session/host-room-session.ts +379 -0
  406. package/src/room-session/input-lane.ts +125 -0
  407. package/src/room-session/listeners.ts +22 -0
  408. package/src/room-session/loopback-link.ts +58 -0
  409. package/src/room-session/phone-room-session.ts +368 -0
  410. package/src/room-session/port-link.ts +116 -0
  411. package/src/room-session/room-key.ts +9 -0
  412. package/src/room-session/roster.ts +23 -0
  413. package/src/room-session/scoped-connector.ts +577 -0
  414. package/src/room-session/session-link.ts +306 -0
  415. package/src/room-session/web-socket-link.ts +57 -0
  416. package/src/room-session.ts +12 -0
  417. package/src/runtime/air-jam-config.ts +53 -51
  418. package/src/runtime/air-jam-error-boundary.tsx +52 -2
  419. package/src/runtime/arcade-runtime-url.ts +8 -9
  420. package/src/runtime/browser-controller-environment.ts +116 -0
  421. package/src/runtime/browser-host-environment.ts +137 -0
  422. package/src/runtime/browser-replay.ts +211 -0
  423. package/src/runtime/browser-room.ts +41 -0
  424. package/src/runtime/connected-runtime-error-boundary.tsx +58 -0
  425. package/src/runtime/contracts/agent-control.ts +195 -0
  426. package/src/runtime/contracts/clip.ts +169 -0
  427. package/src/runtime/contracts/control.ts +744 -59
  428. package/src/runtime/contracts/controller-agent.ts +130 -0
  429. package/src/runtime/contracts/input-log.ts +38 -0
  430. package/src/runtime/contracts/inspection.ts +25 -22
  431. package/src/runtime/contracts/observation.ts +186 -0
  432. package/src/runtime/contracts/runtime-identity.ts +43 -0
  433. package/src/runtime/contracts/timeline.ts +59 -0
  434. package/src/runtime/controller-agent-bridge.ts +152 -0
  435. package/src/runtime/controller-bridge-context.ts +29 -0
  436. package/src/runtime/controller-capture.ts +50 -0
  437. package/src/runtime/controller-identity.ts +54 -115
  438. package/src/runtime/controller-presentation.ts +47 -17
  439. package/src/runtime/controller-press.ts +271 -0
  440. package/src/runtime/create-air-jam-app.tsx +120 -83
  441. package/src/runtime/dev-runtime-events.ts +6 -18
  442. package/src/runtime/embedded-parent.ts +22 -0
  443. package/src/runtime/embedded-runtime-adapters.ts +10 -9
  444. package/src/runtime/host-agent-bridge.ts +127 -0
  445. package/src/runtime/host-capture-boundary.tsx +92 -0
  446. package/src/runtime/host-capture-context.ts +7 -0
  447. package/src/runtime/host-clip-reader.ts +93 -0
  448. package/src/runtime/host-clip-recorder.ts +342 -0
  449. package/src/runtime/host-input-log-reader.ts +120 -0
  450. package/src/runtime/host-observation.ts +207 -0
  451. package/src/runtime/host-pace.ts +36 -0
  452. package/src/runtime/host-runtime-fault.ts +51 -0
  453. package/src/runtime/host-timeline.ts +152 -0
  454. package/src/runtime/local-players.tsx +96 -0
  455. package/src/runtime/local-reference-game-identity.ts +13 -0
  456. package/src/runtime/local-seats.ts +612 -0
  457. package/src/runtime/participant-control.ts +162 -0
  458. package/src/runtime/prefab-inspection-bridge.ts +174 -0
  459. package/src/runtime/prefab-inspection-clock.ts +12 -0
  460. package/src/runtime/prefab-inspection-context.ts +20 -0
  461. package/src/runtime/prefab-inspection-runtime.tsx +328 -0
  462. package/src/runtime/preview-inspection-bridge.ts +130 -0
  463. package/src/runtime/replay-runtime.tsx +101 -0
  464. package/src/runtime/room-bridge.ts +110 -0
  465. package/src/runtime/runtime-owner-contexts.tsx +9 -0
  466. package/src/runtime/runtime-session-params.ts +23 -86
  467. package/src/runtime/session-runtimes.tsx +231 -64
  468. package/src/runtime/stage-capture.ts +25 -0
  469. package/src/runtime-control.ts +44 -8
  470. package/src/runtime-inspection.ts +14 -7
  471. package/src/runtime-topology.ts +7 -8
  472. package/src/settings/platform-settings-runtime.tsx +48 -8
  473. package/src/settings/platform-settings.ts +0 -96
  474. package/src/store/use-air-jam-store.ts +73 -0
  475. package/src/ui.ts +3 -3
  476. package/src/utils/is-rpc-serializable.ts +11 -19
  477. package/src/utils/url-builder.ts +0 -7
  478. package/dist/arcade/bridge.cjs +0 -1067
  479. package/dist/arcade/bridge.cjs.map +0 -1
  480. package/dist/arcade/bridge.d.cts +0 -11
  481. package/dist/arcade/bridge.d.ts +0 -11
  482. package/dist/arcade/bridge.js +0 -76
  483. package/dist/arcade/bridge.js.map +0 -1
  484. package/dist/arcade-runtime-url-BW-MUAOY.d.cts +0 -28
  485. package/dist/arcade-runtime-url-l8qj01Pg.d.ts +0 -28
  486. package/dist/arcade.cjs +0 -1737
  487. package/dist/arcade.cjs.map +0 -1
  488. package/dist/arcade.d.cts +0 -18
  489. package/dist/arcade.d.ts +0 -18
  490. package/dist/arcade.js +0 -123
  491. package/dist/arcade.js.map +0 -1
  492. package/dist/capabilities.cjs +0 -39
  493. package/dist/capabilities.cjs.map +0 -1
  494. package/dist/capabilities.d.cts +0 -31
  495. package/dist/capabilities.d.ts +0 -31
  496. package/dist/capabilities.js +0 -12
  497. package/dist/capabilities.js.map +0 -1
  498. package/dist/chunk-32NYL7XI.js +0 -539
  499. package/dist/chunk-32NYL7XI.js.map +0 -1
  500. package/dist/chunk-3USU7GY2.js.map +0 -1
  501. package/dist/chunk-6CMB356F.js +0 -193
  502. package/dist/chunk-6CMB356F.js.map +0 -1
  503. package/dist/chunk-7OTQTT6H.js.map +0 -1
  504. package/dist/chunk-BE6Z4SIT.js.map +0 -1
  505. package/dist/chunk-CVYR5RJT.js +0 -184
  506. package/dist/chunk-CVYR5RJT.js.map +0 -1
  507. package/dist/chunk-DY67S3LR.js.map +0 -1
  508. package/dist/chunk-E5PVHEAX.js +0 -126
  509. package/dist/chunk-E5PVHEAX.js.map +0 -1
  510. package/dist/chunk-EDRQIOCV.js +0 -25
  511. package/dist/chunk-EDRQIOCV.js.map +0 -1
  512. package/dist/chunk-EVZJA6RN.js.map +0 -1
  513. package/dist/chunk-IKKDONQL.js +0 -1
  514. package/dist/chunk-IKKDONQL.js.map +0 -1
  515. package/dist/chunk-JO2HR5PV.js +0 -43
  516. package/dist/chunk-JO2HR5PV.js.map +0 -1
  517. package/dist/chunk-JTF47B56.js +0 -1
  518. package/dist/chunk-JTF47B56.js.map +0 -1
  519. package/dist/chunk-KOB73SPD.js +0 -216
  520. package/dist/chunk-KOB73SPD.js.map +0 -1
  521. package/dist/chunk-LIXPF4P6.js +0 -22
  522. package/dist/chunk-LIXPF4P6.js.map +0 -1
  523. package/dist/chunk-M4DKOI42.js +0 -32
  524. package/dist/chunk-M4DKOI42.js.map +0 -1
  525. package/dist/chunk-MPDMJIX5.js +0 -1
  526. package/dist/chunk-MPDMJIX5.js.map +0 -1
  527. package/dist/chunk-NCL6WJCL.js.map +0 -1
  528. package/dist/chunk-NUJBMV3O.js.map +0 -1
  529. package/dist/chunk-Q4UALQO4.js +0 -288
  530. package/dist/chunk-Q4UALQO4.js.map +0 -1
  531. package/dist/chunk-TGRTEWYV.js +0 -195
  532. package/dist/chunk-TGRTEWYV.js.map +0 -1
  533. package/dist/chunk-UWJOIVON.js.map +0 -1
  534. package/dist/chunk-W2VK36RO.js +0 -11
  535. package/dist/chunk-W2VK36RO.js.map +0 -1
  536. package/dist/chunk-X24K52DG.js.map +0 -1
  537. package/dist/chunk-YXHH3MQT.js +0 -230
  538. package/dist/chunk-YXHH3MQT.js.map +0 -1
  539. package/dist/chunk-ZLPL2R3R.js.map +0 -1
  540. package/dist/chunk-ZTQ5SP4H.js +0 -9
  541. package/dist/chunk-ZTQ5SP4H.js.map +0 -1
  542. package/dist/controller-Bod8YYr1.d.cts +0 -172
  543. package/dist/controller-DxVchfiA.d.ts +0 -172
  544. package/dist/dev-log-events-CVAM29hx.d.cts +0 -123
  545. package/dist/dev-log-events-CVAM29hx.d.ts +0 -123
  546. package/dist/handshake-CC_LrK5g.d.cts +0 -31
  547. package/dist/handshake-CC_LrK5g.d.ts +0 -31
  548. package/dist/host-8hmq_Sgc.d.cts +0 -109
  549. package/dist/host-Cw-N6e-5.d.ts +0 -109
  550. package/dist/notices-CnGaaS8B.d.ts +0 -38
  551. package/dist/notices-NyDM5Mtz.d.cts +0 -38
  552. package/dist/realtime-client-Cu8yVJFw.d.cts +0 -15
  553. package/dist/realtime-client-Cu8yVJFw.d.ts +0 -15
  554. package/dist/sync-B6QsNrXz.d.cts +0 -125
  555. package/dist/sync-B6QsNrXz.d.ts +0 -125
  556. package/src/arcade/bridge.ts +0 -3
  557. package/src/arcade.ts +0 -4
  558. package/src/capabilities/manifest.ts +0 -71
  559. package/src/capabilities.ts +0 -12
  560. package/src/context/socket-manager.ts +0 -79
  561. package/src/events.ts +0 -124
  562. package/src/hooks/internal/use-runtime-control-contract.ts +0 -15
  563. package/src/hooks/internal/use-runtime-observability-contract.ts +0 -24
  564. package/src/hooks/use-connection-status.ts +0 -8
  565. package/src/hooks/use-controller-tick.ts +0 -65
  566. package/src/hooks/use-get-input.ts +0 -100
  567. package/src/hooks/use-host-arcade-restore.ts +0 -30
  568. package/src/hooks/use-host-tick.ts +0 -171
  569. package/src/hooks/use-input-writer.ts +0 -83
  570. package/src/hooks/use-players.ts +0 -12
  571. package/src/internal/input-manager.ts +0 -356
  572. package/src/protocol/errors.ts +0 -21
  573. package/src/protocol/host-grant.ts +0 -185
  574. package/src/protocol/host.ts +0 -138
  575. package/src/protocol/notices.ts +0 -43
  576. package/src/protocol/signals.ts +0 -38
  577. package/src/protocol/socket-events.ts +0 -154
  578. package/src/protocol/sync.ts +0 -121
  579. package/src/runtime/contracts/observability.ts +0 -94
  580. package/src/runtime/controller-bridge.ts +0 -451
  581. package/src/runtime/controller-realtime-client.ts +0 -591
  582. package/src/runtime/host-bridge.ts +0 -388
  583. package/src/runtime/host-realtime-client.ts +0 -550
  584. package/src/runtime/realtime-client.ts +0 -20
  585. package/src/runtime/validate-arcade-bridge-attach.ts +0 -14
  586. package/src/runtime-observability.ts +0 -9
  587. package/src/state/connection-store.ts +0 -152
  588. package/src/store/create-air-jam-store.ts +0 -1141
  589. package/src/store/index.ts +0 -1
  590. package/src/utils/ids.ts +0 -31
@@ -1,1141 +0,0 @@
1
- import { useEffect, useMemo, useRef, type MutableRefObject } from "react";
2
- import { create, type StateCreator } from "zustand";
3
- import type { StoreApi } from "zustand/vanilla";
4
- import { useAirJamContext, useAirJamState } from "../context/air-jam-context";
5
- import { emitAirJamDiagnostic } from "../diagnostics";
6
- import type {
7
- AirJamActionActorRole,
8
- AirJamActionInvocationResult,
9
- AirJamActionPayload,
10
- AirJamActionRpcPayload,
11
- AirJamStateSyncPayload,
12
- } from "../protocol";
13
- import { resolveImplicitReplicatedStoreDomainFromWindow } from "../runtime/arcade-runtime-url";
14
- import { getControllerRealtimeClient } from "../runtime/controller-realtime-client";
15
- import { getHostRealtimeClient } from "../runtime/host-realtime-client";
16
- import type { AirJamRealtimeClient } from "../runtime/realtime-client";
17
- import { isRpcSerializable } from "../utils/is-rpc-serializable";
18
- import {
19
- AIR_JAM_ARCADE_SURFACE_STORE_DOMAIN,
20
- AIR_JAM_DEFAULT_STORE_DOMAIN,
21
- } from "./air-jam-store-domain-constants";
22
-
23
- const INTERNAL_ACTION_PREFIX = "_";
24
- const UNRESOLVED_ACTOR_ID = "unknown";
25
-
26
- export interface CreateAirJamStoreOptions {
27
- storeDomain?: string;
28
- }
29
-
30
- const isEventLikePayload = (payload: unknown): boolean => {
31
- if (payload === null || typeof payload !== "object") {
32
- return false;
33
- }
34
-
35
- const candidate = payload as Record<string, unknown>;
36
- return (
37
- typeof candidate.preventDefault === "function" ||
38
- typeof candidate.stopPropagation === "function" ||
39
- typeof candidate.persist === "function" ||
40
- ("nativeEvent" in candidate &&
41
- ("target" in candidate || "currentTarget" in candidate))
42
- );
43
- };
44
-
45
- const isPlainActionPayload = (
46
- payload: unknown,
47
- ): payload is AirJamActionPayload => {
48
- if (
49
- payload === null ||
50
- typeof payload !== "object" ||
51
- Array.isArray(payload)
52
- ) {
53
- return false;
54
- }
55
-
56
- const prototype = Object.getPrototypeOf(payload);
57
- return prototype === Object.prototype || prototype === null;
58
- };
59
-
60
- export interface AirJamActionContext {
61
- actorId: string;
62
- role: AirJamActionActorRole;
63
- connectedPlayerIds: string[];
64
- }
65
-
66
- export interface AirJamActionRejection {
67
- readonly __airJamActionRejection: true;
68
- reason: string;
69
- message: string;
70
- details?: AirJamActionPayload;
71
- }
72
-
73
- export interface AirJamActionAcceptance<TResult = unknown> {
74
- readonly __airJamActionAcceptance: true;
75
- result?: TResult;
76
- }
77
-
78
- type AirJamActionHandler = (
79
- ctx: AirJamActionContext,
80
- // Internal existential handler shape for heterogenous action maps.
81
- // Store authors still get precise payload types through inference at the public API.
82
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
- payload: any,
84
- ) => unknown;
85
- type AirJamActionMap = Record<string, AirJamActionHandler>;
86
- type AirJamActionName<TActions extends AirJamActionMap> = Extract<
87
- keyof TActions,
88
- string
89
- >;
90
-
91
- type IsValidActionPayloadType<TPayload> = [TPayload] extends [undefined]
92
- ? true
93
- : TPayload extends readonly unknown[]
94
- ? false
95
- : TPayload extends object
96
- ? true
97
- : false;
98
-
99
- type InvalidActionPayloadKeys<TActions extends AirJamActionMap> = {
100
- [K in keyof TActions]: TActions[K] extends (
101
- ctx: AirJamActionContext,
102
- payload: infer TPayload,
103
- ) => unknown
104
- ? IsValidActionPayloadType<TPayload> extends true
105
- ? never
106
- : K
107
- : K;
108
- }[keyof TActions];
109
-
110
- type InvalidActionPayloadKeyNames<TActions extends AirJamActionMap> = Extract<
111
- InvalidActionPayloadKeys<TActions>,
112
- string
113
- >;
114
-
115
- type AirJamInvalidActionPayloadDiagnostic<TActions extends AirJamActionMap> = {
116
- readonly __airJamInvalidActionPayloads__: InvalidActionPayloadKeyNames<TActions>;
117
- readonly __airJamActionPayloadRule__: "Networked store action payloads must be omitted or one plain object payload. Unions like T | undefined, arrays, and primitives are not valid payload roots.";
118
- };
119
-
120
- type AirJamNetworkedState<TActions extends AirJamActionMap = AirJamActionMap> =
121
- {
122
- actions: TActions;
123
- };
124
-
125
- type AirJamActionDispatchMap<TActions extends AirJamActionMap> = {
126
- [K in keyof TActions]: TActions[K] extends (
127
- ctx: AirJamActionContext,
128
- payload: infer TPayload,
129
- ) => infer TResult
130
- ? [TPayload] extends [undefined]
131
- ? () => Promise<
132
- AirJamActionInvocationResult<InferAirJamActionResult<TResult>>
133
- >
134
- : TPayload extends readonly unknown[]
135
- ? never
136
- : TPayload extends object
137
- ? (
138
- payload: TPayload,
139
- ) => Promise<
140
- AirJamActionInvocationResult<InferAirJamActionResult<TResult>>
141
- >
142
- : never
143
- : never;
144
- };
145
-
146
- type InferAirJamActionResult<TResult> =
147
- TResult extends AirJamActionAcceptance<infer TAccepted>
148
- ? TAccepted
149
- : TResult extends AirJamActionRejection
150
- ? never
151
- : TResult;
152
-
153
- interface StoreRuntimeSnapshot {
154
- socket: AirJamRealtimeClient | null;
155
- role: AirJamActionActorRole | null;
156
- roomId: string | null;
157
- resolvedStoreDomain: string;
158
- connectionStatus: string | null;
159
- canBroadcastHostState: boolean;
160
- connectedPlayerIds: string[];
161
- }
162
-
163
- interface StoreRuntimeBinding {
164
- refCount: number;
165
- flushHostStateSync?: () => void;
166
- lastHostFlushKey?: string;
167
- cleanup: () => void;
168
- }
169
-
170
- export type AirJamHostActionEvent<
171
- TActions extends AirJamActionMap = AirJamActionMap,
172
- TActionName extends AirJamActionName<TActions> = AirJamActionName<TActions>,
173
- > =
174
- TActionName extends AirJamActionName<TActions>
175
- ? TActions[TActionName] extends (
176
- ctx: AirJamActionContext,
177
- payload: infer TPayload,
178
- ) => infer TResult
179
- ? {
180
- actionName: TActionName;
181
- payload: TPayload;
182
- context: AirJamActionContext;
183
- acknowledgement: AirJamActionInvocationResult<
184
- InferAirJamActionResult<TResult>
185
- >;
186
- invocationKind: "local" | "rpc";
187
- roomId: string | null;
188
- storeDomain: string;
189
- }
190
- : never
191
- : never;
192
-
193
- export interface AirJamHostActionSubscriptionOptions<
194
- TActions extends AirJamActionMap = AirJamActionMap,
195
- > {
196
- actionNames?: readonly AirJamActionName<TActions>[];
197
- includeRejected?: boolean;
198
- }
199
-
200
- export type AirJamHostActionListener<
201
- TActions extends AirJamActionMap = AirJamActionMap,
202
- > = (event: AirJamHostActionEvent<TActions>) => void;
203
-
204
- interface HostActionListenerRegistration<TActions extends AirJamActionMap> {
205
- actionNames: ReadonlySet<string> | null;
206
- includeRejected: boolean;
207
- listener: AirJamHostActionListener<TActions>;
208
- }
209
-
210
- export type AirJamSyncedStoreHook<T extends AirJamNetworkedState> = {
211
- <U>(selector?: (state: T) => U): U;
212
- useActions: () => AirJamActionDispatchMap<T["actions"]>;
213
- asPlayer: (controllerId: string) => AirJamActionDispatchMap<T["actions"]>;
214
- getState: () => T;
215
- subscribe: StoreApi<T>["subscribe"];
216
- subscribeHostActions: (
217
- listener: AirJamHostActionListener<T["actions"]>,
218
- options?: AirJamHostActionSubscriptionOptions<T["actions"]>,
219
- ) => () => void;
220
- useHostActionListener: (
221
- listener: AirJamHostActionListener<T["actions"]>,
222
- options?: AirJamHostActionSubscriptionOptions<T["actions"]>,
223
- ) => void;
224
- useLiveStateRef: () => MutableRefObject<T>;
225
- };
226
-
227
- export const isInternalActionName = (actionName: string): boolean =>
228
- actionName.startsWith(INTERNAL_ACTION_PREFIX);
229
-
230
- export const acceptAirJamAction = <TResult = unknown>(
231
- result?: TResult,
232
- ): AirJamActionAcceptance<TResult> => ({
233
- __airJamActionAcceptance: true,
234
- ...(result !== undefined ? { result } : {}),
235
- });
236
-
237
- export const rejectAirJamAction = (
238
- reason: string,
239
- message: string,
240
- details?: AirJamActionPayload,
241
- ): AirJamActionRejection => ({
242
- __airJamActionRejection: true,
243
- reason,
244
- message,
245
- ...(details ? { details } : {}),
246
- });
247
-
248
- const isAirJamActionAcceptance = (
249
- value: unknown,
250
- ): value is AirJamActionAcceptance<unknown> =>
251
- typeof value === "object" &&
252
- value !== null &&
253
- (value as { __airJamActionAcceptance?: boolean }).__airJamActionAcceptance ===
254
- true;
255
-
256
- const isAirJamActionRejection = (
257
- value: unknown,
258
- ): value is AirJamActionRejection =>
259
- typeof value === "object" &&
260
- value !== null &&
261
- (value as { __airJamActionRejection?: boolean }).__airJamActionRejection ===
262
- true;
263
-
264
- const createClientRejectedActionResult = (
265
- reason: string,
266
- message: string,
267
- details?: AirJamActionPayload,
268
- ): AirJamActionInvocationResult => ({
269
- ok: false,
270
- status: "rejected",
271
- source: "client",
272
- reason,
273
- message,
274
- ...(details ? { details } : {}),
275
- });
276
-
277
- const createServerRejectedActionResult = (
278
- reason: string,
279
- message: string,
280
- details?: AirJamActionPayload,
281
- ): AirJamActionInvocationResult => ({
282
- ok: false,
283
- status: "rejected",
284
- source: "server",
285
- reason,
286
- message,
287
- ...(details ? { details } : {}),
288
- });
289
-
290
- const normalizeAirJamActionInvocationResult = (
291
- value: unknown,
292
- source: "client" | "host",
293
- ): AirJamActionInvocationResult => {
294
- if (isAirJamActionRejection(value)) {
295
- return {
296
- ok: false,
297
- status: "rejected",
298
- source,
299
- reason: value.reason,
300
- message: value.message,
301
- ...(value.details ? { details: value.details } : {}),
302
- };
303
- }
304
-
305
- if (isAirJamActionAcceptance(value)) {
306
- return {
307
- ok: true,
308
- status: "accepted",
309
- source,
310
- ...(value.result !== undefined ? { result: value.result } : {}),
311
- };
312
- }
313
-
314
- return {
315
- ok: true,
316
- status: "accepted",
317
- source,
318
- ...(value !== undefined ? { result: value } : {}),
319
- };
320
- };
321
-
322
- const stripActionsFromState = <T extends AirJamNetworkedState>(
323
- data: Partial<T>,
324
- ): Partial<T> => {
325
- const { actions: _ignored, ...stateData } = data as Partial<T> & {
326
- actions?: unknown;
327
- };
328
- return stateData as Partial<T>;
329
- };
330
-
331
- const toActionContext = (
332
- payload: AirJamActionRpcPayload,
333
- connectedPlayerIds: string[],
334
- ): AirJamActionContext => ({
335
- actorId: payload.actor.id || UNRESOLVED_ACTOR_ID,
336
- role: payload.actor.role,
337
- connectedPlayerIds,
338
- });
339
-
340
- /**
341
- * Creates a networked Zustand store that syncs state from host to controllers.
342
- *
343
- * Host is the source of truth:
344
- * - Host broadcasts state updates to all controllers.
345
- * - Controllers invoke actions through RPC to host.
346
- *
347
- * `storeDomain` isolates sync and action RPC so multiple replicated stores can coexist in one room
348
- * (e.g. arcade shell vs running game). When omitted, resolves from the runtime URL: explicit
349
- * `aj_store_domain`, else embedded Arcade identity (`aj_arcade_*`), else the default domain.
350
- */
351
- export function createAirJamStore<T extends AirJamNetworkedState>(
352
- initializer: InvalidActionPayloadKeys<T["actions"]> extends never
353
- ? StateCreator<T>
354
- : StateCreator<T> & AirJamInvalidActionPayloadDiagnostic<T["actions"]>,
355
- options?: CreateAirJamStoreOptions,
356
- ): AirJamSyncedStoreHook<T> {
357
- const store = create<T>((set, get, api) => initializer(set, get, api));
358
- const runtimeSnapshotRef: { current: StoreRuntimeSnapshot } = {
359
- current: {
360
- socket: null,
361
- role: null,
362
- roomId: null,
363
- resolvedStoreDomain: AIR_JAM_DEFAULT_STORE_DOMAIN,
364
- connectionStatus: null,
365
- canBroadcastHostState: false,
366
- connectedPlayerIds: [],
367
- },
368
- };
369
- const runtimeBindings = new Map<string, StoreRuntimeBinding>();
370
- const hostActionListeners = new Set<
371
- HostActionListenerRegistration<T["actions"]>
372
- >();
373
-
374
- const createDispatchedActions = (
375
- mode:
376
- | {
377
- kind: "default";
378
- }
379
- | {
380
- kind: "player";
381
- controllerId: string;
382
- },
383
- ): AirJamActionDispatchMap<T["actions"]> => {
384
- const sourceActions = store.getState().actions;
385
- const actionDispatch = {} as AirJamActionDispatchMap<T["actions"]>;
386
-
387
- for (const actionName of Object.keys(sourceActions)) {
388
- if (isInternalActionName(actionName)) {
389
- continue;
390
- }
391
-
392
- const sourceAction = sourceActions[actionName];
393
-
394
- (
395
- actionDispatch as unknown as Record<
396
- string,
397
- (payload: unknown) => Promise<AirJamActionInvocationResult>
398
- >
399
- )[actionName] = async (
400
- payload: unknown,
401
- ): Promise<AirJamActionInvocationResult> => {
402
- const normalizedPayload = isEventLikePayload(payload)
403
- ? undefined
404
- : payload;
405
-
406
- if (normalizedPayload !== payload) {
407
- emitAirJamDiagnostic({
408
- code: "AJ_STORE_ACTION_EVENT_PAYLOAD_DROPPED",
409
- severity: "warn",
410
- message: `[AirJamStore] Action "${actionName}" received an event-like payload. Dropping payload and continuing.`,
411
- details: { actionName },
412
- });
413
- }
414
-
415
- const runtime = runtimeSnapshotRef.current;
416
-
417
- if (mode.kind === "player" && runtime.role !== "host") {
418
- emitAirJamDiagnostic({
419
- code: "AJ_STORE_PLAYER_ACTIONS_HOST_ONLY",
420
- severity: "warn",
421
- message: `[AirJamStore] Player impersonation for action "${actionName}" is host-only.`,
422
- details: {
423
- actionName,
424
- role: runtime.role,
425
- controllerId: mode.controllerId,
426
- },
427
- });
428
- return createClientRejectedActionResult(
429
- "player_actions_host_only",
430
- `Store action "${actionName}" cannot impersonate a player outside the host runtime.`,
431
- {
432
- actionName,
433
- ...(runtime.role ? { role: runtime.role } : {}),
434
- controllerId: mode.controllerId,
435
- },
436
- );
437
- }
438
-
439
- if (mode.kind === "player") {
440
- const normalizedControllerId = mode.controllerId.trim();
441
- if (normalizedControllerId.length === 0) {
442
- emitAirJamDiagnostic({
443
- code: "AJ_STORE_PLAYER_ACTIONS_INVALID_ACTOR_ID",
444
- severity: "warn",
445
- message: `[AirJamStore] Player impersonation for action "${actionName}" requires a non-empty controller id.`,
446
- details: {
447
- actionName,
448
- },
449
- });
450
- return createClientRejectedActionResult(
451
- "player_actor_id_invalid",
452
- `Store action "${actionName}" requires a non-empty controller id for player impersonation.`,
453
- {
454
- actionName,
455
- },
456
- );
457
- }
458
-
459
- if (!runtime.connectedPlayerIds.includes(normalizedControllerId)) {
460
- emitAirJamDiagnostic({
461
- code: "AJ_STORE_PLAYER_ACTIONS_PLAYER_NOT_CONNECTED",
462
- severity: "warn",
463
- message: `[AirJamStore] Player impersonation for action "${actionName}" requires a currently connected controller.`,
464
- details: {
465
- actionName,
466
- controllerId: normalizedControllerId,
467
- connectedPlayerIds: runtime.connectedPlayerIds,
468
- },
469
- });
470
- return createClientRejectedActionResult(
471
- "player_not_connected",
472
- `Store action "${actionName}" cannot impersonate controller "${normalizedControllerId}" because it is not currently connected.`,
473
- {
474
- actionName,
475
- controllerId: normalizedControllerId,
476
- },
477
- );
478
- }
479
- }
480
-
481
- if (runtime.role === "host") {
482
- try {
483
- const context =
484
- mode.kind === "player"
485
- ? ({
486
- actorId: mode.controllerId.trim(),
487
- role: "controller",
488
- connectedPlayerIds: runtime.connectedPlayerIds,
489
- } satisfies AirJamActionContext)
490
- : ({
491
- actorId: "host",
492
- role: "host",
493
- connectedPlayerIds: runtime.connectedPlayerIds,
494
- } satisfies AirJamActionContext);
495
- const result = sourceAction(context, normalizedPayload);
496
- const acknowledgement = normalizeAirJamActionInvocationResult(
497
- result,
498
- "host",
499
- );
500
- const typedActionName = actionName as AirJamActionName<
501
- T["actions"]
502
- >;
503
- notifyHostActionListeners({
504
- actionName: typedActionName,
505
- payload: normalizedPayload,
506
- context,
507
- acknowledgement,
508
- invocationKind: "local",
509
- roomId: runtime.roomId,
510
- storeDomain: runtime.resolvedStoreDomain,
511
- } as AirJamHostActionEvent<T["actions"], typeof typedActionName>);
512
- return acknowledgement;
513
- } catch (error) {
514
- const acknowledgement = {
515
- ok: false,
516
- status: "rejected",
517
- source: "host",
518
- reason: "handler_error",
519
- message: error instanceof Error ? error.message : String(error),
520
- } satisfies AirJamActionInvocationResult;
521
- const typedActionName = actionName as AirJamActionName<
522
- T["actions"]
523
- >;
524
- notifyHostActionListeners({
525
- actionName: typedActionName,
526
- payload: normalizedPayload,
527
- context: {
528
- actorId:
529
- mode.kind === "player" ? mode.controllerId.trim() : "host",
530
- role: mode.kind === "player" ? "controller" : "host",
531
- connectedPlayerIds: runtime.connectedPlayerIds,
532
- },
533
- acknowledgement,
534
- invocationKind: "local",
535
- roomId: runtime.roomId,
536
- storeDomain: runtime.resolvedStoreDomain,
537
- } as AirJamHostActionEvent<T["actions"], typeof typedActionName>);
538
- return acknowledgement;
539
- }
540
- }
541
-
542
- if (runtime.role !== "controller" || !runtime.roomId) {
543
- emitAirJamDiagnostic({
544
- code: "AJ_STORE_ACTION_SESSION_NOT_READY",
545
- severity: "warn",
546
- message: `[AirJamStore] Action "${actionName}" blocked: session role not ready.`,
547
- details: {
548
- actionName,
549
- role: runtime.role,
550
- roomId: runtime.roomId,
551
- },
552
- });
553
- return createClientRejectedActionResult(
554
- "session_not_ready",
555
- `Store action "${actionName}" cannot run because the controller session is not ready.`,
556
- {
557
- actionName,
558
- ...(runtime.role ? { role: runtime.role } : {}),
559
- ...(runtime.roomId ? { roomId: runtime.roomId } : {}),
560
- },
561
- );
562
- }
563
-
564
- const activeSocket = runtime.socket;
565
- if (!activeSocket || !activeSocket.connected) {
566
- emitAirJamDiagnostic({
567
- code: "AJ_STORE_ACTION_SOCKET_DISCONNECTED",
568
- severity: "warn",
569
- message: `[AirJamStore] Action "${actionName}" blocked: Socket disconnected.`,
570
- details: { actionName },
571
- });
572
- return createClientRejectedActionResult(
573
- "socket_disconnected",
574
- `Store action "${actionName}" cannot run because the controller socket is disconnected.`,
575
- {
576
- actionName,
577
- },
578
- );
579
- }
580
-
581
- if (!isRpcSerializable(normalizedPayload)) {
582
- emitAirJamDiagnostic({
583
- code: "AJ_STORE_ACTION_PAYLOAD_NOT_SERIALIZABLE",
584
- severity: "warn",
585
- message: `[AirJamStore] Action "${actionName}" blocked: payload must be RPC-serializable.`,
586
- details: { actionName },
587
- });
588
- return createClientRejectedActionResult(
589
- "payload_not_serializable",
590
- `Store action "${actionName}" requires an RPC-serializable payload.`,
591
- {
592
- actionName,
593
- },
594
- );
595
- }
596
-
597
- if (
598
- normalizedPayload !== undefined &&
599
- !isPlainActionPayload(normalizedPayload)
600
- ) {
601
- emitAirJamDiagnostic({
602
- code: "AJ_STORE_ACTION_PAYLOAD_INVALID_SHAPE",
603
- severity: "warn",
604
- message: `[AirJamStore] Action "${actionName}" blocked: payload must be omitted or a plain object.`,
605
- details: { actionName },
606
- });
607
- return createClientRejectedActionResult(
608
- "payload_invalid_shape",
609
- `Store action "${actionName}" requires an omitted payload or a plain object payload.`,
610
- {
611
- actionName,
612
- },
613
- );
614
- }
615
-
616
- return await activeSocket.emitWithAck<AirJamActionInvocationResult>(
617
- "controller:action_rpc",
618
- {
619
- roomId: runtime.roomId,
620
- actionName,
621
- payload: normalizedPayload,
622
- storeDomain: runtime.resolvedStoreDomain,
623
- },
624
- );
625
- };
626
- }
627
-
628
- return actionDispatch;
629
- };
630
-
631
- const notifyHostActionListeners = <
632
- TActionName extends AirJamActionName<T["actions"]>,
633
- >(
634
- event: AirJamHostActionEvent<T["actions"], TActionName>,
635
- ): void => {
636
- for (const registration of hostActionListeners) {
637
- if (
638
- registration.actionNames &&
639
- !registration.actionNames.has(event.actionName)
640
- ) {
641
- continue;
642
- }
643
-
644
- if (
645
- !registration.includeRejected &&
646
- event.acknowledgement.status === "rejected"
647
- ) {
648
- continue;
649
- }
650
-
651
- try {
652
- registration.listener(event);
653
- } catch (error) {
654
- console.error(
655
- `[AirJamStore] Host action listener for "${event.actionName}" threw.`,
656
- error,
657
- );
658
- }
659
- }
660
- };
661
-
662
- const createRuntimeBinding = ({
663
- socket,
664
- role,
665
- roomId,
666
- resolvedStoreDomain,
667
- }: {
668
- socket: AirJamRealtimeClient;
669
- role: AirJamActionActorRole;
670
- roomId: string;
671
- resolvedStoreDomain: string;
672
- }): StoreRuntimeBinding => {
673
- if (role === "host") {
674
- let syncedStateData = stripActionsFromState(store.getState());
675
- let syncedStateSignature = JSON.stringify(syncedStateData);
676
- let syncRevision = 0;
677
-
678
- const emitHostStateSync = (requestId?: string): void => {
679
- const runtime = runtimeSnapshotRef.current;
680
- if (
681
- runtime.role !== "host" ||
682
- runtime.roomId !== roomId ||
683
- runtime.resolvedStoreDomain !== resolvedStoreDomain ||
684
- !runtime.canBroadcastHostState ||
685
- !runtime.socket
686
- ) {
687
- return;
688
- }
689
-
690
- runtime.socket.emit("host:state_sync", {
691
- roomId,
692
- data: syncedStateData,
693
- storeDomain: resolvedStoreDomain,
694
- revision: syncRevision,
695
- ...(requestId ? { requestId } : {}),
696
- });
697
- };
698
-
699
- const flushHostStateSync = (requestId?: string): void => {
700
- emitHostStateSync(requestId);
701
- };
702
-
703
- const handleControllerJoined = (): void => {
704
- flushHostStateSync();
705
- };
706
-
707
- const unsubscribe = store.subscribe((newState) => {
708
- const runtime = runtimeSnapshotRef.current;
709
- if (
710
- runtime.role !== "host" ||
711
- runtime.roomId !== roomId ||
712
- runtime.resolvedStoreDomain !== resolvedStoreDomain ||
713
- !runtime.canBroadcastHostState ||
714
- !runtime.socket
715
- ) {
716
- return;
717
- }
718
-
719
- const stateData = stripActionsFromState(newState);
720
- const nextSignature = JSON.stringify(stateData);
721
- if (nextSignature === syncedStateSignature) {
722
- return;
723
- }
724
-
725
- syncedStateData = stateData;
726
- syncedStateSignature = nextSignature;
727
- syncRevision += 1;
728
- emitHostStateSync();
729
- });
730
-
731
- const handleAction = (
732
- payload: AirJamActionRpcPayload,
733
- callback?: (ack: AirJamActionInvocationResult) => void,
734
- ) => {
735
- if (payload.storeDomain !== resolvedStoreDomain) {
736
- return;
737
- }
738
- const { actionName } = payload;
739
- if (isInternalActionName(actionName)) {
740
- return;
741
- }
742
-
743
- const runtime = runtimeSnapshotRef.current;
744
- const actionFn = store.getState().actions[actionName];
745
- if (typeof actionFn !== "function") {
746
- callback?.(
747
- createServerRejectedActionResult(
748
- "action_not_found",
749
- `Store action "${actionName}" is not registered on the host.`,
750
- {
751
- actionName,
752
- storeDomain: resolvedStoreDomain,
753
- },
754
- ),
755
- );
756
- return;
757
- }
758
-
759
- try {
760
- const context = toActionContext(payload, runtime.connectedPlayerIds);
761
- const result = actionFn(context, payload.payload);
762
- const acknowledgement = normalizeAirJamActionInvocationResult(
763
- result,
764
- "host",
765
- );
766
- const actionName = payload.actionName as AirJamActionName<
767
- T["actions"]
768
- >;
769
- notifyHostActionListeners({
770
- actionName,
771
- payload: payload.payload,
772
- context,
773
- acknowledgement,
774
- invocationKind: "rpc",
775
- roomId,
776
- storeDomain: resolvedStoreDomain,
777
- } as AirJamHostActionEvent<T["actions"], typeof actionName>);
778
- callback?.(acknowledgement);
779
- } catch (error) {
780
- const acknowledgement = {
781
- ok: false,
782
- status: "rejected",
783
- source: "host",
784
- reason: "handler_error",
785
- message: error instanceof Error ? error.message : String(error),
786
- } satisfies AirJamActionInvocationResult;
787
- const actionName = payload.actionName as AirJamActionName<
788
- T["actions"]
789
- >;
790
- notifyHostActionListeners({
791
- actionName,
792
- payload: payload.payload,
793
- context: toActionContext(payload, runtime.connectedPlayerIds),
794
- acknowledgement,
795
- invocationKind: "rpc",
796
- roomId,
797
- storeDomain: resolvedStoreDomain,
798
- } as AirJamHostActionEvent<T["actions"], typeof actionName>);
799
- callback?.(acknowledgement);
800
- }
801
- };
802
-
803
- const handleStateSyncRequest = (payload: {
804
- roomId: string;
805
- storeDomain: string;
806
- requestId?: string;
807
- }): void => {
808
- if (payload.roomId !== roomId) {
809
- return;
810
- }
811
- if (payload.storeDomain !== resolvedStoreDomain) {
812
- return;
813
- }
814
- flushHostStateSync(payload.requestId);
815
- };
816
-
817
- const handleSync = (payload: AirJamStateSyncPayload) => {
818
- if (payload.roomId !== roomId) {
819
- return;
820
- }
821
- if (payload.storeDomain !== resolvedStoreDomain) {
822
- return;
823
- }
824
- if (payload.revision < syncRevision) {
825
- return;
826
- }
827
-
828
- const nextStateData = stripActionsFromState(payload.data as Partial<T>);
829
- const nextSignature = JSON.stringify(nextStateData);
830
- const currentSignature = JSON.stringify(
831
- stripActionsFromState(store.getState()),
832
- );
833
- syncRevision = payload.revision;
834
- syncedStateData = nextStateData;
835
- syncedStateSignature = nextSignature;
836
-
837
- if (nextSignature === currentSignature) {
838
- return;
839
- }
840
-
841
- store.setState(nextStateData);
842
- };
843
-
844
- socket.on("server:controllerJoined", handleControllerJoined);
845
- socket.on("airjam:state_sync", handleSync);
846
- socket.on("airjam:state_sync_request", handleStateSyncRequest);
847
- socket.on("airjam:action_rpc", handleAction);
848
-
849
- return {
850
- refCount: 0,
851
- flushHostStateSync,
852
- cleanup: () => {
853
- unsubscribe();
854
- socket.off("server:controllerJoined", handleControllerJoined);
855
- socket.off("airjam:state_sync", handleSync);
856
- socket.off("airjam:state_sync_request", handleStateSyncRequest);
857
- socket.off("airjam:action_rpc", handleAction);
858
- },
859
- };
860
- }
861
-
862
- let latestSyncRevision = -1;
863
-
864
- const handleSync = (payload: AirJamStateSyncPayload) => {
865
- if (payload.storeDomain !== resolvedStoreDomain) {
866
- return;
867
- }
868
- if (payload.revision < latestSyncRevision) {
869
- return;
870
- }
871
- latestSyncRevision = payload.revision;
872
- const stateData = stripActionsFromState(payload.data as Partial<T>);
873
- store.setState(stateData);
874
- };
875
-
876
- socket.on("airjam:state_sync", handleSync);
877
-
878
- if (runtimeSnapshotRef.current.connectionStatus === "connected") {
879
- socket.emit("controller:state_sync_request", {
880
- roomId,
881
- storeDomain: resolvedStoreDomain,
882
- });
883
- }
884
-
885
- return {
886
- refCount: 0,
887
- cleanup: () => {
888
- socket.off("airjam:state_sync", handleSync);
889
- },
890
- };
891
- };
892
-
893
- const useSyncedStore = <U>(
894
- selector: (state: T) => U = (state) => state as unknown as U,
895
- ): U => {
896
- const slice = store(selector);
897
-
898
- const explicitDomain = options?.storeDomain;
899
- let resolvedStoreDomain: string;
900
- if (
901
- typeof explicitDomain === "string" &&
902
- explicitDomain.trim().length > 0
903
- ) {
904
- const trimmed = explicitDomain.trim();
905
- resolvedStoreDomain =
906
- trimmed.length > 128 ? trimmed.slice(0, 128) : trimmed;
907
- } else {
908
- resolvedStoreDomain = resolveImplicitReplicatedStoreDomainFromWindow();
909
- }
910
-
911
- const { getSocket } = useAirJamContext();
912
- const role = useAirJamState((state) => state.role);
913
- const roomId = useAirJamState((state) => state.roomId);
914
- const registeredRoomId = useAirJamState((state) => state.registeredRoomId);
915
- const connectionStatus = useAirJamState((state) => state.connectionStatus);
916
- const players = useAirJamState((state) => state.players);
917
- const hostArcadeRestore = useAirJamState(
918
- (state) => state.hostArcadeRestore,
919
- );
920
- const blockArcadeShellHostBroadcast =
921
- resolvedStoreDomain === AIR_JAM_ARCADE_SURFACE_STORE_DOMAIN &&
922
- hostArcadeRestore.phase !== "idle";
923
- const canBroadcastHostState =
924
- role === "host" &&
925
- !!roomId &&
926
- registeredRoomId === roomId &&
927
- !blockArcadeShellHostBroadcast;
928
- const connectedPlayerIds = useMemo(
929
- () => Array.from(new Set(players.map((player) => player.id))).sort(),
930
- [players],
931
- );
932
- const playerRosterKey = useMemo(
933
- () =>
934
- players
935
- .map((player) => player.id)
936
- .sort()
937
- .join(","),
938
- [players],
939
- );
940
- const socket: AirJamRealtimeClient =
941
- role === "host"
942
- ? getHostRealtimeClient((runtimeRole) => getSocket(runtimeRole))
943
- : getControllerRealtimeClient((runtimeRole) => getSocket(runtimeRole));
944
-
945
- runtimeSnapshotRef.current = {
946
- socket,
947
- role,
948
- roomId,
949
- resolvedStoreDomain,
950
- connectionStatus,
951
- canBroadcastHostState,
952
- connectedPlayerIds,
953
- };
954
-
955
- const runtimeBindingKey =
956
- socket && roomId && role
957
- ? [
958
- role,
959
- roomId,
960
- resolvedStoreDomain,
961
- socket.id ?? "socket",
962
- role === "controller" ? connectionStatus : "host",
963
- ].join(":")
964
- : null;
965
-
966
- useEffect(() => {
967
- if (!runtimeBindingKey || !socket || !roomId || !role) {
968
- return;
969
- }
970
-
971
- let binding = runtimeBindings.get(runtimeBindingKey);
972
- if (!binding) {
973
- binding = createRuntimeBinding({
974
- socket,
975
- role,
976
- roomId,
977
- resolvedStoreDomain,
978
- });
979
- runtimeBindings.set(runtimeBindingKey, binding);
980
- }
981
-
982
- binding.refCount += 1;
983
-
984
- return () => {
985
- const currentBinding = runtimeBindings.get(runtimeBindingKey);
986
- if (!currentBinding) {
987
- return;
988
- }
989
-
990
- currentBinding.refCount -= 1;
991
- if (currentBinding.refCount > 0) {
992
- return;
993
- }
994
-
995
- currentBinding.cleanup();
996
- runtimeBindings.delete(runtimeBindingKey);
997
- };
998
- }, [
999
- runtimeBindingKey,
1000
- socket,
1001
- role,
1002
- roomId,
1003
- socket?.id,
1004
- resolvedStoreDomain,
1005
- ]);
1006
-
1007
- useEffect(() => {
1008
- if (
1009
- !runtimeBindingKey ||
1010
- role !== "host" ||
1011
- !canBroadcastHostState ||
1012
- !roomId
1013
- ) {
1014
- return;
1015
- }
1016
-
1017
- const binding = runtimeBindings.get(runtimeBindingKey);
1018
- if (!binding?.flushHostStateSync) {
1019
- return;
1020
- }
1021
-
1022
- const flushKey = [
1023
- roomId,
1024
- registeredRoomId,
1025
- socket?.id ?? "socket",
1026
- resolvedStoreDomain,
1027
- playerRosterKey,
1028
- canBroadcastHostState ? "broadcast" : "blocked",
1029
- ].join(":");
1030
-
1031
- if (binding.lastHostFlushKey === flushKey) {
1032
- return;
1033
- }
1034
-
1035
- binding.lastHostFlushKey = flushKey;
1036
- binding.flushHostStateSync();
1037
- }, [
1038
- role,
1039
- roomId,
1040
- registeredRoomId,
1041
- socket?.id,
1042
- playerRosterKey,
1043
- resolvedStoreDomain,
1044
- canBroadcastHostState,
1045
- runtimeBindingKey,
1046
- ]);
1047
-
1048
- const dispatchedActions = useMemo<AirJamActionDispatchMap<T["actions"]>>(
1049
- () => createDispatchedActions({ kind: "default" }),
1050
- [],
1051
- );
1052
-
1053
- const stateActions = store.getState().actions;
1054
-
1055
- if (slice === stateActions) {
1056
- return dispatchedActions as U;
1057
- }
1058
-
1059
- if (
1060
- typeof slice === "object" &&
1061
- slice !== null &&
1062
- "actions" in slice &&
1063
- (slice as { actions: unknown }).actions === stateActions
1064
- ) {
1065
- return {
1066
- ...(slice as Record<string, unknown>),
1067
- actions: dispatchedActions,
1068
- } as U;
1069
- }
1070
-
1071
- return slice;
1072
- };
1073
-
1074
- const syncedStore = useSyncedStore as AirJamSyncedStoreHook<T>;
1075
- const useActions = (): AirJamActionDispatchMap<T["actions"]> =>
1076
- useSyncedStore((state) => state.actions) as AirJamActionDispatchMap<
1077
- T["actions"]
1078
- >;
1079
- const asPlayer = (
1080
- controllerId: string,
1081
- ): AirJamActionDispatchMap<T["actions"]> =>
1082
- createDispatchedActions({
1083
- kind: "player",
1084
- controllerId,
1085
- });
1086
- const subscribeHostActions = (
1087
- listener: AirJamHostActionListener<T["actions"]>,
1088
- options?: AirJamHostActionSubscriptionOptions<T["actions"]>,
1089
- ): (() => void) => {
1090
- const actionNames =
1091
- options?.actionNames && options.actionNames.length > 0
1092
- ? new Set<string>(options.actionNames)
1093
- : null;
1094
- const registration: HostActionListenerRegistration<T["actions"]> = {
1095
- actionNames,
1096
- includeRejected: options?.includeRejected ?? false,
1097
- listener,
1098
- };
1099
- hostActionListeners.add(registration);
1100
- return () => {
1101
- hostActionListeners.delete(registration);
1102
- };
1103
- };
1104
- const useHostActionListener = (
1105
- listener: AirJamHostActionListener<T["actions"]>,
1106
- options?: AirJamHostActionSubscriptionOptions<T["actions"]>,
1107
- ): void => {
1108
- const listenerRef = useRef(listener);
1109
- useEffect(() => {
1110
- listenerRef.current = listener;
1111
- }, [listener]);
1112
-
1113
- const includeRejected = options?.includeRejected ?? false;
1114
- const actionNamesKey = options?.actionNames?.join("\u0000") ?? "";
1115
-
1116
- useEffect(() => {
1117
- return subscribeHostActions((event) => {
1118
- listenerRef.current(event);
1119
- }, options);
1120
- }, [actionNamesKey, includeRejected, options]);
1121
- };
1122
- const useLiveStateRef = (): MutableRefObject<T> => {
1123
- const latestState = useSyncedStore((state) => state);
1124
- const stateRef = useRef<T>(latestState);
1125
-
1126
- useEffect(() => {
1127
- stateRef.current = latestState;
1128
- }, [latestState]);
1129
-
1130
- return stateRef;
1131
- };
1132
- syncedStore.useActions = useActions;
1133
- syncedStore.asPlayer = asPlayer;
1134
- syncedStore.getState = store.getState;
1135
- syncedStore.subscribe = store.subscribe;
1136
- syncedStore.subscribeHostActions = subscribeHostActions;
1137
- syncedStore.useHostActionListener = useHostActionListener;
1138
- syncedStore.useLiveStateRef = useLiveStateRef;
1139
-
1140
- return syncedStore;
1141
- }