@threenative/core 0.3.4 → 0.3.5

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 (176) hide show
  1. package/capabilities.json +1505 -79
  2. package/dist/{audio-7i3Xl0l3.d.ts → audio-DIJE5HTr.d.ts} +43 -3
  3. package/dist/{canvas-layer-DDmC_VVF.d.ts → canvas-layer-C1-5CviZ.d.ts} +5 -1
  4. package/dist/{game-CljaDv4D.d.ts → game-Bj8G1wmD.d.ts} +102 -11
  5. package/dist/hot.d.ts +4 -4
  6. package/dist/index.d.ts +712 -118
  7. package/dist/index.js +12423 -4955
  8. package/dist/{gpu-readback-CqJEfWNQ.d.ts → model-lod-BDAUYgsS.d.ts} +80 -5
  9. package/dist/playtest.d.ts +6 -10
  10. package/dist/playtest.js +47 -24
  11. package/dist/react-css.d.ts +62 -0
  12. package/dist/react-css.js +591 -0
  13. package/dist/react.d.ts +2 -2
  14. package/dist/{renderer-CfsS2hxi.d.ts → renderer-9yEo9hPa.d.ts} +489 -22
  15. package/dist/terrain-jobs-worker.d.ts +2 -0
  16. package/dist/terrain-jobs-worker.js +130 -0
  17. package/dist/ui-layer.js +59 -8
  18. package/dist/world.d.ts +766 -73
  19. package/dist/world.js +9307 -3543
  20. package/gpl/recipes/export_world.py +18 -1
  21. package/mcp/blender-server.mjs +7 -4
  22. package/mcp/blender.mjs +3 -4
  23. package/mcp/engine-server.mjs +2 -2
  24. package/mcp/servers.mjs +4 -4
  25. package/package.json +15 -9
  26. package/patches/three@0.185.1-prd269-upgrade.patch +22418 -0
  27. package/patches/three@0.185.1.patch +6594 -700
  28. package/scripts/apply-three-patch.mjs +62 -4
  29. package/src/animation-blend.ts +264 -0
  30. package/src/animation-composition.ts +362 -0
  31. package/src/animation-layers.ts +260 -0
  32. package/src/animation-root-motion.ts +283 -0
  33. package/src/animation.ts +896 -0
  34. package/src/assets.ts +1303 -0
  35. package/src/atmosphere/index.ts +335 -0
  36. package/src/atmosphere/luts.ts +298 -0
  37. package/src/atmosphere/params.ts +323 -0
  38. package/src/audio.ts +912 -0
  39. package/src/billboard.ts +124 -0
  40. package/src/bone-lengths.ts +157 -0
  41. package/src/camera-shake.ts +116 -0
  42. package/src/canvas-layer.ts +47 -0
  43. package/src/clip-audit.ts +401 -0
  44. package/src/clustered-batch.ts +492 -0
  45. package/src/clustered-mesh.ts +638 -0
  46. package/src/compute-driven.ts +176 -0
  47. package/src/compute-timing.ts +318 -0
  48. package/src/config.ts +593 -0
  49. package/src/debug.ts +82 -0
  50. package/src/engine-render-hook.ts +25 -0
  51. package/src/entities.ts +79 -0
  52. package/src/entity-snapshot.ts +53 -0
  53. package/src/flight.ts +683 -0
  54. package/src/fluid-field.ts +748 -0
  55. package/src/fluid-particles.ts +1253 -0
  56. package/src/frame-budget.ts +1368 -0
  57. package/src/game.ts +2390 -0
  58. package/src/geometry-capture.ts +1098 -0
  59. package/src/gpu-class.ts +160 -0
  60. package/src/gpu-frame-observation.ts +424 -0
  61. package/src/gpu-readback.ts +143 -0
  62. package/src/gpu-scene-bvh.ts +464 -0
  63. package/src/grounding.ts +100 -0
  64. package/src/hot.ts +161 -0
  65. package/src/import-meta.d.ts +13 -0
  66. package/src/index.ts +1536 -0
  67. package/src/input.ts +690 -0
  68. package/src/instanced-batch-lod.ts +283 -0
  69. package/src/instanced-batch.ts +274 -0
  70. package/src/interpolation.ts +129 -0
  71. package/src/launch-diagnostics.ts +161 -0
  72. package/src/loop.ts +564 -0
  73. package/src/matrix-world.ts +245 -0
  74. package/src/merge-parts.ts +812 -0
  75. package/src/model-lod.ts +1315 -0
  76. package/src/net-protocol.ts +333 -0
  77. package/src/net-session.ts +477 -0
  78. package/src/net.ts +80 -0
  79. package/src/ocean/fft.ts +72 -0
  80. package/src/ocean/spectral.ts +728 -0
  81. package/src/particle-significance.ts +102 -0
  82. package/src/particles.ts +648 -0
  83. package/src/path-follow.ts +175 -0
  84. package/src/picking.ts +250 -0
  85. package/src/pipeline-census.ts +1566 -0
  86. package/src/platform.ts +210 -0
  87. package/src/playtest.ts +839 -0
  88. package/src/pointer-events.ts +411 -0
  89. package/src/pose-measure.ts +435 -0
  90. package/src/profiling/FrameCounters.ts +231 -0
  91. package/src/profiling/Spans.ts +548 -0
  92. package/src/profiling/opacity-convention.ts +21 -0
  93. package/src/profiling/render-list-validate.ts +175 -0
  94. package/src/profiling/render-trap-audit.ts +84 -0
  95. package/src/profiling/scene-warning.ts +190 -0
  96. package/src/profiling/span-probes.ts +191 -0
  97. package/src/profiling/unlit-foliage-warning.ts +149 -0
  98. package/src/projection-apply.ts +1529 -0
  99. package/src/projection-marker.ts +142 -0
  100. package/src/projection-plan.ts +1258 -0
  101. package/src/projection-skinned.ts +419 -0
  102. package/src/projection-stability.ts +409 -0
  103. package/src/projection-uniform.ts +328 -0
  104. package/src/random.ts +41 -0
  105. package/src/react-css.ts +604 -0
  106. package/src/react-glyphs.ts +114 -0
  107. package/src/react-host.ts +576 -0
  108. package/src/react-layout.ts +265 -0
  109. package/src/react.ts +60 -0
  110. package/src/render/alpha-antialiasing.ts +159 -0
  111. package/src/render/batched-velocity.ts +126 -0
  112. package/src/render/chain.ts +1021 -0
  113. package/src/render/daylight.ts +189 -0
  114. package/src/render/depth-pyramid.ts +596 -0
  115. package/src/render/foliage-alpha.ts +166 -0
  116. package/src/render/material-key.ts +60 -0
  117. package/src/render/mesh-pool.ts +144 -0
  118. package/src/render/probe-volume.ts +1668 -0
  119. package/src/render/resolution-subrect.ts +253 -0
  120. package/src/render/velocity.ts +442 -0
  121. package/src/render/virtual-shadow-pages.ts +827 -0
  122. package/src/render/virtual-shadow-partial.ts +174 -0
  123. package/src/render/virtual-shadow.ts +3330 -0
  124. package/src/render/world-impostor-surface.ts +722 -0
  125. package/src/render/world-impostor.ts +558 -0
  126. package/src/render-camera-cull.ts +441 -0
  127. package/src/render-pass-budget.ts +501 -0
  128. package/src/renderProjection.ts +758 -0
  129. package/src/renderer-config.ts +127 -0
  130. package/src/renderer.ts +1847 -0
  131. package/src/replay-protocol.ts +85 -0
  132. package/src/replay.ts +205 -0
  133. package/src/resolution-scaler.ts +819 -0
  134. package/src/rig-preparation.ts +271 -0
  135. package/src/ripple-field.ts +432 -0
  136. package/src/scale.ts +109 -0
  137. package/src/scene.ts +239 -0
  138. package/src/schedule.ts +155 -0
  139. package/src/skeletal-mesh.ts +61 -0
  140. package/src/skeleton.ts +100 -0
  141. package/src/snow-field.ts +744 -0
  142. package/src/softbody-topology.ts +269 -0
  143. package/src/softbody.ts +516 -0
  144. package/src/sprite-animator.ts +222 -0
  145. package/src/startup-readiness.ts +386 -0
  146. package/src/state.ts +66 -0
  147. package/src/static-transform.ts +274 -0
  148. package/src/storage-buffer.ts +253 -0
  149. package/src/streaming.ts +308 -0
  150. package/src/target-fps.ts +92 -0
  151. package/src/terrain-jobs-worker.ts +17 -0
  152. package/src/terrain-jobs.ts +353 -0
  153. package/src/textures.ts +39 -0
  154. package/src/tracers.ts +237 -0
  155. package/src/ui-bridge.ts +368 -0
  156. package/src/ui-hit-regions.ts +380 -0
  157. package/src/ui-layer.ts +83 -0
  158. package/src/ui-state.ts +220 -0
  159. package/src/version.ts +2 -0
  160. package/src/viewport.ts +253 -0
  161. package/src/warmup.ts +1357 -0
  162. package/src/water-surface.ts +384 -0
  163. package/src/wave-field.ts +524 -0
  164. package/src/world-capabilities.ts +131 -0
  165. package/src/world-cell-proxy.ts +392 -0
  166. package/src/world-cells.ts +10564 -0
  167. package/src/world-gpu-scene.ts +3354 -0
  168. package/src/world-heightmap.ts +82 -0
  169. package/src/world-package.ts +469 -0
  170. package/src/world-passes.ts +582 -0
  171. package/src/world-region.ts +17 -0
  172. package/src/world-terrain-splat.ts +564 -0
  173. package/src/world-tiles.ts +3826 -0
  174. package/src/world-topology.ts +370 -0
  175. package/src/world-validate.ts +48 -0
  176. package/src/world.ts +805 -0
@@ -0,0 +1,368 @@
1
+ /**
2
+ * The UI bridge — one message channel between the game and the UI, identical on every host.
3
+ *
4
+ * The UI runs in the platform's own browser-class renderer (a `WebView` on Android, a
5
+ * `WKWebView` on iOS, a child web view on desktop) while the game runs in the native
6
+ * runtime beside it. They are two JavaScript realms in, usually, two processes, so
7
+ * everything that crosses is a message.
8
+ *
9
+ * Every host already offers a `MessagePort`-shaped primitive — `addWebMessageListener` on
10
+ * Android, `WKScriptMessageHandler` on iOS, an IPC handler on desktop — so this file adopts
11
+ * that shape rather than inventing one, and each host only has to fill in two slots:
12
+ *
13
+ * - **outbound**, a function that takes one JSON string;
14
+ * - **inbound**, a global the host calls with one JSON string.
15
+ *
16
+ * On the web target there is no host and no second realm: the game and the UI are the same
17
+ * page, so both ends connect to an in-process broker and the same `src/ui/` code runs
18
+ * unchanged. That is the whole point — the transport differs, the protocol does not.
19
+ *
20
+ * Fail closed: a message that is not a JSON object with a string `type` throws on the way
21
+ * out, and a malformed frame throws on the way in. A dropped message is a UI that silently
22
+ * stops updating, which is the failure this refuses to have.
23
+ */
24
+
25
+ /** Which side of the bridge a caller is on. `ui` runs in the web view; `game` in the runtime. */
26
+ export type UiBridgeEnd = "game" | "ui";
27
+
28
+ /** How a connected bridge actually moves bytes. Reported, never chosen by a game. */
29
+ export type UiBridgeTransport = "host" | "in-process";
30
+
31
+ /** Every frame on the bridge is a JSON object whose `type` names it. */
32
+ export interface IUiMessage {
33
+ readonly type: string;
34
+ readonly [key: string]: unknown;
35
+ }
36
+
37
+ export interface IUiBridge {
38
+ /** Which end this handle is. */
39
+ readonly end: UiBridgeEnd;
40
+ /** `host` when a platform channel was found, `in-process` on the web target. */
41
+ readonly transport: UiBridgeTransport;
42
+ /**
43
+ * Whether anything is listening on the other end.
44
+ *
45
+ * A game whose UI renderer is `native` has no peer and never will, and publishing state to
46
+ * nobody is a JSON serialisation of the whole store several times a second for no reader.
47
+ * Reported rather than assumed: on a host transport this asks the host, so it also answers
48
+ * "did the overlay actually come up" honestly.
49
+ */
50
+ hasPeer(): boolean;
51
+ /** Send one message to the other end. Throws if it is not a typed JSON object. */
52
+ post(message: IUiMessage): void;
53
+ /** Listen for messages from the other end. Returns an unsubscribe function. */
54
+ onMessage(listener: (message: IUiMessage) => void): () => void;
55
+ /** Detach every listener and release the host slots this end installed. */
56
+ close(): void;
57
+ }
58
+
59
+ /** The message the UI end publishes whenever its interactive rectangles move. */
60
+ export const HIT_REGIONS_MESSAGE = "tn:hit-regions";
61
+ /** The message the game end publishes when its shared state changes. */
62
+ export const GAME_STATE_MESSAGE = "tn:state";
63
+ /** The message the UI end sends when the player acts on a control. */
64
+ export const UI_INTENT_MESSAGE = "tn:intent";
65
+ /**
66
+ * The scene-shape verdict the game end publishes when it was launched in dev mode.
67
+ *
68
+ * Separate from the state stream because it is not game state: it is the engine's own measurement
69
+ * of the scene, and a game that never asked for dev mode never sends it. No frame rate rides it —
70
+ * the loop's rAF rate reads throttled under a compositor or a virtual display, so a number drawn
71
+ * from it lies in exactly the sessions where somebody is trying to measure.
72
+ */
73
+ export const UI_DEV_METRICS_MESSAGE = "tn:dev-metrics";
74
+ /**
75
+ * The intent the UI layer sends once, when its tree has rendered and its rectangles are published.
76
+ *
77
+ * Namespaced so it cannot collide with a game's own vocabulary, and framework-sent so a game does
78
+ * not have to remember to announce itself. It is what lets "the overlay never came up" be told
79
+ * apart from "the game has no HUD" — two states that look identical in a screenshot.
80
+ */
81
+ export const UI_READY_INTENT = "tn:ready";
82
+
83
+ /**
84
+ * The globals each host fills in. Named here so a host implementation and this file cannot
85
+ * drift: an Android, iOS or desktop host that writes different names has no bridge at all,
86
+ * and a typo is a silent dead channel rather than a build error.
87
+ */
88
+ export const UI_BRIDGE_GLOBALS = {
89
+ /** UI end, inbound: the host calls this with one JSON string. */
90
+ uiReceive: "__tnUiReceive",
91
+ /** UI end, outbound: an injected object with `postMessage(string)`. */
92
+ uiHost: "tnHost",
93
+ /** Game end, inbound: the runtime calls this with one JSON string. */
94
+ gameReceive: "__tnUiGameReceive",
95
+ /** Game end, outbound: the runtime installs this and forwards to the web view. */
96
+ gamePost: "__tnUiPost",
97
+ } as const;
98
+
99
+ /**
100
+ * The realm a bridge end installs into.
101
+ *
102
+ * Indexed rather than typed as `Window`, because the host globals it looks for are ones no
103
+ * TypeScript DOM library has heard of and the tests drive it with a plain object.
104
+ */
105
+ interface IScope {
106
+ [key: string]: unknown;
107
+ }
108
+
109
+ interface IConnectOptions {
110
+ readonly end: UiBridgeEnd;
111
+ /** The realm to install into. Defaults to `globalThis`; injected by tests and by hosts. */
112
+ readonly scope?: IScope;
113
+ }
114
+
115
+ // Prepared state frames stay internal; ordinary posts always validate and serialise themselves.
116
+ const stateFrames = new WeakMap<IUiMessage, string>();
117
+
118
+ /** @internal Serialise the complete envelope to preserve JSON's `toJSON` key semantics. */
119
+ export function serializeUiState(state: unknown): string {
120
+ return assertSendable({ type: GAME_STATE_MESSAGE, state });
121
+ }
122
+
123
+ /** @internal Reuse the publisher's frame without extending the public bridge interface. */
124
+ export function postUiState(bridge: IUiBridge, state: unknown, frame: string): void {
125
+ const message = { type: GAME_STATE_MESSAGE, state };
126
+ stateFrames.set(message, frame);
127
+ try {
128
+ bridge.post(message);
129
+ } finally {
130
+ stateFrames.delete(message);
131
+ }
132
+ }
133
+
134
+ function assertSendable(message: IUiMessage): string {
135
+ if (typeof message !== "object" || message === null || Array.isArray(message)) {
136
+ throw new Error("TN_UI_BRIDGE_MESSAGE_INVALID: a bridge message must be a JSON object.");
137
+ }
138
+ if (typeof message.type !== "string" || message.type.length === 0) {
139
+ throw new Error("TN_UI_BRIDGE_MESSAGE_INVALID: a bridge message must carry a string 'type'.");
140
+ }
141
+ let frame: string;
142
+ try {
143
+ frame = stateFrames.get(message) ?? JSON.stringify(message);
144
+ } catch (error) {
145
+ throw new Error(
146
+ `TN_UI_BRIDGE_MESSAGE_INVALID: '${message.type}' is not JSON-serialisable: ${
147
+ error instanceof Error ? error.message : String(error)
148
+ }`,
149
+ );
150
+ }
151
+ if (frame === undefined) {
152
+ throw new Error(`TN_UI_BRIDGE_MESSAGE_INVALID: '${message.type}' serialised to nothing.`);
153
+ }
154
+ return frame;
155
+ }
156
+
157
+ function parseFrame(frame: unknown): IUiMessage {
158
+ const text = typeof frame === "string" ? frame : (frame as { data?: unknown })?.data;
159
+ if (typeof text !== "string") {
160
+ throw new Error("TN_UI_BRIDGE_FRAME_INVALID: expected a JSON string from the host.");
161
+ }
162
+ let parsed: unknown;
163
+ try {
164
+ parsed = JSON.parse(text);
165
+ } catch (error) {
166
+ throw new Error(
167
+ `TN_UI_BRIDGE_FRAME_INVALID: not JSON: ${error instanceof Error ? error.message : String(error)}`,
168
+ );
169
+ }
170
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
171
+ throw new Error("TN_UI_BRIDGE_FRAME_INVALID: a bridge frame must be a JSON object.");
172
+ }
173
+ if (typeof (parsed as IUiMessage).type !== "string") {
174
+ throw new Error("TN_UI_BRIDGE_FRAME_INVALID: a bridge frame must carry a string 'type'.");
175
+ }
176
+ return parsed as IUiMessage;
177
+ }
178
+
179
+ /**
180
+ * The outbound half, per end. Every host shape reduces to one function taking a JSON string,
181
+ * which is what lets the sibling-layer hosts and an offscreen-texture host share this file.
182
+ */
183
+ function findOutbound(scope: IScope, end: UiBridgeEnd): ((frame: string) => void) | undefined {
184
+ if (end === "game") {
185
+ const post = scope[UI_BRIDGE_GLOBALS.gamePost];
186
+ return typeof post === "function" ? (frame) => (post as (f: string) => void)(frame) : undefined;
187
+ }
188
+ const injected = scope[UI_BRIDGE_GLOBALS.uiHost] as { postMessage?: unknown } | undefined;
189
+ if (typeof injected?.postMessage === "function") {
190
+ return (frame) => (injected.postMessage as (f: string) => void)(frame);
191
+ }
192
+ // iOS installs its handlers under `webkit.messageHandlers`; the name is the host's, not ours.
193
+ const webkit = scope.webkit as
194
+ | { messageHandlers?: Record<string, { postMessage?: unknown }> }
195
+ | undefined;
196
+ const handler = webkit?.messageHandlers?.[UI_BRIDGE_GLOBALS.uiHost];
197
+ if (typeof handler?.postMessage === "function") {
198
+ return (frame) => (handler.postMessage as (f: string) => void)(frame);
199
+ }
200
+ const ipc = scope.ipc as { postMessage?: unknown } | undefined;
201
+ if (typeof ipc?.postMessage === "function") {
202
+ return (frame) => (ipc.postMessage as (f: string) => void)(frame);
203
+ }
204
+ return undefined;
205
+ }
206
+
207
+ const BROKER_KEY = "__tnUiBridgeBroker";
208
+
209
+ interface IInbound {
210
+ readonly receivers: Set<(frame: unknown) => void>;
211
+ readonly dispatch: (frame: unknown) => void;
212
+ }
213
+
214
+ interface IBroker {
215
+ game: Set<(message: IUiMessage) => void>;
216
+ ui: Set<(message: IUiMessage) => void>;
217
+ /** One dispatcher per host inbound global, however many connections share it. */
218
+ inbound: Map<string, IInbound>;
219
+ }
220
+
221
+ function broker(scope: IScope): IBroker {
222
+ const existing = scope[BROKER_KEY] as IBroker | undefined;
223
+ if (existing !== undefined) return existing;
224
+ const created: IBroker = { game: new Set(), ui: new Set(), inbound: new Map() };
225
+ scope[BROKER_KEY] = created;
226
+ return created;
227
+ }
228
+
229
+ /**
230
+ * Install `deliver` as one receiver of a host inbound global.
231
+ *
232
+ * The host calls one global with each frame, so a second connection that simply assigned it would
233
+ * silence the first — a game's own `game.ui` bridge starting after a React host had connected took
234
+ * every click away from the React host. Receivers share one dispatcher instead, and a closed
235
+ * connection removes only itself.
236
+ */
237
+ function installInbound(scope: IScope, key: string, deliver: (frame: unknown) => void): () => void {
238
+ const bus = broker(scope);
239
+ let slot = bus.inbound.get(key);
240
+ if (slot === undefined) {
241
+ const receivers = new Set<(frame: unknown) => void>();
242
+ slot = {
243
+ receivers,
244
+ dispatch: (frame) => {
245
+ // One throwing receiver must not starve the others; the first error is rethrown after
246
+ // the loop so the host still logs it.
247
+ let firstError: unknown;
248
+ let failed = false;
249
+ for (const receiver of [...receivers]) {
250
+ try {
251
+ receiver(frame);
252
+ } catch (error) {
253
+ if (!failed) {
254
+ failed = true;
255
+ firstError = error;
256
+ }
257
+ }
258
+ }
259
+ if (failed) throw firstError;
260
+ },
261
+ };
262
+ bus.inbound.set(key, slot);
263
+ }
264
+ slot.receivers.add(deliver);
265
+ scope[key] = slot.dispatch;
266
+ const installed = slot;
267
+ return () => {
268
+ installed.receivers.delete(deliver);
269
+ if (installed.receivers.size === 0 && scope[key] === installed.dispatch) scope[key] = undefined;
270
+ };
271
+ }
272
+
273
+ /**
274
+ * Connect one end of the bridge.
275
+ *
276
+ * The transport is discovered, never configured: a game asks for `ui` or `game` and gets the
277
+ * platform channel when one exists and the in-process broker when it does not. That is what
278
+ * keeps the backend — which web view, which host API — out of every game's source.
279
+ * @situation connect game code to a platform-owned UI realm
280
+ * @situation share one UI bridge implementation across web, Android, iOS, and desktop
281
+ * @example const bridge = connectUiBridge({ end: "game" });
282
+ */
283
+ export function connectUiBridge(options: IConnectOptions): IUiBridge {
284
+ const { end } = options;
285
+ if (end !== "game" && end !== "ui") {
286
+ throw new Error(`TN_UI_BRIDGE_END_INVALID: expected 'game' or 'ui', got '${String(end)}'.`);
287
+ }
288
+ const scope: IScope = options.scope ?? globalThis;
289
+ const listeners = new Set<(message: IUiMessage) => void>();
290
+ const deliver = (frame: unknown): void => {
291
+ const message = parseFrame(frame);
292
+ for (const listener of [...listeners]) listener(message);
293
+ };
294
+
295
+ const outbound = findOutbound(scope, end);
296
+ const inboundKey = end === "ui" ? UI_BRIDGE_GLOBALS.uiReceive : UI_BRIDGE_GLOBALS.gameReceive;
297
+ let closed = false;
298
+
299
+ if (outbound === undefined) {
300
+ // No host channel: the web target, where both ends share one realm.
301
+ const bus = broker(scope);
302
+ const peer = end === "ui" ? "game" : "ui";
303
+ const own = (message: IUiMessage): void => {
304
+ for (const listener of [...listeners]) listener(message);
305
+ };
306
+ bus[end].add(own);
307
+ return {
308
+ end,
309
+ transport: "in-process",
310
+ hasPeer: () => bus[peer].size > 0,
311
+ post(message) {
312
+ // The in-process peer shares this realm, so the frame is round-tripped through JSON
313
+ // anyway: a game must not be able to hand the UI a live object on web and a copy on
314
+ // native, or it will only find out on the phone.
315
+ const frame = assertSendable(message);
316
+ if (closed) throw new Error("TN_UI_BRIDGE_CLOSED: post on a closed bridge.");
317
+ const copy = JSON.parse(frame) as IUiMessage;
318
+ for (const listener of [...bus[peer]]) listener(copy);
319
+ },
320
+ onMessage(listener) {
321
+ listeners.add(listener);
322
+ return () => listeners.delete(listener);
323
+ },
324
+ close() {
325
+ closed = true;
326
+ listeners.clear();
327
+ bus[end].delete(own);
328
+ },
329
+ };
330
+ }
331
+
332
+ const uninstall = installInbound(scope, inboundKey, deliver);
333
+ // Android's injected object also raises `onmessage`; wiring it to the same entry point keeps
334
+ // one inbound path on every host rather than one per host.
335
+ const injected = scope[UI_BRIDGE_GLOBALS.uiHost] as { onmessage?: unknown } | undefined;
336
+ if (end === "ui" && injected !== undefined && typeof injected === "object") {
337
+ injected.onmessage = (event: unknown) => deliver(event);
338
+ }
339
+
340
+ return {
341
+ end,
342
+ transport: "host",
343
+ hasPeer() {
344
+ const attached = scope.__tnUiOverlayAttached;
345
+ // Only the game end can ask the host. The UI end is itself the peer the host attached,
346
+ // so if this code is running the overlay exists.
347
+ if (end === "ui") return true;
348
+ return typeof attached === "function" ? (attached as () => boolean)() === true : false;
349
+ },
350
+ post(message) {
351
+ const frame = assertSendable(message);
352
+ if (closed) throw new Error("TN_UI_BRIDGE_CLOSED: post on a closed bridge.");
353
+ outbound(frame);
354
+ },
355
+ onMessage(listener) {
356
+ listeners.add(listener);
357
+ return () => listeners.delete(listener);
358
+ },
359
+ close() {
360
+ closed = true;
361
+ listeners.clear();
362
+ uninstall();
363
+ if (end === "ui" && injected !== undefined && typeof injected === "object") {
364
+ injected.onmessage = undefined;
365
+ }
366
+ },
367
+ };
368
+ }