@tinyactors/scxmljs 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/LICENSE +21 -0
- package/README.md +174 -0
- package/THIRD_PARTY_NOTICES +63 -0
- package/dist/bridge.d.ts +44 -0
- package/dist/bridge.js +254 -0
- package/dist/bridge.js.map +1 -0
- package/dist/clock.d.ts +113 -0
- package/dist/clock.js +310 -0
- package/dist/clock.js.map +1 -0
- package/dist/common.d.ts +15 -0
- package/dist/common.js +14 -0
- package/dist/common.js.map +1 -0
- package/dist/datamodel-base.d.ts +122 -0
- package/dist/datamodel-base.js +268 -0
- package/dist/datamodel-base.js.map +1 -0
- package/dist/datamodel-quickjs.d.ts +21 -0
- package/dist/datamodel-quickjs.js +286 -0
- package/dist/datamodel-quickjs.js.map +1 -0
- package/dist/datamodel-trusted.d.ts +1 -0
- package/dist/datamodel-trusted.js +149 -0
- package/dist/datamodel-trusted.js.map +1 -0
- package/dist/diagnostics.d.ts +39 -0
- package/dist/diagnostics.js +154 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/element-events.d.ts +85 -0
- package/dist/element-events.js +129 -0
- package/dist/element-events.js.map +1 -0
- package/dist/events.d.ts +31 -0
- package/dist/events.js +33 -0
- package/dist/events.js.map +1 -0
- package/dist/explorer/element.d.ts +150 -0
- package/dist/explorer/element.js +1791 -0
- package/dist/explorer/element.js.map +1 -0
- package/dist/explorer/layout.d.ts +43 -0
- package/dist/explorer/layout.js +50 -0
- package/dist/explorer/layout.js.map +1 -0
- package/dist/explorer/strings.d.ts +275 -0
- package/dist/explorer/strings.js +135 -0
- package/dist/explorer/strings.js.map +1 -0
- package/dist/explorer/styles.d.ts +10 -0
- package/dist/explorer/styles.js +265 -0
- package/dist/explorer/styles.js.map +1 -0
- package/dist/explorer/viewmodel.d.ts +284 -0
- package/dist/explorer/viewmodel.js +481 -0
- package/dist/explorer/viewmodel.js.map +1 -0
- package/dist/explorer.d.ts +21 -0
- package/dist/explorer.js +22 -0
- package/dist/explorer.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/model.d.ts +157 -0
- package/dist/model.js +485 -0
- package/dist/model.js.map +1 -0
- package/dist/session-events.d.ts +136 -0
- package/dist/session-events.js +114 -0
- package/dist/session-events.js.map +1 -0
- package/dist/session.d.ts +412 -0
- package/dist/session.js +1399 -0
- package/dist/session.js.map +1 -0
- package/dist/themes/tinyactors.css +44 -0
- package/dist/trusted.d.ts +15 -0
- package/dist/trusted.js +30 -0
- package/dist/trusted.js.map +1 -0
- package/dist/ui/announcer.d.ts +22 -0
- package/dist/ui/announcer.js +31 -0
- package/dist/ui/announcer.js.map +1 -0
- package/dist/ui/svg.d.ts +8 -0
- package/dist/ui/svg.js +29 -0
- package/dist/ui/svg.js.map +1 -0
- package/dist/ui/theme.d.ts +34 -0
- package/dist/ui/theme.js +151 -0
- package/dist/ui/theme.js.map +1 -0
- package/dist/view/element.d.ts +172 -0
- package/dist/view/element.js +1045 -0
- package/dist/view/element.js.map +1 -0
- package/dist/view/layout.d.ts +154 -0
- package/dist/view/layout.js +1122 -0
- package/dist/view/layout.js.map +1 -0
- package/dist/view/strings.d.ts +72 -0
- package/dist/view/strings.js +48 -0
- package/dist/view/strings.js.map +1 -0
- package/dist/view/styles.d.ts +1 -0
- package/dist/view/styles.js +115 -0
- package/dist/view/styles.js.map +1 -0
- package/dist/view.d.ts +20 -0
- package/dist/view.js +21 -0
- package/dist/view.js.map +1 -0
- package/package.json +87 -0
package/dist/clock.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timers and deferred work. Sessions never touch setTimeout/queueMicrotask
|
|
3
|
+
* directly, so tests (and the conformance runner) can use a VirtualClock:
|
|
4
|
+
* deterministic, and delayed events fire without real waiting.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Where a session gets its time: timers for delayed `<send>`s and deferred
|
|
8
|
+
* work for external events. Pass one as `SessionOptions.clock`.
|
|
9
|
+
*/
|
|
10
|
+
export interface Clock {
|
|
11
|
+
/** The current time in ms (real or virtual). */
|
|
12
|
+
now(): number;
|
|
13
|
+
/** Run `fn` after `ms` milliseconds of this clock's time; returns a handle for `clearTimeout`. */
|
|
14
|
+
setTimeout(fn: () => void, ms: number): unknown;
|
|
15
|
+
/** Cancel a timer from `setTimeout`. Unknown or already-fired handles are ignored. */
|
|
16
|
+
clearTimeout(handle: unknown): void;
|
|
17
|
+
/** Run `fn` soon, after the current task (a microtask on the real clock). */
|
|
18
|
+
defer(fn: () => void): void;
|
|
19
|
+
}
|
|
20
|
+
/** The default clock: `Date.now`, the global `setTimeout` and microtasks. */
|
|
21
|
+
export declare const realClock: Clock;
|
|
22
|
+
/**
|
|
23
|
+
* Deterministic time for tests and playback. Timers live in a binary heap
|
|
24
|
+
* (O(log n) to schedule and fire) and cancelling is O(1), so a clock with
|
|
25
|
+
* hundreds of thousands of pending timers stays fast.
|
|
26
|
+
*/
|
|
27
|
+
export declare class VirtualClock implements Clock {
|
|
28
|
+
private time;
|
|
29
|
+
private seq;
|
|
30
|
+
/** timers that are still scheduled, by id */
|
|
31
|
+
private live;
|
|
32
|
+
/** min-heap of timers; cancelled ones are skipped lazily */
|
|
33
|
+
private heap;
|
|
34
|
+
private deferred;
|
|
35
|
+
private head;
|
|
36
|
+
now(): number;
|
|
37
|
+
setTimeout(fn: () => void, ms: number): number;
|
|
38
|
+
clearTimeout(handle: unknown): void;
|
|
39
|
+
defer(fn: () => void): void;
|
|
40
|
+
/** True when nothing is scheduled: no deferred work and no pending timers. */
|
|
41
|
+
get idle(): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Runs deferred work, then fires timers in time order, advancing virtual
|
|
44
|
+
* time, until nothing is pending or the next timer is later than `until`
|
|
45
|
+
* (absolute virtual ms). Returns the number of tasks run.
|
|
46
|
+
*/
|
|
47
|
+
run(until?: number, maxTasks?: number): number;
|
|
48
|
+
/** Advance by `ms`, running everything due. */
|
|
49
|
+
advance(ms: number): number;
|
|
50
|
+
/**
|
|
51
|
+
* Run exactly one task: the oldest deferred one, else the earliest timer
|
|
52
|
+
* (advancing virtual time to it). Returns false when nothing is pending.
|
|
53
|
+
*/
|
|
54
|
+
runNext(): boolean;
|
|
55
|
+
/** Virtual time of the next timer, if any. */
|
|
56
|
+
get nextTimerAt(): number | undefined;
|
|
57
|
+
/** Number of pending tasks (deferred work + timers). */
|
|
58
|
+
get pending(): number;
|
|
59
|
+
/** Deferred work waiting to run now (e.g. queued external events). */
|
|
60
|
+
get ready(): number;
|
|
61
|
+
private takeDeferred;
|
|
62
|
+
/** Remove `timer` (the heap's top) and move time to it. */
|
|
63
|
+
private fire;
|
|
64
|
+
/** The earliest live timer, dropping cancelled ones from the top. */
|
|
65
|
+
private peek;
|
|
66
|
+
private push;
|
|
67
|
+
private pop;
|
|
68
|
+
private rebuild;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A VirtualClock that real time drives forward — at an adjustable speed —
|
|
72
|
+
* and that can be paused and stepped. For visualisations: everything that
|
|
73
|
+
* takes its time from this clock (sessions, delayed sends, fake services,
|
|
74
|
+
* simulated environments) slows down, stops and single-steps together.
|
|
75
|
+
*
|
|
76
|
+
* const clock = new PlaybackClock({ speed: 0.5 });
|
|
77
|
+
* const session = await createSession(src, { clock });
|
|
78
|
+
* clock.pause(); clock.step(); clock.play();
|
|
79
|
+
*/
|
|
80
|
+
export declare class PlaybackClock extends VirtualClock {
|
|
81
|
+
#private;
|
|
82
|
+
/** `speed` defaults to 1; `playing` (default true) starts driving the clock right away. */
|
|
83
|
+
constructor(opts?: {
|
|
84
|
+
speed?: number;
|
|
85
|
+
playing?: boolean;
|
|
86
|
+
});
|
|
87
|
+
/** Whether real time is currently driving the clock. */
|
|
88
|
+
get playing(): boolean;
|
|
89
|
+
/** Virtual ms per real ms (1 = real time, 0.5 = half speed). */
|
|
90
|
+
get speed(): number;
|
|
91
|
+
/** Change the speed; negative values are treated as 0. */
|
|
92
|
+
set speed(v: number);
|
|
93
|
+
/** Let real time drive the clock (at `speed`). */
|
|
94
|
+
play(): void;
|
|
95
|
+
/** Stop time: nothing runs until `play()` or `step()`. */
|
|
96
|
+
pause(): void;
|
|
97
|
+
/** Play if paused, pause if playing. */
|
|
98
|
+
toggle(): void;
|
|
99
|
+
/**
|
|
100
|
+
* Run tasks one at a time — jumping virtual time to the next timer when
|
|
101
|
+
* nothing is ready — until `until()` holds after a task, or nothing is
|
|
102
|
+
* pending. Pauses first. Returns the number of tasks run.
|
|
103
|
+
*/
|
|
104
|
+
step(until?: () => boolean, maxTasks?: number): number;
|
|
105
|
+
/** Like `VirtualClock.setTimeout`, and notifies subscribers that work is queued. */
|
|
106
|
+
setTimeout(fn: () => void, ms: number): number;
|
|
107
|
+
/** Like `VirtualClock.defer`, and notifies subscribers that work is queued. */
|
|
108
|
+
defer(fn: () => void): void;
|
|
109
|
+
/** Called after every tick, play/pause, speed change and step, and when work is queued. Returns an unsubscribe function. */
|
|
110
|
+
subscribe(fn: () => void): () => void;
|
|
111
|
+
/** Pause and drop every subscriber. */
|
|
112
|
+
dispose(): void;
|
|
113
|
+
}
|
package/dist/clock.js
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/** The default clock: `Date.now`, the global `setTimeout` and microtasks. */
|
|
2
|
+
export const realClock = {
|
|
3
|
+
now: () => Date.now(),
|
|
4
|
+
setTimeout: (fn, ms) => setTimeout(fn, ms),
|
|
5
|
+
clearTimeout: (h) => clearTimeout(h),
|
|
6
|
+
defer: (fn) => queueMicrotask(fn),
|
|
7
|
+
};
|
|
8
|
+
/** Earlier time first; equal times in the order they were scheduled. */
|
|
9
|
+
const before = (a, b) => a.at < b.at || (a.at === b.at && a.id < b.id);
|
|
10
|
+
/**
|
|
11
|
+
* Deterministic time for tests and playback. Timers live in a binary heap
|
|
12
|
+
* (O(log n) to schedule and fire) and cancelling is O(1), so a clock with
|
|
13
|
+
* hundreds of thousands of pending timers stays fast.
|
|
14
|
+
*/
|
|
15
|
+
export class VirtualClock {
|
|
16
|
+
time = 0;
|
|
17
|
+
seq = 0;
|
|
18
|
+
/** timers that are still scheduled, by id */
|
|
19
|
+
live = new Map();
|
|
20
|
+
/** min-heap of timers; cancelled ones are skipped lazily */
|
|
21
|
+
heap = [];
|
|
22
|
+
deferred = [];
|
|
23
|
+
head = 0;
|
|
24
|
+
now() {
|
|
25
|
+
return this.time;
|
|
26
|
+
}
|
|
27
|
+
setTimeout(fn, ms) {
|
|
28
|
+
const timer = { id: ++this.seq, at: this.time + Math.max(0, ms), fn };
|
|
29
|
+
this.live.set(timer.id, timer);
|
|
30
|
+
this.push(timer);
|
|
31
|
+
return timer.id;
|
|
32
|
+
}
|
|
33
|
+
clearTimeout(handle) {
|
|
34
|
+
this.live.delete(handle);
|
|
35
|
+
// keep the heap from filling up with cancelled timers
|
|
36
|
+
if (this.heap.length > 64 && this.heap.length > 4 * this.live.size)
|
|
37
|
+
this.rebuild();
|
|
38
|
+
}
|
|
39
|
+
defer(fn) {
|
|
40
|
+
this.deferred.push(fn);
|
|
41
|
+
}
|
|
42
|
+
/** True when nothing is scheduled: no deferred work and no pending timers. */
|
|
43
|
+
get idle() {
|
|
44
|
+
return this.ready === 0 && this.live.size === 0;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Runs deferred work, then fires timers in time order, advancing virtual
|
|
48
|
+
* time, until nothing is pending or the next timer is later than `until`
|
|
49
|
+
* (absolute virtual ms). Returns the number of tasks run.
|
|
50
|
+
*/
|
|
51
|
+
run(until = Infinity, maxTasks = 1_000_000) {
|
|
52
|
+
let n = 0;
|
|
53
|
+
for (;;) {
|
|
54
|
+
for (let d = this.takeDeferred(); d; d = this.takeDeferred()) {
|
|
55
|
+
if (++n > maxTasks)
|
|
56
|
+
throw new Error("VirtualClock: task limit exceeded");
|
|
57
|
+
d();
|
|
58
|
+
}
|
|
59
|
+
const next = this.peek();
|
|
60
|
+
if (!next || next.at > until)
|
|
61
|
+
break;
|
|
62
|
+
this.fire(next);
|
|
63
|
+
if (++n > maxTasks)
|
|
64
|
+
throw new Error("VirtualClock: task limit exceeded");
|
|
65
|
+
next.fn();
|
|
66
|
+
}
|
|
67
|
+
if (until !== Infinity && this.time < until)
|
|
68
|
+
this.time = until;
|
|
69
|
+
return n;
|
|
70
|
+
}
|
|
71
|
+
/** Advance by `ms`, running everything due. */
|
|
72
|
+
advance(ms) {
|
|
73
|
+
return this.run(this.time + ms);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Run exactly one task: the oldest deferred one, else the earliest timer
|
|
77
|
+
* (advancing virtual time to it). Returns false when nothing is pending.
|
|
78
|
+
*/
|
|
79
|
+
runNext() {
|
|
80
|
+
const d = this.takeDeferred();
|
|
81
|
+
if (d) {
|
|
82
|
+
d();
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
const next = this.peek();
|
|
86
|
+
if (!next)
|
|
87
|
+
return false;
|
|
88
|
+
this.fire(next);
|
|
89
|
+
next.fn();
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
/** Virtual time of the next timer, if any. */
|
|
93
|
+
get nextTimerAt() {
|
|
94
|
+
return this.peek()?.at;
|
|
95
|
+
}
|
|
96
|
+
/** Number of pending tasks (deferred work + timers). */
|
|
97
|
+
get pending() {
|
|
98
|
+
return this.ready + this.live.size;
|
|
99
|
+
}
|
|
100
|
+
/** Deferred work waiting to run now (e.g. queued external events). */
|
|
101
|
+
get ready() {
|
|
102
|
+
return this.deferred.length - this.head;
|
|
103
|
+
}
|
|
104
|
+
takeDeferred() {
|
|
105
|
+
if (this.head >= this.deferred.length)
|
|
106
|
+
return undefined;
|
|
107
|
+
const fn = this.deferred[this.head];
|
|
108
|
+
this.deferred[this.head++] = undefined;
|
|
109
|
+
if (this.head === this.deferred.length) {
|
|
110
|
+
this.deferred = [];
|
|
111
|
+
this.head = 0;
|
|
112
|
+
}
|
|
113
|
+
else if (this.head > 1024 && this.head * 2 > this.deferred.length) {
|
|
114
|
+
this.deferred = this.deferred.slice(this.head);
|
|
115
|
+
this.head = 0;
|
|
116
|
+
}
|
|
117
|
+
return fn;
|
|
118
|
+
}
|
|
119
|
+
/** Remove `timer` (the heap's top) and move time to it. */
|
|
120
|
+
fire(timer) {
|
|
121
|
+
this.pop();
|
|
122
|
+
this.live.delete(timer.id);
|
|
123
|
+
this.time = Math.max(this.time, timer.at);
|
|
124
|
+
}
|
|
125
|
+
/** The earliest live timer, dropping cancelled ones from the top. */
|
|
126
|
+
peek() {
|
|
127
|
+
while (this.heap.length && !this.live.has(this.heap[0].id))
|
|
128
|
+
this.pop();
|
|
129
|
+
return this.heap[0];
|
|
130
|
+
}
|
|
131
|
+
push(t) {
|
|
132
|
+
const h = this.heap;
|
|
133
|
+
h.push(t);
|
|
134
|
+
let i = h.length - 1;
|
|
135
|
+
while (i > 0) {
|
|
136
|
+
const p = (i - 1) >> 1;
|
|
137
|
+
if (!before(h[i], h[p]))
|
|
138
|
+
break;
|
|
139
|
+
[h[i], h[p]] = [h[p], h[i]];
|
|
140
|
+
i = p;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
pop() {
|
|
144
|
+
const h = this.heap;
|
|
145
|
+
const last = h.pop();
|
|
146
|
+
if (!last || !h.length)
|
|
147
|
+
return;
|
|
148
|
+
h[0] = last;
|
|
149
|
+
let i = 0;
|
|
150
|
+
for (;;) {
|
|
151
|
+
const l = 2 * i + 1;
|
|
152
|
+
const r = l + 1;
|
|
153
|
+
let m = i;
|
|
154
|
+
if (l < h.length && before(h[l], h[m]))
|
|
155
|
+
m = l;
|
|
156
|
+
if (r < h.length && before(h[r], h[m]))
|
|
157
|
+
m = r;
|
|
158
|
+
if (m === i)
|
|
159
|
+
break;
|
|
160
|
+
[h[i], h[m]] = [h[m], h[i]];
|
|
161
|
+
i = m;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
rebuild() {
|
|
165
|
+
const timers = [...this.live.values()];
|
|
166
|
+
this.heap = [];
|
|
167
|
+
for (const t of timers)
|
|
168
|
+
this.push(t);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* A VirtualClock that real time drives forward — at an adjustable speed —
|
|
173
|
+
* and that can be paused and stepped. For visualisations: everything that
|
|
174
|
+
* takes its time from this clock (sessions, delayed sends, fake services,
|
|
175
|
+
* simulated environments) slows down, stops and single-steps together.
|
|
176
|
+
*
|
|
177
|
+
* const clock = new PlaybackClock({ speed: 0.5 });
|
|
178
|
+
* const session = await createSession(src, { clock });
|
|
179
|
+
* clock.pause(); clock.step(); clock.play();
|
|
180
|
+
*/
|
|
181
|
+
export class PlaybackClock extends VirtualClock {
|
|
182
|
+
#speed;
|
|
183
|
+
#playing = false;
|
|
184
|
+
#last = 0;
|
|
185
|
+
#handle;
|
|
186
|
+
#listeners = new Set();
|
|
187
|
+
/** `speed` defaults to 1; `playing` (default true) starts driving the clock right away. */
|
|
188
|
+
constructor(opts = {}) {
|
|
189
|
+
super();
|
|
190
|
+
this.#speed = opts.speed ?? 1;
|
|
191
|
+
if (opts.playing ?? true)
|
|
192
|
+
this.play();
|
|
193
|
+
}
|
|
194
|
+
/** Whether real time is currently driving the clock. */
|
|
195
|
+
get playing() {
|
|
196
|
+
return this.#playing;
|
|
197
|
+
}
|
|
198
|
+
/** Virtual ms per real ms (1 = real time, 0.5 = half speed). */
|
|
199
|
+
get speed() {
|
|
200
|
+
return this.#speed;
|
|
201
|
+
}
|
|
202
|
+
/** Change the speed; negative values are treated as 0. */
|
|
203
|
+
set speed(v) {
|
|
204
|
+
this.#speed = Math.max(0, v);
|
|
205
|
+
this.#notify();
|
|
206
|
+
}
|
|
207
|
+
/** Let real time drive the clock (at `speed`). */
|
|
208
|
+
play() {
|
|
209
|
+
if (this.#playing)
|
|
210
|
+
return;
|
|
211
|
+
this.#playing = true;
|
|
212
|
+
this.#last = realNow();
|
|
213
|
+
this.#schedule();
|
|
214
|
+
this.#notify();
|
|
215
|
+
}
|
|
216
|
+
/** Stop time: nothing runs until `play()` or `step()`. */
|
|
217
|
+
pause() {
|
|
218
|
+
if (!this.#playing)
|
|
219
|
+
return;
|
|
220
|
+
this.#playing = false;
|
|
221
|
+
cancelFrame(this.#handle);
|
|
222
|
+
this.#notify();
|
|
223
|
+
}
|
|
224
|
+
/** Play if paused, pause if playing. */
|
|
225
|
+
toggle() {
|
|
226
|
+
if (this.#playing)
|
|
227
|
+
this.pause();
|
|
228
|
+
else
|
|
229
|
+
this.play();
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Run tasks one at a time — jumping virtual time to the next timer when
|
|
233
|
+
* nothing is ready — until `until()` holds after a task, or nothing is
|
|
234
|
+
* pending. Pauses first. Returns the number of tasks run.
|
|
235
|
+
*/
|
|
236
|
+
step(until = () => true, maxTasks = 10_000) {
|
|
237
|
+
this.pause();
|
|
238
|
+
let n = 0;
|
|
239
|
+
while (n < maxTasks && this.runNext()) {
|
|
240
|
+
n++;
|
|
241
|
+
if (until())
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
this.#notify();
|
|
245
|
+
return n;
|
|
246
|
+
}
|
|
247
|
+
// queued work changes what a UI shows (pending count, whether Step can do anything): tell subscribers
|
|
248
|
+
/** Like `VirtualClock.setTimeout`, and notifies subscribers that work is queued. */
|
|
249
|
+
setTimeout(fn, ms) {
|
|
250
|
+
const id = super.setTimeout(fn, ms);
|
|
251
|
+
this.#notifySoon();
|
|
252
|
+
return id;
|
|
253
|
+
}
|
|
254
|
+
/** Like `VirtualClock.defer`, and notifies subscribers that work is queued. */
|
|
255
|
+
defer(fn) {
|
|
256
|
+
super.defer(fn);
|
|
257
|
+
this.#notifySoon();
|
|
258
|
+
}
|
|
259
|
+
#notifyQueued = false;
|
|
260
|
+
#notifySoon() {
|
|
261
|
+
if (this.#notifyQueued || !this.#listeners.size)
|
|
262
|
+
return;
|
|
263
|
+
this.#notifyQueued = true;
|
|
264
|
+
queueMicrotask(() => {
|
|
265
|
+
this.#notifyQueued = false;
|
|
266
|
+
this.#notify();
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
/** Called after every tick, play/pause, speed change and step, and when work is queued. Returns an unsubscribe function. */
|
|
270
|
+
subscribe(fn) {
|
|
271
|
+
this.#listeners.add(fn);
|
|
272
|
+
return () => this.#listeners.delete(fn);
|
|
273
|
+
}
|
|
274
|
+
/** Pause and drop every subscriber. */
|
|
275
|
+
dispose() {
|
|
276
|
+
this.pause();
|
|
277
|
+
this.#listeners.clear();
|
|
278
|
+
}
|
|
279
|
+
#tick = () => {
|
|
280
|
+
if (!this.#playing)
|
|
281
|
+
return;
|
|
282
|
+
const now = realNow();
|
|
283
|
+
const dt = Math.min(now - this.#last, 250) * this.#speed; // cap: a background tab shouldn't fast-forward minutes
|
|
284
|
+
this.#last = now;
|
|
285
|
+
this.run(this.now() + dt);
|
|
286
|
+
this.#notify();
|
|
287
|
+
this.#schedule();
|
|
288
|
+
};
|
|
289
|
+
#schedule() {
|
|
290
|
+
this.#handle = requestFrame(this.#tick);
|
|
291
|
+
}
|
|
292
|
+
#notify() {
|
|
293
|
+
for (const fn of this.#listeners) {
|
|
294
|
+
try {
|
|
295
|
+
fn();
|
|
296
|
+
}
|
|
297
|
+
catch (e) {
|
|
298
|
+
console.error(e);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const realNow = () => (typeof performance !== "undefined" ? performance.now() : Date.now());
|
|
304
|
+
const requestFrame = (fn) => typeof requestAnimationFrame === "function" ? requestAnimationFrame(fn) : setTimeout(fn, 16);
|
|
305
|
+
const cancelFrame = (h) => {
|
|
306
|
+
if (typeof cancelAnimationFrame === "function")
|
|
307
|
+
cancelAnimationFrame(h);
|
|
308
|
+
clearTimeout(h);
|
|
309
|
+
};
|
|
310
|
+
//# sourceMappingURL=clock.js.map
|
|
@@ -0,0 +1 @@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AAC,SAAS,EAAE,CAAC;IACnB,CAAC,CAAC;IAEF,SAAS;QACP,IAAI,CAAC,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO;QACL,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACjC,IAAI,CAAC;gBACH,EAAE,EAAE,CAAC;YACP,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACnB,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAED,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,CAAC,OAAO,WAAW,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;AAC5F,MAAM,YAAY,GAAG,CAAC,EAAc,EAAW,EAAE,CAC/C,OAAO,qBAAqB,KAAK,UAAU,CAAC,CAAC,CAAC,qBAAqB,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;AAC/F,MAAM,WAAW,GAAG,CAAC,CAAU,EAAE,EAAE;IACjC,IAAI,OAAO,oBAAoB,KAAK,UAAU;QAAE,oBAAoB,CAAC,CAAW,CAAC,CAAC;IAClF,YAAY,CAAC,CAAkC,CAAC,CAAC;AACnD,CAAC,CAAC","sourcesContent":["/**\n * Timers and deferred work. Sessions never touch setTimeout/queueMicrotask\n * directly, so tests (and the conformance runner) can use a VirtualClock:\n * deterministic, and delayed events fire without real waiting.\n */\n/**\n * Where a session gets its time: timers for delayed `<send>`s and deferred\n * work for external events. Pass one as `SessionOptions.clock`.\n */\nexport interface Clock {\n /** The current time in ms (real or virtual). */\n now(): number;\n /** Run `fn` after `ms` milliseconds of this clock's time; returns a handle for `clearTimeout`. */\n setTimeout(fn: () => void, ms: number): unknown;\n /** Cancel a timer from `setTimeout`. Unknown or already-fired handles are ignored. */\n clearTimeout(handle: unknown): void;\n /** Run `fn` soon, after the current task (a microtask on the real clock). */\n defer(fn: () => void): void;\n}\n\n/** The default clock: `Date.now`, the global `setTimeout` and microtasks. */\nexport const realClock: Clock = {\n now: () => Date.now(),\n setTimeout: (fn, ms) => setTimeout(fn, ms),\n clearTimeout: (h) => clearTimeout(h as ReturnType<typeof setTimeout>),\n defer: (fn) => queueMicrotask(fn),\n};\n\ninterface Timer {\n id: number;\n at: number;\n fn: () => void;\n}\n\n/** Earlier time first; equal times in the order they were scheduled. */\nconst before = (a: Timer, b: Timer) => a.at < b.at || (a.at === b.at && a.id < b.id);\n\n/**\n * Deterministic time for tests and playback. Timers live in a binary heap\n * (O(log n) to schedule and fire) and cancelling is O(1), so a clock with\n * hundreds of thousands of pending timers stays fast.\n */\nexport class VirtualClock implements Clock {\n private time = 0;\n private seq = 0;\n /** timers that are still scheduled, by id */\n private live = new Map<number, Timer>();\n /** min-heap of timers; cancelled ones are skipped lazily */\n private heap: Timer[] = [];\n private deferred: ((() => void) | undefined)[] = [];\n private head = 0;\n\n now() {\n return this.time;\n }\n\n setTimeout(fn: () => void, ms: number) {\n const timer = { id: ++this.seq, at: this.time + Math.max(0, ms), fn };\n this.live.set(timer.id, timer);\n this.push(timer);\n return timer.id;\n }\n\n clearTimeout(handle: unknown) {\n this.live.delete(handle as number);\n // keep the heap from filling up with cancelled timers\n if (this.heap.length > 64 && this.heap.length > 4 * this.live.size) this.rebuild();\n }\n\n defer(fn: () => void) {\n this.deferred.push(fn);\n }\n\n /** True when nothing is scheduled: no deferred work and no pending timers. */\n get idle() {\n return this.ready === 0 && this.live.size === 0;\n }\n\n /**\n * Runs deferred work, then fires timers in time order, advancing virtual\n * time, until nothing is pending or the next timer is later than `until`\n * (absolute virtual ms). Returns the number of tasks run.\n */\n run(until = Infinity, maxTasks = 1_000_000): number {\n let n = 0;\n for (;;) {\n for (let d = this.takeDeferred(); d; d = this.takeDeferred()) {\n if (++n > maxTasks) throw new Error(\"VirtualClock: task limit exceeded\");\n d();\n }\n const next = this.peek();\n if (!next || next.at > until) break;\n this.fire(next);\n if (++n > maxTasks) throw new Error(\"VirtualClock: task limit exceeded\");\n next.fn();\n }\n if (until !== Infinity && this.time < until) this.time = until;\n return n;\n }\n\n /** Advance by `ms`, running everything due. */\n advance(ms: number) {\n return this.run(this.time + ms);\n }\n\n /**\n * Run exactly one task: the oldest deferred one, else the earliest timer\n * (advancing virtual time to it). Returns false when nothing is pending.\n */\n runNext(): boolean {\n const d = this.takeDeferred();\n if (d) {\n d();\n return true;\n }\n const next = this.peek();\n if (!next) return false;\n this.fire(next);\n next.fn();\n return true;\n }\n\n /** Virtual time of the next timer, if any. */\n get nextTimerAt(): number | undefined {\n return this.peek()?.at;\n }\n\n /** Number of pending tasks (deferred work + timers). */\n get pending(): number {\n return this.ready + this.live.size;\n }\n\n /** Deferred work waiting to run now (e.g. queued external events). */\n get ready(): number {\n return this.deferred.length - this.head;\n }\n\n private takeDeferred(): (() => void) | undefined {\n if (this.head >= this.deferred.length) return undefined;\n const fn = this.deferred[this.head];\n this.deferred[this.head++] = undefined;\n if (this.head === this.deferred.length) {\n this.deferred = [];\n this.head = 0;\n } else if (this.head > 1024 && this.head * 2 > this.deferred.length) {\n this.deferred = this.deferred.slice(this.head);\n this.head = 0;\n }\n return fn;\n }\n\n /** Remove `timer` (the heap's top) and move time to it. */\n private fire(timer: Timer) {\n this.pop();\n this.live.delete(timer.id);\n this.time = Math.max(this.time, timer.at);\n }\n\n /** The earliest live timer, dropping cancelled ones from the top. */\n private peek(): Timer | undefined {\n while (this.heap.length && !this.live.has(this.heap[0]!.id)) this.pop();\n return this.heap[0];\n }\n\n private push(t: Timer) {\n const h = this.heap;\n h.push(t);\n let i = h.length - 1;\n while (i > 0) {\n const p = (i - 1) >> 1;\n if (!before(h[i]!, h[p]!)) break;\n [h[i], h[p]] = [h[p]!, h[i]!];\n i = p;\n }\n }\n\n private pop() {\n const h = this.heap;\n const last = h.pop();\n if (!last || !h.length) return;\n h[0] = last;\n let i = 0;\n for (;;) {\n const l = 2 * i + 1;\n const r = l + 1;\n let m = i;\n if (l < h.length && before(h[l]!, h[m]!)) m = l;\n if (r < h.length && before(h[r]!, h[m]!)) m = r;\n if (m === i) break;\n [h[i], h[m]] = [h[m]!, h[i]!];\n i = m;\n }\n }\n\n private rebuild() {\n const timers = [...this.live.values()];\n this.heap = [];\n for (const t of timers) this.push(t);\n }\n}\n\n/**\n * A VirtualClock that real time drives forward — at an adjustable speed —\n * and that can be paused and stepped. For visualisations: everything that\n * takes its time from this clock (sessions, delayed sends, fake services,\n * simulated environments) slows down, stops and single-steps together.\n *\n * const clock = new PlaybackClock({ speed: 0.5 });\n * const session = await createSession(src, { clock });\n * clock.pause(); clock.step(); clock.play();\n */\nexport class PlaybackClock extends VirtualClock {\n #speed: number;\n #playing = false;\n #last = 0;\n #handle: unknown;\n #listeners = new Set<() => void>();\n\n /** `speed` defaults to 1; `playing` (default true) starts driving the clock right away. */\n constructor(opts: { speed?: number; playing?: boolean } = {}) {\n super();\n this.#speed = opts.speed ?? 1;\n if (opts.playing ?? true) this.play();\n }\n\n /** Whether real time is currently driving the clock. */\n get playing() {\n return this.#playing;\n }\n\n /** Virtual ms per real ms (1 = real time, 0.5 = half speed). */\n get speed() {\n return this.#speed;\n }\n\n /** Change the speed; negative values are treated as 0. */\n set speed(v: number) {\n this.#speed = Math.max(0, v);\n this.#notify();\n }\n\n /** Let real time drive the clock (at `speed`). */\n play() {\n if (this.#playing) return;\n this.#playing = true;\n this.#last = realNow();\n this.#schedule();\n this.#notify();\n }\n\n /** Stop time: nothing runs until `play()` or `step()`. */\n pause() {\n if (!this.#playing) return;\n this.#playing = false;\n cancelFrame(this.#handle);\n this.#notify();\n }\n\n /** Play if paused, pause if playing. */\n toggle() {\n if (this.#playing) this.pause();\n else this.play();\n }\n\n /**\n * Run tasks one at a time — jumping virtual time to the next timer when\n * nothing is ready — until `until()` holds after a task, or nothing is\n * pending. Pauses first. Returns the number of tasks run.\n */\n step(until: () => boolean = () => true, maxTasks = 10_000): number {\n this.pause();\n let n = 0;\n while (n < maxTasks && this.runNext()) {\n n++;\n if (until()) break;\n }\n this.#notify();\n return n;\n }\n\n // queued work changes what a UI shows (pending count, whether Step can do anything): tell subscribers\n /** Like `VirtualClock.setTimeout`, and notifies subscribers that work is queued. */\n override setTimeout(fn: () => void, ms: number) {\n const id = super.setTimeout(fn, ms);\n this.#notifySoon();\n return id;\n }\n\n /** Like `VirtualClock.defer`, and notifies subscribers that work is queued. */\n override defer(fn: () => void) {\n super.defer(fn);\n this.#notifySoon();\n }\n\n #notifyQueued = false;\n #notifySoon() {\n if (this.#notifyQueued || !this.#listeners.size) return;\n this.#notifyQueued = true;\n queueMicrotask(() => {\n this.#notifyQueued = false;\n this.#notify();\n });\n }\n\n /** Called after every tick, play/pause, speed change and step, and when work is queued. Returns an unsubscribe function. */\n subscribe(fn: () => void): () => void {\n this.#listeners.add(fn);\n return () => this.#listeners.delete(fn);\n }\n\n /** Pause and drop every subscriber. */\n dispose() {\n this.pause();\n this.#listeners.clear();\n }\n\n #tick = () => {\n if (!this.#playing) return;\n const now = realNow();\n const dt = Math.min(now - this.#last, 250) * this.#speed; // cap: a background tab shouldn't fast-forward minutes\n this.#last = now;\n this.run(this.now() + dt);\n this.#notify();\n this.#schedule();\n };\n\n #schedule() {\n this.#handle = requestFrame(this.#tick);\n }\n\n #notify() {\n for (const fn of this.#listeners) {\n try {\n fn();\n } catch (e) {\n console.error(e);\n }\n }\n }\n}\n\nconst realNow = () => (typeof performance !== \"undefined\" ? performance.now() : Date.now());\nconst requestFrame = (fn: () => void): unknown =>\n typeof requestAnimationFrame === \"function\" ? requestAnimationFrame(fn) : setTimeout(fn, 16);\nconst cancelFrame = (h: unknown) => {\n if (typeof cancelAnimationFrame === \"function\") cancelAnimationFrame(h as number);\n clearTimeout(h as ReturnType<typeof setTimeout>);\n};\n"]}
|
package/dist/common.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything both entry points share. Import from `@tinyactors/scxmljs`
|
|
3
|
+
* (sandboxed) or `@tinyactors/scxmljs/trusted` instead of this file.
|
|
4
|
+
*/
|
|
5
|
+
export { type BindOptions, bind, type ConnectOptions, connect, type EventNameMapper } from "./bridge.js";
|
|
6
|
+
export { type Clock, PlaybackClock, realClock, VirtualClock } from "./clock.js";
|
|
7
|
+
export { type DataModel, DataModelError, type DataModelFactory, type DataModelOptions } from "./datamodel-base.js";
|
|
8
|
+
export type { Diagnostic, DiagnosticCode } from "./diagnostics.js";
|
|
9
|
+
export type { ChartDoneElementEvent, ElementEventsMap, ReflectOptions, StateElementEvent, TransitionElementEvent, } from "./element-events.js";
|
|
10
|
+
export { nameMatch, parseDelay, SCXML_IOPROCESSOR, SCXML_NS, type SCXMLEvent } from "./events.js";
|
|
11
|
+
export type { InvokeNode, Loader, Model, StateKind, StateNode, TransitionNode } from "./model.js";
|
|
12
|
+
export { compile, documentOrder, isAtomic, isCompound, isDescendant, properAncestors, SCXMLParseError, SCXMLValidationError, scxmlChildren, } from "./model.js";
|
|
13
|
+
export type { DOMParserLike, Invocation, InvokeContext, InvokedService, Invoker, IOProcessor, IOSession, OutboundSend, ParentInvocation, SessionOptions, WaitCondition, WaitForOptions, } from "./session.js";
|
|
14
|
+
export { parseSCXML, SCXMLSession } from "./session.js";
|
|
15
|
+
export { ChildSessionEvent, DoneEvent, InvokeEvent, LogEvent, MacrostepEvent, MicrostepEvent, SCXMLErrorEvent, type SCXMLErrorKind, SendEvent, type SessionEventMap, } from "./session-events.js";
|
package/dist/common.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything both entry points share. Import from `@tinyactors/scxmljs`
|
|
3
|
+
* (sandboxed) or `@tinyactors/scxmljs/trusted` instead of this file.
|
|
4
|
+
*/
|
|
5
|
+
export { bind, connect } from "./bridge.js";
|
|
6
|
+
export { PlaybackClock, realClock, VirtualClock } from "./clock.js";
|
|
7
|
+
// Data model extension point (advanced): implement `DataModel` to plug in another engine
|
|
8
|
+
// via `SessionOptions.datamodel`.
|
|
9
|
+
export { DataModelError } from "./datamodel-base.js";
|
|
10
|
+
export { nameMatch, parseDelay, SCXML_IOPROCESSOR, SCXML_NS } from "./events.js";
|
|
11
|
+
export { compile, documentOrder, isAtomic, isCompound, isDescendant, properAncestors, SCXMLParseError, SCXMLValidationError, scxmlChildren, } from "./model.js";
|
|
12
|
+
export { parseSCXML, SCXMLSession } from "./session.js";
|
|
13
|
+
export { ChildSessionEvent, DoneEvent, InvokeEvent, LogEvent, MacrostepEvent, MicrostepEvent, SCXMLErrorEvent, SendEvent, } from "./session-events.js";
|
|
14
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAoB,IAAI,EAAuB,OAAO,EAAwB,MAAM,aAAa,CAAC;AACzG,OAAO,EAAc,aAAa,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAChF,yFAAyF;AACzF,kCAAkC;AAClC,OAAO,EAAkB,cAAc,EAAgD,MAAM,qBAAqB,CAAC;AASnH,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,iBAAiB,EAAE,QAAQ,EAAmB,MAAM,aAAa,CAAC;AAElG,OAAO,EACL,OAAO,EACP,aAAa,EACb,QAAQ,EACR,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,oBAAoB,EACpB,aAAa,GACd,MAAM,YAAY,CAAC;AAepB,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EACL,iBAAiB,EACjB,SAAS,EACT,WAAW,EACX,QAAQ,EACR,cAAc,EACd,cAAc,EACd,eAAe,EAEf,SAAS,GAEV,MAAM,qBAAqB,CAAC","sourcesContent":["/**\n * Everything both entry points share. Import from `@tinyactors/scxmljs`\n * (sandboxed) or `@tinyactors/scxmljs/trusted` instead of this file.\n */\n\nexport { type BindOptions, bind, type ConnectOptions, connect, type EventNameMapper } from \"./bridge.ts\";\nexport { type Clock, PlaybackClock, realClock, VirtualClock } from \"./clock.ts\";\n// Data model extension point (advanced): implement `DataModel` to plug in another engine\n// via `SessionOptions.datamodel`.\nexport { type DataModel, DataModelError, type DataModelFactory, type DataModelOptions } from \"./datamodel-base.ts\";\nexport type { Diagnostic, DiagnosticCode } from \"./diagnostics.ts\";\nexport type {\n ChartDoneElementEvent,\n ElementEventsMap,\n ReflectOptions,\n StateElementEvent,\n TransitionElementEvent,\n} from \"./element-events.ts\";\nexport { nameMatch, parseDelay, SCXML_IOPROCESSOR, SCXML_NS, type SCXMLEvent } from \"./events.ts\";\nexport type { InvokeNode, Loader, Model, StateKind, StateNode, TransitionNode } from \"./model.ts\";\nexport {\n compile,\n documentOrder,\n isAtomic,\n isCompound,\n isDescendant,\n properAncestors,\n SCXMLParseError,\n SCXMLValidationError,\n scxmlChildren,\n} from \"./model.ts\";\nexport type {\n DOMParserLike,\n Invocation,\n InvokeContext,\n InvokedService,\n Invoker,\n IOProcessor,\n IOSession,\n OutboundSend,\n ParentInvocation,\n SessionOptions,\n WaitCondition,\n WaitForOptions,\n} from \"./session.ts\";\nexport { parseSCXML, SCXMLSession } from \"./session.ts\";\nexport {\n ChildSessionEvent,\n DoneEvent,\n InvokeEvent,\n LogEvent,\n MacrostepEvent,\n MicrostepEvent,\n SCXMLErrorEvent,\n type SCXMLErrorKind,\n SendEvent,\n type SessionEventMap,\n} from \"./session-events.ts\";\n"]}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ECMAScript data model (spec Appendix B.2), independent of the engine
|
|
3
|
+
* that runs it.
|
|
4
|
+
*
|
|
5
|
+
* All the spec-relevant behaviour lives here, written as JavaScript source
|
|
6
|
+
* that runs *inside* the engine: read-only system variables, strict-mode
|
|
7
|
+
* `<assign>` (so undeclared locations fail), `<foreach>` over a shallow
|
|
8
|
+
* copy, and location checks. An engine only has to run code in a per-session
|
|
9
|
+
* global scope and move values in and out. Two engines exist:
|
|
10
|
+
*
|
|
11
|
+
* - sandboxed (datamodel-quickjs.ts): QuickJS in WebAssembly — isolated,
|
|
12
|
+
* interruptible; the default entry point.
|
|
13
|
+
* - trusted (datamodel-trusted.ts): the host's own JS engine in a separate
|
|
14
|
+
* realm (an iframe, or node:vm) — no download, but no isolation.
|
|
15
|
+
*/
|
|
16
|
+
import type { SCXMLEvent } from "./events.js";
|
|
17
|
+
/** What a session hands a data model when it creates one (see `DataModelFactory`). */
|
|
18
|
+
export interface DataModelOptions {
|
|
19
|
+
/** The value of the read-only `_sessionid` system variable. */
|
|
20
|
+
sessionId: string;
|
|
21
|
+
/** The value of `_name` (the `<scxml name>` attribute). */
|
|
22
|
+
name: string;
|
|
23
|
+
/** The value of `_ioprocessors`: each processor's type URI (and aliases) with its location. */
|
|
24
|
+
ioprocessors: Record<string, {
|
|
25
|
+
/** The address to send to this session through that processor. */
|
|
26
|
+
location: string;
|
|
27
|
+
}>;
|
|
28
|
+
/** Implements the `In(stateId)` predicate against the session's configuration. */
|
|
29
|
+
In: (stateId: string) => boolean;
|
|
30
|
+
/** Maximum run time of a single evaluation before it is interrupted (ms). Sandboxed engine only. */
|
|
31
|
+
timeoutMs?: number;
|
|
32
|
+
/** Maximum memory of one session's engine, in bytes. Sandboxed engine only (default 64 MiB). */
|
|
33
|
+
memoryLimitBytes?: number;
|
|
34
|
+
}
|
|
35
|
+
/** What a session needs from its data model. Values in and out are plain host data. */
|
|
36
|
+
export interface DataModel {
|
|
37
|
+
/** Evaluate an expression; the value is returned as plain data. */
|
|
38
|
+
evaluate(expr: string): unknown;
|
|
39
|
+
/** Evaluate a conditional expression (spec §5.9.1); errors propagate. */
|
|
40
|
+
condition(expr: string): boolean;
|
|
41
|
+
/** Run `<script>` content as a global script. */
|
|
42
|
+
script(code: string): void;
|
|
43
|
+
/** Declare (or re-initialise) a variable from an expression, keeping the value inside the engine. */
|
|
44
|
+
declareExpr(id: string, expr: string): void;
|
|
45
|
+
/** Declare (or re-initialise) a variable from a host value. */
|
|
46
|
+
declareValue(id: string, value: unknown): void;
|
|
47
|
+
/** `<assign location expr>`: evaluated and assigned inside the engine. */
|
|
48
|
+
assignExpr(location: string, expr: string): void;
|
|
49
|
+
/** Assign a host value (e.g. inline content) to a location. */
|
|
50
|
+
assignValue(location: string, value: unknown): void;
|
|
51
|
+
/** Is `location` a valid location right now? */
|
|
52
|
+
isValidLocation(location: string): boolean;
|
|
53
|
+
/** `<foreach>` (spec §4.6). */
|
|
54
|
+
foreach(arrayExpr: string, item: string, index: string | null, body: () => void): void;
|
|
55
|
+
/** Bind `_event`. */
|
|
56
|
+
setEvent(ev: SCXMLEvent): void;
|
|
57
|
+
/** Current values of the given variables, as plain data. */
|
|
58
|
+
snapshot(ids: Iterable<string>): Record<string, unknown>;
|
|
59
|
+
/** Release the engine (QuickJS context or realm). The data model isn't used afterwards. */
|
|
60
|
+
dispose(): void;
|
|
61
|
+
}
|
|
62
|
+
/** Creates one data model per session. Pass one as `SessionOptions.datamodel` to plug in another engine. */
|
|
63
|
+
export type DataModelFactory = (opts: DataModelOptions) => DataModel;
|
|
64
|
+
/** An error thrown by user code or by an illegal data model operation. */
|
|
65
|
+
export declare class DataModelError extends Error {
|
|
66
|
+
/** Always `"SCXML_DATAMODEL"`, for matching without `instanceof`. */
|
|
67
|
+
readonly code: "SCXML_DATAMODEL";
|
|
68
|
+
constructor(message: string);
|
|
69
|
+
}
|
|
70
|
+
export declare function isLegalVariableName(name: string): boolean;
|
|
71
|
+
/** Expressions are wrapped in parentheses; tolerate a trailing `;` as authors often write one. */
|
|
72
|
+
export declare function exprSource(expr: string): string;
|
|
73
|
+
/**
|
|
74
|
+
* Read-only system variables (spec §5.10): assigning one throws, which becomes
|
|
75
|
+
* error.execution. The source is CONSTANT and evaluates to an installer that
|
|
76
|
+
* receives the values as data: per-session source text (with the session id
|
|
77
|
+
* inlined) made the host engines' compilation caches grow with every session
|
|
78
|
+
* created in trusted mode — about 2 KB per session, measured in phase 5.
|
|
79
|
+
*/
|
|
80
|
+
export declare const SYSTEM_VARIABLES_SOURCE = "(function () {\n function deepFreeze(o) { if (o && typeof o === \"object\") { Object.freeze(o); for (var k in o) deepFreeze(o[k]); } return o; }\n function define(name, value) {\n Object.defineProperty(globalThis, name, { enumerable: true, configurable: false,\n get: function () { return value; },\n set: function () { throw new Error(name + \" is a read-only system variable\"); } });\n }\n return function (values) {\n define(\"_sessionid\", deepFreeze(values.sessionid));\n define(\"_name\", deepFreeze(values.name));\n define(\"_ioprocessors\", deepFreeze(values.ioprocessors));\n };\n})()";
|
|
81
|
+
/** The values SYSTEM_VARIABLES_SOURCE's installer is called with. */
|
|
82
|
+
export declare function systemVariableValues(opts: DataModelOptions): {
|
|
83
|
+
sessionid: string;
|
|
84
|
+
name: string;
|
|
85
|
+
ioprocessors: Record<string, {
|
|
86
|
+
/** The address to send to this session through that processor. */
|
|
87
|
+
location: string;
|
|
88
|
+
}>;
|
|
89
|
+
};
|
|
90
|
+
/** Defines the read-only `_event` and evaluates to the function that rebinds it. */
|
|
91
|
+
export declare const EVENT_SETTER_SOURCE = "(function () { var ev;\n Object.defineProperty(globalThis, \"_event\", { enumerable: true, configurable: false,\n get: function () { return ev; },\n set: function () { throw new Error(\"_event is a read-only system variable\"); } });\n return function (v) { ev = v; }; })()";
|
|
92
|
+
/** The `_event` object with every field present (spec §5.10.1). */
|
|
93
|
+
export declare function eventObject(ev: SCXMLEvent): {
|
|
94
|
+
name: string;
|
|
95
|
+
type: "external" | "internal" | "platform";
|
|
96
|
+
sendid: string | undefined;
|
|
97
|
+
origin: string | undefined;
|
|
98
|
+
origintype: string | undefined;
|
|
99
|
+
invokeid: string | undefined;
|
|
100
|
+
data: unknown;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* How every value leaves a data model (logs, send payloads, done data,
|
|
104
|
+
* snapshots, `evaluate()`), identical in both engines:
|
|
105
|
+
*
|
|
106
|
+
* - top-level primitives pass through unchanged (including `undefined`,
|
|
107
|
+
* `NaN`, `-0` and bigints); a top-level function or symbol becomes `undefined`;
|
|
108
|
+
* - inside objects and arrays the JSON rules apply: keys holding `undefined`,
|
|
109
|
+
* functions or symbols are dropped, such array entries become `null`,
|
|
110
|
+
* non-finite numbers become `null`, bigints become strings;
|
|
111
|
+
* - anything with `toJSON()` (Dates, the sandbox's XML values) is replaced by its result;
|
|
112
|
+
* - Maps, Sets and class instances become plain objects of their own enumerable keys;
|
|
113
|
+
* - a reference back into its own ancestors (a cycle) is dropped;
|
|
114
|
+
* - `keep(v)` objects pass through untouched (the trusted engine keeps DOM nodes).
|
|
115
|
+
*
|
|
116
|
+
* `PLAIN_DATA_SOURCE` is the same algorithm as engine-side source; a test
|
|
117
|
+
* checks the two agree.
|
|
118
|
+
*/
|
|
119
|
+
export declare function plainData(value: unknown, keep?: (v: object) => boolean): unknown;
|
|
120
|
+
/** `plainData` as engine-side source: evaluates to a function of one value. */
|
|
121
|
+
export declare const PLAIN_DATA_SOURCE = "(function () {\n return function (value) {\n var ancestors = [];\n function convert(v, nested) {\n if (typeof v === \"function\" || typeof v === \"symbol\") return undefined;\n if (typeof v === \"number\") return nested && !isFinite(v) ? null : v;\n if (typeof v === \"bigint\") return nested ? String(v) : v;\n if (v === null || typeof v !== \"object\") return v;\n if (typeof v.toJSON === \"function\") return convert(v.toJSON(), nested);\n if (ancestors.indexOf(v) >= 0) return undefined;\n ancestors.push(v);\n try {\n var out, c, i, k, keys;\n if (Array.isArray(v)) {\n out = [];\n for (i = 0; i < v.length; i++) { c = convert(v[i], true); out.push(c === undefined ? null : c); }\n return out;\n }\n out = {};\n keys = Object.keys(v);\n for (i = 0; i < keys.length; i++) { k = keys[i]; c = convert(v[k], true); if (c !== undefined) out[k] = c; }\n return out;\n } finally {\n ancestors.pop();\n }\n }\n return convert(value, false);\n };\n})()";
|
|
122
|
+
export declare function isDomNode(v: unknown): v is Node;
|