@three-flatland/devtools 0.1.0-alpha.3 → 1.0.0-alpha.6

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 (172) hide show
  1. package/README.md +1 -1
  2. package/dist/buffers-modal.d.ts +16 -52
  3. package/dist/buffers-modal.d.ts.map +1 -0
  4. package/dist/buffers-modal.js +459 -464
  5. package/dist/buffers-modal.js.map +1 -1
  6. package/dist/buffers-view.d.ts +22 -34
  7. package/dist/buffers-view.d.ts.map +1 -0
  8. package/dist/buffers-view.js +252 -240
  9. package/dist/buffers-view.js.map +1 -1
  10. package/dist/create-pane.d.ts +38 -37
  11. package/dist/create-pane.d.ts.map +1 -0
  12. package/dist/create-pane.js +197 -166
  13. package/dist/create-pane.js.map +1 -1
  14. package/dist/dashboard/app.tsx +2 -0
  15. package/dist/dashboard/export.ts +1 -3
  16. package/dist/dashboard/frame-cursor.ts +81 -0
  17. package/dist/dashboard/hooks.ts +10 -2
  18. package/dist/dashboard/index.html +61 -0
  19. package/dist/dashboard/panels/batches.tsx +5 -18
  20. package/dist/dashboard/panels/buffers.tsx +52 -26
  21. package/dist/dashboard/panels/env.tsx +5 -6
  22. package/dist/dashboard/panels/protocol-log.tsx +146 -58
  23. package/dist/dashboard/panels/registry.tsx +55 -69
  24. package/dist/dashboard/panels/scrubber.tsx +100 -0
  25. package/dist/dashboard/panels/stats.tsx +60 -32
  26. package/dist/dashboard/protocol-store.ts +25 -8
  27. package/dist/dashboard/vendor/hooks.module.d.ts +146 -0
  28. package/dist/dashboard/vendor/jsx-runtime.d.ts +55 -0
  29. package/dist/dashboard/vendor/jsx.d.ts +3123 -0
  30. package/dist/dashboard/vendor/preact.module.d.ts +397 -0
  31. package/dist/devtools-client.d.ts +312 -300
  32. package/dist/devtools-client.d.ts.map +1 -0
  33. package/dist/devtools-client.js +866 -838
  34. package/dist/devtools-client.js.map +1 -1
  35. package/dist/index.d.ts +6 -8
  36. package/dist/index.js +5 -12
  37. package/dist/perf-trace.d.ts +6 -4
  38. package/dist/perf-trace.d.ts.map +1 -0
  39. package/dist/perf-trace.js +80 -66
  40. package/dist/perf-trace.js.map +1 -1
  41. package/dist/plugins.d.ts +6 -6
  42. package/dist/plugins.d.ts.map +1 -0
  43. package/dist/plugins.js +5 -5
  44. package/dist/plugins.js.map +1 -1
  45. package/dist/provider-switcher.d.ts +8 -15
  46. package/dist/provider-switcher.d.ts.map +1 -0
  47. package/dist/provider-switcher.js +68 -68
  48. package/dist/provider-switcher.js.map +1 -1
  49. package/dist/react/devtools-provider.d.ts +11 -10
  50. package/dist/react/devtools-provider.d.ts.map +1 -0
  51. package/dist/react/devtools-provider.js +60 -32
  52. package/dist/react/devtools-provider.js.map +1 -1
  53. package/dist/react/use-fps-graph.d.ts +7 -7
  54. package/dist/react/use-fps-graph.d.ts.map +1 -0
  55. package/dist/react/use-fps-graph.js +27 -21
  56. package/dist/react/use-fps-graph.js.map +1 -1
  57. package/dist/react/use-pane-button.d.ts +4 -4
  58. package/dist/react/use-pane-button.d.ts.map +1 -0
  59. package/dist/react/use-pane-button.js +35 -30
  60. package/dist/react/use-pane-button.js.map +1 -1
  61. package/dist/react/use-pane-folder.d.ts +5 -4
  62. package/dist/react/use-pane-folder.d.ts.map +1 -0
  63. package/dist/react/use-pane-folder.js +33 -20
  64. package/dist/react/use-pane-folder.js.map +1 -1
  65. package/dist/react/use-pane-input.d.ts +31 -30
  66. package/dist/react/use-pane-input.d.ts.map +1 -0
  67. package/dist/react/use-pane-input.js +46 -42
  68. package/dist/react/use-pane-input.js.map +1 -1
  69. package/dist/react/use-pane-radio-grid.d.ts +20 -20
  70. package/dist/react/use-pane-radio-grid.d.ts.map +1 -0
  71. package/dist/react/use-pane-radio-grid.js +72 -50
  72. package/dist/react/use-pane-radio-grid.js.map +1 -1
  73. package/dist/react/use-pane.d.ts +4 -4
  74. package/dist/react/use-pane.d.ts.map +1 -0
  75. package/dist/react/use-pane.js +52 -17
  76. package/dist/react/use-pane.js.map +1 -1
  77. package/dist/react.d.ts +10 -12
  78. package/dist/react.js +2 -12
  79. package/dist/registry-view.d.ts +8 -21
  80. package/dist/registry-view.d.ts.map +1 -0
  81. package/dist/registry-view.js +357 -338
  82. package/dist/registry-view.js.map +1 -1
  83. package/dist/relay.d.ts +16 -0
  84. package/dist/relay.d.ts.map +1 -0
  85. package/dist/relay.js +189 -0
  86. package/dist/relay.js.map +1 -0
  87. package/dist/remote.d.ts +10 -0
  88. package/dist/remote.d.ts.map +1 -0
  89. package/dist/remote.js +28 -0
  90. package/dist/remote.js.map +1 -0
  91. package/dist/stats-graph.d.ts +23 -31
  92. package/dist/stats-graph.d.ts.map +1 -0
  93. package/dist/stats-graph.js +323 -264
  94. package/dist/stats-graph.js.map +1 -1
  95. package/dist/stats-row.d.ts +8 -21
  96. package/dist/stats-row.d.ts.map +1 -0
  97. package/dist/stats-row.js +99 -101
  98. package/dist/stats-row.js.map +1 -1
  99. package/dist/theme.d.ts +3 -1
  100. package/dist/theme.d.ts.map +1 -0
  101. package/dist/theme.js +43 -54
  102. package/dist/theme.js.map +1 -1
  103. package/dist/vite-plugin.d.ts +7 -6
  104. package/dist/vite-plugin.d.ts.map +1 -0
  105. package/dist/vite-plugin.js +87 -78
  106. package/dist/vite-plugin.js.map +1 -1
  107. package/package.json +33 -29
  108. package/dist/buffers-modal.cjs +0 -491
  109. package/dist/buffers-modal.cjs.map +0 -1
  110. package/dist/buffers-modal.d.cts +0 -62
  111. package/dist/buffers-view.cjs +0 -269
  112. package/dist/buffers-view.cjs.map +0 -1
  113. package/dist/buffers-view.d.cts +0 -39
  114. package/dist/create-pane.cjs +0 -203
  115. package/dist/create-pane.cjs.map +0 -1
  116. package/dist/create-pane.d.cts +0 -66
  117. package/dist/devtools-client.cjs +0 -870
  118. package/dist/devtools-client.cjs.map +0 -1
  119. package/dist/devtools-client.d.cts +0 -362
  120. package/dist/index.cjs +0 -42
  121. package/dist/index.cjs.map +0 -1
  122. package/dist/index.d.cts +0 -8
  123. package/dist/index.js.map +0 -1
  124. package/dist/perf-trace.cjs +0 -103
  125. package/dist/perf-trace.cjs.map +0 -1
  126. package/dist/perf-trace.d.cts +0 -27
  127. package/dist/plugins.cjs +0 -44
  128. package/dist/plugins.cjs.map +0 -1
  129. package/dist/plugins.d.cts +0 -7
  130. package/dist/provider-switcher.cjs +0 -95
  131. package/dist/provider-switcher.cjs.map +0 -1
  132. package/dist/provider-switcher.d.cts +0 -18
  133. package/dist/react/devtools-provider.cjs +0 -59
  134. package/dist/react/devtools-provider.cjs.map +0 -1
  135. package/dist/react/devtools-provider.d.cts +0 -39
  136. package/dist/react/use-fps-graph.cjs +0 -49
  137. package/dist/react/use-fps-graph.cjs.map +0 -1
  138. package/dist/react/use-fps-graph.d.cts +0 -15
  139. package/dist/react/use-pane-button.cjs +0 -58
  140. package/dist/react/use-pane-button.cjs.map +0 -1
  141. package/dist/react/use-pane-button.d.cts +0 -10
  142. package/dist/react/use-pane-folder.cjs +0 -48
  143. package/dist/react/use-pane-folder.cjs.map +0 -1
  144. package/dist/react/use-pane-folder.d.cts +0 -15
  145. package/dist/react/use-pane-input.cjs +0 -70
  146. package/dist/react/use-pane-input.cjs.map +0 -1
  147. package/dist/react/use-pane-input.d.cts +0 -44
  148. package/dist/react/use-pane-radio-grid.cjs +0 -78
  149. package/dist/react/use-pane-radio-grid.cjs.map +0 -1
  150. package/dist/react/use-pane-radio-grid.d.cts +0 -35
  151. package/dist/react/use-pane.cjs +0 -45
  152. package/dist/react/use-pane.cjs.map +0 -1
  153. package/dist/react/use-pane.d.cts +0 -40
  154. package/dist/react.cjs +0 -50
  155. package/dist/react.cjs.map +0 -1
  156. package/dist/react.d.cts +0 -12
  157. package/dist/react.js.map +0 -1
  158. package/dist/registry-view.cjs +0 -371
  159. package/dist/registry-view.cjs.map +0 -1
  160. package/dist/registry-view.d.cts +0 -24
  161. package/dist/stats-graph.cjs +0 -297
  162. package/dist/stats-graph.cjs.map +0 -1
  163. package/dist/stats-graph.d.cts +0 -40
  164. package/dist/stats-row.cjs +0 -153
  165. package/dist/stats-row.cjs.map +0 -1
  166. package/dist/stats-row.d.cts +0 -24
  167. package/dist/theme.cjs +0 -260
  168. package/dist/theme.cjs.map +0 -1
  169. package/dist/theme.d.cts +0 -10
  170. package/dist/vite-plugin.cjs +0 -113
  171. package/dist/vite-plugin.cjs.map +0 -1
  172. package/dist/vite-plugin.d.cts +0 -9
@@ -1,870 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var devtools_client_exports = {};
20
- __export(devtools_client_exports, {
21
- DevtoolsClient: () => DevtoolsClient
22
- });
23
- module.exports = __toCommonJS(devtools_client_exports);
24
- var import_debug_protocol = require("three-flatland/debug-protocol");
25
- var import_perf_trace = require("./perf-trace.js");
26
- const QUERY_RETRY_MIN_MS = 500;
27
- const QUERY_RETRY_MAX_MS = 5e3;
28
- const QUERY_MAX_RETRIES = 10;
29
- const CLIENT_SERIES_SIZE = 1024;
30
- const EMPTY_SAMPLE = new Float32Array(0);
31
- class DevtoolsClient {
32
- id;
33
- state;
34
- /** Shared discovery bus — carries query/announce/gone only. */
35
- _discoveryBus;
36
- /** Per-provider data bus. Opened when we subscribe; closed on switch / gone. */
37
- _dataBus = null;
38
- /** Bound handler for the current `_dataBus`. Tracked so we can detach on close. */
39
- _dataHandler = null;
40
- _features;
41
- /**
42
- * Registry entry filter. `null` = all entries; `string[]` = only
43
- * these. Starts empty-array so providers don't drain (sometimes
44
- * expensive) registry payloads until the pane actually needs them.
45
- */
46
- _registrySelection = [];
47
- /**
48
- * Buffer subscription. Map of entry-name → { mode, thumbSize? }.
49
- * Empty map = metadata-only (consumer is connected to the `buffers`
50
- * feature but isn't actually watching anything — provider skips all
51
- * readbacks). `null` is not a distinct state; represented by `{}`.
52
- */
53
- _buffersSelection = {};
54
- _listeners = /* @__PURE__ */ new Set();
55
- _chunkListeners = /* @__PURE__ */ new Set();
56
- _rawMessageListeners = /* @__PURE__ */ new Set();
57
- _ackTimer = null;
58
- _livenessTimer = null;
59
- _queryRetryTimer = null;
60
- _lastServerAt = 0;
61
- _subscribed = false;
62
- _disposed = false;
63
- /** Known providers by id — updated live via `provider:announce` + `provider:gone`. */
64
- _providers = /* @__PURE__ */ new Map();
65
- constructor(options) {
66
- this.id = generateUuid();
67
- this._features = [...options.features];
68
- if (options.onChange) this._listeners.add(options.onChange);
69
- this._discoveryBus = new BroadcastChannel(options.discoveryChannelName ?? import_debug_protocol.DISCOVERY_CHANNEL);
70
- const mkSeries = () => ({
71
- data: new Float32Array(CLIENT_SERIES_SIZE),
72
- write: 0,
73
- length: 0
74
- });
75
- this.state = {
76
- series: {
77
- fps: mkSeries(),
78
- cpuMs: mkSeries(),
79
- gpuMs: mkSeries(),
80
- heapUsedMB: mkSeries(),
81
- drawCalls: mkSeries(),
82
- triangles: mkSeries(),
83
- primitives: mkSeries(),
84
- geometries: mkSeries(),
85
- textures: mkSeries()
86
- },
87
- registry: /* @__PURE__ */ new Map(),
88
- buffers: /* @__PURE__ */ new Map(),
89
- batches: { frame: 0, passes: [], batches: [] },
90
- providers: [],
91
- selectedProviderId: null,
92
- serverAlive: false,
93
- serverLagMs: 0
94
- };
95
- this._discoveryBus.addEventListener("message", (ev) => {
96
- this._onDiscoveryMessage(ev.data);
97
- });
98
- }
99
- /**
100
- * Begin discovery + subscription. Sends a `provider:query`, waits
101
- * `DISCOVERY_WINDOW_MS` for `provider:announce` responses, then picks
102
- * the best provider (user > system) and subscribes to it.
103
- *
104
- * Idempotent — calling again while already started is a no-op.
105
- */
106
- start() {
107
- if (this._disposed || this._subscribed) return;
108
- this._subscribed = true;
109
- this._sendQuery();
110
- let retries = 0;
111
- let nextDelay = QUERY_RETRY_MIN_MS;
112
- const tick = () => {
113
- if (this._disposed || this.state.selectedProviderId !== null) {
114
- this._queryRetryTimer = null;
115
- return;
116
- }
117
- if (retries >= QUERY_MAX_RETRIES) {
118
- this._queryRetryTimer = null;
119
- return;
120
- }
121
- retries++;
122
- this._sendQuery();
123
- nextDelay = Math.min(nextDelay * 2, QUERY_RETRY_MAX_MS);
124
- this._queryRetryTimer = setTimeout(tick, nextDelay);
125
- };
126
- this._queryRetryTimer = setTimeout(tick, nextDelay);
127
- this._livenessTimer = setInterval(() => this._checkLiveness(), 1e3);
128
- }
129
- _sendQuery() {
130
- this._postDiscovery({
131
- type: "provider:query",
132
- payload: { requesterId: this.id }
133
- });
134
- }
135
- /**
136
- * Update the feature set. Re-posts `subscribe` on the current
137
- * provider's data channel.
138
- */
139
- setFeatures(features) {
140
- if (sameSet(this._features, features)) return;
141
- this._features = [...features];
142
- this._resubscribe();
143
- }
144
- /**
145
- * Update the registry entry filter. Pass `null` for "every entry",
146
- * `[]` to stop all registry traffic, or a list of names to narrow.
147
- * Re-posts `subscribe` if the filter actually changed.
148
- */
149
- /**
150
- * Narrow which registry entries the provider should ship samples
151
- * for. `null` = all entries; `[]` = none; `[name, …]` = only these.
152
- * Metadata still flows regardless — only sample bytes are gated.
153
- */
154
- setRegistry(names) {
155
- if (sameFilter(this._registrySelection, names)) return;
156
- this._registrySelection = names === null ? null : [...names];
157
- this._resubscribe();
158
- }
159
- /**
160
- * Update the buffer subscription. Map of entry-name → (mode, thumbSize?).
161
- * - Empty map / missing names → metadata-only (provider sends no pixels,
162
- * runs no readbacks). This is the idle state when no preview or
163
- * modal is visible.
164
- * - `{ name: { mode: 'thumbnail', thumbSize: 256 } }` → small
165
- * downsampled preview. Use when a thumbnail card is visible.
166
- * - `{ name: { mode: 'stream' } }` → full-size VP9 video stream.
167
- * Use when the fullscreen modal is open.
168
- *
169
- * Mode changes (incl. unsubscribing) take effect on the next
170
- * provider flush; the producer will drop old readbacks immediately.
171
- */
172
- setBuffers(subscription) {
173
- if (sameBufferSubscription(this._buffersSelection, subscription)) return;
174
- this._buffersSelection = { ...subscription };
175
- this._resubscribe();
176
- }
177
- _resubscribe() {
178
- if (this._subscribed && this._dataBus !== null) {
179
- const hasBuffers = Object.keys(this._buffersSelection).length > 0;
180
- this._postData({
181
- type: "subscribe",
182
- payload: {
183
- id: this.id,
184
- features: this._features,
185
- registry: this._registrySelection ?? void 0,
186
- buffers: hasBuffers ? this._buffersSelection : void 0
187
- }
188
- });
189
- }
190
- }
191
- /**
192
- * Manually switch to a different provider. No-op if unknown or if
193
- * already selected. Closes the old data channel and opens the new one.
194
- */
195
- selectProvider(providerId) {
196
- if (!this._providers.has(providerId)) return;
197
- if (this.state.selectedProviderId === providerId) return;
198
- this._leaveDataChannel();
199
- this.state.selectedProviderId = providerId;
200
- this._resetAccumulatedState();
201
- this._joinDataChannel(providerId);
202
- this._postData({
203
- type: "subscribe",
204
- payload: {
205
- id: this.id,
206
- features: this._features,
207
- registry: this._registrySelection ?? void 0,
208
- buffers: Object.keys(this._buffersSelection).length > 0 ? this._buffersSelection : void 0
209
- }
210
- });
211
- this._fire();
212
- }
213
- dispose() {
214
- if (this._disposed) return;
215
- this._disposed = true;
216
- this._leaveDataChannel();
217
- this._subscribed = false;
218
- if (this._ackTimer !== null) {
219
- clearInterval(this._ackTimer);
220
- this._ackTimer = null;
221
- }
222
- if (this._livenessTimer !== null) {
223
- clearInterval(this._livenessTimer);
224
- this._livenessTimer = null;
225
- }
226
- if (this._queryRetryTimer !== null) {
227
- clearTimeout(this._queryRetryTimer);
228
- this._queryRetryTimer = null;
229
- }
230
- try {
231
- this._discoveryBus.close();
232
- } catch {
233
- }
234
- }
235
- /**
236
- * Open a fresh per-provider data channel and start listening on it.
237
- * Caller is responsible for posting the initial `subscribe`.
238
- */
239
- _joinDataChannel(providerId) {
240
- this._dataBus = new BroadcastChannel((0, import_debug_protocol.providerChannelName)(providerId));
241
- this._dataHandler = (ev) => {
242
- this._onDataMessage(ev.data);
243
- };
244
- this._dataBus.addEventListener("message", this._dataHandler);
245
- }
246
- /**
247
- * Send `unsubscribe` (if we have someone to tell) and close the data
248
- * channel. Idempotent — safe to call when there's no active channel.
249
- */
250
- _leaveDataChannel() {
251
- if (this._dataBus !== null) {
252
- if (this._subscribed) {
253
- try {
254
- this._postData({ type: "unsubscribe", payload: { id: this.id } });
255
- } catch {
256
- }
257
- }
258
- if (this._dataHandler !== null) {
259
- try {
260
- this._dataBus.removeEventListener("message", this._dataHandler);
261
- } catch {
262
- }
263
- }
264
- try {
265
- this._dataBus.close();
266
- } catch {
267
- }
268
- this._dataBus = null;
269
- this._dataHandler = null;
270
- }
271
- }
272
- // ── Internal ──────────────────────────────────────────────────────────
273
- /**
274
- * Handler for the shared discovery bus. Only discovery messages land
275
- * here by protocol; anything else is ignored.
276
- */
277
- _onDiscoveryMessage(msg) {
278
- if (!msg || typeof msg !== "object" || !("type" in msg)) return;
279
- (0, import_perf_trace.tracePerf)(msg);
280
- switch (msg.type) {
281
- case "provider:announce": {
282
- const id = msg.payload.identity.id;
283
- this._providers.set(id, msg.payload.identity);
284
- this.state.providers = Array.from(this._providers.values());
285
- this._fire();
286
- if (this.state.selectedProviderId === null) this._pickProviderAndSubscribe();
287
- break;
288
- }
289
- case "provider:gone": {
290
- const id = msg.payload.id;
291
- if (!this._providers.has(id)) return;
292
- this._providers.delete(id);
293
- this.state.providers = Array.from(this._providers.values());
294
- if (this.state.selectedProviderId === id) {
295
- this._leaveDataChannel();
296
- this.state.selectedProviderId = null;
297
- this._resetAccumulatedState();
298
- this._pickProviderAndSubscribe();
299
- }
300
- this._fire();
301
- break;
302
- }
303
- default:
304
- break;
305
- }
306
- }
307
- /**
308
- * Handler for the per-provider data channel. Everything here is
309
- * already implicitly addressed to us (we opened the channel with the
310
- * provider's id), so no per-message filtering is required beyond
311
- * matching our consumer `id` on `subscribe:ack`.
312
- */
313
- _onDataMessage(msg) {
314
- if (!msg || typeof msg !== "object" || !("type" in msg)) return;
315
- (0, import_perf_trace.tracePerf)(msg);
316
- if (this._rawMessageListeners.size > 0) {
317
- for (const cb of this._rawMessageListeners) {
318
- try {
319
- cb(msg, "in");
320
- } catch {
321
- }
322
- }
323
- }
324
- switch (msg.type) {
325
- case "subscribe:ack": {
326
- if (msg.payload.id !== this.id) return;
327
- this._markServerAlive();
328
- this._applyEnv(msg.payload.env);
329
- if (this._ackTimer === null) {
330
- this._ackTimer = setInterval(() => this._sendAck(), import_debug_protocol.ACK_INTERVAL_MS);
331
- }
332
- this._fire();
333
- break;
334
- }
335
- case "data": {
336
- this._markServerAlive();
337
- this.state.frame = msg.payload.frame;
338
- const features = msg.payload.features;
339
- if (features.stats !== void 0) this._applyStats(features.stats);
340
- if (features.env !== void 0) this._applyEnv(features.env);
341
- if (features.registry !== void 0) this._applyRegistry(features.registry);
342
- if (features.buffers !== void 0) this._applyBuffers(features.buffers);
343
- if (features.batches !== void 0) this._applyBatches(features.batches);
344
- this._fire();
345
- break;
346
- }
347
- case "buffer:chunk": {
348
- this._markServerAlive();
349
- const payload = msg.payload;
350
- for (const cb of this._chunkListeners) {
351
- try {
352
- cb(payload);
353
- } catch {
354
- }
355
- }
356
- break;
357
- }
358
- case "buffer:raw": {
359
- this._markServerAlive();
360
- const payload = msg.payload;
361
- let snap = this.state.buffers.get(payload.name);
362
- if (snap !== void 0) {
363
- snap.pixels = new Uint8Array(payload.data);
364
- snap.width = payload.width;
365
- snap.height = payload.height;
366
- snap.version++;
367
- } else {
368
- snap = {
369
- name: payload.name,
370
- width: payload.width,
371
- height: payload.height,
372
- srcWidth: payload.width,
373
- srcHeight: payload.height,
374
- pixelType: "rgba8",
375
- display: "colors",
376
- version: 0,
377
- pixels: new Uint8Array(payload.data)
378
- };
379
- this.state.buffers.set(payload.name, snap);
380
- }
381
- this._fire();
382
- break;
383
- }
384
- case "ping": {
385
- this._markServerAlive();
386
- break;
387
- }
388
- default:
389
- break;
390
- }
391
- }
392
- _markServerAlive() {
393
- this._lastServerAt = Date.now();
394
- if (!this.state.serverAlive) {
395
- this.state.serverAlive = true;
396
- this._fire();
397
- }
398
- }
399
- /**
400
- * Pick the best provider from the currently-known set and subscribe
401
- * to it. Preference: `user` over `system`; first-announced as
402
- * tiebreak. No-op if zero providers are known (we'll stay waiting;
403
- * any future `provider:announce` that arrives doesn't auto-trigger
404
- * this — call it again or restart discovery).
405
- */
406
- _pickProviderAndSubscribe() {
407
- if (this.state.selectedProviderId !== null) return;
408
- const providers = Array.from(this._providers.values());
409
- if (providers.length === 0) return;
410
- const user = providers.find((p) => p.kind === "user");
411
- const chosen = user ?? providers[0];
412
- this.state.selectedProviderId = chosen.id;
413
- this._joinDataChannel(chosen.id);
414
- this._postData({
415
- type: "subscribe",
416
- payload: {
417
- id: this.id,
418
- features: this._features,
419
- registry: this._registrySelection ?? void 0,
420
- buffers: Object.keys(this._buffersSelection).length > 0 ? this._buffersSelection : void 0
421
- }
422
- });
423
- this._fire();
424
- }
425
- /** Clear all feature-derived state so the next data packet can start fresh. */
426
- _resetAccumulatedState() {
427
- this.state.frame = void 0;
428
- this._applyStats(null);
429
- this._applyEnv(null);
430
- this.state.registry.clear();
431
- this.state.buffers.clear();
432
- this._applyBatches(null);
433
- }
434
- /**
435
- * Apply a stats batch. Null = reset (feature cleared / provider gone).
436
- * Otherwise: decode each typed array onto the matching Float32 ring
437
- * and update the scalar with the batch mean (natural smoothing for
438
- * the text label).
439
- */
440
- _applyStats(batch) {
441
- if (batch === null) {
442
- this.state.drawCalls = void 0;
443
- this.state.triangles = void 0;
444
- this.state.primitives = void 0;
445
- this.state.geometries = void 0;
446
- this.state.textures = void 0;
447
- this.state.cpuMs = void 0;
448
- this.state.fps = void 0;
449
- this.state.gpuMs = void 0;
450
- this.state.heapUsedMB = void 0;
451
- this.state.heapLimitMB = void 0;
452
- const s2 = this.state.series;
453
- s2.fps.write = 0;
454
- s2.fps.length = 0;
455
- s2.cpuMs.write = 0;
456
- s2.cpuMs.length = 0;
457
- s2.gpuMs.write = 0;
458
- s2.gpuMs.length = 0;
459
- s2.heapUsedMB.write = 0;
460
- s2.heapUsedMB.length = 0;
461
- s2.drawCalls.write = 0;
462
- s2.drawCalls.length = 0;
463
- s2.triangles.write = 0;
464
- s2.triangles.length = 0;
465
- s2.primitives.write = 0;
466
- s2.primitives.length = 0;
467
- s2.geometries.write = 0;
468
- s2.geometries.length = 0;
469
- s2.textures.write = 0;
470
- s2.textures.length = 0;
471
- return;
472
- }
473
- const count = batch.count;
474
- if (count <= 0) return;
475
- const s = this.state.series;
476
- if (batch.fps) this.state.fps = this._ingestI16(s.fps, batch.fps, count, 0.1);
477
- if (batch.cpuMs) this.state.cpuMs = this._ingestU16(s.cpuMs, batch.cpuMs, count, 0.01);
478
- if (batch.gpuMs) this.state.gpuMs = this._ingestU16(s.gpuMs, batch.gpuMs, count, 0.01);
479
- if (batch.heapUsedMB) this.state.heapUsedMB = this._ingestU16(s.heapUsedMB, batch.heapUsedMB, count, 1);
480
- if (batch.drawCalls) this.state.drawCalls = this._ingestU32(s.drawCalls, batch.drawCalls, count);
481
- if (batch.triangles) this.state.triangles = this._ingestU32(s.triangles, batch.triangles, count);
482
- if (batch.primitives) this.state.primitives = this._ingestU32(s.primitives, batch.primitives, count);
483
- if (batch.geometries) this.state.geometries = this._ingestU32(s.geometries, batch.geometries, count);
484
- if (batch.textures) this.state.textures = this._ingestU32(s.textures, batch.textures, count);
485
- if (batch.heapLimitMB !== void 0) this.state.heapLimitMB = batch.heapLimitMB;
486
- }
487
- /**
488
- * Append `count` samples from a scaled integer view to a Float32 ring;
489
- * return the batch mean (scaled). Zero-alloc past the ring itself.
490
- */
491
- _ingestI16(ring, src, count, scale) {
492
- const size = ring.data.length;
493
- let sum = 0;
494
- let w = ring.write;
495
- for (let i = 0; i < count; i++) {
496
- const v = src[i] * scale;
497
- ring.data[w] = v;
498
- sum += v;
499
- w = (w + 1) % size;
500
- }
501
- ring.write = w;
502
- ring.length = Math.min(size, ring.length + count);
503
- return sum / count;
504
- }
505
- _ingestU16(ring, src, count, scale) {
506
- const size = ring.data.length;
507
- let sum = 0;
508
- let w = ring.write;
509
- for (let i = 0; i < count; i++) {
510
- const v = src[i] * scale;
511
- ring.data[w] = v;
512
- sum += v;
513
- w = (w + 1) % size;
514
- }
515
- ring.write = w;
516
- ring.length = Math.min(size, ring.length + count);
517
- return sum / count;
518
- }
519
- _ingestU32(ring, src, count) {
520
- const size = ring.data.length;
521
- let sum = 0;
522
- let w = ring.write;
523
- for (let i = 0; i < count; i++) {
524
- const v = src[i];
525
- ring.data[w] = v;
526
- sum += v;
527
- w = (w + 1) % size;
528
- }
529
- ring.write = w;
530
- ring.length = Math.min(size, ring.length + count);
531
- return sum / count;
532
- }
533
- /**
534
- * Apply a batches feature payload — replaces the snapshot wholesale.
535
- * The wire arrays are scratch pools on the producer; we truncate by
536
- * the provided counts and copy fields into persistent snapshot
537
- * objects the UI can hold across subsequent frames.
538
- *
539
- * The client-side snapshot arrays grow monotonically to the high
540
- * watermark and entries are overwritten in place — same alloc
541
- * discipline as the producer.
542
- */
543
- _applyBatches(delta) {
544
- if (delta === null) {
545
- this.state.batches.frame = 0;
546
- this.state.batches.passes.length = 0;
547
- this.state.batches.batches.length = 0;
548
- return;
549
- }
550
- this.state.batches.frame = delta.frame;
551
- const passesIn = delta.passes;
552
- const passCount = delta.passCount | 0;
553
- const passesOut = this.state.batches.passes;
554
- if (passesIn !== void 0) {
555
- for (let i = 0; i < passCount; i++) {
556
- const src = passesIn[i];
557
- if (src === void 0) continue;
558
- let dst = passesOut[i];
559
- if (dst === void 0) {
560
- dst = { label: "", calls: 0, triangles: 0, cpuMs: 0, depth: 0, parent: -1 };
561
- passesOut[i] = dst;
562
- }
563
- dst.label = src.label;
564
- dst.calls = src.calls;
565
- dst.triangles = src.triangles;
566
- dst.cpuMs = src.cpuMs;
567
- dst.depth = src.depth;
568
- dst.parent = src.parent;
569
- }
570
- }
571
- if (passesOut.length > passCount) passesOut.length = passCount;
572
- const batchesIn = delta.batches;
573
- const batchCount = delta.batchCount | 0;
574
- const batchesOut = this.state.batches.batches;
575
- if (batchesIn !== void 0) {
576
- for (let i = 0; i < batchCount; i++) {
577
- const src = batchesIn[i];
578
- if (src === void 0) continue;
579
- let dst = batchesOut[i];
580
- if (dst === void 0) {
581
- dst = {
582
- runKey: 0,
583
- materialId: 0,
584
- layer: 0,
585
- materialName: "",
586
- spriteCount: 0,
587
- batchIdx: 0,
588
- kind: "sprite",
589
- label: ""
590
- };
591
- batchesOut[i] = dst;
592
- }
593
- dst.runKey = src.runKey;
594
- dst.materialId = src.materialId;
595
- dst.layer = src.layer;
596
- dst.materialName = src.materialName;
597
- dst.spriteCount = src.spriteCount;
598
- dst.batchIdx = src.batchIdx;
599
- dst.kind = src.kind ?? "sprite";
600
- dst.label = src.label ?? "";
601
- }
602
- }
603
- if (batchesOut.length > batchCount) batchesOut.length = batchCount;
604
- }
605
- /**
606
- * Apply a registry delta. `entries[name] === null` → remove;
607
- * `entries[name]` present → upsert a snapshot keyed by `name`.
608
- * A `null` batch (feature cleared) drops every entry.
609
- */
610
- _applyRegistry(delta) {
611
- if (delta === null) {
612
- this.state.registry.clear();
613
- return;
614
- }
615
- if (!delta.entries) return;
616
- const entries = delta.entries;
617
- for (const name in entries) {
618
- const d = entries[name];
619
- if (d === void 0) continue;
620
- if (d === null) {
621
- this.state.registry.delete(name);
622
- continue;
623
- }
624
- let snap = this.state.registry.get(name);
625
- const sample = d.sample ?? snap?.sample ?? EMPTY_SAMPLE;
626
- if (snap === void 0) {
627
- snap = {
628
- name,
629
- kind: d.kind,
630
- version: d.version,
631
- count: d.count,
632
- sample,
633
- label: d.label
634
- };
635
- this.state.registry.set(name, snap);
636
- } else {
637
- snap.kind = d.kind;
638
- snap.version = d.version;
639
- snap.count = d.count;
640
- snap.sample = sample;
641
- snap.label = d.label;
642
- }
643
- }
644
- }
645
- /**
646
- * Apply a buffers delta. Metadata (width/height/version/pixelType)
647
- * ships regardless of the client's selection; `pixels` only arrives
648
- * when this entry is in the current selection. Retain the previous
649
- * `pixels` when the delta is metadata-only so the UI keeps showing
650
- * the last thumbnail while browsing.
651
- */
652
- _applyBuffers(delta) {
653
- if (delta === null) {
654
- this.state.buffers.clear();
655
- return;
656
- }
657
- if (!delta.entries) return;
658
- const entries = delta.entries;
659
- for (const name in entries) {
660
- const d = entries[name];
661
- if (d === void 0) continue;
662
- if (d === null) {
663
- this.state.buffers.delete(name);
664
- continue;
665
- }
666
- let snap = this.state.buffers.get(name);
667
- const pixels = d.pixels ?? snap?.pixels ?? null;
668
- const isFloat = d.pixelType === "rgba16f" || d.pixelType === "rgba32f";
669
- const display = d.display ?? (isFloat ? "normalize" : "colors");
670
- const srcW = d.srcWidth ?? d.width;
671
- const srcH = d.srcHeight ?? d.height;
672
- if (snap === void 0) {
673
- snap = {
674
- name,
675
- width: d.width,
676
- height: d.height,
677
- srcWidth: srcW,
678
- srcHeight: srcH,
679
- pixelType: d.pixelType,
680
- display,
681
- version: d.version,
682
- pixels,
683
- label: d.label
684
- };
685
- this.state.buffers.set(name, snap);
686
- } else {
687
- snap.width = d.width;
688
- snap.height = d.height;
689
- snap.srcWidth = srcW;
690
- snap.srcHeight = srcH;
691
- snap.pixelType = d.pixelType;
692
- snap.display = display;
693
- snap.version = d.version;
694
- snap.pixels = pixels;
695
- snap.label = d.label;
696
- }
697
- }
698
- }
699
- _applyEnv(delta) {
700
- if (delta === null) {
701
- this.state.threeFlatlandVersion = void 0;
702
- this.state.threeRevision = void 0;
703
- this.state.backendName = void 0;
704
- this.state.backendTrackTimestamp = void 0;
705
- this.state.backendDisjoint = void 0;
706
- this.state.gpuModeEnabled = void 0;
707
- this.state.canvasWidth = void 0;
708
- this.state.canvasHeight = void 0;
709
- this.state.canvasPixelRatio = void 0;
710
- return;
711
- }
712
- if ("threeFlatlandVersion" in delta) this.state.threeFlatlandVersion = delta.threeFlatlandVersion ?? void 0;
713
- if ("threeRevision" in delta) this.state.threeRevision = delta.threeRevision ?? void 0;
714
- if (delta.backend !== void 0) {
715
- const b = delta.backend;
716
- if (b === null) {
717
- this.state.backendName = void 0;
718
- this.state.backendTrackTimestamp = void 0;
719
- this.state.backendDisjoint = void 0;
720
- this.state.gpuModeEnabled = void 0;
721
- } else {
722
- if ("name" in b) this.state.backendName = b.name ?? void 0;
723
- if ("trackTimestamp" in b) this.state.backendTrackTimestamp = b.trackTimestamp ?? void 0;
724
- if ("disjoint" in b) this.state.backendDisjoint = b.disjoint ?? void 0;
725
- if ("gpuModeEnabled" in b) this.state.gpuModeEnabled = b.gpuModeEnabled ?? void 0;
726
- }
727
- }
728
- if (delta.canvas !== void 0) {
729
- const c = delta.canvas;
730
- if (c === null) {
731
- this.state.canvasWidth = void 0;
732
- this.state.canvasHeight = void 0;
733
- this.state.canvasPixelRatio = void 0;
734
- } else {
735
- if ("width" in c) this.state.canvasWidth = c.width ?? void 0;
736
- if ("height" in c) this.state.canvasHeight = c.height ?? void 0;
737
- if ("pixelRatio" in c) this.state.canvasPixelRatio = c.pixelRatio ?? void 0;
738
- }
739
- }
740
- }
741
- _checkLiveness() {
742
- const now = Date.now();
743
- const lag = this._lastServerAt === 0 ? now : now - this._lastServerAt;
744
- this.state.serverLagMs = lag;
745
- if (this._lastServerAt !== 0 && lag > import_debug_protocol.SERVER_LIVENESS_MS) {
746
- if (this.state.serverAlive) {
747
- this.state.serverAlive = false;
748
- }
749
- if (this._dataBus !== null) {
750
- this._postData({
751
- type: "subscribe",
752
- payload: {
753
- id: this.id,
754
- features: this._features,
755
- registry: this._registrySelection ?? void 0,
756
- buffers: Object.keys(this._buffersSelection).length > 0 ? this._buffersSelection : void 0
757
- }
758
- });
759
- }
760
- }
761
- this._fire();
762
- }
763
- _sendAck() {
764
- if (this._disposed || this._dataBus === null) return;
765
- this._postData({ type: "ack", payload: { id: this.id } });
766
- }
767
- _postDiscovery(body) {
768
- const msg = { v: import_debug_protocol.DEBUG_PROTOCOL_VERSION, ts: Date.now(), ...body };
769
- this._fireRawMessage(msg, "out");
770
- try {
771
- this._discoveryBus.postMessage(msg);
772
- } catch {
773
- }
774
- }
775
- _postData(body) {
776
- if (this._dataBus === null) return;
777
- const msg = { v: import_debug_protocol.DEBUG_PROTOCOL_VERSION, ts: Date.now(), ...body };
778
- this._fireRawMessage(msg, "out");
779
- try {
780
- this._dataBus.postMessage(msg);
781
- } catch {
782
- }
783
- }
784
- _fireRawMessage(msg, direction) {
785
- if (this._rawMessageListeners.size === 0) return;
786
- for (const cb of this._rawMessageListeners) {
787
- try {
788
- cb(msg, direction);
789
- } catch {
790
- }
791
- }
792
- }
793
- /** Subscribe to state-change events. Returns an unsubscribe function. */
794
- addListener(cb) {
795
- this._listeners.add(cb);
796
- return () => {
797
- this._listeners.delete(cb);
798
- };
799
- }
800
- /** Explicit remove — equivalent to calling the unsubscribe returned from `addListener`. */
801
- removeListener(cb) {
802
- this._listeners.delete(cb);
803
- }
804
- /** Subscribe to incoming WebCodecs buffer chunks. Returns an unsubscribe function. */
805
- addChunkListener(cb) {
806
- this._chunkListeners.add(cb);
807
- return () => {
808
- this._chunkListeners.delete(cb);
809
- };
810
- }
811
- /**
812
- * Subscribe to every bus message — both inbound (from providers) and
813
- * outbound (our own `subscribe`/`ack`/etc.). Used by the dashboard's
814
- * protocol-log panel. Listeners fire on the hot path so keep them
815
- * cheap — filter and format lazily.
816
- */
817
- addRawMessageListener(cb) {
818
- this._rawMessageListeners.add(cb);
819
- return () => {
820
- this._rawMessageListeners.delete(cb);
821
- };
822
- }
823
- _fire() {
824
- for (const cb of this._listeners) {
825
- try {
826
- cb(this.state);
827
- } catch {
828
- }
829
- }
830
- }
831
- }
832
- function sameSet(a, b) {
833
- if (a.length !== b.length) return false;
834
- for (const v of a) if (!b.includes(v)) return false;
835
- return true;
836
- }
837
- function sameFilter(a, b) {
838
- if (a === b) return true;
839
- if (a === null || b === null) return false;
840
- return sameSet(a, b);
841
- }
842
- function sameBufferSubscription(a, b) {
843
- const ak = Object.keys(a);
844
- const bk = Object.keys(b);
845
- if (ak.length !== bk.length) return false;
846
- for (const k of ak) {
847
- const av = a[k];
848
- const bv = b[k];
849
- if (av === void 0 || bv === void 0) return false;
850
- if (av.mode !== bv.mode) return false;
851
- if ((av.thumbSize ?? 256) !== (bv.thumbSize ?? 256)) return false;
852
- }
853
- return true;
854
- }
855
- function generateUuid() {
856
- const c = typeof crypto !== "undefined" ? crypto : void 0;
857
- if (c && typeof c.randomUUID === "function") return c.randomUUID();
858
- const bytes = new Uint8Array(16);
859
- if (c && typeof c.getRandomValues === "function") c.getRandomValues(bytes);
860
- else for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
861
- bytes[6] = bytes[6] & 15 | 64;
862
- bytes[8] = bytes[8] & 63 | 128;
863
- const h = Array.from(bytes, (b) => b.toString(16).padStart(2, "0"));
864
- return `${h.slice(0, 4).join("")}-${h.slice(4, 6).join("")}-${h.slice(6, 8).join("")}-${h.slice(8, 10).join("")}-${h.slice(10).join("")}`;
865
- }
866
- // Annotate the CommonJS export names for ESM import in node:
867
- 0 && (module.exports = {
868
- DevtoolsClient
869
- });
870
- //# sourceMappingURL=devtools-client.cjs.map