@mark1russell7/algebra 0.8.10 → 0.9.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 +12 -1
- package/build/{clock-CjLibabw.d.ts → clock-oJqrb5Q4.d.ts} +30 -6
- package/build/clock.d.ts +2 -2
- package/build/clock.js +38 -7
- package/build/clock.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/{run-sAafntBU.d.ts → run-BgUWHAcf.d.ts} +1 -1
- package/build/run.d.ts +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -125,7 +125,8 @@ An app gives `realClock` of `@mark1russell7/algebra/clock` in place of the fake
|
|
|
125
125
|
| --- | --- |
|
|
126
126
|
| `Clock` | `now()` gives the monotonic time in milliseconds. `after(ms, run)` starts `run` after `ms` and gives its `Cancel`. |
|
|
127
127
|
| `platformClock({ unref, speed, start })` | A clock on `performance.now()` and `setTimeout`. With `unref: true`, its timers do not keep a Node process alive. With a speed or a start, the clock is scaled. |
|
|
128
|
-
| `realClock` |
|
|
128
|
+
| `realClock` | `platformClock()`: its timers keep a Node process alive, as `setTimeout` does. |
|
|
129
|
+
| `unrefClock(clock)` | A clock with the time of `clock`, whose timers do not keep a Node process alive. A wall clock stays a wall clock. |
|
|
129
130
|
| `wallClock({ unref, speed, start })` | A `WallClock`: `now()` gives the instant of `Date.now()`, and its timers are the timers of `platformClock`. With a speed or a start, the clock is scaled. |
|
|
130
131
|
| `fakeClock(start)` | A clock that moves only on `advance(ms)`. `block(ms)` moves it and starts no timer, as a blocked event loop does. `pending` counts the live timers. |
|
|
131
132
|
| `fakeWallClock(start)` | A fake `WallClock`. `set(instant)` steps its instant forward or back, and no timer moves. |
|
|
@@ -142,6 +143,16 @@ await clock.advance(30_000); // the sweep runs, at 30 s exactly
|
|
|
142
143
|
await delay(realClock, retryAfter, controller.signal);
|
|
143
144
|
```
|
|
144
145
|
|
|
146
|
+
### The rule of `unref`
|
|
147
|
+
|
|
148
|
+
Each timer of `realClock` keeps a Node process alive, as `setTimeout` does. Thus a process does not end in a wait that a caller awaits, for example the wait of a retry.
|
|
149
|
+
|
|
150
|
+
- The owner of a timer uses the `unref` of its cancel only when a socket, a child process or another hold keeps the process alive. Examples are the heartbeat of a connection and the health checks of a service.
|
|
151
|
+
- Each other timer stays referenced, a ticker too. Its caller stops it.
|
|
152
|
+
- `unrefClock(clock)` gives a part a clock whose timers do not keep the process alive. The owner of the part gives it only when another hold keeps the process alive.
|
|
153
|
+
- `platformClock({ unref: true })` makes each timer of a new clock an exception, with the same condition.
|
|
154
|
+
- A hang shows. A silent exit with the code 0 and lost work does not. Before algebra 0.9.0, the timers of `realClock` did not keep the process alive, and a command-line tool ended in the wait of a retry.
|
|
155
|
+
|
|
145
156
|
### A scaled clock
|
|
146
157
|
|
|
147
158
|
A scaled clock runs a long schedule in a short real time, on the real timers. A fake clock moves only on `advance`. Thus it cannot drive a server, a proxy or a socket that waits on real time.
|
|
@@ -59,6 +59,13 @@ interface PlatformClockOptions {
|
|
|
59
59
|
* This function makes a clock of the platform: `performance.now()` and `setTimeout`. Each cancel
|
|
60
60
|
* that it gives also has `unref`. A delay longer than `setTimeout` takes waits in steps.
|
|
61
61
|
*
|
|
62
|
+
* The rule of `unref`: each timer keeps a Node process alive, as `setTimeout` does. Thus a process
|
|
63
|
+
* does not end in a wait that a caller awaits. A hang shows, but a silent exit with lost work does not.
|
|
64
|
+
*
|
|
65
|
+
* The owner of a timer uses its `unref` only when a socket, a child process or another hold keeps the
|
|
66
|
+
* process alive. An example is the heartbeat of a connection. Each other timer stays referenced, a
|
|
67
|
+
* ticker too: its caller stops it. The option `unref` makes each timer of the clock an exception.
|
|
68
|
+
*
|
|
62
69
|
* With a speed or a start, `now()` is the start plus the monotonic time since the call, times the
|
|
63
70
|
* speed. A scaled clock reads `performance.now()` at each timer. Thus the fake timers of a test
|
|
64
71
|
* also fake `performance`, else its timers wait again and again.
|
|
@@ -67,10 +74,27 @@ interface PlatformClockOptions {
|
|
|
67
74
|
*/
|
|
68
75
|
declare const platformClock: (options?: PlatformClockOptions) => Clock;
|
|
69
76
|
/**
|
|
70
|
-
* This clock reads `performance.now()` and uses `setTimeout`.
|
|
71
|
-
* process alive,
|
|
77
|
+
* This clock reads `performance.now()` and uses `setTimeout`. It is `platformClock()`, thus its
|
|
78
|
+
* timers keep a Node process alive, as `setTimeout` does. A process does not end in a wait that a
|
|
79
|
+
* caller awaits, for example a retry.
|
|
80
|
+
*
|
|
81
|
+
* The owner of a timer uses its `unref` only when a socket, a child process or another hold keeps the
|
|
82
|
+
* process alive. Each other timer stays referenced: its caller stops it. A timer that is not stopped
|
|
83
|
+
* delays the exit, and that delay shows.
|
|
84
|
+
*
|
|
85
|
+
* Before algebra 0.9.0, the timers of this clock did not keep the process alive. A process then ended
|
|
86
|
+
* with the code 0 in a wait, and lost its work.
|
|
72
87
|
*/
|
|
73
88
|
declare const realClock: Clock;
|
|
89
|
+
/**
|
|
90
|
+
* This function gives a clock with the time of `clock`. Its timers do not keep a Node process alive:
|
|
91
|
+
* each timer uses the `unref` of its cancel. The owner of a part gives it to the part only
|
|
92
|
+
* when a socket, a child process or another hold keeps the process alive. An example is the
|
|
93
|
+
* heartbeat of a connection. A wall clock stays a wall clock. The timers of a fake clock have no
|
|
94
|
+
* `unref`, thus they do not change.
|
|
95
|
+
*/
|
|
96
|
+
declare function unrefClock(clock: Clock): Clock;
|
|
97
|
+
declare function unrefClock(clock: WallClock): WallClock;
|
|
74
98
|
/**
|
|
75
99
|
* This function makes the wall clock of the platform: `Date.now()`, and the timers of
|
|
76
100
|
* `platformClock`. The options are the options of `platformClock`.
|
|
@@ -86,9 +110,9 @@ declare const wallClock: (options?: PlatformClockOptions) => WallClock;
|
|
|
86
110
|
* This function gives a promise that resolves after `ms` milliseconds on the clock. When the signal
|
|
87
111
|
* aborts, the promise stops its timer and rejects with the reason of the signal. A signal that
|
|
88
112
|
* aborted before the call rejects at once, and starts no timer. Each end removes the listener from
|
|
89
|
-
* the signal. A timer of `realClock`
|
|
90
|
-
*
|
|
91
|
-
*
|
|
113
|
+
* the signal. A timer of `realClock` keeps a Node process alive until the delay ends. The clock is
|
|
114
|
+
* a `Clock` or a `WallClock`: the function uses only its timers, and each timer of the two waits
|
|
115
|
+
* on the monotonic time.
|
|
92
116
|
*/
|
|
93
117
|
declare const delay: (clock: Pick<Clock, "after">, ms: number, signal?: AbortSignal) => Promise<void>;
|
|
94
118
|
/** A fake clock moves only when a test tells it to. */
|
|
@@ -224,4 +248,4 @@ declare function firstInstantOf(wall: number, zone: string): number | undefined;
|
|
|
224
248
|
declare function nextBoundary(instant: number, window: TimeWindow): WindowBoundary | undefined;
|
|
225
249
|
/** This function gives the problems of a window. With a problem, `inWindow` gives `false` and `nextBoundary` gives `undefined`. */
|
|
226
250
|
declare function windowProblems(window: TimeWindow): string[];
|
|
227
|
-
export { windowProblems as S, nextBoundary as _, LocalTime as a, realClock as b, WallClock as c, fakeClock as d, fakeWallClock as f, localTime as g, instantsOf as h, FakeWallClock as i, WindowBoundary as l, inWindow as m, Clock as n, PlatformClockOptions as o, firstInstantOf as p, FakeClock as r, TimeWindow as s, Cancel as t, delay as u, offsetChanges as v,
|
|
251
|
+
export { windowProblems as C, wallClock as S, nextBoundary as _, LocalTime as a, realClock as b, WallClock as c, fakeClock as d, fakeWallClock as f, localTime as g, instantsOf as h, FakeWallClock as i, WindowBoundary as l, inWindow as m, Clock as n, PlatformClockOptions as o, firstInstantOf as p, FakeClock as r, TimeWindow as s, Cancel as t, delay as u, offsetChanges as v, unrefClock as x, platformClock as y };
|
package/build/clock.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { Cancel, Clock, FakeClock, FakeWallClock, LocalTime, PlatformClockOptions, TimeWindow, WallClock, WindowBoundary, delay, fakeClock, fakeWallClock, firstInstantOf, inWindow, instantsOf, localTime, nextBoundary, offsetChanges, platformClock, realClock, wallClock, windowProblems };
|
|
1
|
+
import { C as windowProblems, S as wallClock, _ as nextBoundary, a as LocalTime, b as realClock, c as WallClock, d as fakeClock, f as fakeWallClock, g as localTime, h as instantsOf, i as FakeWallClock, l as WindowBoundary, m as inWindow, n as Clock, o as PlatformClockOptions, p as firstInstantOf, r as FakeClock, s as TimeWindow, t as Cancel, u as delay, v as offsetChanges, x as unrefClock, y as platformClock } from "./clock-oJqrb5Q4.js";
|
|
2
|
+
export { Cancel, Clock, FakeClock, FakeWallClock, LocalTime, PlatformClockOptions, TimeWindow, WallClock, WindowBoundary, delay, fakeClock, fakeWallClock, firstInstantOf, inWindow, instantsOf, localTime, nextBoundary, offsetChanges, platformClock, realClock, unrefClock, wallClock, windowProblems };
|
package/build/clock.js
CHANGED
|
@@ -44,6 +44,13 @@ const timersOf = (unrefAll, speed, now) => (ms, run) => {
|
|
|
44
44
|
* This function makes a clock of the platform: `performance.now()` and `setTimeout`. Each cancel
|
|
45
45
|
* that it gives also has `unref`. A delay longer than `setTimeout` takes waits in steps.
|
|
46
46
|
*
|
|
47
|
+
* The rule of `unref`: each timer keeps a Node process alive, as `setTimeout` does. Thus a process
|
|
48
|
+
* does not end in a wait that a caller awaits. A hang shows, but a silent exit with lost work does not.
|
|
49
|
+
*
|
|
50
|
+
* The owner of a timer uses its `unref` only when a socket, a child process or another hold keeps the
|
|
51
|
+
* process alive. An example is the heartbeat of a connection. Each other timer stays referenced, a
|
|
52
|
+
* ticker too: its caller stops it. The option `unref` makes each timer of the clock an exception.
|
|
53
|
+
*
|
|
47
54
|
* With a speed or a start, `now()` is the start plus the monotonic time since the call, times the
|
|
48
55
|
* speed. A scaled clock reads `performance.now()` at each timer. Thus the fake timers of a test
|
|
49
56
|
* also fake `performance`, else its timers wait again and again.
|
|
@@ -66,10 +73,34 @@ const platformClock = (options = {}) => {
|
|
|
66
73
|
};
|
|
67
74
|
};
|
|
68
75
|
/**
|
|
69
|
-
* This clock reads `performance.now()` and uses `setTimeout`.
|
|
70
|
-
* process alive,
|
|
76
|
+
* This clock reads `performance.now()` and uses `setTimeout`. It is `platformClock()`, thus its
|
|
77
|
+
* timers keep a Node process alive, as `setTimeout` does. A process does not end in a wait that a
|
|
78
|
+
* caller awaits, for example a retry.
|
|
79
|
+
*
|
|
80
|
+
* The owner of a timer uses its `unref` only when a socket, a child process or another hold keeps the
|
|
81
|
+
* process alive. Each other timer stays referenced: its caller stops it. A timer that is not stopped
|
|
82
|
+
* delays the exit, and that delay shows.
|
|
83
|
+
*
|
|
84
|
+
* Before algebra 0.9.0, the timers of this clock did not keep the process alive. A process then ended
|
|
85
|
+
* with the code 0 in a wait, and lost its work.
|
|
71
86
|
*/
|
|
72
|
-
const realClock = /* @__PURE__ */ platformClock(
|
|
87
|
+
const realClock = /* @__PURE__ */ platformClock();
|
|
88
|
+
function unrefClock(clock) {
|
|
89
|
+
const after = (ms, run) => {
|
|
90
|
+
const cancel = clock.after(ms, run);
|
|
91
|
+
cancel.unref?.();
|
|
92
|
+
return cancel;
|
|
93
|
+
};
|
|
94
|
+
const now = () => clock.now();
|
|
95
|
+
return clock.time === "wall" ? {
|
|
96
|
+
time: "wall",
|
|
97
|
+
now,
|
|
98
|
+
after
|
|
99
|
+
} : {
|
|
100
|
+
now,
|
|
101
|
+
after
|
|
102
|
+
};
|
|
103
|
+
}
|
|
73
104
|
/**
|
|
74
105
|
* This function makes the wall clock of the platform: `Date.now()`, and the timers of
|
|
75
106
|
* `platformClock`. The options are the options of `platformClock`.
|
|
@@ -101,9 +132,9 @@ const wallClock = (options = {}) => {
|
|
|
101
132
|
* This function gives a promise that resolves after `ms` milliseconds on the clock. When the signal
|
|
102
133
|
* aborts, the promise stops its timer and rejects with the reason of the signal. A signal that
|
|
103
134
|
* aborted before the call rejects at once, and starts no timer. Each end removes the listener from
|
|
104
|
-
* the signal. A timer of `realClock`
|
|
105
|
-
*
|
|
106
|
-
*
|
|
135
|
+
* the signal. A timer of `realClock` keeps a Node process alive until the delay ends. The clock is
|
|
136
|
+
* a `Clock` or a `WallClock`: the function uses only its timers, and each timer of the two waits
|
|
137
|
+
* on the monotonic time.
|
|
107
138
|
*/
|
|
108
139
|
const delay = (clock, ms, signal) => new Promise((resolve, reject) => {
|
|
109
140
|
if (signal?.aborted === true) {
|
|
@@ -455,6 +486,6 @@ function windowProblems(window) {
|
|
|
455
486
|
if (window.days !== void 0 && window.days.length === 0) problems.push("The window has no day, thus it does not open.");
|
|
456
487
|
return problems;
|
|
457
488
|
}
|
|
458
|
-
export { delay, fakeClock, fakeWallClock, firstInstantOf, inWindow, instantsOf, localTime, nextBoundary, offsetChanges, platformClock, realClock, wallClock, windowProblems };
|
|
489
|
+
export { delay, fakeClock, fakeWallClock, firstInstantOf, inWindow, instantsOf, localTime, nextBoundary, offsetChanges, platformClock, realClock, unrefClock, wallClock, windowProblems };
|
|
459
490
|
|
|
460
491
|
//# sourceMappingURL=clock.js.map
|
package/build/clock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clock.js","names":[],"sources":["../src/clock.ts"],"sourcesContent":["// @mark1russell7/algebra/clock: the clock of a runtime, a fake clock for tests, and civil time. It imports\n// nothing: no `node:` module, no zod and no other package. Thus a client in a browser can use it.\n// The service gives this clock again as @mark1russell7/service/clock.\n\n/**\n * This function stops a timer that `Clock.after` started. A second use does nothing. A clock of\n * the platform also gives `unref`: after it, the timer does not keep a Node process alive.\n */\nexport type Cancel = (() => void) & { readonly unref?: (() => void) | undefined };\n\n/**\n * A clock gives the monotonic time in milliseconds, and it starts a function after a delay. The\n * runtime reads time only through a clock. Thus a test can give a fake clock, and the timing of\n * the runtime is then exact and repeatable.\n */\nexport interface Clock {\n /**\n * This mark tells the time of `now()`. A clock gives the monotonic time, thus the mark is\n * `\"monotonic\"` or absent. A `WallClock` has the mark `\"wall\"`, thus it is not a `Clock`.\n */\n readonly time?: \"monotonic\" | undefined;\n /** This method gives the monotonic time, in milliseconds. A step of the wall clock does not move it. */\n now(): number;\n /** This method starts `run` after `ms` milliseconds, and gives the function that stops it. */\n after(ms: number, run: () => void): Cancel;\n}\n\n/**\n * A wall clock gives the instant: the time in milliseconds from 1970-01-01T00:00:00Z, as `Date.now()`\n * gives it. Civil time, a calendar and a time that another computer sends need the instant. A step\n * of the wall clock moves `now()`, also back. Each timer waits on the monotonic time, thus a step\n * does not move a timer. The mark `\"wall\"` keeps the two clocks apart: a function that needs the\n * monotonic time does not get a wall clock, and the reverse.\n */\nexport interface WallClock {\n readonly time: \"wall\";\n /** This method gives the instant, in milliseconds from the epoch. A step of the wall clock moves it. */\n now(): number;\n /** This method starts `run` after `ms` milliseconds of monotonic time, and gives the function that stops it. */\n after(ms: number, run: () => void): Cancel;\n}\n\n/**\n * These options make a clock of the platform. With a speed or a start, the clock is scaled: it\n * counts from its start on the monotonic time of the platform, `speed` times faster. Thus a test\n * runs a long schedule in a short real time, on the real timers.\n */\nexport interface PlatformClockOptions {\n /** True makes each timer not keep a Node process alive. The default is false, as with `setTimeout`. */\n readonly unref?: boolean | undefined;\n /**\n * The milliseconds of the clock in one real millisecond: a finite number above 0. The default is 1.\n * At the speed 60, `now()` moves 60 ms in each real millisecond, and a timer of 60 s ends after 1\n * real second. A speed below 1 gives a slow clock.\n */\n readonly speed?: number | undefined;\n /**\n * The time of `now()` when the clock is made, in milliseconds: a finite number. A wall clock reads\n * it as an instant. The default is the time of the platform at that moment.\n */\n readonly start?: number | undefined;\n}\n\n/** This function gives the monotonic time: `performance.now()`, or `Date.now()` on a platform without `performance`. */\nconst monotonic = (): number => {\n const performance = (globalThis as { performance?: { now(): number } }).performance;\n return performance === undefined ? Date.now() : performance.now();\n};\n\n/** This delay is the longest that `setTimeout` takes, in milliseconds. A longer delay starts the function after 1 ms. */\nconst LONGEST_DELAY = 0x7fffffff;\n\n/** This function gives the speed of the options. It throws a `RangeError` for a bad speed or a bad start. */\nconst speedOf = (options: PlatformClockOptions): number => {\n const speed = options.speed ?? 1;\n if (typeof speed !== \"number\" || !Number.isFinite(speed) || speed <= 0) throw new RangeError(`The speed ${String(speed)} is not a finite number above 0.`);\n const start = options.start;\n if (start !== undefined && (typeof start !== \"number\" || !Number.isFinite(start))) throw new RangeError(`The start ${String(start)} is not a finite number.`);\n return speed;\n};\n\n/**\n * This function makes the timers of a clock of the platform. A timer of `ms` milliseconds on the\n * clock waits `ms / speed` real milliseconds, in steps when `setTimeout` does not take that delay.\n * With `now`, the timer reads the clock when the platform starts it. When the clock is before the\n * due time, the timer waits again for the rest. Thus `run` reads its due time or a later time.\n */\nconst timersOf =\n (unrefAll: boolean, speed: number, now?: () => number): Clock[\"after\"] =>\n (ms, run) => {\n let unrefed = unrefAll;\n const due = now === undefined ? 0 : now() + Math.max(0, ms);\n const fire = (): void => {\n const early = now === undefined ? 0 : due - now();\n if (early > 0) timer = arm(early / speed);\n else run();\n };\n const arm = (left: number): ReturnType<typeof setTimeout> => {\n const step =\n left > LONGEST_DELAY ? setTimeout(() => (timer = arm(left - LONGEST_DELAY)), LONGEST_DELAY) : setTimeout(fire, Math.max(0, left));\n if (unrefed) (step as { unref?: () => void }).unref?.();\n return step;\n };\n let timer = arm(ms / speed);\n const unref = (): void => {\n unrefed = true;\n (timer as { unref?: () => void }).unref?.();\n };\n return Object.assign(() => clearTimeout(timer), { unref });\n };\n\n/**\n * This function makes a clock of the platform: `performance.now()` and `setTimeout`. Each cancel\n * that it gives also has `unref`. A delay longer than `setTimeout` takes waits in steps.\n *\n * With a speed or a start, `now()` is the start plus the monotonic time since the call, times the\n * speed. A scaled clock reads `performance.now()` at each timer. Thus the fake timers of a test\n * also fake `performance`, else its timers wait again and again.\n *\n * @throws {RangeError} for a speed that is not a finite number above 0, and for a start that is not finite.\n */\nexport const platformClock = (options: PlatformClockOptions = {}): Clock => {\n const speed = speedOf(options);\n const unref = options.unref === true;\n if (speed === 1 && options.start === undefined) return { now: monotonic, after: timersOf(unref, 1) };\n const origin = monotonic();\n const start = options.start ?? origin;\n const now = (): number => start + (monotonic() - origin) * speed;\n return { now, after: timersOf(unref, speed, now) };\n};\n\n/**\n * This clock reads `performance.now()` and uses `setTimeout`. Its timers do not keep a Node\n * process alive, thus a timer that is not stopped cannot stop the exit of the process.\n */\nexport const realClock: Clock = /* @__PURE__ */ platformClock({ unref: true });\n\n/**\n * This function makes the wall clock of the platform: `Date.now()`, and the timers of\n * `platformClock`. The options are the options of `platformClock`.\n *\n * With a speed or a start, `now()` is the start plus the monotonic time since the call, times the\n * speed. It gives whole milliseconds, as `Date.now()` does. The default start is `Date.now()`.\n * A step of the wall clock of the platform does not move a scaled clock.\n *\n * @throws {RangeError} for a speed that is not a finite number above 0, and for a start that is not finite.\n */\nexport const wallClock = (options: PlatformClockOptions = {}): WallClock => {\n const speed = speedOf(options);\n const unref = options.unref === true;\n if (speed === 1 && options.start === undefined) return { time: \"wall\", now: () => Date.now(), after: timersOf(unref, 1) };\n const origin = monotonic();\n const start = options.start ?? Date.now();\n const now = (): number => Math.floor(start + (monotonic() - origin) * speed);\n return { time: \"wall\", now, after: timersOf(unref, speed, now) };\n};\n\n/**\n * This function gives a promise that resolves after `ms` milliseconds on the clock. When the signal\n * aborts, the promise stops its timer and rejects with the reason of the signal. A signal that\n * aborted before the call rejects at once, and starts no timer. Each end removes the listener from\n * the signal. A timer of `realClock` does not keep a Node process alive: give `platformClock()` to\n * keep the process alive until the delay ends. The clock is a `Clock` or a `WallClock`: the\n * function uses only its timers, and each timer of the two waits on the monotonic time.\n */\nexport const delay = (clock: Pick<Clock, \"after\">, ms: number, signal?: AbortSignal): Promise<void> =>\n new Promise<void>((resolve, reject) => {\n if (signal?.aborted === true) {\n reject(signal.reason);\n return;\n }\n const abort = (): void => {\n cancel();\n reject(signal?.reason);\n };\n const cancel = clock.after(ms, () => {\n signal?.removeEventListener(\"abort\", abort);\n resolve();\n });\n signal?.addEventListener(\"abort\", abort, { once: true });\n });\n\n/** A fake clock moves only when a test tells it to. */\nexport interface FakeClock extends Clock {\n /** This method moves the clock forward by `ms`, and it starts each timer that comes due, in sequence. */\n advance(ms: number): Promise<void>;\n /**\n * This method moves the clock forward by `ms` and starts no timer, as a blocked event loop does.\n * The next `advance` then starts each timer that came due during the block, late.\n */\n block(ms: number): void;\n /** This count is the number of timers that are not stopped and not called. */\n readonly pending: number;\n}\n\n/** This function lets the promises that are ready continue, before the clock moves again. */\nconst settle = async (): Promise<void> => {\n for (let turn = 0; turn < 64; turn += 1) await Promise.resolve();\n};\n\n/** This function makes a fake clock. Its time starts at `start` milliseconds. */\nexport const fakeClock = (start = 0): FakeClock => {\n let now = start;\n let sequence = 0;\n const timers: Array<{ at: number; order: number; run: () => void; live: boolean }> = [];\n const due = (end: number) => {\n let best: (typeof timers)[number] | undefined;\n for (const timer of timers)\n if (timer.live && timer.at <= end && (best === undefined || timer.at < best.at || (timer.at === best.at && timer.order < best.order)))\n best = timer;\n return best;\n };\n return {\n now: () => now,\n after(ms, run): Cancel {\n const timer = { at: now + Math.max(0, ms), order: sequence++, run, live: true };\n timers.push(timer);\n return () => {\n timer.live = false;\n };\n },\n async advance(ms) {\n // a monotonic clock does not go back\n const end = now + Math.max(0, ms);\n await settle();\n for (;;) {\n const timer = due(end);\n if (timer === undefined) break;\n timer.live = false;\n // a timer that came due during a block runs late, at the time of the clock\n now = Math.max(now, timer.at);\n timer.run();\n await settle();\n }\n now = Math.max(now, end);\n for (let at = timers.length - 1; at >= 0; at -= 1) if (!timers[at]!.live) timers.splice(at, 1);\n await settle();\n },\n block(ms) {\n now += Math.max(0, ms);\n },\n get pending() {\n return timers.filter((timer) => timer.live).length;\n },\n };\n};\n\n/** A fake wall clock moves only when a test tells it to. A test can also step its instant, as a wall clock steps. */\nexport interface FakeWallClock extends WallClock {\n /** This method moves the instant and the monotonic time forward by `ms`, and it starts each timer that comes due, in sequence. */\n advance(ms: number): Promise<void>;\n /** This method moves the instant and the monotonic time forward by `ms`, and starts no timer, as a blocked event loop does. */\n block(ms: number): void;\n /** This method steps the instant to `instant`, forward or back. The monotonic time does not move, thus no timer comes due. */\n set(instant: number): void;\n /** This count is the number of timers that are not stopped and not called. */\n readonly pending: number;\n}\n\n/**\n * This function makes a fake wall clock. Its instant starts at `start`, in milliseconds from the\n * epoch. Its timers are the timers of a `fakeClock`, thus a step of the instant moves no timer.\n */\nexport const fakeWallClock = (start: number): FakeWallClock => {\n const monotonicTime = fakeClock(0);\n let offset = start;\n return {\n time: \"wall\",\n now: () => monotonicTime.now() + offset,\n after: (ms, run) => monotonicTime.after(ms, run),\n advance: (ms) => monotonicTime.advance(ms),\n block(ms) {\n monotonicTime.block(ms);\n },\n set(instant) {\n offset = instant - monotonicTime.now();\n },\n get pending() {\n return monotonicTime.pending;\n },\n };\n};\n\n// Civil time: the wall-clock time of an instant in a time zone, and windows of local time. A clock\n// gives instants, and civil time maps an instant to the time on the wall of a zone. It uses only\n// `Intl.DateTimeFormat`, thus this entry still imports nothing.\n\n/**\n * The wall-clock time of an instant in a time zone. An instant is a time in milliseconds from\n * 1970-01-01T00:00:00Z, as `Date.now()` and `WallClock.now()` give. It is not the monotonic time of `Clock.now()`.\n */\nexport interface LocalTime {\n readonly year: number;\n /** The month, from 1 (January) to 12. */\n readonly month: number;\n /** The day of the month, from 1. */\n readonly day: number;\n /** The hour, from 0 to 23. */\n readonly hour: number;\n readonly minute: number;\n readonly second: number;\n /** The day of the week, from 0 (Sunday) to 6 (Saturday). */\n readonly weekday: number;\n /** The offset of the zone from UTC at the instant, in minutes. It is positive east of Greenwich. */\n readonly offset: number;\n}\n\n/**\n * A window of local time that occurs on each day, or on some days of the week. The window is\n * data, thus a configuration can hold it.\n *\n * - The start is in the window, and the end is not.\n * - An end before the start crosses midnight. An end equal to the start gives a window of a full day.\n * - In a change of the clock, the window follows the time on the wall. A start or an end in a gap\n * moves to the end of the gap. Thus a window with its start and its end in one gap does not open.\n * - A local time that occurs two times is in the window two times.\n */\nexport interface TimeWindow {\n /** The IANA name of the time zone, for example `Europe/Berlin`. */\n readonly zone: string;\n /** The start, a local time `HH:MM` from `00:00` to `23:59`. */\n readonly start: string;\n /** The end, a local time `HH:MM` from `00:00` to `23:59`. */\n readonly end: string;\n /**\n * The days of the week on which the window starts, from 0 (Sunday) to 6. When it is absent, the\n * window starts on each day. A window across midnight continues into the next day.\n */\n readonly days?: readonly number[] | undefined;\n}\n\n/** A change of a window: the instant at which it opens or closes. */\nexport interface WindowBoundary {\n readonly at: number;\n /** True: the window opens at `at`. False: it closes. */\n readonly opens: boolean;\n}\n\nconst MINUTE_MS = 60_000;\nconst DAY_MS = 86_400_000;\nconst WEEKDAYS: Readonly<Record<string, number>> = { Sun: 0, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6 };\nconst LOCAL_TIME = /^([01]\\d|2[0-3]):([0-5]\\d)$/;\n\n/**\n * The cache has one format for each good zone name. It keeps no bad name, and it starts again at\n * `ZONE_CACHE_MAX` names. Thus names from configuration input cannot make it grow without a limit.\n */\nconst ZONE_FORMATS = new Map<string, Intl.DateTimeFormat>();\nconst ZONE_CACHE_MAX = 512;\n/** The longest IANA name has 32 characters. A longer name than this limit is not a zone. */\nconst ZONE_NAME_MAX = 100;\n\nconst zoneFormat = (zone: string): Intl.DateTimeFormat | undefined => {\n let format = ZONE_FORMATS.get(zone);\n if (format === undefined) {\n if (typeof zone !== \"string\" || zone.length > ZONE_NAME_MAX) return undefined;\n try {\n format = new Intl.DateTimeFormat(\"en-US\", {\n timeZone: zone,\n hourCycle: \"h23\",\n era: \"short\",\n year: \"numeric\",\n month: \"numeric\",\n day: \"numeric\",\n hour: \"numeric\",\n minute: \"numeric\",\n second: \"numeric\",\n weekday: \"short\",\n });\n } catch {\n return undefined;\n }\n if (ZONE_FORMATS.size >= ZONE_CACHE_MAX) ZONE_FORMATS.clear();\n ZONE_FORMATS.set(zone, format);\n }\n return format;\n};\n\n/**\n * This function gives the wall-clock time of `instant` in `zone`. It gives `undefined` for a name\n * that is not a time zone, and for an instant that is not a date.\n */\nexport function localTime(instant: number, zone: string): LocalTime | undefined {\n const format = zoneFormat(zone);\n if (format === undefined || !Number.isFinite(instant)) return undefined;\n let parts: Intl.DateTimeFormatPart[];\n try {\n parts = format.formatToParts(instant);\n } catch {\n // an instant out of the range of a date\n return undefined;\n }\n const field: Record<string, string> = {};\n for (const part of parts) field[part.type] = part.value;\n const before = field[\"era\"] === \"BC\" || field[\"era\"] === \"B\";\n const year = before ? 1 - Number(field[\"year\"]) : Number(field[\"year\"]);\n const month = Number(field[\"month\"]);\n const day = Number(field[\"day\"]);\n const hour = Number(field[\"hour\"]) % 24;\n const minute = Number(field[\"minute\"]);\n const second = Number(field[\"second\"]);\n const whole = Math.floor(instant / 1000) * 1000;\n return { year, month, day, hour, minute, second, weekday: WEEKDAYS[field[\"weekday\"] ?? \"\"] ?? 0, offset: (wallOf(year, month, day, hour, minute, second) - whole) / MINUTE_MS };\n}\n\n/** The wall-clock time as an instant in UTC. `Date.UTC` reads a year from 0 to 99 as 1900 to 1999, thus this function sets the year by itself. */\nconst wallOf = (year: number, month: number, day: number, hour: number, minute: number, second: number): number => {\n const wall = new Date(0);\n wall.setUTCFullYear(year, month - 1, day);\n wall.setUTCHours(hour, minute, second, 0);\n return wall.getTime();\n};\n\n/** The minutes from midnight of a local time `HH:MM`. */\nconst minutesOf = (time: string): number | undefined => {\n const match = LOCAL_TIME.exec(time);\n return match === null ? undefined : Number(match[1]) * 60 + Number(match[2]);\n};\n\ninterface CheckedWindow {\n readonly start: number;\n readonly end: number;\n readonly days: ReadonlySet<number> | undefined;\n}\n\nconst isDay = (day: number): boolean => Number.isInteger(day) && day >= 0 && day <= 6;\n\nconst checked = (window: TimeWindow): CheckedWindow | undefined => {\n const start = minutesOf(window.start);\n const end = minutesOf(window.end);\n if (start === undefined || end === undefined || zoneFormat(window.zone) === undefined) return undefined;\n if (window.days !== undefined && !window.days.every(isDay)) return undefined;\n return { start, end, days: window.days === undefined ? undefined : new Set(window.days) };\n};\n\nconst within = (window: CheckedWindow, local: LocalTime): boolean => {\n const minute = local.hour * 60 + local.minute;\n const startsOn = (weekday: number): boolean => window.days === undefined || window.days.has(weekday);\n if (window.start < window.end) return window.start <= minute && minute < window.end && startsOn(local.weekday);\n // across midnight, or a full day: the part from the start, then the part before the end on the next day\n return (minute >= window.start && startsOn(local.weekday)) || (minute < window.end && startsOn((local.weekday + 6) % 7));\n};\n\n/**\n * This function tells if `instant` is in the window. It does not throw. A bad window or a bad\n * instant gives `false`, and `windowProblems` finds the bad window.\n */\nexport function inWindow(instant: number, window: TimeWindow): boolean {\n const valid = checked(window);\n const local = valid === undefined ? undefined : localTime(instant, window.zone);\n return valid !== undefined && local !== undefined && within(valid, local);\n}\n\n/**\n * This function gives the instants at which the wall clock of `zone` shows `wall`, in order. It is\n * the inverse of `localTime`. The wall-clock time is in milliseconds with the arithmetic of UTC: the\n * UTC fields of `new Date(wall)` are the fields of the wall clock.\n *\n * - A wall-clock time in the gap of a change forward gives no instant.\n * - A wall-clock time in the hour of a change back gives two instants.\n * - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.\n *\n * The rule for a gap or for two instants is a choice of the caller. For example, RFC 5545 takes the\n * first of two instants, and in a gap it takes the offset before the gap. The search reads the\n * offset one day before and one day after `wall`. Thus it finds each instant when the offset\n * changes at most one time in these 2 days.\n */\nexport function instantsOf(wall: number, zone: string): number[] | undefined {\n if (zoneFormat(zone) === undefined || !Number.isFinite(wall)) return undefined;\n // localTime gives whole seconds: the search uses the whole second, and adds the milliseconds at the end\n const whole = Math.floor(wall / 1000) * 1000;\n const offsets = new Set<number>();\n for (const probe of [whole - DAY_MS, whole, whole + DAY_MS]) {\n const local = localTime(probe, zone);\n if (local !== undefined) offsets.add(local.offset);\n }\n // no offset: the wall-clock time is out of the range of a date\n if (offsets.size === 0) return undefined;\n const found: number[] = [];\n for (const offset of offsets) {\n // an old offset of local mean time has seconds, thus the product is rounded to the millisecond\n const instant = whole - Math.round(offset * MINUTE_MS);\n const local = localTime(instant, zone);\n if (local === undefined) continue;\n const back = wallOf(local.year, local.month, local.day, local.hour, local.minute, local.second);\n if (back === whole && !found.includes(instant)) found.push(instant);\n }\n return found.sort((a, b) => a - b).map((instant) => instant + (wall - whole));\n}\n\n/**\n * This function gives the instants in `(from, to]` at which the offset of `zone` changes, in order\n * and exact to the millisecond. At each instant, the new offset starts. The search reads the offset\n * each 6 hours of the range, thus a long range costs more reads. No zone changes its offset two\n * times in 6 hours. A name that is not a time zone, or a bound that is not a date, gives `undefined`.\n */\nexport function offsetChanges(from: number, to: number, zone: string): number[] | undefined {\n if (localTime(from, zone) === undefined || localTime(to, zone) === undefined) return undefined;\n return changesOf(from, to, zone);\n}\n\n/** The instants in (from, to] at which the offset of the zone changes. `offsetChanges` tells the rule. */\nconst changesOf = (from: number, to: number, zone: string): number[] => {\n const offsetAt = (instant: number): number | undefined => localTime(instant, zone)?.offset;\n const found: number[] = [];\n let low = from;\n let before = offsetAt(low);\n while (low < to) {\n const high = Math.min(low + DAY_MS / 4, to);\n const after = offsetAt(high);\n if (before !== undefined && after !== undefined && after !== before) {\n // the first instant with the new offset\n let left = low;\n let right = high;\n while (right - left > 1) {\n const middle = Math.floor((left + right) / 2);\n if (offsetAt(middle) === before) left = middle;\n else right = middle;\n }\n found.push(right);\n }\n low = high;\n before = after;\n }\n return found;\n};\n\n/**\n * This function gives the first instant at which the wall clock of `zone` shows `wall` or a later\n * wall-clock time. The wall-clock time is in milliseconds with the arithmetic of UTC, as for\n * `instantsOf`. Thus a time on the wall, for example the start of a day, always has an instant.\n *\n * - A wall-clock time that occurs one time gives its instant.\n * - A wall-clock time in the hour of a change back gives the first of its two instants.\n * - A wall-clock time in the gap of a change forward gives the instant of the change: the end of the gap.\n * - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.\n *\n * The search reads the offset one day before and one day after `wall`, as `instantsOf` does. A gap\n * that is not in these 2 days also gives `undefined`.\n */\nexport function firstInstantOf(wall: number, zone: string): number | undefined {\n const instants = instantsOf(wall, zone);\n if (instants === undefined) return undefined;\n if (instants.length > 0) return instants[0];\n const whole = Math.floor(wall / 1000) * 1000;\n const offsets: number[] = [];\n for (const probe of [whole - DAY_MS, whole, whole + DAY_MS]) {\n const local = localTime(probe, zone);\n if (local !== undefined) offsets.push(Math.round(local.offset * MINUTE_MS));\n }\n // A change at the instant c, from the offset o1 to the larger offset o2, skips the wall-clock times\n // from c + o1 to c + o2, without the end. Thus c is after wall - o2, and at or before wall - o1.\n const from = wall - Math.max(...offsets);\n const to = wall - Math.min(...offsets);\n const offsetAt = (instant: number): number | undefined => {\n const local = localTime(instant, zone);\n return local === undefined ? undefined : Math.round(local.offset * MINUTE_MS);\n };\n for (const change of changesOf(from, to, zone)) {\n const before = offsetAt(change - 1);\n const after = offsetAt(change);\n if (before !== undefined && after !== undefined && change + before <= wall && wall < change + after) return change;\n }\n return undefined;\n}\n\n/**\n * This function gives the next change of the window after `instant`: the first later instant at\n * which the window opens or closes. A timer can wait for it in closed form. It gives `undefined`\n * for a bad window, and when the window does not change in the 9 days after `instant`. A window\n * with no day and a full-day window on each day do not change.\n */\nexport function nextBoundary(instant: number, window: TimeWindow): WindowBoundary | undefined {\n const valid = checked(window);\n const local = valid === undefined ? undefined : localTime(instant, window.zone);\n if (valid === undefined || local === undefined) return undefined;\n const inside = within(valid, local);\n const horizon = instant + 9 * DAY_MS;\n // The state changes only where the wall clock shows the start or the end, or where the offset changes.\n const candidates = changesOf(instant, horizon, window.zone);\n const midnight = wallOf(local.year, local.month, local.day, 0, 0, 0);\n for (let day = -1; day <= 10; day += 1) {\n for (const minutes of [valid.start, valid.end]) candidates.push(...(instantsOf(midnight + day * DAY_MS + minutes * MINUTE_MS, window.zone) ?? []));\n }\n candidates.sort((a, b) => a - b);\n for (const at of candidates) {\n if (at <= instant || at > horizon) continue;\n const there = localTime(at, window.zone);\n if (there !== undefined && within(valid, there) !== inside) return { at, opens: !inside };\n }\n return undefined;\n}\n\n/** This function gives the problems of a window. With a problem, `inWindow` gives `false` and `nextBoundary` gives `undefined`. */\nexport function windowProblems(window: TimeWindow): string[] {\n const problems: string[] = [];\n if (zoneFormat(window.zone) === undefined) problems.push(`\"${window.zone}\" is not the IANA name of a time zone.`);\n if (minutesOf(window.start) === undefined) problems.push(`The start \"${window.start}\" is not a local time HH:MM from 00:00 to 23:59.`);\n if (minutesOf(window.end) === undefined) problems.push(`The end \"${window.end}\" is not a local time HH:MM from 00:00 to 23:59.`);\n for (const day of window.days ?? []) {\n if (!isDay(day)) problems.push(`The day ${day} is not a day of the week from 0 (Sunday) to 6.`);\n }\n if (window.days !== undefined && window.days.length === 0) problems.push(\"The window has no day, thus it does not open.\");\n return problems;\n}\n"],"mappings":";;AAgEA,MAAM,kBAA0B;CAC9B,MAAM,cAAe,WAAmD;CACxE,OAAO,gBAAgB,KAAA,IAAY,KAAK,IAAI,IAAI,YAAY,IAAI;AAClE;;AAGA,MAAM,gBAAgB;;AAGtB,MAAM,WAAW,YAA0C;CACzD,MAAM,QAAQ,QAAQ,SAAS;CAC/B,IAAI,OAAO,UAAU,YAAY,CAAC,OAAO,SAAS,KAAK,KAAK,SAAS,GAAG,MAAM,IAAI,WAAW,aAAa,OAAO,KAAK,EAAE,iCAAiC;CACzJ,MAAM,QAAQ,QAAQ;CACtB,IAAI,UAAU,KAAA,MAAc,OAAO,UAAU,YAAY,CAAC,OAAO,SAAS,KAAK,IAAI,MAAM,IAAI,WAAW,aAAa,OAAO,KAAK,EAAE,yBAAyB;CAC5J,OAAO;AACT;;;;;;;AAQA,MAAM,YACH,UAAmB,OAAe,SAClC,IAAI,QAAQ;CACX,IAAI,UAAU;CACd,MAAM,MAAM,QAAQ,KAAA,IAAY,IAAI,IAAI,IAAI,KAAK,IAAI,GAAG,EAAE;CAC1D,MAAM,aAAmB;EACvB,MAAM,QAAQ,QAAQ,KAAA,IAAY,IAAI,MAAM,IAAI;EAChD,IAAI,QAAQ,GAAG,QAAQ,IAAI,QAAQ,KAAK;OACnC,IAAI;CACX;CACA,MAAM,OAAO,SAAgD;EAC3D,MAAM,OACJ,OAAO,gBAAgB,iBAAkB,QAAQ,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AAG,SAAS,KAAK,WAAW,IAAI,gDAAgD;CAEhG,IAAI,OAAO,SAAS,KAAA,KAAa,OAAO,KAAK,WAAW,GAAG,SAAS,KAAK,+CAA+C;CACxH,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"clock.js","names":[],"sources":["../src/clock.ts"],"sourcesContent":["// @mark1russell7/algebra/clock: the clock of a runtime, a fake clock for tests, and civil time. It imports\n// nothing: no `node:` module, no zod and no other package. Thus a client in a browser can use it.\n// The service gives this clock again as @mark1russell7/service/clock.\n\n/**\n * This function stops a timer that `Clock.after` started. A second use does nothing. A clock of\n * the platform also gives `unref`: after it, the timer does not keep a Node process alive.\n */\nexport type Cancel = (() => void) & { readonly unref?: (() => void) | undefined };\n\n/**\n * A clock gives the monotonic time in milliseconds, and it starts a function after a delay. The\n * runtime reads time only through a clock. Thus a test can give a fake clock, and the timing of\n * the runtime is then exact and repeatable.\n */\nexport interface Clock {\n /**\n * This mark tells the time of `now()`. A clock gives the monotonic time, thus the mark is\n * `\"monotonic\"` or absent. A `WallClock` has the mark `\"wall\"`, thus it is not a `Clock`.\n */\n readonly time?: \"monotonic\" | undefined;\n /** This method gives the monotonic time, in milliseconds. A step of the wall clock does not move it. */\n now(): number;\n /** This method starts `run` after `ms` milliseconds, and gives the function that stops it. */\n after(ms: number, run: () => void): Cancel;\n}\n\n/**\n * A wall clock gives the instant: the time in milliseconds from 1970-01-01T00:00:00Z, as `Date.now()`\n * gives it. Civil time, a calendar and a time that another computer sends need the instant. A step\n * of the wall clock moves `now()`, also back. Each timer waits on the monotonic time, thus a step\n * does not move a timer. The mark `\"wall\"` keeps the two clocks apart: a function that needs the\n * monotonic time does not get a wall clock, and the reverse.\n */\nexport interface WallClock {\n readonly time: \"wall\";\n /** This method gives the instant, in milliseconds from the epoch. A step of the wall clock moves it. */\n now(): number;\n /** This method starts `run` after `ms` milliseconds of monotonic time, and gives the function that stops it. */\n after(ms: number, run: () => void): Cancel;\n}\n\n/**\n * These options make a clock of the platform. With a speed or a start, the clock is scaled: it\n * counts from its start on the monotonic time of the platform, `speed` times faster. Thus a test\n * runs a long schedule in a short real time, on the real timers.\n */\nexport interface PlatformClockOptions {\n /** True makes each timer not keep a Node process alive. The default is false, as with `setTimeout`. */\n readonly unref?: boolean | undefined;\n /**\n * The milliseconds of the clock in one real millisecond: a finite number above 0. The default is 1.\n * At the speed 60, `now()` moves 60 ms in each real millisecond, and a timer of 60 s ends after 1\n * real second. A speed below 1 gives a slow clock.\n */\n readonly speed?: number | undefined;\n /**\n * The time of `now()` when the clock is made, in milliseconds: a finite number. A wall clock reads\n * it as an instant. The default is the time of the platform at that moment.\n */\n readonly start?: number | undefined;\n}\n\n/** This function gives the monotonic time: `performance.now()`, or `Date.now()` on a platform without `performance`. */\nconst monotonic = (): number => {\n const performance = (globalThis as { performance?: { now(): number } }).performance;\n return performance === undefined ? Date.now() : performance.now();\n};\n\n/** This delay is the longest that `setTimeout` takes, in milliseconds. A longer delay starts the function after 1 ms. */\nconst LONGEST_DELAY = 0x7fffffff;\n\n/** This function gives the speed of the options. It throws a `RangeError` for a bad speed or a bad start. */\nconst speedOf = (options: PlatformClockOptions): number => {\n const speed = options.speed ?? 1;\n if (typeof speed !== \"number\" || !Number.isFinite(speed) || speed <= 0) throw new RangeError(`The speed ${String(speed)} is not a finite number above 0.`);\n const start = options.start;\n if (start !== undefined && (typeof start !== \"number\" || !Number.isFinite(start))) throw new RangeError(`The start ${String(start)} is not a finite number.`);\n return speed;\n};\n\n/**\n * This function makes the timers of a clock of the platform. A timer of `ms` milliseconds on the\n * clock waits `ms / speed` real milliseconds, in steps when `setTimeout` does not take that delay.\n * With `now`, the timer reads the clock when the platform starts it. When the clock is before the\n * due time, the timer waits again for the rest. Thus `run` reads its due time or a later time.\n */\nconst timersOf =\n (unrefAll: boolean, speed: number, now?: () => number): Clock[\"after\"] =>\n (ms, run) => {\n let unrefed = unrefAll;\n const due = now === undefined ? 0 : now() + Math.max(0, ms);\n const fire = (): void => {\n const early = now === undefined ? 0 : due - now();\n if (early > 0) timer = arm(early / speed);\n else run();\n };\n const arm = (left: number): ReturnType<typeof setTimeout> => {\n const step =\n left > LONGEST_DELAY ? setTimeout(() => (timer = arm(left - LONGEST_DELAY)), LONGEST_DELAY) : setTimeout(fire, Math.max(0, left));\n if (unrefed) (step as { unref?: () => void }).unref?.();\n return step;\n };\n let timer = arm(ms / speed);\n const unref = (): void => {\n unrefed = true;\n (timer as { unref?: () => void }).unref?.();\n };\n return Object.assign(() => clearTimeout(timer), { unref });\n };\n\n/**\n * This function makes a clock of the platform: `performance.now()` and `setTimeout`. Each cancel\n * that it gives also has `unref`. A delay longer than `setTimeout` takes waits in steps.\n *\n * The rule of `unref`: each timer keeps a Node process alive, as `setTimeout` does. Thus a process\n * does not end in a wait that a caller awaits. A hang shows, but a silent exit with lost work does not.\n *\n * The owner of a timer uses its `unref` only when a socket, a child process or another hold keeps the\n * process alive. An example is the heartbeat of a connection. Each other timer stays referenced, a\n * ticker too: its caller stops it. The option `unref` makes each timer of the clock an exception.\n *\n * With a speed or a start, `now()` is the start plus the monotonic time since the call, times the\n * speed. A scaled clock reads `performance.now()` at each timer. Thus the fake timers of a test\n * also fake `performance`, else its timers wait again and again.\n *\n * @throws {RangeError} for a speed that is not a finite number above 0, and for a start that is not finite.\n */\nexport const platformClock = (options: PlatformClockOptions = {}): Clock => {\n const speed = speedOf(options);\n const unref = options.unref === true;\n if (speed === 1 && options.start === undefined) return { now: monotonic, after: timersOf(unref, 1) };\n const origin = monotonic();\n const start = options.start ?? origin;\n const now = (): number => start + (monotonic() - origin) * speed;\n return { now, after: timersOf(unref, speed, now) };\n};\n\n/**\n * This clock reads `performance.now()` and uses `setTimeout`. It is `platformClock()`, thus its\n * timers keep a Node process alive, as `setTimeout` does. A process does not end in a wait that a\n * caller awaits, for example a retry.\n *\n * The owner of a timer uses its `unref` only when a socket, a child process or another hold keeps the\n * process alive. Each other timer stays referenced: its caller stops it. A timer that is not stopped\n * delays the exit, and that delay shows.\n *\n * Before algebra 0.9.0, the timers of this clock did not keep the process alive. A process then ended\n * with the code 0 in a wait, and lost its work.\n */\nexport const realClock: Clock = /* @__PURE__ */ platformClock();\n\n/**\n * This function gives a clock with the time of `clock`. Its timers do not keep a Node process alive:\n * each timer uses the `unref` of its cancel. The owner of a part gives it to the part only\n * when a socket, a child process or another hold keeps the process alive. An example is the\n * heartbeat of a connection. A wall clock stays a wall clock. The timers of a fake clock have no\n * `unref`, thus they do not change.\n */\nexport function unrefClock(clock: Clock): Clock;\nexport function unrefClock(clock: WallClock): WallClock;\nexport function unrefClock(clock: Clock | WallClock): Clock | WallClock {\n const after = (ms: number, run: () => void): Cancel => {\n const cancel = clock.after(ms, run);\n cancel.unref?.();\n return cancel;\n };\n const now = (): number => clock.now();\n return clock.time === \"wall\" ? { time: \"wall\", now, after } : { now, after };\n}\n\n/**\n * This function makes the wall clock of the platform: `Date.now()`, and the timers of\n * `platformClock`. The options are the options of `platformClock`.\n *\n * With a speed or a start, `now()` is the start plus the monotonic time since the call, times the\n * speed. It gives whole milliseconds, as `Date.now()` does. The default start is `Date.now()`.\n * A step of the wall clock of the platform does not move a scaled clock.\n *\n * @throws {RangeError} for a speed that is not a finite number above 0, and for a start that is not finite.\n */\nexport const wallClock = (options: PlatformClockOptions = {}): WallClock => {\n const speed = speedOf(options);\n const unref = options.unref === true;\n if (speed === 1 && options.start === undefined) return { time: \"wall\", now: () => Date.now(), after: timersOf(unref, 1) };\n const origin = monotonic();\n const start = options.start ?? Date.now();\n const now = (): number => Math.floor(start + (monotonic() - origin) * speed);\n return { time: \"wall\", now, after: timersOf(unref, speed, now) };\n};\n\n/**\n * This function gives a promise that resolves after `ms` milliseconds on the clock. When the signal\n * aborts, the promise stops its timer and rejects with the reason of the signal. A signal that\n * aborted before the call rejects at once, and starts no timer. Each end removes the listener from\n * the signal. A timer of `realClock` keeps a Node process alive until the delay ends. The clock is\n * a `Clock` or a `WallClock`: the function uses only its timers, and each timer of the two waits\n * on the monotonic time.\n */\nexport const delay = (clock: Pick<Clock, \"after\">, ms: number, signal?: AbortSignal): Promise<void> =>\n new Promise<void>((resolve, reject) => {\n if (signal?.aborted === true) {\n reject(signal.reason);\n return;\n }\n const abort = (): void => {\n cancel();\n reject(signal?.reason);\n };\n const cancel = clock.after(ms, () => {\n signal?.removeEventListener(\"abort\", abort);\n resolve();\n });\n signal?.addEventListener(\"abort\", abort, { once: true });\n });\n\n/** A fake clock moves only when a test tells it to. */\nexport interface FakeClock extends Clock {\n /** This method moves the clock forward by `ms`, and it starts each timer that comes due, in sequence. */\n advance(ms: number): Promise<void>;\n /**\n * This method moves the clock forward by `ms` and starts no timer, as a blocked event loop does.\n * The next `advance` then starts each timer that came due during the block, late.\n */\n block(ms: number): void;\n /** This count is the number of timers that are not stopped and not called. */\n readonly pending: number;\n}\n\n/** This function lets the promises that are ready continue, before the clock moves again. */\nconst settle = async (): Promise<void> => {\n for (let turn = 0; turn < 64; turn += 1) await Promise.resolve();\n};\n\n/** This function makes a fake clock. Its time starts at `start` milliseconds. */\nexport const fakeClock = (start = 0): FakeClock => {\n let now = start;\n let sequence = 0;\n const timers: Array<{ at: number; order: number; run: () => void; live: boolean }> = [];\n const due = (end: number) => {\n let best: (typeof timers)[number] | undefined;\n for (const timer of timers)\n if (timer.live && timer.at <= end && (best === undefined || timer.at < best.at || (timer.at === best.at && timer.order < best.order)))\n best = timer;\n return best;\n };\n return {\n now: () => now,\n after(ms, run): Cancel {\n const timer = { at: now + Math.max(0, ms), order: sequence++, run, live: true };\n timers.push(timer);\n return () => {\n timer.live = false;\n };\n },\n async advance(ms) {\n // a monotonic clock does not go back\n const end = now + Math.max(0, ms);\n await settle();\n for (;;) {\n const timer = due(end);\n if (timer === undefined) break;\n timer.live = false;\n // a timer that came due during a block runs late, at the time of the clock\n now = Math.max(now, timer.at);\n timer.run();\n await settle();\n }\n now = Math.max(now, end);\n for (let at = timers.length - 1; at >= 0; at -= 1) if (!timers[at]!.live) timers.splice(at, 1);\n await settle();\n },\n block(ms) {\n now += Math.max(0, ms);\n },\n get pending() {\n return timers.filter((timer) => timer.live).length;\n },\n };\n};\n\n/** A fake wall clock moves only when a test tells it to. A test can also step its instant, as a wall clock steps. */\nexport interface FakeWallClock extends WallClock {\n /** This method moves the instant and the monotonic time forward by `ms`, and it starts each timer that comes due, in sequence. */\n advance(ms: number): Promise<void>;\n /** This method moves the instant and the monotonic time forward by `ms`, and starts no timer, as a blocked event loop does. */\n block(ms: number): void;\n /** This method steps the instant to `instant`, forward or back. The monotonic time does not move, thus no timer comes due. */\n set(instant: number): void;\n /** This count is the number of timers that are not stopped and not called. */\n readonly pending: number;\n}\n\n/**\n * This function makes a fake wall clock. Its instant starts at `start`, in milliseconds from the\n * epoch. Its timers are the timers of a `fakeClock`, thus a step of the instant moves no timer.\n */\nexport const fakeWallClock = (start: number): FakeWallClock => {\n const monotonicTime = fakeClock(0);\n let offset = start;\n return {\n time: \"wall\",\n now: () => monotonicTime.now() + offset,\n after: (ms, run) => monotonicTime.after(ms, run),\n advance: (ms) => monotonicTime.advance(ms),\n block(ms) {\n monotonicTime.block(ms);\n },\n set(instant) {\n offset = instant - monotonicTime.now();\n },\n get pending() {\n return monotonicTime.pending;\n },\n };\n};\n\n// Civil time: the wall-clock time of an instant in a time zone, and windows of local time. A clock\n// gives instants, and civil time maps an instant to the time on the wall of a zone. It uses only\n// `Intl.DateTimeFormat`, thus this entry still imports nothing.\n\n/**\n * The wall-clock time of an instant in a time zone. An instant is a time in milliseconds from\n * 1970-01-01T00:00:00Z, as `Date.now()` and `WallClock.now()` give. It is not the monotonic time of `Clock.now()`.\n */\nexport interface LocalTime {\n readonly year: number;\n /** The month, from 1 (January) to 12. */\n readonly month: number;\n /** The day of the month, from 1. */\n readonly day: number;\n /** The hour, from 0 to 23. */\n readonly hour: number;\n readonly minute: number;\n readonly second: number;\n /** The day of the week, from 0 (Sunday) to 6 (Saturday). */\n readonly weekday: number;\n /** The offset of the zone from UTC at the instant, in minutes. It is positive east of Greenwich. */\n readonly offset: number;\n}\n\n/**\n * A window of local time that occurs on each day, or on some days of the week. The window is\n * data, thus a configuration can hold it.\n *\n * - The start is in the window, and the end is not.\n * - An end before the start crosses midnight. An end equal to the start gives a window of a full day.\n * - In a change of the clock, the window follows the time on the wall. A start or an end in a gap\n * moves to the end of the gap. Thus a window with its start and its end in one gap does not open.\n * - A local time that occurs two times is in the window two times.\n */\nexport interface TimeWindow {\n /** The IANA name of the time zone, for example `Europe/Berlin`. */\n readonly zone: string;\n /** The start, a local time `HH:MM` from `00:00` to `23:59`. */\n readonly start: string;\n /** The end, a local time `HH:MM` from `00:00` to `23:59`. */\n readonly end: string;\n /**\n * The days of the week on which the window starts, from 0 (Sunday) to 6. When it is absent, the\n * window starts on each day. A window across midnight continues into the next day.\n */\n readonly days?: readonly number[] | undefined;\n}\n\n/** A change of a window: the instant at which it opens or closes. */\nexport interface WindowBoundary {\n readonly at: number;\n /** True: the window opens at `at`. False: it closes. */\n readonly opens: boolean;\n}\n\nconst MINUTE_MS = 60_000;\nconst DAY_MS = 86_400_000;\nconst WEEKDAYS: Readonly<Record<string, number>> = { Sun: 0, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6 };\nconst LOCAL_TIME = /^([01]\\d|2[0-3]):([0-5]\\d)$/;\n\n/**\n * The cache has one format for each good zone name. It keeps no bad name, and it starts again at\n * `ZONE_CACHE_MAX` names. Thus names from configuration input cannot make it grow without a limit.\n */\nconst ZONE_FORMATS = new Map<string, Intl.DateTimeFormat>();\nconst ZONE_CACHE_MAX = 512;\n/** The longest IANA name has 32 characters. A longer name than this limit is not a zone. */\nconst ZONE_NAME_MAX = 100;\n\nconst zoneFormat = (zone: string): Intl.DateTimeFormat | undefined => {\n let format = ZONE_FORMATS.get(zone);\n if (format === undefined) {\n if (typeof zone !== \"string\" || zone.length > ZONE_NAME_MAX) return undefined;\n try {\n format = new Intl.DateTimeFormat(\"en-US\", {\n timeZone: zone,\n hourCycle: \"h23\",\n era: \"short\",\n year: \"numeric\",\n month: \"numeric\",\n day: \"numeric\",\n hour: \"numeric\",\n minute: \"numeric\",\n second: \"numeric\",\n weekday: \"short\",\n });\n } catch {\n return undefined;\n }\n if (ZONE_FORMATS.size >= ZONE_CACHE_MAX) ZONE_FORMATS.clear();\n ZONE_FORMATS.set(zone, format);\n }\n return format;\n};\n\n/**\n * This function gives the wall-clock time of `instant` in `zone`. It gives `undefined` for a name\n * that is not a time zone, and for an instant that is not a date.\n */\nexport function localTime(instant: number, zone: string): LocalTime | undefined {\n const format = zoneFormat(zone);\n if (format === undefined || !Number.isFinite(instant)) return undefined;\n let parts: Intl.DateTimeFormatPart[];\n try {\n parts = format.formatToParts(instant);\n } catch {\n // an instant out of the range of a date\n return undefined;\n }\n const field: Record<string, string> = {};\n for (const part of parts) field[part.type] = part.value;\n const before = field[\"era\"] === \"BC\" || field[\"era\"] === \"B\";\n const year = before ? 1 - Number(field[\"year\"]) : Number(field[\"year\"]);\n const month = Number(field[\"month\"]);\n const day = Number(field[\"day\"]);\n const hour = Number(field[\"hour\"]) % 24;\n const minute = Number(field[\"minute\"]);\n const second = Number(field[\"second\"]);\n const whole = Math.floor(instant / 1000) * 1000;\n return { year, month, day, hour, minute, second, weekday: WEEKDAYS[field[\"weekday\"] ?? \"\"] ?? 0, offset: (wallOf(year, month, day, hour, minute, second) - whole) / MINUTE_MS };\n}\n\n/** The wall-clock time as an instant in UTC. `Date.UTC` reads a year from 0 to 99 as 1900 to 1999, thus this function sets the year by itself. */\nconst wallOf = (year: number, month: number, day: number, hour: number, minute: number, second: number): number => {\n const wall = new Date(0);\n wall.setUTCFullYear(year, month - 1, day);\n wall.setUTCHours(hour, minute, second, 0);\n return wall.getTime();\n};\n\n/** The minutes from midnight of a local time `HH:MM`. */\nconst minutesOf = (time: string): number | undefined => {\n const match = LOCAL_TIME.exec(time);\n return match === null ? undefined : Number(match[1]) * 60 + Number(match[2]);\n};\n\ninterface CheckedWindow {\n readonly start: number;\n readonly end: number;\n readonly days: ReadonlySet<number> | undefined;\n}\n\nconst isDay = (day: number): boolean => Number.isInteger(day) && day >= 0 && day <= 6;\n\nconst checked = (window: TimeWindow): CheckedWindow | undefined => {\n const start = minutesOf(window.start);\n const end = minutesOf(window.end);\n if (start === undefined || end === undefined || zoneFormat(window.zone) === undefined) return undefined;\n if (window.days !== undefined && !window.days.every(isDay)) return undefined;\n return { start, end, days: window.days === undefined ? undefined : new Set(window.days) };\n};\n\nconst within = (window: CheckedWindow, local: LocalTime): boolean => {\n const minute = local.hour * 60 + local.minute;\n const startsOn = (weekday: number): boolean => window.days === undefined || window.days.has(weekday);\n if (window.start < window.end) return window.start <= minute && minute < window.end && startsOn(local.weekday);\n // across midnight, or a full day: the part from the start, then the part before the end on the next day\n return (minute >= window.start && startsOn(local.weekday)) || (minute < window.end && startsOn((local.weekday + 6) % 7));\n};\n\n/**\n * This function tells if `instant` is in the window. It does not throw. A bad window or a bad\n * instant gives `false`, and `windowProblems` finds the bad window.\n */\nexport function inWindow(instant: number, window: TimeWindow): boolean {\n const valid = checked(window);\n const local = valid === undefined ? undefined : localTime(instant, window.zone);\n return valid !== undefined && local !== undefined && within(valid, local);\n}\n\n/**\n * This function gives the instants at which the wall clock of `zone` shows `wall`, in order. It is\n * the inverse of `localTime`. The wall-clock time is in milliseconds with the arithmetic of UTC: the\n * UTC fields of `new Date(wall)` are the fields of the wall clock.\n *\n * - A wall-clock time in the gap of a change forward gives no instant.\n * - A wall-clock time in the hour of a change back gives two instants.\n * - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.\n *\n * The rule for a gap or for two instants is a choice of the caller. For example, RFC 5545 takes the\n * first of two instants, and in a gap it takes the offset before the gap. The search reads the\n * offset one day before and one day after `wall`. Thus it finds each instant when the offset\n * changes at most one time in these 2 days.\n */\nexport function instantsOf(wall: number, zone: string): number[] | undefined {\n if (zoneFormat(zone) === undefined || !Number.isFinite(wall)) return undefined;\n // localTime gives whole seconds: the search uses the whole second, and adds the milliseconds at the end\n const whole = Math.floor(wall / 1000) * 1000;\n const offsets = new Set<number>();\n for (const probe of [whole - DAY_MS, whole, whole + DAY_MS]) {\n const local = localTime(probe, zone);\n if (local !== undefined) offsets.add(local.offset);\n }\n // no offset: the wall-clock time is out of the range of a date\n if (offsets.size === 0) return undefined;\n const found: number[] = [];\n for (const offset of offsets) {\n // an old offset of local mean time has seconds, thus the product is rounded to the millisecond\n const instant = whole - Math.round(offset * MINUTE_MS);\n const local = localTime(instant, zone);\n if (local === undefined) continue;\n const back = wallOf(local.year, local.month, local.day, local.hour, local.minute, local.second);\n if (back === whole && !found.includes(instant)) found.push(instant);\n }\n return found.sort((a, b) => a - b).map((instant) => instant + (wall - whole));\n}\n\n/**\n * This function gives the instants in `(from, to]` at which the offset of `zone` changes, in order\n * and exact to the millisecond. At each instant, the new offset starts. The search reads the offset\n * each 6 hours of the range, thus a long range costs more reads. No zone changes its offset two\n * times in 6 hours. A name that is not a time zone, or a bound that is not a date, gives `undefined`.\n */\nexport function offsetChanges(from: number, to: number, zone: string): number[] | undefined {\n if (localTime(from, zone) === undefined || localTime(to, zone) === undefined) return undefined;\n return changesOf(from, to, zone);\n}\n\n/** The instants in (from, to] at which the offset of the zone changes. `offsetChanges` tells the rule. */\nconst changesOf = (from: number, to: number, zone: string): number[] => {\n const offsetAt = (instant: number): number | undefined => localTime(instant, zone)?.offset;\n const found: number[] = [];\n let low = from;\n let before = offsetAt(low);\n while (low < to) {\n const high = Math.min(low + DAY_MS / 4, to);\n const after = offsetAt(high);\n if (before !== undefined && after !== undefined && after !== before) {\n // the first instant with the new offset\n let left = low;\n let right = high;\n while (right - left > 1) {\n const middle = Math.floor((left + right) / 2);\n if (offsetAt(middle) === before) left = middle;\n else right = middle;\n }\n found.push(right);\n }\n low = high;\n before = after;\n }\n return found;\n};\n\n/**\n * This function gives the first instant at which the wall clock of `zone` shows `wall` or a later\n * wall-clock time. The wall-clock time is in milliseconds with the arithmetic of UTC, as for\n * `instantsOf`. Thus a time on the wall, for example the start of a day, always has an instant.\n *\n * - A wall-clock time that occurs one time gives its instant.\n * - A wall-clock time in the hour of a change back gives the first of its two instants.\n * - A wall-clock time in the gap of a change forward gives the instant of the change: the end of the gap.\n * - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.\n *\n * The search reads the offset one day before and one day after `wall`, as `instantsOf` does. A gap\n * that is not in these 2 days also gives `undefined`.\n */\nexport function firstInstantOf(wall: number, zone: string): number | undefined {\n const instants = instantsOf(wall, zone);\n if (instants === undefined) return undefined;\n if (instants.length > 0) return instants[0];\n const whole = Math.floor(wall / 1000) * 1000;\n const offsets: number[] = [];\n for (const probe of [whole - DAY_MS, whole, whole + DAY_MS]) {\n const local = localTime(probe, zone);\n if (local !== undefined) offsets.push(Math.round(local.offset * MINUTE_MS));\n }\n // A change at the instant c, from the offset o1 to the larger offset o2, skips the wall-clock times\n // from c + o1 to c + o2, without the end. Thus c is after wall - o2, and at or before wall - o1.\n const from = wall - Math.max(...offsets);\n const to = wall - Math.min(...offsets);\n const offsetAt = (instant: number): number | undefined => {\n const local = localTime(instant, zone);\n return local === undefined ? undefined : Math.round(local.offset * MINUTE_MS);\n };\n for (const change of changesOf(from, to, zone)) {\n const before = offsetAt(change - 1);\n const after = offsetAt(change);\n if (before !== undefined && after !== undefined && change + before <= wall && wall < change + after) return change;\n }\n return undefined;\n}\n\n/**\n * This function gives the next change of the window after `instant`: the first later instant at\n * which the window opens or closes. A timer can wait for it in closed form. It gives `undefined`\n * for a bad window, and when the window does not change in the 9 days after `instant`. A window\n * with no day and a full-day window on each day do not change.\n */\nexport function nextBoundary(instant: number, window: TimeWindow): WindowBoundary | undefined {\n const valid = checked(window);\n const local = valid === undefined ? undefined : localTime(instant, window.zone);\n if (valid === undefined || local === undefined) return undefined;\n const inside = within(valid, local);\n const horizon = instant + 9 * DAY_MS;\n // The state changes only where the wall clock shows the start or the end, or where the offset changes.\n const candidates = changesOf(instant, horizon, window.zone);\n const midnight = wallOf(local.year, local.month, local.day, 0, 0, 0);\n for (let day = -1; day <= 10; day += 1) {\n for (const minutes of [valid.start, valid.end]) candidates.push(...(instantsOf(midnight + day * DAY_MS + minutes * MINUTE_MS, window.zone) ?? []));\n }\n candidates.sort((a, b) => a - b);\n for (const at of candidates) {\n if (at <= instant || at > horizon) continue;\n const there = localTime(at, window.zone);\n if (there !== undefined && within(valid, there) !== inside) return { at, opens: !inside };\n }\n return undefined;\n}\n\n/** This function gives the problems of a window. With a problem, `inWindow` gives `false` and `nextBoundary` gives `undefined`. */\nexport function windowProblems(window: TimeWindow): string[] {\n const problems: string[] = [];\n if (zoneFormat(window.zone) === undefined) problems.push(`\"${window.zone}\" is not the IANA name of a time zone.`);\n if (minutesOf(window.start) === undefined) problems.push(`The start \"${window.start}\" is not a local time HH:MM from 00:00 to 23:59.`);\n if (minutesOf(window.end) === undefined) problems.push(`The end \"${window.end}\" is not a local time HH:MM from 00:00 to 23:59.`);\n for (const day of window.days ?? []) {\n if (!isDay(day)) problems.push(`The day ${day} is not a day of the week from 0 (Sunday) to 6.`);\n }\n if (window.days !== undefined && window.days.length === 0) problems.push(\"The window has no day, thus it does not open.\");\n return problems;\n}\n"],"mappings":";;AAgEA,MAAM,kBAA0B;CAC9B,MAAM,cAAe,WAAmD;CACxE,OAAO,gBAAgB,KAAA,IAAY,KAAK,IAAI,IAAI,YAAY,IAAI;AAClE;;AAGA,MAAM,gBAAgB;;AAGtB,MAAM,WAAW,YAA0C;CACzD,MAAM,QAAQ,QAAQ,SAAS;CAC/B,IAAI,OAAO,UAAU,YAAY,CAAC,OAAO,SAAS,KAAK,KAAK,SAAS,GAAG,MAAM,IAAI,WAAW,aAAa,OAAO,KAAK,EAAE,iCAAiC;CACzJ,MAAM,QAAQ,QAAQ;CACtB,IAAI,UAAU,KAAA,MAAc,OAAO,UAAU,YAAY,CAAC,OAAO,SAAS,KAAK,IAAI,MAAM,IAAI,WAAW,aAAa,OAAO,KAAK,EAAE,yBAAyB;CAC5J,OAAO;AACT;;;;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CAAC;CAC/B,KAAK,MAAM,MAAM,YAAY;EAC3B,IAAI,MAAM,WAAW,KAAK,SAAS;EACnC,MAAM,QAAQ,UAAU,IAAI,OAAO,IAAI;EACvC,IAAI,UAAU,KAAA,KAAa,OAAO,OAAO,KAAK,MAAM,QAAQ,OAAO;GAAE;GAAI,OAAO,CAAC;EAAO;CAC1F;AAEF;;AAGA,SAAgB,eAAe,QAA8B;CAC3D,MAAM,WAAqB,CAAC;CAC5B,IAAI,WAAW,OAAO,IAAI,MAAM,KAAA,GAAW,SAAS,KAAK,IAAI,OAAO,KAAK,uCAAuC;CAChH,IAAI,UAAU,OAAO,KAAK,MAAM,KAAA,GAAW,SAAS,KAAK,cAAc,OAAO,MAAM,iDAAiD;CACrI,IAAI,UAAU,OAAO,GAAG,MAAM,KAAA,GAAW,SAAS,KAAK,YAAY,OAAO,IAAI,iDAAiD;CAC/H,KAAK,MAAM,OAAO,OAAO,QAAQ,CAAC,GAChC,IAAI,CAAC,MAAM,GAAG,GAAG,SAAS,KAAK,WAAW,IAAI,gDAAgD;CAEhG,IAAI,OAAO,SAAS,KAAA,KAAa,OAAO,KAAK,WAAW,GAAG,SAAS,KAAK,+CAA+C;CACxH,OAAO;AACT"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { a as Realization, c as Unit, d as machineProblems, f as readsFired, i as Over, l as Vantage, n as FIRED, o as Timer, r as Machine, s as TransitionDef, t as Expr, u as exprKnobs } from "./machine-yGpVFyfh.js";
|
|
2
2
|
import { i as replay, n as Replayable, r as Transition, t as ReplayOptions } from "./replay-DRTZc7Pw.js";
|
|
3
|
-
import { A as timerBand, C as parseExpr, D as recovery, E as realizationOf, M as timerUnder, N as transitionOn, O as stackedDetection, S as overValues, T as realizationKnobs, _ as detection, a as Detection, b as firings, c as KnobValues, d as Recovery, f as ScheduleOptions, g as algebraProblems, h as TimerBounds, i as Band, j as timerOf, k as synGiveUpSends, l as Manifestation, m as Situation, n as RunMachineOptions, o as Detector, p as Scheduled, r as runMachine, s as Firing, t as MachineRun, u as Node, v as errnoOf, w as probeTrain, x as modelSource, y as evaluate } from "./run-
|
|
3
|
+
import { A as timerBand, C as parseExpr, D as recovery, E as realizationOf, M as timerUnder, N as transitionOn, O as stackedDetection, S as overValues, T as realizationKnobs, _ as detection, a as Detection, b as firings, c as KnobValues, d as Recovery, f as ScheduleOptions, g as algebraProblems, h as TimerBounds, i as Band, j as timerOf, k as synGiveUpSends, l as Manifestation, m as Situation, n as RunMachineOptions, o as Detector, p as Scheduled, r as runMachine, s as Firing, t as MachineRun, u as Node, v as errnoOf, w as probeTrain, x as modelSource, y as evaluate } from "./run-BgUWHAcf.js";
|
|
4
4
|
/** This function gives one value or many values as an array. */
|
|
5
5
|
export declare const arr: <T>(value: T | readonly T[]) => readonly T[];
|
|
6
6
|
/** This function gives the states that a transition leaves. The state "*" is each state that is not final. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { r as Machine, s as TransitionDef, t as Expr } from "./machine-yGpVFyfh.js";
|
|
2
|
-
import { n as Clock } from "./clock-
|
|
2
|
+
import { n as Clock } from "./clock-oJqrb5Q4.js";
|
|
3
3
|
import { r as Transition } from "./replay-DRTZc7Pw.js";
|
|
4
4
|
import { Extent, Quantity, Rational, Unknown } from "@mark1russell7/quantity";
|
|
5
5
|
/**
|
package/build/run.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as RunMachineOptions, r as runMachine, t as MachineRun } from "./run-
|
|
1
|
+
import { n as RunMachineOptions, r as runMachine, t as MachineRun } from "./run-BgUWHAcf.js";
|
|
2
2
|
export { MachineRun, RunMachineOptions, runMachine };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/algebra",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.9.0",
|
|
5
5
|
"description": "State machines as data, and the timer algebra over them: closed forms for a machine's timers in exact rationals, detection as the first live timer, recovery after a stall, and the replay of a trace.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"state-machine",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"node": ">=22.19"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@mark1russell7/quantity": "^0.4.
|
|
63
|
+
"@mark1russell7/quantity": "^0.4.1"
|
|
64
64
|
},
|
|
65
65
|
"peerDependencies": {
|
|
66
66
|
"zod": "^4.1.0"
|