@pontlabsai/eventdispatch 0.0.0-stage → 0.3.2
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 +201 -0
- package/README.md +115 -2
- package/dist/cs.d.ts +32 -0
- package/dist/cs.js +203 -0
- package/dist/cs.js.map +1 -0
- package/dist/dispatcher.d.ts +66 -0
- package/dist/dispatcher.js +170 -0
- package/dist/dispatcher.js.map +1 -0
- package/dist/event.d.ts +77 -0
- package/dist/event.js +138 -0
- package/dist/event.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/recording.d.ts +103 -0
- package/dist/recording.js +239 -0
- package/dist/recording.js.map +1 -0
- package/dist/semaphore.d.ts +45 -0
- package/dist/semaphore.js +125 -0
- package/dist/semaphore.js.map +1 -0
- package/dist/sim.d.ts +68 -0
- package/dist/sim.js +203 -0
- package/dist/sim.js.map +1 -0
- package/package.json +50 -4
- package/src/cs.ts +192 -0
- package/src/dispatcher.ts +203 -0
- package/src/event.ts +172 -0
- package/src/index.ts +19 -0
- package/src/recording.ts +282 -0
- package/src/semaphore.ts +128 -0
- package/src/sim.ts +217 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { Dispatcher } from "./dispatcher.js";
|
|
2
|
+
import { Deferred, Deserializers, Handoff } from "./event.js";
|
|
3
|
+
import { SimDispatcher } from "./sim.js";
|
|
4
|
+
/** The blackboard key of the recordings field (spec recording.md#blackboard-keys). */
|
|
5
|
+
export const RECORDINGS_KEY = "recordings";
|
|
6
|
+
/** The outcome of a preempted event's record. */
|
|
7
|
+
export const PREEMPTED = "PREEMPTED";
|
|
8
|
+
/**
|
|
9
|
+
* The recordings field: a buffer with exactly one consumer (spec
|
|
10
|
+
* recording.md#the-recordings-field).
|
|
11
|
+
*/
|
|
12
|
+
export class RecordingLog {
|
|
13
|
+
records = [];
|
|
14
|
+
arrived = new Deferred();
|
|
15
|
+
/** @internal */
|
|
16
|
+
append(r) {
|
|
17
|
+
this.records.push(r);
|
|
18
|
+
const a = this.arrived;
|
|
19
|
+
this.arrived = new Deferred();
|
|
20
|
+
a.resolve();
|
|
21
|
+
}
|
|
22
|
+
/** Resolves once at least one record is buffered, with everything buffered, in order; removes them. */
|
|
23
|
+
async take() {
|
|
24
|
+
while (this.records.length === 0)
|
|
25
|
+
await this.arrived.promise;
|
|
26
|
+
const out = this.records;
|
|
27
|
+
this.records = [];
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
/** Records buffered now (not yet taken). */
|
|
31
|
+
get pending() {
|
|
32
|
+
return this.records.length;
|
|
33
|
+
}
|
|
34
|
+
/** A copy of everything buffered; does not consume. For tests and diagnostics. */
|
|
35
|
+
snapshot() {
|
|
36
|
+
return [...this.records];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* An event that opts in to recording (spec recording.md#recording-protocol).
|
|
41
|
+
* Subclasses implement `runOutcome`, and `dispatch` (the quote) to run
|
|
42
|
+
* on the sim clock; never `finish`. Register them with
|
|
43
|
+
* `RecordingEvent.deserialize`, which accepts zero arguments.
|
|
44
|
+
*/
|
|
45
|
+
export class RecordingEvent {
|
|
46
|
+
/** The recording deserialize: any arguments, including none. */
|
|
47
|
+
static deserialize = Deserializers.anyArguments;
|
|
48
|
+
/** Whether this record requests the consumer's attention; the event's own decision. */
|
|
49
|
+
wake = true;
|
|
50
|
+
/** Set by runOutcome when the outcome took less than the quote. */
|
|
51
|
+
actualDuration = undefined;
|
|
52
|
+
preempt = new AbortController();
|
|
53
|
+
/** Scheduling only: keep the arguments, return a quote. Required on the sim clock; ignored on the wall clock. */
|
|
54
|
+
dispatch(_env, _args) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
/** Called when the event is preempted; may set wake and return notes. Must not change world state. */
|
|
58
|
+
onPreempt(_env) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
/** On a dispatch that does not record: runs the outcome, records nothing. */
|
|
62
|
+
async finish(env, args) {
|
|
63
|
+
await this.runOutcome(env, args);
|
|
64
|
+
}
|
|
65
|
+
// -- wall-clock cooperation ----------------------------------------
|
|
66
|
+
/** Aborted once the dispatch asks this event to stop. */
|
|
67
|
+
get preemptSignal() {
|
|
68
|
+
return this.preempt.signal;
|
|
69
|
+
}
|
|
70
|
+
preemptRequested() {
|
|
71
|
+
return this.preempt.signal.aborted;
|
|
72
|
+
}
|
|
73
|
+
/** Resolves once a preempt is requested. */
|
|
74
|
+
waitPreempt() {
|
|
75
|
+
const s = this.preempt.signal;
|
|
76
|
+
if (s.aborted)
|
|
77
|
+
return Promise.resolve();
|
|
78
|
+
return new Promise((r) => s.addEventListener("abort", () => r(), { once: true }));
|
|
79
|
+
}
|
|
80
|
+
/** @internal */
|
|
81
|
+
requestPreempt() {
|
|
82
|
+
this.preempt.abort();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Builds a record. */
|
|
86
|
+
function record(env, ev, args, startedAt, t, outcome, fields) {
|
|
87
|
+
const r = {
|
|
88
|
+
event_id: env.id, event: env.name, args: [...args], started_at: startedAt, t, wake: Boolean(ev.wake), outcome,
|
|
89
|
+
};
|
|
90
|
+
if (ev.actualDuration !== undefined)
|
|
91
|
+
r.actual_duration = ev.actualDuration;
|
|
92
|
+
return Object.assign(r, fields);
|
|
93
|
+
}
|
|
94
|
+
/** on_preempt's notes, then the elapsed (and progress) fields. */
|
|
95
|
+
function preemptFields(env, ev, elapsed, duration) {
|
|
96
|
+
const f = {};
|
|
97
|
+
const notes = ev.onPreempt(env);
|
|
98
|
+
if (notes && typeof notes === "object" && Object.keys(notes).length > 0)
|
|
99
|
+
f.notes = notes;
|
|
100
|
+
f.elapsed = elapsed;
|
|
101
|
+
if (duration !== undefined && duration > 0)
|
|
102
|
+
f.progress = elapsed / duration;
|
|
103
|
+
return f;
|
|
104
|
+
}
|
|
105
|
+
function registerLog(d) {
|
|
106
|
+
let log = d.blackboard.get(RECORDINGS_KEY);
|
|
107
|
+
if (!(log instanceof RecordingLog)) {
|
|
108
|
+
log = new RecordingLog();
|
|
109
|
+
d.blackboard.set(RECORDINGS_KEY, log);
|
|
110
|
+
}
|
|
111
|
+
return log;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Recording on the sim clock (spec recording.md). A recording event's
|
|
115
|
+
* runOutcome runs at completion time; its record carries the quote.
|
|
116
|
+
*/
|
|
117
|
+
export class SimRecordingDispatcher extends SimDispatcher {
|
|
118
|
+
recordings;
|
|
119
|
+
constructor(name, options = {}) {
|
|
120
|
+
super(name, options);
|
|
121
|
+
this.recordings = registerLog(this);
|
|
122
|
+
}
|
|
123
|
+
async complete(s) {
|
|
124
|
+
const ev = s.event;
|
|
125
|
+
if (!(ev instanceof RecordingEvent))
|
|
126
|
+
return super.complete(s);
|
|
127
|
+
let outcome;
|
|
128
|
+
try {
|
|
129
|
+
outcome = await ev.runOutcome(s.env, s.args);
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
this.logger("error", `${this.name}: ${s.env.name}@${s.env.id} run_outcome failed`, e);
|
|
133
|
+
return; // no record; the event is released
|
|
134
|
+
}
|
|
135
|
+
this.recordings.append(record(s.env, ev, s.args, s.startedAt, this.now, outcome, { duration: s.duration }));
|
|
136
|
+
this.settle(s, ev);
|
|
137
|
+
}
|
|
138
|
+
/** Settles the clock back to started_at + actual_duration (spec recording.md#settling). */
|
|
139
|
+
settle(s, ev) {
|
|
140
|
+
if (ev.actualDuration === undefined)
|
|
141
|
+
return;
|
|
142
|
+
const settled = s.startedAt + ev.actualDuration;
|
|
143
|
+
if (settled >= this.now)
|
|
144
|
+
return; // at or beyond the quote: recorded, never settled
|
|
145
|
+
if (this.engineAttached) {
|
|
146
|
+
this.logger("error", `${this.name}: cannot settle the clock for ${s.env.name}@${s.env.id} back from `
|
|
147
|
+
+ `${this.now} to ${settled}: an engine is attached, and has already stepped to the quote`, new Error("settle with an engine attached"));
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const next = this.nextAt();
|
|
151
|
+
if (next !== null && next < settled)
|
|
152
|
+
return;
|
|
153
|
+
this.now = settled;
|
|
154
|
+
}
|
|
155
|
+
/** A cancelled recording event's PREEMPTED record is written now. */
|
|
156
|
+
cancelled(s) {
|
|
157
|
+
const ev = s.event;
|
|
158
|
+
if (!(ev instanceof RecordingEvent))
|
|
159
|
+
return;
|
|
160
|
+
const elapsed = this.now - s.startedAt;
|
|
161
|
+
this.recordings.append(record(s.env, ev, s.args, s.startedAt, this.now, PREEMPTED, { duration: s.duration, ...preemptFields(s.env, ev, elapsed, s.duration) }));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/** A monotonic clock in seconds (web standard `performance.now`). */
|
|
165
|
+
function monotonicSeconds() {
|
|
166
|
+
return performance.now() / 1000;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Recording on the wall clock (spec recording.md#wall-clock): a CS
|
|
170
|
+
* queue dispatch whose `now` is monotonic seconds. A recording event's
|
|
171
|
+
* dispatch runs inline (its quote is ignored), then runOutcome runs in
|
|
172
|
+
* its own task; the record is written when it returns.
|
|
173
|
+
*/
|
|
174
|
+
export class WallRecordingDispatcher extends Dispatcher {
|
|
175
|
+
recordings;
|
|
176
|
+
active = new Map();
|
|
177
|
+
constructor(name, options = {}) {
|
|
178
|
+
super(name, { ...options, cs: true });
|
|
179
|
+
this.recordings = registerLog(this);
|
|
180
|
+
}
|
|
181
|
+
get now() {
|
|
182
|
+
return monotonicSeconds();
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Asks a running recording event to stop; its PREEMPTED record is
|
|
186
|
+
* written when it returns. Returns whether it asked.
|
|
187
|
+
*/
|
|
188
|
+
cancel(eventId) {
|
|
189
|
+
const r = this.active.get(eventId);
|
|
190
|
+
if (!r)
|
|
191
|
+
return false;
|
|
192
|
+
r.cancelled = true;
|
|
193
|
+
r.ev.requestPreempt();
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
/** Recording events running now. */
|
|
197
|
+
get runningRecordings() {
|
|
198
|
+
return [...this.active.keys()];
|
|
199
|
+
}
|
|
200
|
+
startEvent(event, env, args) {
|
|
201
|
+
if (!(event instanceof RecordingEvent))
|
|
202
|
+
return super.startEvent(event, env, args);
|
|
203
|
+
event.dispatch(env, args); // inline: keeps the arguments; the quote is ignored
|
|
204
|
+
const startedAt = this.now;
|
|
205
|
+
const entry = { ev: event, cancelled: false };
|
|
206
|
+
this.active.set(env.id, entry);
|
|
207
|
+
this.live++;
|
|
208
|
+
void this.runRecording(event, env, args, startedAt, entry);
|
|
209
|
+
}
|
|
210
|
+
async runRecording(ev, env, args, startedAt, entry) {
|
|
211
|
+
try {
|
|
212
|
+
await Handoff.at("dispatch");
|
|
213
|
+
let outcome;
|
|
214
|
+
let failed = false;
|
|
215
|
+
try {
|
|
216
|
+
outcome = await ev.runOutcome(env, args);
|
|
217
|
+
}
|
|
218
|
+
catch (e) {
|
|
219
|
+
failed = true;
|
|
220
|
+
this.logger("error", `${this.name}: ${env.name}@${env.id} run_outcome failed`, e);
|
|
221
|
+
}
|
|
222
|
+
await Handoff.at("finish");
|
|
223
|
+
this.active.delete(env.id);
|
|
224
|
+
if (failed)
|
|
225
|
+
return;
|
|
226
|
+
const t = this.now;
|
|
227
|
+
if (entry.cancelled) {
|
|
228
|
+
this.recordings.append(record(env, ev, args, startedAt, t, PREEMPTED, { ...preemptFields(env, ev, t - startedAt), partial_outcome: outcome }));
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
this.recordings.append(record(env, ev, args, startedAt, t, outcome, {}));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
finally {
|
|
235
|
+
this.live--;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
//# sourceMappingURL=recording.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
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AS,EAAE,CAAC,EAAE,SAAS,EAClE,EAAE,GAAG,aAAa,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,GAAG,SAAS,CAAC,EAAE,eAAe,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;YAC7E,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;YAC3E,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,IAAI,EAAE,CAAC;QACd,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** The round counter wraps here (spec cs.md#rounds). */
|
|
2
|
+
export declare const ROLLOVER = 100;
|
|
3
|
+
/** Thrown by acquire when the right is already waiting. */
|
|
4
|
+
export declare class RightWaitingError extends Error {
|
|
5
|
+
constructor(id: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Releases every waiting right once each of its lefts has been
|
|
9
|
+
* released in the current round (spec cs.md#composite-semaphore).
|
|
10
|
+
*
|
|
11
|
+
* The cut point: a right that acquires before a round completes is
|
|
12
|
+
* released by it; one that acquires after waits for the next round.
|
|
13
|
+
* Released rights are forgotten, so an identifier may acquire again.
|
|
14
|
+
*/
|
|
15
|
+
export declare class CompositeSemaphore {
|
|
16
|
+
private readonly counters;
|
|
17
|
+
private tick;
|
|
18
|
+
private keyCount;
|
|
19
|
+
private inRound;
|
|
20
|
+
private roundDone;
|
|
21
|
+
private rights;
|
|
22
|
+
constructor(lefts: Iterable<string>);
|
|
23
|
+
/** Advances k's counter to this round; false if already released this round. */
|
|
24
|
+
private tickLeft;
|
|
25
|
+
/**
|
|
26
|
+
* Releases left k for the current round. If that completes the
|
|
27
|
+
* round, every waiting right is released with status (1 if none)
|
|
28
|
+
* and a new round begins. A key that is not a left does nothing.
|
|
29
|
+
*/
|
|
30
|
+
release(k: string, status?: unknown): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Registers the right id and waits until a round releases it,
|
|
33
|
+
* returning the round's status. onRegistered, if given, runs once
|
|
34
|
+
* the right is waiting: a round that completes after it releases
|
|
35
|
+
* this right.
|
|
36
|
+
*/
|
|
37
|
+
acquire(id: string, onRegistered?: () => void | Promise<void>): Promise<unknown>;
|
|
38
|
+
/** Whether the right id is waiting. */
|
|
39
|
+
waiting(id: string): boolean;
|
|
40
|
+
/** Adds a left; if a round is in progress, waits for it to end. */
|
|
41
|
+
addLeft(k: string): Promise<void>;
|
|
42
|
+
/** Removes a left (a key that is not a left does nothing); waits for a round in progress. */
|
|
43
|
+
clearLeft(k: string): Promise<void>;
|
|
44
|
+
private betweenRounds;
|
|
45
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Deferred, Handoff } from "./event.js";
|
|
2
|
+
/** The round counter wraps here (spec cs.md#rounds). */
|
|
3
|
+
export const ROLLOVER = 100;
|
|
4
|
+
/** Thrown by acquire when the right is already waiting. */
|
|
5
|
+
export class RightWaitingError extends Error {
|
|
6
|
+
constructor(id) {
|
|
7
|
+
super(`right is already waiting: ${id}`);
|
|
8
|
+
this.name = "RightWaitingError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Releases every waiting right once each of its lefts has been
|
|
13
|
+
* released in the current round (spec cs.md#composite-semaphore).
|
|
14
|
+
*
|
|
15
|
+
* The cut point: a right that acquires before a round completes is
|
|
16
|
+
* released by it; one that acquires after waits for the next round.
|
|
17
|
+
* Released rights are forgotten, so an identifier may acquire again.
|
|
18
|
+
*/
|
|
19
|
+
export class CompositeSemaphore {
|
|
20
|
+
counters = new Map();
|
|
21
|
+
tick = 1;
|
|
22
|
+
keyCount;
|
|
23
|
+
inRound = false;
|
|
24
|
+
roundDone = new Deferred();
|
|
25
|
+
rights = new Map();
|
|
26
|
+
/** @internal CSWaits using this semaphore. */
|
|
27
|
+
users = 0;
|
|
28
|
+
constructor(lefts) {
|
|
29
|
+
for (const l of lefts)
|
|
30
|
+
this.counters.set(l, 0);
|
|
31
|
+
this.keyCount = this.counters.size;
|
|
32
|
+
}
|
|
33
|
+
/** Advances k's counter to this round; false if already released this round. */
|
|
34
|
+
tickLeft(k) {
|
|
35
|
+
const v = this.counters.get(k);
|
|
36
|
+
if (this.tick > v) {
|
|
37
|
+
this.counters.set(k, v + 1);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
if (this.tick < v) {
|
|
41
|
+
this.counters.set(k, 0); // the round counter wrapped
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Releases left k for the current round. If that completes the
|
|
48
|
+
* round, every waiting right is released with status (1 if none)
|
|
49
|
+
* and a new round begins. A key that is not a left does nothing.
|
|
50
|
+
*/
|
|
51
|
+
async release(k, status = null) {
|
|
52
|
+
await Handoff.at("semaphore.release");
|
|
53
|
+
this.releaseNow(k, status);
|
|
54
|
+
}
|
|
55
|
+
/** @internal release without the hand-off. */
|
|
56
|
+
releaseNow(k, status) {
|
|
57
|
+
if (!this.counters.has(k) || !this.tickLeft(k))
|
|
58
|
+
return;
|
|
59
|
+
this.inRound = true;
|
|
60
|
+
if (this.keyCount > 0)
|
|
61
|
+
this.keyCount--;
|
|
62
|
+
if (this.keyCount > 0)
|
|
63
|
+
return;
|
|
64
|
+
// take exactly the rights waiting now, in O(1)
|
|
65
|
+
const taken = this.rights;
|
|
66
|
+
this.rights = new Map();
|
|
67
|
+
this.tick = (this.tick + 1) % ROLLOVER;
|
|
68
|
+
this.keyCount = this.counters.size;
|
|
69
|
+
this.inRound = false;
|
|
70
|
+
const finished = this.roundDone;
|
|
71
|
+
this.roundDone = new Deferred();
|
|
72
|
+
const st = status ?? 1;
|
|
73
|
+
for (const r of taken.values())
|
|
74
|
+
r.resolve(st);
|
|
75
|
+
finished.resolve();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Registers the right id and waits until a round releases it,
|
|
79
|
+
* returning the round's status. onRegistered, if given, runs once
|
|
80
|
+
* the right is waiting: a round that completes after it releases
|
|
81
|
+
* this right.
|
|
82
|
+
*/
|
|
83
|
+
async acquire(id, onRegistered) {
|
|
84
|
+
await Handoff.at("semaphore.acquire");
|
|
85
|
+
if (this.rights.has(id))
|
|
86
|
+
throw new RightWaitingError(id);
|
|
87
|
+
const right = new Deferred();
|
|
88
|
+
this.rights.set(id, right);
|
|
89
|
+
await onRegistered?.();
|
|
90
|
+
return right.promise;
|
|
91
|
+
}
|
|
92
|
+
/** Whether the right id is waiting. */
|
|
93
|
+
waiting(id) {
|
|
94
|
+
return this.rights.has(id);
|
|
95
|
+
}
|
|
96
|
+
/** @internal Releases every waiting right with status, without a round (clearing CS waits). */
|
|
97
|
+
releaseAll(status) {
|
|
98
|
+
const taken = this.rights;
|
|
99
|
+
this.rights = new Map();
|
|
100
|
+
for (const r of taken.values())
|
|
101
|
+
r.resolve(status);
|
|
102
|
+
}
|
|
103
|
+
/** Adds a left; if a round is in progress, waits for it to end. */
|
|
104
|
+
addLeft(k) {
|
|
105
|
+
return this.betweenRounds(() => {
|
|
106
|
+
if (!this.counters.has(k)) {
|
|
107
|
+
this.counters.set(k, (((this.tick - 1) % ROLLOVER) + ROLLOVER) % ROLLOVER);
|
|
108
|
+
this.keyCount++;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
/** Removes a left (a key that is not a left does nothing); waits for a round in progress. */
|
|
113
|
+
clearLeft(k) {
|
|
114
|
+
return this.betweenRounds(() => {
|
|
115
|
+
if (this.counters.delete(k))
|
|
116
|
+
this.keyCount--;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async betweenRounds(change) {
|
|
120
|
+
while (this.inRound)
|
|
121
|
+
await this.roundDone.promise;
|
|
122
|
+
change();
|
|
123
|
+
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AAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,GAAG,QAAQ,CAAC,GAAG,QAAQ,CAAC,CAAC;gBAC3E,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,6FAA6F;IAC7F,SAAS,CAAC,CAAS;QACjB,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE;YAC7B,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;gBAAE,IAAI,CAAC,QAAQ,EAAE,CAAC;QAC/C,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,KAAK,CAAC,aAAa,CAAC,MAAkB;QAC5C,OAAO,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;QAClD,MAAM,EAAE,CAAC;IACX,CAAC;CACF"}
|
package/dist/sim.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type DispatcherOptions, Dispatcher } from "./dispatcher.js";
|
|
2
|
+
import { type Env, type Event } from "./event.js";
|
|
3
|
+
/** A scheduled completion. */
|
|
4
|
+
export interface Scheduled {
|
|
5
|
+
readonly event: Event;
|
|
6
|
+
readonly env: Env;
|
|
7
|
+
readonly args: unknown[];
|
|
8
|
+
readonly startedAt: number;
|
|
9
|
+
readonly duration: number;
|
|
10
|
+
readonly completeAt: number;
|
|
11
|
+
}
|
|
12
|
+
export interface SimDispatcherOptions extends Omit<DispatcherOptions, "cs"> {
|
|
13
|
+
/**
|
|
14
|
+
* Steps an engine's world from one clock time to the next, before
|
|
15
|
+
* each completion fires (spec sim.md#world-stepping). Passing it is
|
|
16
|
+
* what makes an engine attached.
|
|
17
|
+
*/
|
|
18
|
+
stepWorld?: (from: number, to: number) => void | Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A sim dispatch (spec sim.md): a CS queue dispatch whose events quote
|
|
22
|
+
* durations. Its clock, `now`, starts at 0 and moves only when an
|
|
23
|
+
* event completes. One driver fires completions in order of completion
|
|
24
|
+
* time, ties in quote order, so a run is deterministic.
|
|
25
|
+
*
|
|
26
|
+
* An event's `dispatch` returns its quote synchronously (a number
|
|
27
|
+
* >= 0) and does nothing else; its `finish` is the completion step.
|
|
28
|
+
* CSWait and CSRelease stay outside the clock, in their own tasks.
|
|
29
|
+
*/
|
|
30
|
+
export declare class SimDispatcher extends Dispatcher {
|
|
31
|
+
/** The simulated clock. */
|
|
32
|
+
now: number;
|
|
33
|
+
/** Whether an engine is attached (a stepWorld was given). */
|
|
34
|
+
readonly engineAttached: boolean;
|
|
35
|
+
private readonly stepWorldFn;
|
|
36
|
+
private readonly heap;
|
|
37
|
+
private readonly scheduledMap;
|
|
38
|
+
private seq;
|
|
39
|
+
private readonly outstanding;
|
|
40
|
+
private fired;
|
|
41
|
+
constructor(name: string, options?: SimDispatcherOptions);
|
|
42
|
+
/** Completions scheduled and not yet fired or cancelled. */
|
|
43
|
+
get scheduledEvents(): number;
|
|
44
|
+
/** Clock time until the next scheduled completion, or null. */
|
|
45
|
+
timeToNext(): number | null;
|
|
46
|
+
/**
|
|
47
|
+
* Removes a scheduled completion: the event never finishes (spec
|
|
48
|
+
* sim.md#cancel). An id that is not scheduled does nothing. Returns
|
|
49
|
+
* whether it cancelled.
|
|
50
|
+
*/
|
|
51
|
+
cancel(eventId: number): boolean;
|
|
52
|
+
/** Called when a scheduled event is cancelled. */
|
|
53
|
+
protected cancelled(_s: Scheduled): void;
|
|
54
|
+
protected run(): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* One drain: the CS gate as a verdict (spec sim.md#cs). While an
|
|
57
|
+
* accepted CSWait is not yet waiting, the rest of the queue stays
|
|
58
|
+
* queued and the driver keeps firing completions.
|
|
59
|
+
*/
|
|
60
|
+
private drainCycle;
|
|
61
|
+
/** Fires the soonest scheduled completion; returns its event id, or null. */
|
|
62
|
+
private advanceOne;
|
|
63
|
+
/** The completion step: finish. */
|
|
64
|
+
protected complete(s: Scheduled): Promise<void>;
|
|
65
|
+
private less;
|
|
66
|
+
private push;
|
|
67
|
+
private pop;
|
|
68
|
+
}
|