@flare-engine/devtools 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,386 +1,406 @@
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
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- DevtoolsContext: () => DevtoolsContext,
24
- DevtoolsHUD: () => DevtoolsHUD,
25
- DevtoolsPanel: () => DevtoolsPanel,
26
- DevtoolsProvider: () => DevtoolsProvider,
27
- TimeScrubber: () => TimeScrubber,
28
- createSnapshotRingBuffer: () => createSnapshotRingBuffer,
29
- isDevMode: () => isDevMode,
30
- useDevtools: () => useDevtools,
31
- useEntityInspector: () => useEntityInspector,
32
- useTimeScrub: () => useTimeScrub
33
- });
34
- module.exports = __toCommonJS(index_exports);
35
-
36
- // src/dev-mode.ts
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let react = require("react");
3
+ let react_native = require("react-native");
4
+ let react_jsx_runtime = require("react/jsx-runtime");
5
+ //#region src/context.ts
6
+ const DevtoolsContext = (0, react.createContext)(null);
7
+ //#endregion
8
+ //#region src/dev-mode.ts
37
9
  function isDevMode() {
38
- return typeof __DEV__ !== "undefined" && Boolean(__DEV__);
10
+ return typeof __DEV__ !== "undefined" && Boolean(__DEV__);
39
11
  }
40
-
41
- // src/ring-buffer.ts
42
- function createSnapshotRingBuffer(size, source) {
43
- if (size <= 0 || !Number.isFinite(size)) {
44
- throw new Error(`SnapshotRingBuffer size must be a positive finite number, got ${size}`);
45
- }
46
- const capacity = Math.floor(size);
47
- const slots = new Array(capacity).fill(null);
48
- let head = 0;
49
- let count = 0;
50
- return {
51
- size: capacity,
52
- get length() {
53
- return count;
54
- },
55
- get latest() {
56
- if (count === 0) return null;
57
- const idx = (head - 1 + capacity) % capacity;
58
- return slots[idx] ?? null;
59
- },
60
- capture() {
61
- slots[head] = source();
62
- head = (head + 1) % capacity;
63
- if (count < capacity) count++;
64
- },
65
- rewind(framesAgo) {
66
- if (framesAgo < 0 || !Number.isFinite(framesAgo)) return null;
67
- const offset = Math.floor(framesAgo);
68
- if (offset >= count) return null;
69
- const idx = (head - 1 - offset + capacity * 2) % capacity;
70
- return slots[idx] ?? null;
71
- },
72
- reset() {
73
- for (let i = 0; i < capacity; i++) slots[i] = null;
74
- head = 0;
75
- count = 0;
76
- }
77
- };
78
- }
79
-
80
- // src/time-scrubber.ts
81
- var TimeScrubber = class {
82
- loop;
83
- buffer;
84
- restore;
85
- _stepRequested = false;
86
- _scrubFrames = 0;
87
- constructor(options) {
88
- this.loop = options.loop;
89
- this.buffer = options.buffer;
90
- this.restore = options.restore;
91
- }
92
- /** Whether the loop is currently paused. */
93
- get paused() {
94
- return this.loop.clock.timescale === 0;
95
- }
96
- /** Whether the scrubber is currently displaying a historical snapshot. */
97
- get scrubbing() {
98
- return this._scrubFrames > 0;
99
- }
100
- /** Current scrub offset (0 = latest, N = N frames back). */
101
- get scrubOffset() {
102
- return this._scrubFrames;
103
- }
104
- /** Pause the loop (timescale → 0). */
105
- pause() {
106
- this.loop.clock.timescale = 0;
107
- }
108
- /** Resume the loop at normal speed and clear any scrub state. */
109
- resume() {
110
- this.loop.clock.timescale = 1;
111
- this._scrubFrames = 0;
112
- this._stepRequested = false;
113
- }
114
- /** Request a single-frame step. Caller polls `consumeStep()` each frame. */
115
- step() {
116
- this._stepRequested = true;
117
- }
118
- /** Ring-buffer depth available for rewind. */
119
- get available() {
120
- return this.buffer.length;
121
- }
122
- /**
123
- * Pause the loop and restore the snapshot from `framesAgo` captures back.
124
- * Clamps to available history. Returns the snapshot applied, or `null`
125
- * when the buffer is empty.
126
- */
127
- rewind(framesAgo) {
128
- if (framesAgo < 0 || !Number.isFinite(framesAgo)) return null;
129
- const max = this.buffer.length - 1;
130
- if (max < 0) return null;
131
- const offset = Math.min(Math.floor(framesAgo), max);
132
- const snap = this.buffer.rewind(offset);
133
- if (snap === null) return null;
134
- this.pause();
135
- this._scrubFrames = offset;
136
- if (this.restore) this.restore(snap);
137
- return snap;
138
- }
139
- /**
140
- * Returns true once per `step()` call, then resets. Frame-driver code is
141
- * expected to advance the simulation by one fixed tick when this is true.
142
- */
143
- consumeStep() {
144
- if (!this._stepRequested) return false;
145
- this._stepRequested = false;
146
- return true;
147
- }
148
- };
149
-
150
- // src/context.ts
151
- var import_react = require("react");
152
- var DevtoolsContext = (0, import_react.createContext)(null);
153
-
154
- // src/provider.tsx
155
- var import_react2 = require("react");
156
- var import_jsx_runtime = require("react/jsx-runtime");
157
- var DEFAULT_CONFIG = {
158
- ringSize: 120,
159
- captureInterval: 1,
160
- enabledOnNative: true
161
- };
162
- function DevtoolsProvider({
163
- world,
164
- loop,
165
- snapshot,
166
- restore,
167
- config,
168
- children
169
- }) {
170
- const ringSize = config?.ringSize ?? DEFAULT_CONFIG.ringSize;
171
- const captureInterval = config?.captureInterval ?? DEFAULT_CONFIG.captureInterval;
172
- const [selectedEntity, select] = (0, import_react2.useState)(null);
173
- const snapshotRef = (0, import_react2.useRef)(snapshot);
174
- snapshotRef.current = snapshot;
175
- const restoreRef = (0, import_react2.useRef)(restore);
176
- restoreRef.current = restore;
177
- const { buffer, scrubber } = (0, import_react2.useMemo)(() => {
178
- const buf = createSnapshotRingBuffer(ringSize, () => snapshotRef.current());
179
- const scrub = new TimeScrubber({
180
- loop,
181
- buffer: buf,
182
- restore: (s) => restoreRef.current?.(s)
183
- });
184
- return { buffer: buf, scrubber: scrub };
185
- }, [loop, ringSize]);
186
- (0, import_react2.useEffect)(() => {
187
- let ticks = 0;
188
- const unsub = loop.onUpdate.add(() => {
189
- ticks++;
190
- if (ticks % captureInterval === 0) buffer.capture();
191
- });
192
- return () => {
193
- unsub();
194
- };
195
- }, [loop, buffer, captureInterval]);
196
- const value = {
197
- world,
198
- loop,
199
- buffer,
200
- scrubber,
201
- selectedEntity,
202
- select,
203
- pause: () => scrubber.pause(),
204
- step: () => scrubber.step(),
205
- resume: () => scrubber.resume()
206
- };
207
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DevtoolsContext.Provider, { value, children });
208
- }
209
-
210
- // src/hooks.ts
211
- var import_react3 = require("react");
12
+ //#endregion
13
+ //#region src/hooks.ts
14
+ /** Returns the active devtools context, or throws if no provider is mounted. */
212
15
  function useDevtools() {
213
- const ctx = (0, import_react3.useContext)(DevtoolsContext);
214
- if (!ctx) {
215
- throw new Error("useDevtools must be used inside a <DevtoolsProvider>");
216
- }
217
- return ctx;
16
+ const ctx = (0, react.useContext)(DevtoolsContext);
17
+ if (!ctx) throw new Error("useDevtools must be used inside a <DevtoolsProvider>");
18
+ return ctx;
218
19
  }
20
+ /** Entity-inspector hook. Never mutates the world — read-only snapshot. */
219
21
  function useEntityInspector() {
220
- const { world, selectedEntity, select } = useDevtools();
221
- const entities = [];
222
- const max = world.nextId ?? 0;
223
- for (let id = 1; id < max; id++) {
224
- if (world.exists(id)) entities.push(id);
225
- }
226
- return { selected: selectedEntity, select, entities };
22
+ const { world, selectedEntity, select } = useDevtools();
23
+ const entities = [];
24
+ world.forEachEntity((id) => entities.push(id));
25
+ return {
26
+ selected: selectedEntity,
27
+ select,
28
+ entities
29
+ };
227
30
  }
31
+ /** Time-scrub hook — exposes the scrubber's imperative controls. */
228
32
  function useTimeScrub() {
229
- const { scrubber } = useDevtools();
230
- return {
231
- paused: scrubber.paused,
232
- scrubbing: scrubber.scrubbing,
233
- available: scrubber.available,
234
- pause: () => scrubber.pause(),
235
- resume: () => scrubber.resume(),
236
- step: () => scrubber.step(),
237
- rewind: (framesAgo) => {
238
- scrubber.rewind(framesAgo);
239
- }
240
- };
33
+ const { scrubber } = useDevtools();
34
+ return {
35
+ paused: scrubber.paused,
36
+ scrubbing: scrubber.scrubbing,
37
+ available: scrubber.available,
38
+ pause: () => scrubber.pause(),
39
+ resume: () => scrubber.resume(),
40
+ step: () => scrubber.step(),
41
+ rewind: (framesAgo) => {
42
+ scrubber.rewind(framesAgo);
43
+ }
44
+ };
241
45
  }
242
-
243
- // src/panel.tsx
244
- var import_react_native = require("react-native");
245
- var import_jsx_runtime2 = require("react/jsx-runtime");
46
+ //#endregion
47
+ //#region src/hud.tsx
48
+ /**
49
+ * DevtoolsHUD — compact native overlay. Shows buffer depth + pause/step/resume
50
+ * buttons. No-op on web (renders `null`).
51
+ */
52
+ function DevtoolsHUD() {
53
+ if (react_native.Platform.OS === "web") return null;
54
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(HUDBody, {});
55
+ }
56
+ function HUDBody() {
57
+ const { buffer } = useDevtools();
58
+ const scrub = useTimeScrub();
59
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.View, {
60
+ style: {
61
+ position: "absolute",
62
+ bottom: 16,
63
+ right: 16,
64
+ flexDirection: "row",
65
+ alignItems: "center",
66
+ padding: 6,
67
+ backgroundColor: "rgba(0,0,0,0.75)",
68
+ borderRadius: 4
69
+ },
70
+ children: [
71
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.Text, {
72
+ style: {
73
+ color: "#fff",
74
+ marginRight: 8
75
+ },
76
+ children: [
77
+ buffer.length,
78
+ "/",
79
+ buffer.size
80
+ ]
81
+ }),
82
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(HudButton, {
83
+ label: "||",
84
+ onPress: scrub.pause
85
+ }),
86
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(HudButton, {
87
+ label: ">|",
88
+ onPress: scrub.step
89
+ }),
90
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(HudButton, {
91
+ label: ">",
92
+ onPress: scrub.resume
93
+ })
94
+ ]
95
+ });
96
+ }
97
+ function HudButton({ label, onPress }) {
98
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.Pressable, {
99
+ onPress,
100
+ style: {
101
+ paddingHorizontal: 6,
102
+ paddingVertical: 2,
103
+ marginLeft: 4,
104
+ backgroundColor: "#444",
105
+ borderRadius: 2
106
+ },
107
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.Text, {
108
+ style: { color: "#fff" },
109
+ children: label
110
+ })
111
+ });
112
+ }
113
+ //#endregion
114
+ //#region src/panel.tsx
115
+ /**
116
+ * DevtoolsPanel — full web-side panel. Renders the entity tree, scrub
117
+ * controls, and buffer depth. No-op on native (renders `null`).
118
+ */
246
119
  function DevtoolsPanel() {
247
- if (import_react_native.Platform.OS !== "web") return null;
248
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(PanelBody, {});
120
+ if (react_native.Platform.OS !== "web") return null;
121
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(PanelBody, {});
249
122
  }
250
123
  function PanelBody() {
251
- const { buffer } = useDevtools();
252
- const { entities, selected, select } = useEntityInspector();
253
- const scrub = useTimeScrub();
254
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
255
- import_react_native.View,
256
- {
257
- style: {
258
- position: "absolute",
259
- top: 8,
260
- right: 8,
261
- padding: 8,
262
- backgroundColor: "rgba(0,0,0,0.75)",
263
- borderRadius: 4,
264
- minWidth: 200
265
- },
266
- children: [
267
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native.Text, { style: { color: "#fff", fontWeight: "bold" }, children: "Devtools" }),
268
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_native.Text, { style: { color: "#ccc" }, children: [
269
- "buffer ",
270
- buffer.length,
271
- "/",
272
- buffer.size
273
- ] }),
274
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_native.Text, { style: { color: "#ccc" }, children: [
275
- scrub.paused ? "paused" : "running",
276
- scrub.scrubbing ? " (scrub)" : ""
277
- ] }),
278
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_native.View, { style: { flexDirection: "row", marginTop: 4 }, children: [
279
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DevButton, { label: "Pause", onPress: scrub.pause }),
280
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DevButton, { label: "Step", onPress: scrub.step }),
281
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DevButton, { label: "Resume", onPress: scrub.resume })
282
- ] }),
283
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_native.View, { style: { marginTop: 8 }, children: [
284
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_native.Text, { style: { color: "#fff" }, children: [
285
- "entities (",
286
- entities.length,
287
- ")"
288
- ] }),
289
- entities.slice(0, 20).map((id) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native.Pressable, { onPress: () => select(id), children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_native.Text, { style: { color: id === selected ? "#ff0" : "#fff" }, children: [
290
- "#",
291
- id
292
- ] }) }, id))
293
- ] })
294
- ]
295
- }
296
- );
124
+ const { buffer } = useDevtools();
125
+ const { entities, selected, select } = useEntityInspector();
126
+ const scrub = useTimeScrub();
127
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.View, {
128
+ style: {
129
+ position: "absolute",
130
+ top: 8,
131
+ right: 8,
132
+ padding: 8,
133
+ backgroundColor: "rgba(0,0,0,0.75)",
134
+ borderRadius: 4,
135
+ minWidth: 200
136
+ },
137
+ children: [
138
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.Text, {
139
+ style: {
140
+ color: "#fff",
141
+ fontWeight: "bold"
142
+ },
143
+ children: "Devtools"
144
+ }),
145
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.Text, {
146
+ style: { color: "#ccc" },
147
+ children: [
148
+ "buffer ",
149
+ buffer.length,
150
+ "/",
151
+ buffer.size
152
+ ]
153
+ }),
154
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.Text, {
155
+ style: { color: "#ccc" },
156
+ children: [scrub.paused ? "paused" : "running", scrub.scrubbing ? " (scrub)" : ""]
157
+ }),
158
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.View, {
159
+ style: {
160
+ flexDirection: "row",
161
+ marginTop: 4
162
+ },
163
+ children: [
164
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DevButton, {
165
+ label: "Pause",
166
+ onPress: scrub.pause
167
+ }),
168
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DevButton, {
169
+ label: "Step",
170
+ onPress: scrub.step
171
+ }),
172
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DevButton, {
173
+ label: "Resume",
174
+ onPress: scrub.resume
175
+ })
176
+ ]
177
+ }),
178
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.View, {
179
+ style: { marginTop: 8 },
180
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.Text, {
181
+ style: { color: "#fff" },
182
+ children: [
183
+ "entities (",
184
+ entities.length,
185
+ ")"
186
+ ]
187
+ }), entities.slice(0, 20).map((id) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.Pressable, {
188
+ onPress: () => select(id),
189
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_native.Text, {
190
+ style: { color: id === selected ? "#ff0" : "#fff" },
191
+ children: ["#", id]
192
+ })
193
+ }, id))]
194
+ })
195
+ ]
196
+ });
297
197
  }
298
- function DevButton({
299
- label,
300
- onPress
301
- }) {
302
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
303
- import_react_native.Pressable,
304
- {
305
- onPress,
306
- style: {
307
- paddingHorizontal: 6,
308
- paddingVertical: 2,
309
- marginRight: 4,
310
- backgroundColor: "#444",
311
- borderRadius: 2
312
- },
313
- children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native.Text, { style: { color: "#fff" }, children: label })
314
- }
315
- );
198
+ function DevButton({ label, onPress }) {
199
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.Pressable, {
200
+ onPress,
201
+ style: {
202
+ paddingHorizontal: 6,
203
+ paddingVertical: 2,
204
+ marginRight: 4,
205
+ backgroundColor: "#444",
206
+ borderRadius: 2
207
+ },
208
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_native.Text, {
209
+ style: { color: "#fff" },
210
+ children: label
211
+ })
212
+ });
316
213
  }
317
-
318
- // src/hud.tsx
319
- var import_react_native2 = require("react-native");
320
- var import_jsx_runtime3 = require("react/jsx-runtime");
321
- function DevtoolsHUD() {
322
- if (import_react_native2.Platform.OS === "web") return null;
323
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(HUDBody, {});
324
- }
325
- function HUDBody() {
326
- const { buffer } = useDevtools();
327
- const scrub = useTimeScrub();
328
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
329
- import_react_native2.View,
330
- {
331
- style: {
332
- position: "absolute",
333
- bottom: 16,
334
- right: 16,
335
- flexDirection: "row",
336
- alignItems: "center",
337
- padding: 6,
338
- backgroundColor: "rgba(0,0,0,0.75)",
339
- borderRadius: 4
340
- },
341
- children: [
342
- /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_react_native2.Text, { style: { color: "#fff", marginRight: 8 }, children: [
343
- buffer.length,
344
- "/",
345
- buffer.size
346
- ] }),
347
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(HudButton, { label: "||", onPress: scrub.pause }),
348
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(HudButton, { label: ">|", onPress: scrub.step }),
349
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(HudButton, { label: ">", onPress: scrub.resume })
350
- ]
351
- }
352
- );
214
+ //#endregion
215
+ //#region src/ring-buffer.ts
216
+ function createSnapshotRingBuffer(size, source) {
217
+ if (size <= 0 || !Number.isFinite(size)) throw new Error(`SnapshotRingBuffer size must be a positive finite number, got ${size}`);
218
+ const capacity = Math.floor(size);
219
+ const slots = new Array(capacity).fill(null);
220
+ let head = 0;
221
+ let count = 0;
222
+ return {
223
+ size: capacity,
224
+ get length() {
225
+ return count;
226
+ },
227
+ get latest() {
228
+ if (count === 0) return null;
229
+ const idx = (head - 1 + capacity) % capacity;
230
+ return slots[idx] ?? null;
231
+ },
232
+ capture() {
233
+ slots[head] = source();
234
+ head = (head + 1) % capacity;
235
+ if (count < capacity) count++;
236
+ },
237
+ rewind(framesAgo) {
238
+ if (framesAgo < 0 || !Number.isFinite(framesAgo)) return null;
239
+ const offset = Math.floor(framesAgo);
240
+ if (offset >= count) return null;
241
+ const idx = (head - 1 - offset + capacity * 2) % capacity;
242
+ return slots[idx] ?? null;
243
+ },
244
+ reset() {
245
+ for (let i = 0; i < capacity; i++) slots[i] = null;
246
+ head = 0;
247
+ count = 0;
248
+ }
249
+ };
353
250
  }
354
- function HudButton({
355
- label,
356
- onPress
357
- }) {
358
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
359
- import_react_native2.Pressable,
360
- {
361
- onPress,
362
- style: {
363
- paddingHorizontal: 6,
364
- paddingVertical: 2,
365
- marginLeft: 4,
366
- backgroundColor: "#444",
367
- borderRadius: 2
368
- },
369
- children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_native2.Text, { style: { color: "#fff" }, children: label })
370
- }
371
- );
251
+ //#endregion
252
+ //#region src/time-scrubber.ts
253
+ /**
254
+ * TimeScrubber — imperative controller binding a `GameLoop` to a snapshot
255
+ * ring buffer. Exposes pause / step / resume plus `rewind(framesAgo)` which
256
+ * pauses the loop and hands the historical snapshot to the restore callback.
257
+ */
258
+ var TimeScrubber = class {
259
+ loop;
260
+ buffer;
261
+ restore;
262
+ _stepRequested = false;
263
+ _scrubFrames = 0;
264
+ /** Timescale captured at `pause()`, restored by `resume()`. Defaults to 1. */
265
+ _savedTimescale = 1;
266
+ constructor(options) {
267
+ this.loop = options.loop;
268
+ this.buffer = options.buffer;
269
+ this.restore = options.restore;
270
+ }
271
+ /** Whether the loop is currently paused. */
272
+ get paused() {
273
+ return this.loop.clock.timescale === 0;
274
+ }
275
+ /** Whether the scrubber is currently displaying a historical snapshot. */
276
+ get scrubbing() {
277
+ return this._scrubFrames > 0;
278
+ }
279
+ /** Current scrub offset (0 = latest, N = N frames back). */
280
+ get scrubOffset() {
281
+ return this._scrubFrames;
282
+ }
283
+ /** Pause the loop (timescale → 0), remembering the running speed.
284
+ * The `!== 0` guard means a double pause (e.g. `rewind()` calls `pause()`
285
+ * while already paused) keeps the first saved value instead of storing 0. */
286
+ pause() {
287
+ if (this.loop.clock.timescale !== 0) this._savedTimescale = this.loop.clock.timescale;
288
+ this.loop.clock.timescale = 0;
289
+ }
290
+ /** Resume the loop at its pre-pause speed and clear any scrub state.
291
+ * Restores the timescale captured at `pause()` (defaults to 1 if never
292
+ * paused), so resuming an active slow-mo returns to that rate, not 1×. */
293
+ resume() {
294
+ this.loop.clock.timescale = this._savedTimescale;
295
+ this._scrubFrames = 0;
296
+ this._stepRequested = false;
297
+ }
298
+ /** Request a single-frame step. Caller polls `consumeStep()` each frame. */
299
+ step() {
300
+ this._stepRequested = true;
301
+ }
302
+ /** Ring-buffer depth available for rewind. */
303
+ get available() {
304
+ return this.buffer.length;
305
+ }
306
+ /**
307
+ * Pause the loop and restore the snapshot from `framesAgo` captures back.
308
+ * Clamps to available history. Returns the snapshot applied, or `null`
309
+ * when the buffer is empty.
310
+ */
311
+ rewind(framesAgo) {
312
+ if (framesAgo < 0 || !Number.isFinite(framesAgo)) return null;
313
+ const max = this.buffer.length - 1;
314
+ if (max < 0) return null;
315
+ const offset = Math.min(Math.floor(framesAgo), max);
316
+ const snap = this.buffer.rewind(offset);
317
+ if (snap === null) return null;
318
+ this.pause();
319
+ this._scrubFrames = offset;
320
+ if (this.restore) this.restore(snap);
321
+ return snap;
322
+ }
323
+ /**
324
+ * Returns true once per `step()` call, then resets. Frame-driver code is
325
+ * expected to advance the simulation by one fixed tick when this is true.
326
+ */
327
+ consumeStep() {
328
+ if (!this._stepRequested) return false;
329
+ this._stepRequested = false;
330
+ return true;
331
+ }
332
+ };
333
+ //#endregion
334
+ //#region src/provider.tsx
335
+ const DEFAULT_CONFIG = {
336
+ ringSize: 120,
337
+ captureInterval: 1,
338
+ enabledOnNative: true
339
+ };
340
+ /**
341
+ * DevtoolsProvider — mounts a devtools session for a given `World` + `GameLoop`.
342
+ * Subscribes to `loop.onUpdate` and captures snapshots into a pre-allocated
343
+ * ring buffer; exposes scrubber controls via `DevtoolsContext`.
344
+ */
345
+ function DevtoolsProvider({ world, loop, snapshot, restore, config, children }) {
346
+ const ringSize = config?.ringSize ?? DEFAULT_CONFIG.ringSize;
347
+ const captureInterval = config?.captureInterval ?? DEFAULT_CONFIG.captureInterval;
348
+ const [selectedEntity, select] = (0, react.useState)(null);
349
+ const snapshotRef = (0, react.useRef)(snapshot);
350
+ snapshotRef.current = snapshot;
351
+ const restoreRef = (0, react.useRef)(restore);
352
+ restoreRef.current = restore;
353
+ const { buffer, scrubber } = (0, react.useMemo)(() => {
354
+ const buf = createSnapshotRingBuffer(ringSize, () => snapshotRef.current());
355
+ return {
356
+ buffer: buf,
357
+ scrubber: new TimeScrubber({
358
+ loop,
359
+ buffer: buf,
360
+ restore: (s) => restoreRef.current?.(s)
361
+ })
362
+ };
363
+ }, [loop, ringSize]);
364
+ (0, react.useEffect)(() => {
365
+ let ticks = 0;
366
+ const unsub = loop.onUpdate.add(() => {
367
+ ticks++;
368
+ if (ticks % captureInterval === 0) buffer.capture();
369
+ });
370
+ return () => {
371
+ unsub();
372
+ };
373
+ }, [
374
+ loop,
375
+ buffer,
376
+ captureInterval
377
+ ]);
378
+ const value = {
379
+ world,
380
+ loop,
381
+ buffer,
382
+ scrubber,
383
+ selectedEntity,
384
+ select,
385
+ pause: () => scrubber.pause(),
386
+ step: () => scrubber.step(),
387
+ resume: () => scrubber.resume()
388
+ };
389
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DevtoolsContext.Provider, {
390
+ value,
391
+ children
392
+ });
372
393
  }
373
- // Annotate the CommonJS export names for ESM import in node:
374
- 0 && (module.exports = {
375
- DevtoolsContext,
376
- DevtoolsHUD,
377
- DevtoolsPanel,
378
- DevtoolsProvider,
379
- TimeScrubber,
380
- createSnapshotRingBuffer,
381
- isDevMode,
382
- useDevtools,
383
- useEntityInspector,
384
- useTimeScrub
385
- });
394
+ //#endregion
395
+ exports.DevtoolsContext = DevtoolsContext;
396
+ exports.DevtoolsHUD = DevtoolsHUD;
397
+ exports.DevtoolsPanel = DevtoolsPanel;
398
+ exports.DevtoolsProvider = DevtoolsProvider;
399
+ exports.TimeScrubber = TimeScrubber;
400
+ exports.createSnapshotRingBuffer = createSnapshotRingBuffer;
401
+ exports.isDevMode = isDevMode;
402
+ exports.useDevtools = useDevtools;
403
+ exports.useEntityInspector = useEntityInspector;
404
+ exports.useTimeScrub = useTimeScrub;
405
+
386
406
  //# sourceMappingURL=index.js.map