@al8b/time 0.1.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/README.md +23 -0
- package/dist/constants.d.mts +6 -0
- package/dist/constants.d.ts +6 -0
- package/dist/constants.js +34 -0
- package/dist/constants.js.map +1 -0
- package/dist/constants.mjs +8 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/core/index.d.mts +2 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +522 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/index.mjs +497 -0
- package/dist/core/index.mjs.map +1 -0
- package/dist/core/machine.d.mts +101 -0
- package/dist/core/machine.d.ts +101 -0
- package/dist/core/machine.js +520 -0
- package/dist/core/machine.js.map +1 -0
- package/dist/core/machine.mjs +497 -0
- package/dist/core/machine.mjs.map +1 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +526 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +499 -0
- package/dist/index.mjs.map +1 -0
- package/dist/playback/index.d.mts +2 -0
- package/dist/playback/index.d.ts +2 -0
- package/dist/playback/index.js +172 -0
- package/dist/playback/index.js.map +1 -0
- package/dist/playback/index.mjs +147 -0
- package/dist/playback/index.mjs.map +1 -0
- package/dist/playback/player.d.mts +41 -0
- package/dist/playback/player.d.ts +41 -0
- package/dist/playback/player.js +172 -0
- package/dist/playback/player.js.map +1 -0
- package/dist/playback/player.mjs +147 -0
- package/dist/playback/player.mjs.map +1 -0
- package/dist/recording/index.d.mts +2 -0
- package/dist/recording/index.d.ts +2 -0
- package/dist/recording/index.js +149 -0
- package/dist/recording/index.js.map +1 -0
- package/dist/recording/index.mjs +124 -0
- package/dist/recording/index.mjs.map +1 -0
- package/dist/recording/recorder.d.mts +41 -0
- package/dist/recording/recorder.d.ts +41 -0
- package/dist/recording/recorder.js +149 -0
- package/dist/recording/recorder.js.map +1 -0
- package/dist/recording/recorder.mjs +124 -0
- package/dist/recording/recorder.mjs.map +1 -0
- package/dist/types/index.d.mts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +19 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +1 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/state.d.mts +33 -0
- package/dist/types/state.d.ts +33 -0
- package/dist/types/state.js +19 -0
- package/dist/types/state.js.map +1 -0
- package/dist/types/state.mjs +1 -0
- package/dist/types/state.mjs.map +1 -0
- package/dist/utils.d.mts +9 -0
- package/dist/utils.d.ts +9 -0
- package/dist/utils.js +60 -0
- package/dist/utils.js.map +1 -0
- package/dist/utils.mjs +37 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @al8b/time
|
|
2
|
+
|
|
3
|
+
Time-machine debugging support for L8B. It records runtime state, replays snapshots, and exposes the playback and recording primitives used by the runtime debugger flow.
|
|
4
|
+
|
|
5
|
+
## Public API
|
|
6
|
+
|
|
7
|
+
- `TimeMachine`
|
|
8
|
+
- `StatePlayer`
|
|
9
|
+
- `StateRecorder`
|
|
10
|
+
- Types: `TimeMachineRuntime`, `StateSnapshot`, `TimeMachineCommand`, `TimeMachineMessage`, `TimeMachineStatus`
|
|
11
|
+
|
|
12
|
+
## Notes
|
|
13
|
+
|
|
14
|
+
- Consumed by `@al8b/runtime`.
|
|
15
|
+
- This package is about recording and replay infrastructure, not the frame loop itself.
|
|
16
|
+
|
|
17
|
+
## Scripts
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
bun run build
|
|
21
|
+
bun run test
|
|
22
|
+
bun run clean
|
|
23
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Default recording buffer: 30 seconds at 60fps (1800 frames) */
|
|
2
|
+
declare const DEFAULT_RECORD_BUFFER_FRAMES: number;
|
|
3
|
+
/** Default loop playback buffer: 4 seconds at 60fps (240 frames) */
|
|
4
|
+
declare const DEFAULT_LOOP_BUFFER_FRAMES: number;
|
|
5
|
+
|
|
6
|
+
export { DEFAULT_LOOP_BUFFER_FRAMES, DEFAULT_RECORD_BUFFER_FRAMES };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Default recording buffer: 30 seconds at 60fps (1800 frames) */
|
|
2
|
+
declare const DEFAULT_RECORD_BUFFER_FRAMES: number;
|
|
3
|
+
/** Default loop playback buffer: 4 seconds at 60fps (240 frames) */
|
|
4
|
+
declare const DEFAULT_LOOP_BUFFER_FRAMES: number;
|
|
5
|
+
|
|
6
|
+
export { DEFAULT_LOOP_BUFFER_FRAMES, DEFAULT_RECORD_BUFFER_FRAMES };
|
|
@@ -0,0 +1,34 @@
|
|
|
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/constants.ts
|
|
21
|
+
var constants_exports = {};
|
|
22
|
+
__export(constants_exports, {
|
|
23
|
+
DEFAULT_LOOP_BUFFER_FRAMES: () => DEFAULT_LOOP_BUFFER_FRAMES,
|
|
24
|
+
DEFAULT_RECORD_BUFFER_FRAMES: () => DEFAULT_RECORD_BUFFER_FRAMES
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(constants_exports);
|
|
27
|
+
var DEFAULT_RECORD_BUFFER_FRAMES = 60 * 30;
|
|
28
|
+
var DEFAULT_LOOP_BUFFER_FRAMES = 60 * 4;
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
DEFAULT_LOOP_BUFFER_FRAMES,
|
|
32
|
+
DEFAULT_RECORD_BUFFER_FRAMES
|
|
33
|
+
});
|
|
34
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/constants.ts"],"sourcesContent":["/** Default recording buffer: 30 seconds at 60fps (1800 frames) */\nexport const DEFAULT_RECORD_BUFFER_FRAMES = 60 * 30;\n\n/** Default loop playback buffer: 4 seconds at 60fps (240 frames) */\nexport const DEFAULT_LOOP_BUFFER_FRAMES = 60 * 4;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;AACO,IAAMA,+BAA+B,KAAK;AAG1C,IAAMC,6BAA6B,KAAK;","names":["DEFAULT_RECORD_BUFFER_FRAMES","DEFAULT_LOOP_BUFFER_FRAMES"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/constants.ts"],"sourcesContent":["/** Default recording buffer: 30 seconds at 60fps (1800 frames) */\nexport const DEFAULT_RECORD_BUFFER_FRAMES = 60 * 30;\n\n/** Default loop playback buffer: 4 seconds at 60fps (240 frames) */\nexport const DEFAULT_LOOP_BUFFER_FRAMES = 60 * 4;\n"],"mappings":";AACO,IAAMA,+BAA+B,KAAK;AAG1C,IAAMC,6BAA6B,KAAK;","names":["DEFAULT_RECORD_BUFFER_FRAMES","DEFAULT_LOOP_BUFFER_FRAMES"]}
|
|
@@ -0,0 +1,522 @@
|
|
|
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/core/index.ts
|
|
22
|
+
var core_exports = {};
|
|
23
|
+
__export(core_exports, {
|
|
24
|
+
TimeMachine: () => TimeMachine
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(core_exports);
|
|
27
|
+
|
|
28
|
+
// src/core/machine.ts
|
|
29
|
+
var import_diagnostics = require("@al8b/diagnostics");
|
|
30
|
+
|
|
31
|
+
// src/constants.ts
|
|
32
|
+
var DEFAULT_RECORD_BUFFER_FRAMES = 60 * 30;
|
|
33
|
+
var DEFAULT_LOOP_BUFFER_FRAMES = 60 * 4;
|
|
34
|
+
|
|
35
|
+
// src/utils.ts
|
|
36
|
+
function deepCopy(value, excluded) {
|
|
37
|
+
if (value == null) {
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
if (excluded && excluded.includes(value)) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(value)) {
|
|
47
|
+
const result = [];
|
|
48
|
+
for (let i = 0; i < value.length; i++) {
|
|
49
|
+
result[i] = deepCopy(value[i], excluded);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
if (typeof value === "object") {
|
|
54
|
+
const result = {};
|
|
55
|
+
for (const key in value) {
|
|
56
|
+
if (Object.hasOwn(value, key)) {
|
|
57
|
+
result[key] = deepCopy(value[key], excluded);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
return excluded ? null : value;
|
|
63
|
+
}
|
|
64
|
+
__name(deepCopy, "deepCopy");
|
|
65
|
+
|
|
66
|
+
// src/playback/player.ts
|
|
67
|
+
var StatePlayer = class {
|
|
68
|
+
static {
|
|
69
|
+
__name(this, "StatePlayer");
|
|
70
|
+
}
|
|
71
|
+
looping = false;
|
|
72
|
+
loopStart = 0;
|
|
73
|
+
loopIndex = 0;
|
|
74
|
+
loopLength;
|
|
75
|
+
loopCallback = null;
|
|
76
|
+
constructor(loopLength = DEFAULT_LOOP_BUFFER_FRAMES) {
|
|
77
|
+
this.loopLength = loopLength;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Check if currently looping
|
|
81
|
+
*/
|
|
82
|
+
isLooping() {
|
|
83
|
+
return this.looping;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Start loop playback
|
|
87
|
+
*/
|
|
88
|
+
startLoop(position, callback) {
|
|
89
|
+
this.looping = true;
|
|
90
|
+
this.loopStart = Math.max(position, 1);
|
|
91
|
+
this.loopIndex = 0;
|
|
92
|
+
this.loopCallback = callback;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Stop loop playback
|
|
96
|
+
*/
|
|
97
|
+
stopLoop() {
|
|
98
|
+
this.looping = false;
|
|
99
|
+
this.loopCallback = null;
|
|
100
|
+
return this.loopStart;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Update loop (call each frame)
|
|
104
|
+
* Returns position to replay, or null if not looping
|
|
105
|
+
*/
|
|
106
|
+
updateLoop() {
|
|
107
|
+
if (!this.looping) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
if (this.loopIndex === 0) {
|
|
111
|
+
this.loopIndex++;
|
|
112
|
+
return this.loopStart;
|
|
113
|
+
}
|
|
114
|
+
this.loopIndex++;
|
|
115
|
+
if (this.loopIndex > this.loopLength) {
|
|
116
|
+
this.loopIndex = 0;
|
|
117
|
+
}
|
|
118
|
+
return this.loopStart - this.loopIndex;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Execute loop callback
|
|
122
|
+
*/
|
|
123
|
+
executeCallback() {
|
|
124
|
+
if (this.loopCallback) {
|
|
125
|
+
this.loopCallback();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Restore state to target object
|
|
130
|
+
*/
|
|
131
|
+
restoreState(target, snapshot) {
|
|
132
|
+
if (!snapshot || !target) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
for (const key in target) {
|
|
136
|
+
if (Object.hasOwn(target, key) && !this.isProtectedKey(key)) {
|
|
137
|
+
delete target[key];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
for (const key in snapshot) {
|
|
141
|
+
if (Object.hasOwn(snapshot, key)) {
|
|
142
|
+
target[key] = deepCopy(snapshot[key]);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Check if key should be protected from restoration
|
|
148
|
+
*/
|
|
149
|
+
isProtectedKey(key) {
|
|
150
|
+
const protected_keys = [
|
|
151
|
+
"screen",
|
|
152
|
+
"audio",
|
|
153
|
+
"keyboard",
|
|
154
|
+
"mouse",
|
|
155
|
+
"touch",
|
|
156
|
+
"gamepad",
|
|
157
|
+
"system",
|
|
158
|
+
"storage",
|
|
159
|
+
"sprites",
|
|
160
|
+
"maps",
|
|
161
|
+
"sounds",
|
|
162
|
+
"music",
|
|
163
|
+
"assets",
|
|
164
|
+
"host",
|
|
165
|
+
"session",
|
|
166
|
+
"memory"
|
|
167
|
+
];
|
|
168
|
+
return protected_keys.includes(key);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// src/recording/recorder.ts
|
|
173
|
+
var StateRecorder = class {
|
|
174
|
+
static {
|
|
175
|
+
__name(this, "StateRecorder");
|
|
176
|
+
}
|
|
177
|
+
history = [];
|
|
178
|
+
recordIndex = 0;
|
|
179
|
+
recordLength = 0;
|
|
180
|
+
maxLength;
|
|
181
|
+
excluded = [];
|
|
182
|
+
constructor(maxLength = DEFAULT_RECORD_BUFFER_FRAMES) {
|
|
183
|
+
this.maxLength = maxLength;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Set objects to exclude from serialization
|
|
187
|
+
*/
|
|
188
|
+
setExcluded(excluded) {
|
|
189
|
+
this.excluded = excluded;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Record a state snapshot
|
|
193
|
+
*/
|
|
194
|
+
record(state) {
|
|
195
|
+
const snapshot = this.makeStorableState(state);
|
|
196
|
+
this.history[this.recordIndex++] = snapshot;
|
|
197
|
+
this.recordLength = Math.min(this.recordLength + 1, this.maxLength);
|
|
198
|
+
if (this.recordIndex >= this.maxLength) {
|
|
199
|
+
this.recordIndex = 0;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Get state at specific position (0 = most recent)
|
|
204
|
+
*/
|
|
205
|
+
getState(position) {
|
|
206
|
+
if (position >= this.recordLength) {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
const index = (this.recordIndex - position - 1 + this.maxLength) % this.maxLength;
|
|
210
|
+
return this.history[index];
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Get current record length
|
|
214
|
+
*/
|
|
215
|
+
getLength() {
|
|
216
|
+
return this.recordLength;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Get maximum record length
|
|
220
|
+
*/
|
|
221
|
+
getMaxLength() {
|
|
222
|
+
return this.maxLength;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Clear all recorded history
|
|
226
|
+
*/
|
|
227
|
+
clear() {
|
|
228
|
+
this.history = [];
|
|
229
|
+
this.recordIndex = 0;
|
|
230
|
+
this.recordLength = 0;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Trim history to specific position
|
|
234
|
+
*/
|
|
235
|
+
trimTo(position) {
|
|
236
|
+
if (position >= this.recordLength) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const histo = [];
|
|
240
|
+
const start = this.recordLength;
|
|
241
|
+
const end = position + 1;
|
|
242
|
+
for (let i = start; i >= end; i--) {
|
|
243
|
+
const index = (this.recordIndex - i + this.maxLength) % this.maxLength;
|
|
244
|
+
histo.push(this.history[index]);
|
|
245
|
+
}
|
|
246
|
+
this.history = histo;
|
|
247
|
+
this.recordIndex = this.history.length;
|
|
248
|
+
this.recordLength = this.history.length;
|
|
249
|
+
}
|
|
250
|
+
makeStorableState(value) {
|
|
251
|
+
return deepCopy(value, this.excluded);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// src/core/machine.ts
|
|
256
|
+
var TimeMachine = class {
|
|
257
|
+
static {
|
|
258
|
+
__name(this, "TimeMachine");
|
|
259
|
+
}
|
|
260
|
+
runtime;
|
|
261
|
+
recorder;
|
|
262
|
+
player;
|
|
263
|
+
recording = false;
|
|
264
|
+
replayPosition = 0;
|
|
265
|
+
statusCallback;
|
|
266
|
+
constructor(runtime) {
|
|
267
|
+
this.runtime = runtime;
|
|
268
|
+
this.recorder = new StateRecorder(DEFAULT_RECORD_BUFFER_FRAMES);
|
|
269
|
+
this.player = new StatePlayer(DEFAULT_LOOP_BUFFER_FRAMES);
|
|
270
|
+
this.setupExclusions();
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Setup objects to exclude from recording
|
|
274
|
+
*/
|
|
275
|
+
setupExclusions() {
|
|
276
|
+
const excluded = [];
|
|
277
|
+
if (this.runtime.vm?.context?.global) {
|
|
278
|
+
const global = this.runtime.vm.context.global;
|
|
279
|
+
if (global.random) excluded.push(global.random);
|
|
280
|
+
if (global.screen) excluded.push(global.screen);
|
|
281
|
+
if (global.audio) excluded.push(global.audio);
|
|
282
|
+
if (global.keyboard) excluded.push(global.keyboard);
|
|
283
|
+
if (global.mouse) excluded.push(global.mouse);
|
|
284
|
+
if (global.touch) excluded.push(global.touch);
|
|
285
|
+
if (global.gamepad) excluded.push(global.gamepad);
|
|
286
|
+
if (global.system) excluded.push(global.system);
|
|
287
|
+
if (global.storage) excluded.push(global.storage);
|
|
288
|
+
if (global.host) excluded.push(global.host);
|
|
289
|
+
if (global.session) excluded.push(global.session);
|
|
290
|
+
if (global.memory) excluded.push(global.memory);
|
|
291
|
+
}
|
|
292
|
+
this.recorder.setExcluded(excluded);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Step function - call each frame
|
|
296
|
+
*/
|
|
297
|
+
step() {
|
|
298
|
+
if (!this.recording) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
try {
|
|
302
|
+
if (this.replayPosition !== 0) {
|
|
303
|
+
this.recorder.trimTo(this.replayPosition);
|
|
304
|
+
if (this.player.isLooping()) {
|
|
305
|
+
this.player.startLoop(this.recorder.getLength(), () => this.loopCallback());
|
|
306
|
+
}
|
|
307
|
+
this.replayPosition = 0;
|
|
308
|
+
}
|
|
309
|
+
if (this.runtime.vm?.context?.global) {
|
|
310
|
+
this.recorder.record(this.runtime.vm.context.global);
|
|
311
|
+
}
|
|
312
|
+
this.sendStatus();
|
|
313
|
+
} catch (err) {
|
|
314
|
+
(0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7082, {
|
|
315
|
+
error: String(err)
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Handle incoming messages
|
|
321
|
+
*/
|
|
322
|
+
messageReceived(data) {
|
|
323
|
+
switch (data.command) {
|
|
324
|
+
case "start_recording":
|
|
325
|
+
this.startRecording();
|
|
326
|
+
break;
|
|
327
|
+
case "stop_recording":
|
|
328
|
+
this.stopRecording();
|
|
329
|
+
break;
|
|
330
|
+
case "step_backward":
|
|
331
|
+
this.stepBackward();
|
|
332
|
+
break;
|
|
333
|
+
case "step_forward":
|
|
334
|
+
this.stepForward();
|
|
335
|
+
break;
|
|
336
|
+
case "replay_position":
|
|
337
|
+
if (data.position != null) {
|
|
338
|
+
this.setReplayPosition(data.position);
|
|
339
|
+
}
|
|
340
|
+
break;
|
|
341
|
+
case "start_looping":
|
|
342
|
+
this.startLooping();
|
|
343
|
+
break;
|
|
344
|
+
case "stop_looping":
|
|
345
|
+
this.stopLooping();
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Start recording
|
|
351
|
+
*/
|
|
352
|
+
startRecording() {
|
|
353
|
+
if (this.recording) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
this.recording = true;
|
|
358
|
+
this.recorder.clear();
|
|
359
|
+
this.replayPosition = 0;
|
|
360
|
+
this.sendStatus();
|
|
361
|
+
} catch (err) {
|
|
362
|
+
this.recording = false;
|
|
363
|
+
(0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7083, {
|
|
364
|
+
error: String(err)
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Stop recording
|
|
370
|
+
*/
|
|
371
|
+
stopRecording() {
|
|
372
|
+
if (!this.recording) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
try {
|
|
376
|
+
this.recording = false;
|
|
377
|
+
this.sendStatus();
|
|
378
|
+
} catch (err) {
|
|
379
|
+
(0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7083, {
|
|
380
|
+
error: String(err)
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Step backward in time
|
|
386
|
+
*/
|
|
387
|
+
stepBackward() {
|
|
388
|
+
if (this.replayPosition + 1 >= this.recorder.getLength()) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
this.stopLooping();
|
|
392
|
+
this.replayPosition++;
|
|
393
|
+
this.replay();
|
|
394
|
+
this.sendStatus();
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Step forward in time
|
|
398
|
+
*/
|
|
399
|
+
stepForward() {
|
|
400
|
+
if (this.replayPosition <= 1) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
this.stopLooping();
|
|
404
|
+
this.replayPosition--;
|
|
405
|
+
this.replay();
|
|
406
|
+
this.sendStatus();
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Set replay position
|
|
410
|
+
*/
|
|
411
|
+
setReplayPosition(position) {
|
|
412
|
+
if (!isFinite(position) || position < 0) {
|
|
413
|
+
(0, import_diagnostics.reportRuntimeError)(this.runtime?.listener, import_diagnostics.APIErrorCode.E7081, {
|
|
414
|
+
value: String(position)
|
|
415
|
+
});
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
const pos = Math.round(position);
|
|
419
|
+
this.replayPosition = Math.max(2, Math.min(this.recorder.getLength() - 1, pos));
|
|
420
|
+
if (this.player.isLooping()) {
|
|
421
|
+
this.player.startLoop(this.replayPosition, () => this.loopCallback());
|
|
422
|
+
}
|
|
423
|
+
this.replay();
|
|
424
|
+
this.sendStatus();
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Start looping
|
|
428
|
+
*/
|
|
429
|
+
startLooping() {
|
|
430
|
+
if (this.recorder.getLength() === 0) {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
this.recording = false;
|
|
434
|
+
const position = Math.max(this.replayPosition, 1);
|
|
435
|
+
this.player.startLoop(position, () => this.loopCallback());
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Stop looping
|
|
439
|
+
*/
|
|
440
|
+
stopLooping() {
|
|
441
|
+
if (!this.player.isLooping()) {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
this.replayPosition = this.player.stopLoop();
|
|
445
|
+
this.sendStatus();
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Replay state at current position
|
|
449
|
+
*/
|
|
450
|
+
replay(skipDraw = false) {
|
|
451
|
+
if (this.replayPosition >= this.recorder.getLength()) {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const snapshot = this.recorder.getState(this.replayPosition);
|
|
455
|
+
if (snapshot && this.runtime.vm?.context?.global) {
|
|
456
|
+
this.player.restoreState(this.runtime.vm.context.global, snapshot);
|
|
457
|
+
}
|
|
458
|
+
if (!skipDraw) {
|
|
459
|
+
this.runtime.drawCall?.();
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Loop callback
|
|
464
|
+
*/
|
|
465
|
+
loopCallback() {
|
|
466
|
+
const position = this.player.updateLoop();
|
|
467
|
+
if (position !== null) {
|
|
468
|
+
this.replayPosition = position;
|
|
469
|
+
this.replay(true);
|
|
470
|
+
this.runtime.updateCall?.();
|
|
471
|
+
this.runtime.drawCall?.();
|
|
472
|
+
}
|
|
473
|
+
this.sendStatus();
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Send status update
|
|
477
|
+
*/
|
|
478
|
+
sendStatus() {
|
|
479
|
+
if (this.statusCallback) {
|
|
480
|
+
this.statusCallback({
|
|
481
|
+
recording: this.recording,
|
|
482
|
+
looping: this.player.isLooping(),
|
|
483
|
+
position: this.replayPosition,
|
|
484
|
+
length: this.recorder.getLength(),
|
|
485
|
+
max: this.recorder.getMaxLength()
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Advance the loop playback by one tick.
|
|
491
|
+
* Call once per real game frame (from the orchestrator's watch-step hook).
|
|
492
|
+
* No-op when not looping.
|
|
493
|
+
*/
|
|
494
|
+
loopStep() {
|
|
495
|
+
if (this.player.isLooping()) {
|
|
496
|
+
this.player.executeCallback();
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Set status callback
|
|
501
|
+
*/
|
|
502
|
+
onStatus(callback) {
|
|
503
|
+
this.statusCallback = callback;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Check if recording
|
|
507
|
+
*/
|
|
508
|
+
isRecording() {
|
|
509
|
+
return this.recording;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Check if looping
|
|
513
|
+
*/
|
|
514
|
+
isLooping() {
|
|
515
|
+
return this.player.isLooping();
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
519
|
+
0 && (module.exports = {
|
|
520
|
+
TimeMachine
|
|
521
|
+
});
|
|
522
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/index.ts","../../src/core/machine.ts","../../src/constants.ts","../../src/utils.ts","../../src/playback/player.ts","../../src/recording/recorder.ts"],"sourcesContent":["/**\n * Core module exports\n */\n\nexport type { TimeMachineRuntime } from \"./machine\";\nexport { TimeMachine } from \"./machine\";\n","/**\n * TimeMachine - Main time machine controller\n *\n * Responsibilities:\n * - Coordinate recording and playback\n * - Handle commands\n * - Manage state\n */\n\nimport { APIErrorCode, reportRuntimeError } from \"@al8b/diagnostics\";\nimport { DEFAULT_LOOP_BUFFER_FRAMES, DEFAULT_RECORD_BUFFER_FRAMES } from \"../constants\";\nimport { StatePlayer } from \"../playback\";\nimport { StateRecorder } from \"../recording\";\nimport type { TimeMachineMessage, TimeMachineStatus } from \"../types\";\n\nexport interface TimeMachineRuntime {\n\tvm?: {\n\t\tcontext?: {\n\t\t\tglobal?: any;\n\t\t};\n\t};\n\tupdateCall?: () => void;\n\tdrawCall?: () => void;\n}\n\nexport class TimeMachine {\n\tprivate runtime: TimeMachineRuntime;\n\tprivate recorder: StateRecorder;\n\tprivate player: StatePlayer;\n\tprivate recording = false;\n\tprivate replayPosition = 0;\n\tprivate statusCallback?: (status: TimeMachineStatus) => void;\n\n\tconstructor(runtime: TimeMachineRuntime) {\n\t\tthis.runtime = runtime;\n\t\tthis.recorder = new StateRecorder(DEFAULT_RECORD_BUFFER_FRAMES);\n\t\tthis.player = new StatePlayer(DEFAULT_LOOP_BUFFER_FRAMES);\n\n\t\t// Configure which objects should be excluded from state recording\n\t\tthis.setupExclusions();\n\t}\n\n\t/**\n\t * Setup objects to exclude from recording\n\t */\n\tprivate setupExclusions(): void {\n\t\tconst excluded: any[] = [];\n\n\t\tif (this.runtime.vm?.context?.global) {\n\t\t\tconst global = this.runtime.vm.context.global;\n\n\t\t\t// Exclude system APIs and non-serializable objects from recording\n\t\t\tif (global.random) excluded.push(global.random);\n\t\t\tif (global.screen) excluded.push(global.screen);\n\t\t\tif (global.audio) excluded.push(global.audio);\n\t\t\tif (global.keyboard) excluded.push(global.keyboard);\n\t\t\tif (global.mouse) excluded.push(global.mouse);\n\t\t\tif (global.touch) excluded.push(global.touch);\n\t\t\tif (global.gamepad) excluded.push(global.gamepad);\n\t\t\tif (global.system) excluded.push(global.system);\n\t\t\tif (global.storage) excluded.push(global.storage);\n\t\t\tif (global.host) excluded.push(global.host);\n\t\t\tif (global.session) excluded.push(global.session);\n\t\t\tif (global.memory) excluded.push(global.memory);\n\t\t}\n\n\t\tthis.recorder.setExcluded(excluded);\n\t}\n\n\t/**\n\t * Step function - call each frame\n\t */\n\tstep(): void {\n\t\tif (!this.recording) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\t// Apply replay position changes and update loop if active\n\t\t\tif (this.replayPosition !== 0) {\n\t\t\t\tthis.recorder.trimTo(this.replayPosition);\n\t\t\t\tif (this.player.isLooping()) {\n\t\t\t\t\tthis.player.startLoop(this.recorder.getLength(), () => this.loopCallback());\n\t\t\t\t}\n\t\t\t\tthis.replayPosition = 0;\n\t\t\t}\n\n\t\t\t// Capture current global state snapshot for time travel\n\t\t\tif (this.runtime.vm?.context?.global) {\n\t\t\t\tthis.recorder.record(this.runtime.vm.context.global);\n\t\t\t}\n\n\t\t\tthis.sendStatus();\n\t\t} catch (err) {\n\t\t\treportRuntimeError((this.runtime as any)?.listener, APIErrorCode.E7082, { error: String(err) });\n\t\t}\n\t}\n\n\t/**\n\t * Handle incoming messages\n\t */\n\tmessageReceived(data: TimeMachineMessage): void {\n\t\tswitch (data.command) {\n\t\t\tcase \"start_recording\":\n\t\t\t\tthis.startRecording();\n\t\t\t\tbreak;\n\n\t\t\tcase \"stop_recording\":\n\t\t\t\tthis.stopRecording();\n\t\t\t\tbreak;\n\n\t\t\tcase \"step_backward\":\n\t\t\t\tthis.stepBackward();\n\t\t\t\tbreak;\n\n\t\t\tcase \"step_forward\":\n\t\t\t\tthis.stepForward();\n\t\t\t\tbreak;\n\n\t\t\tcase \"replay_position\":\n\t\t\t\tif (data.position != null) {\n\t\t\t\t\tthis.setReplayPosition(data.position);\n\t\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\tcase \"start_looping\":\n\t\t\t\tthis.startLooping();\n\t\t\t\tbreak;\n\n\t\t\tcase \"stop_looping\":\n\t\t\t\tthis.stopLooping();\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\t/**\n\t * Start recording\n\t */\n\tprivate startRecording(): void {\n\t\tif (this.recording) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tthis.recording = true;\n\t\t\tthis.recorder.clear();\n\t\t\tthis.replayPosition = 0;\n\t\t\tthis.sendStatus();\n\t\t} catch (err) {\n\t\t\tthis.recording = false;\n\t\t\treportRuntimeError((this.runtime as any)?.listener, APIErrorCode.E7083, { error: String(err) });\n\t\t}\n\t}\n\n\t/**\n\t * Stop recording\n\t */\n\tprivate stopRecording(): void {\n\t\tif (!this.recording) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tthis.recording = false;\n\t\t\tthis.sendStatus();\n\t\t} catch (err) {\n\t\t\treportRuntimeError((this.runtime as any)?.listener, APIErrorCode.E7083, { error: String(err) });\n\t\t}\n\t}\n\n\t/**\n\t * Step backward in time\n\t */\n\tprivate stepBackward(): void {\n\t\tif (this.replayPosition + 1 >= this.recorder.getLength()) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.stopLooping();\n\t\tthis.replayPosition++;\n\t\tthis.replay();\n\t\tthis.sendStatus();\n\t}\n\n\t/**\n\t * Step forward in time\n\t */\n\tprivate stepForward(): void {\n\t\tif (this.replayPosition <= 1) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.stopLooping();\n\t\tthis.replayPosition--;\n\t\tthis.replay();\n\t\tthis.sendStatus();\n\t}\n\n\t/**\n\t * Set replay position\n\t */\n\tprivate setReplayPosition(position: number): void {\n\t\t// Validate time value is finite and non-negative\n\t\tif (!isFinite(position) || position < 0) {\n\t\t\treportRuntimeError((this.runtime as any)?.listener, APIErrorCode.E7081, { value: String(position) });\n\t\t\treturn;\n\t\t}\n\n\t\tconst pos = Math.round(position);\n\t\tthis.replayPosition = Math.max(2, Math.min(this.recorder.getLength() - 1, pos));\n\n\t\tif (this.player.isLooping()) {\n\t\t\tthis.player.startLoop(this.replayPosition, () => this.loopCallback());\n\t\t}\n\n\t\tthis.replay();\n\t\tthis.sendStatus();\n\t}\n\n\t/**\n\t * Start looping\n\t */\n\tprivate startLooping(): void {\n\t\tif (this.recorder.getLength() === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.recording = false;\n\t\tconst position = Math.max(this.replayPosition, 1);\n\t\tthis.player.startLoop(position, () => this.loopCallback());\n\t}\n\n\t/**\n\t * Stop looping\n\t */\n\tprivate stopLooping(): void {\n\t\tif (!this.player.isLooping()) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.replayPosition = this.player.stopLoop();\n\t\tthis.sendStatus();\n\t}\n\n\t/**\n\t * Replay state at current position\n\t */\n\tprivate replay(skipDraw = false): void {\n\t\tif (this.replayPosition >= this.recorder.getLength()) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst snapshot = this.recorder.getState(this.replayPosition);\n\t\tif (snapshot && this.runtime.vm?.context?.global) {\n\t\t\tthis.player.restoreState(this.runtime.vm.context.global, snapshot);\n\t\t}\n\n\t\tif (!skipDraw) {\n\t\t\tthis.runtime.drawCall?.();\n\t\t}\n\t}\n\n\t/**\n\t * Loop callback\n\t */\n\tprivate loopCallback(): void {\n\t\tconst position = this.player.updateLoop();\n\t\tif (position !== null) {\n\t\t\tthis.replayPosition = position;\n\t\t\tthis.replay(true);\n\t\t\tthis.runtime.updateCall?.();\n\t\t\tthis.runtime.drawCall?.();\n\t\t}\n\t\tthis.sendStatus();\n\t}\n\n\t/**\n\t * Send status update\n\t */\n\tprivate sendStatus(): void {\n\t\tif (this.statusCallback) {\n\t\t\tthis.statusCallback({\n\t\t\t\trecording: this.recording,\n\t\t\t\tlooping: this.player.isLooping(),\n\t\t\t\tposition: this.replayPosition,\n\t\t\t\tlength: this.recorder.getLength(),\n\t\t\t\tmax: this.recorder.getMaxLength(),\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Advance the loop playback by one tick.\n\t * Call once per real game frame (from the orchestrator's watch-step hook).\n\t * No-op when not looping.\n\t */\n\tloopStep(): void {\n\t\tif (this.player.isLooping()) {\n\t\t\tthis.player.executeCallback();\n\t\t}\n\t}\n\n\t/**\n\t * Set status callback\n\t */\n\tonStatus(callback: (status: TimeMachineStatus) => void): void {\n\t\tthis.statusCallback = callback;\n\t}\n\n\t/**\n\t * Check if recording\n\t */\n\tisRecording(): boolean {\n\t\treturn this.recording;\n\t}\n\n\t/**\n\t * Check if looping\n\t */\n\tisLooping(): boolean {\n\t\treturn this.player.isLooping();\n\t}\n}\n","/** Default recording buffer: 30 seconds at 60fps (1800 frames) */\nexport const DEFAULT_RECORD_BUFFER_FRAMES = 60 * 30;\n\n/** Default loop playback buffer: 4 seconds at 60fps (240 frames) */\nexport const DEFAULT_LOOP_BUFFER_FRAMES = 60 * 4;\n","/**\n * Deep copy a value, optionally skipping excluded references.\n *\n * @param value - The value to deep copy\n * @param excluded - Optional array of object references to skip (replaced with null)\n */\nexport function deepCopy(value: any, excluded?: any[]): any {\n\tif (value == null) {\n\t\treturn value;\n\t}\n\n\tif (excluded && excluded.includes(value)) {\n\t\treturn null;\n\t}\n\n\tif (typeof value === \"string\" || typeof value === \"number\" || typeof value === \"boolean\") {\n\t\treturn value;\n\t}\n\n\tif (Array.isArray(value)) {\n\t\tconst result: any[] = [];\n\t\tfor (let i = 0; i < value.length; i++) {\n\t\t\tresult[i] = deepCopy(value[i], excluded);\n\t\t}\n\t\treturn result;\n\t}\n\n\tif (typeof value === \"object\") {\n\t\tconst result: any = {};\n\t\tfor (const key in value) {\n\t\t\tif (Object.hasOwn(value, key)) {\n\t\t\t\tresult[key] = deepCopy(value[key], excluded);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\n\t// Non-serializable types: return null when filtering, passthrough otherwise\n\treturn excluded ? null : value;\n}\n","/**\n * StatePlayer - Handles replay and loop playback\n *\n * Responsibilities:\n * - Restore state snapshots\n * - Manage loop playback\n * - Control playback position\n */\nimport { DEFAULT_LOOP_BUFFER_FRAMES } from \"../constants\";\nimport { deepCopy } from \"../utils\";\n\nimport type { StateSnapshot } from \"../types\";\n\nexport class StatePlayer {\n\tprivate looping = false;\n\tprivate loopStart = 0;\n\tprivate loopIndex = 0;\n\tprivate loopLength: number;\n\tprivate loopCallback: (() => void) | null = null;\n\n\tconstructor(loopLength = DEFAULT_LOOP_BUFFER_FRAMES) {\n\t\tthis.loopLength = loopLength;\n\t}\n\n\t/**\n\t * Check if currently looping\n\t */\n\tisLooping(): boolean {\n\t\treturn this.looping;\n\t}\n\n\t/**\n\t * Start loop playback\n\t */\n\tstartLoop(position: number, callback: () => void): void {\n\t\tthis.looping = true;\n\t\tthis.loopStart = Math.max(position, 1);\n\t\tthis.loopIndex = 0;\n\t\tthis.loopCallback = callback;\n\t}\n\n\t/**\n\t * Stop loop playback\n\t */\n\tstopLoop(): number {\n\t\tthis.looping = false;\n\t\tthis.loopCallback = null;\n\t\treturn this.loopStart;\n\t}\n\n\t/**\n\t * Update loop (call each frame)\n\t * Returns position to replay, or null if not looping\n\t */\n\tupdateLoop(): number | null {\n\t\tif (!this.looping) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (this.loopIndex === 0) {\n\t\t\tthis.loopIndex++;\n\t\t\treturn this.loopStart;\n\t\t}\n\n\t\tthis.loopIndex++;\n\t\tif (this.loopIndex > this.loopLength) {\n\t\t\tthis.loopIndex = 0;\n\t\t}\n\n\t\treturn this.loopStart - this.loopIndex;\n\t}\n\n\t/**\n\t * Execute loop callback\n\t */\n\texecuteCallback(): void {\n\t\tif (this.loopCallback) {\n\t\t\tthis.loopCallback();\n\t\t}\n\t}\n\n\t/**\n\t * Restore state to target object\n\t */\n\trestoreState(target: any, snapshot: StateSnapshot): void {\n\t\tif (!snapshot || !target) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Remove all existing properties except protected system APIs\n\t\tfor (const key in target) {\n\t\t\tif (Object.hasOwn(target, key) && !this.isProtectedKey(key)) {\n\t\t\t\tdelete target[key];\n\t\t\t}\n\t\t}\n\n\t\t// Apply snapshot properties to target object via deep copy\n\t\tfor (const key in snapshot) {\n\t\t\tif (Object.hasOwn(snapshot, key)) {\n\t\t\t\ttarget[key] = deepCopy(snapshot[key]);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Check if key should be protected from restoration\n\t */\n\tprivate isProtectedKey(key: string): boolean {\n\t\t// Prevent system APIs and runtime objects from being overwritten during restore\n\t\tconst protected_keys = [\n\t\t\t\"screen\",\n\t\t\t\"audio\",\n\t\t\t\"keyboard\",\n\t\t\t\"mouse\",\n\t\t\t\"touch\",\n\t\t\t\"gamepad\",\n\t\t\t\"system\",\n\t\t\t\"storage\",\n\t\t\t\"sprites\",\n\t\t\t\"maps\",\n\t\t\t\"sounds\",\n\t\t\t\"music\",\n\t\t\t\"assets\",\n\t\t\t\"host\",\n\t\t\t\"session\",\n\t\t\t\"memory\",\n\t\t];\n\t\treturn protected_keys.includes(key);\n\t}\n\n}\n","/**\n * StateRecorder - Records game state history\n *\n * Responsibilities:\n * - Capture state snapshots\n * - Manage circular buffer\n * - Exclude non-serializable objects\n */\nimport { DEFAULT_RECORD_BUFFER_FRAMES } from \"../constants\";\nimport { deepCopy } from \"../utils\";\n\nimport type { StateSnapshot } from \"../types\";\n\nexport class StateRecorder {\n\tprivate history: StateSnapshot[] = [];\n\tprivate recordIndex = 0;\n\tprivate recordLength = 0;\n\tprivate maxLength: number;\n\tprivate excluded: any[] = [];\n\n\tconstructor(maxLength = DEFAULT_RECORD_BUFFER_FRAMES) {\n\t\tthis.maxLength = maxLength;\n\t}\n\n\t/**\n\t * Set objects to exclude from serialization\n\t */\n\tsetExcluded(excluded: any[]): void {\n\t\tthis.excluded = excluded;\n\t}\n\n\t/**\n\t * Record a state snapshot\n\t */\n\trecord(state: any): void {\n\t\tconst snapshot = this.makeStorableState(state);\n\t\tthis.history[this.recordIndex++] = snapshot;\n\t\tthis.recordLength = Math.min(this.recordLength + 1, this.maxLength);\n\n\t\tif (this.recordIndex >= this.maxLength) {\n\t\t\tthis.recordIndex = 0;\n\t\t}\n\t}\n\n\t/**\n\t * Get state at specific position (0 = most recent)\n\t */\n\tgetState(position: number): StateSnapshot | null {\n\t\tif (position >= this.recordLength) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst index = (this.recordIndex - position - 1 + this.maxLength) % this.maxLength;\n\t\treturn this.history[index];\n\t}\n\n\t/**\n\t * Get current record length\n\t */\n\tgetLength(): number {\n\t\treturn this.recordLength;\n\t}\n\n\t/**\n\t * Get maximum record length\n\t */\n\tgetMaxLength(): number {\n\t\treturn this.maxLength;\n\t}\n\n\t/**\n\t * Clear all recorded history\n\t */\n\tclear(): void {\n\t\tthis.history = [];\n\t\tthis.recordIndex = 0;\n\t\tthis.recordLength = 0;\n\t}\n\n\t/**\n\t * Trim history to specific position\n\t */\n\ttrimTo(position: number): void {\n\t\tif (position >= this.recordLength) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst histo: StateSnapshot[] = [];\n\t\tconst start = this.recordLength;\n\t\tconst end = position + 1;\n\n\t\tfor (let i = start; i >= end; i--) {\n\t\t\tconst index = (this.recordIndex - i + this.maxLength) % this.maxLength;\n\t\t\thisto.push(this.history[index]);\n\t\t}\n\n\t\tthis.history = histo;\n\t\tthis.recordIndex = this.history.length;\n\t\tthis.recordLength = this.history.length;\n\t}\n\n\tprivate makeStorableState(value: any): any {\n\t\treturn deepCopy(value, this.excluded);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACSA,yBAAiD;;;ACR1C,IAAMA,+BAA+B,KAAK;AAG1C,IAAMC,6BAA6B,KAAK;;;ACExC,SAASC,SAASC,OAAYC,UAAgB;AACpD,MAAID,SAAS,MAAM;AAClB,WAAOA;EACR;AAEA,MAAIC,YAAYA,SAASC,SAASF,KAAAA,GAAQ;AACzC,WAAO;EACR;AAEA,MAAI,OAAOA,UAAU,YAAY,OAAOA,UAAU,YAAY,OAAOA,UAAU,WAAW;AACzF,WAAOA;EACR;AAEA,MAAIG,MAAMC,QAAQJ,KAAAA,GAAQ;AACzB,UAAMK,SAAgB,CAAA;AACtB,aAASC,IA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nC,WAAU;EAChB;;;;EAKQA,aAAmB;AAC1B,QAAI,KAAK/B,gBAAgB;AACxB,WAAKA,eAAe;QACnBF,WAAW,KAAKA;QAChBuE,SAAS,KAAKxE,OAAO6B,UAAS;QAC9BoB,UAAU,KAAK/C;QACfuE,QAAQ,KAAK1E,SAASgC,UAAS;QAC/B+B,KAAK,KAAK/D,SAAS2E,aAAY;MAChC,CAAA;IACD;EACD;;;;;;EAOAC,WAAiB;AAChB,QAAI,KAAK3E,OAAO6B,UAAS,GAAI;AAC5B,WAAK7B,OAAO4E,gBAAe;IAC5B;EACD;;;;EAKAC,SAASC,UAAqD;AAC7D,SAAK3E,iBAAiB2E;EACvB;;;;EAKAC,cAAuB;AACtB,WAAO,KAAK9E;EACb;;;;EAKA4B,YAAqB;AACpB,WAAO,KAAK7B,OAAO6B,UAAS;EAC7B;AACD;","names":["DEFAULT_RECORD_BUFFER_FRAMES","DEFAULT_LOOP_BUFFER_FRAMES","deepCopy","value","excluded","includes","Array","isArray","result","i","length","key","Object","hasOwn","StatePlayer","looping","loopStart","loopIndex","loopLength","loopCallback","DEFAULT_LOOP_BUFFER_FRAMES","isLooping","startLoop","position","callback","Math","max","stopLoop","updateLoop","executeCallback","restoreState","target","snapshot","key","Object","hasOwn","isProtectedKey","deepCopy","protected_keys","includes","StateRecorder","history","recordIndex","recordLength","maxLength","excluded","DEFAULT_RECORD_BUFFER_FRAMES","setExcluded","record","state","snapshot","makeStorableState","Math","min","getState","position","index","getLength","getMaxLength","clear","trimTo","histo","start","end","i","push","length","value","deepCopy","TimeMachine","runtime","recorder","player","recording","replayPosition","statusCallback","StateRecorder","DEFAULT_RECORD_BUFFER_FRAMES","StatePlayer","DEFAULT_LOOP_BUFFER_FRAMES","setupExclusions","excluded","vm","context","global","random","push","screen","audio","keyboard","mouse","touch","gamepad","system","storage","host","session","memory","setExcluded","step","trimTo","isLooping","startLoop","getLength","loopCallback","record","sendStatus","err","reportRuntimeError","listener","APIErrorCode","E7082","error","String","messageReceived","data","command","startRecording","stopRecording","stepBackward","stepForward","position","setReplayPosition","startLooping","stopLooping","clear","E7083","replay","isFinite","E7081","value","pos","Math","round","max","min","stopLoop","skipDraw","snapshot","getState","restoreState","drawCall","updateLoop","updateCall","looping","length","getMaxLength","loopStep","executeCallback","onStatus","callback","isRecording"]}
|