@northtalk/ts 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/debug.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { a as DebugSnapshot, dt as isReadable, i as DebugPause, n as DebugFrame, o as DebugSource, r as DebugInstruction, s as RepeatedEffect, t as DebugController, yt as sourceForm } from "./debug-D8Z_u244.js";
2
+ export { type DebugController, type DebugFrame, type DebugInstruction, type DebugPause, type DebugSnapshot, type DebugSource, type RepeatedEffect, isReadable, sourceForm };
package/debug.js ADDED
@@ -0,0 +1 @@
1
+ export { w as isReadable, B as sourceForm } from "./errors-DHwz1Vho.js";
package/driver.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import { b as PumpOptions, m as Group, x as PumpResult } from "./debug-D8Z_u244.js";
2
+ //#region src/driver.d.ts
3
+ export type AutoDriveOptions = {
4
+ /** Epoch microseconds. The default reads `Date.now()`; readings never go backwards. */
5
+ clock?: () => bigint;
6
+ /** A Pump that threw, such as a `clock backwards` HostError. The driver keeps going. */
7
+ onError?: (error: unknown) => void;
8
+ /** Each Pump's result, as soon as it returns. It may make worker calls, such as Reload after a `rewound` Pump. */
9
+ onPump?: (result: PumpResult) => void;
10
+ /** Passed to every Pump. A Fuel Slice yields the event loop between Pumps. */
11
+ pump?: PumpOptions;
12
+ /**
13
+ * Calls `fire` once the clock reaches `at`, and returns a cancel function.
14
+ * The default uses `setTimeout`; a Host with its own clock supplies its own.
15
+ */
16
+ setDeadline?: (at: bigint, fire: () => void) => () => void;
17
+ };
18
+ export type Drive = {
19
+ /** Resolves once no Pump is scheduled or running. A Group waiting only on a deadline counts as settled. */
20
+ idle(): Promise<void>;
21
+ /** The Group's onReady: schedules a Pump, once, on a later macrotask. */
22
+ ready(): void;
23
+ /** Cancels the scheduled Pump and the deadline timer; later readiness is ignored. */
24
+ stop(): void;
25
+ };
26
+ export declare const autoDrive: (group: Group, o?: AutoDriveOptions) => Drive;
27
+ //#endregion
package/driver.js ADDED
@@ -0,0 +1,74 @@
1
+ //#region src/driver.ts
2
+ const MAX_DELAY_MS = 2 ** 31 - 1;
3
+ const timeoutDeadline = (read) => (at, fire) => {
4
+ let handle;
5
+ const arm = () => {
6
+ const ms = Number((at - read() + 999n) / 1000n);
7
+ handle = setTimeout(ms > MAX_DELAY_MS ? arm : fire, Math.min(Math.max(ms, 0), MAX_DELAY_MS));
8
+ };
9
+ arm();
10
+ return () => clearTimeout(handle);
11
+ };
12
+ const autoDrive = (group, o = {}) => {
13
+ const read = o.clock ?? (() => BigInt(Date.now()) * 1000n);
14
+ const setDeadline = o.setDeadline ?? timeoutDeadline(read);
15
+ let last = 0n;
16
+ let scheduled;
17
+ let cancelDeadline;
18
+ let stopped = false;
19
+ let waiters = [];
20
+ const settle = () => {
21
+ if (scheduled === void 0) {
22
+ const done = waiters;
23
+ waiters = [];
24
+ for (const resolve of done) resolve();
25
+ }
26
+ };
27
+ const clearDeadline = () => {
28
+ cancelDeadline?.();
29
+ cancelDeadline = void 0;
30
+ };
31
+ const pump = () => {
32
+ scheduled = void 0;
33
+ if (stopped) {
34
+ settle();
35
+ return;
36
+ }
37
+ clearDeadline();
38
+ const reading = read();
39
+ const now = reading > last ? reading : last;
40
+ last = now;
41
+ let again = false;
42
+ try {
43
+ const result = group.pump(now, o.pump);
44
+ o.onPump?.(result);
45
+ again = result.state === "sliced" || result.state === "rewound";
46
+ if (!again && result.nextDeadline !== void 0 && !stopped) cancelDeadline = setDeadline(result.nextDeadline, () => {
47
+ cancelDeadline = void 0;
48
+ ready();
49
+ });
50
+ } catch (error) {
51
+ o.onError?.(error);
52
+ }
53
+ if (again) ready();
54
+ settle();
55
+ };
56
+ const ready = () => {
57
+ if (!stopped && scheduled === void 0) scheduled = setTimeout(pump, 0);
58
+ };
59
+ return {
60
+ ready,
61
+ idle: () => scheduled === void 0 ? Promise.resolve() : new Promise((resolve) => waiters.push(resolve)),
62
+ stop: () => {
63
+ stopped = true;
64
+ if (scheduled !== void 0) {
65
+ clearTimeout(scheduled);
66
+ scheduled = void 0;
67
+ }
68
+ clearDeadline();
69
+ settle();
70
+ }
71
+ };
72
+ };
73
+ //#endregion
74
+ export { autoDrive };