@demlik/tea 0.1.0 → 0.1.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 +21 -0
- package/dist/agent/index.d.ts +907 -0
- package/dist/agent/index.js +20 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/authed-call/index.d.ts +119 -0
- package/dist/authed-call/index.js +148 -0
- package/dist/authed-call/index.js.map +1 -0
- package/dist/await-terminal/index.d.ts +99 -0
- package/dist/await-terminal/index.js +75 -0
- package/dist/await-terminal/index.js.map +1 -0
- package/dist/batch-window/index.d.ts +259 -0
- package/dist/batch-window/index.js +43 -0
- package/dist/batch-window/index.js.map +1 -0
- package/dist/cache/index.d.ts +2 -2
- package/dist/cache/index.js +2 -50
- package/dist/cache/index.js.map +1 -1
- package/dist/chunk-27N2SPTN.js +222 -0
- package/dist/chunk-27N2SPTN.js.map +1 -0
- package/dist/chunk-2YOE5PIS.js +45 -0
- package/dist/chunk-2YOE5PIS.js.map +1 -0
- package/dist/chunk-3545BOYL.js +65 -0
- package/dist/chunk-3545BOYL.js.map +1 -0
- package/dist/chunk-3XJWC7VW.js +61 -0
- package/dist/chunk-3XJWC7VW.js.map +1 -0
- package/dist/chunk-5DEQ5I3U.js +132 -0
- package/dist/chunk-5DEQ5I3U.js.map +1 -0
- package/dist/chunk-5VNWZADP.js +49 -0
- package/dist/chunk-5VNWZADP.js.map +1 -0
- package/dist/chunk-6RGBKODV.js +268 -0
- package/dist/chunk-6RGBKODV.js.map +1 -0
- package/dist/{chunk-ZRUST6BK.js → chunk-7FYQGDMF.js} +2 -2
- package/dist/chunk-7FYQGDMF.js.map +1 -0
- package/dist/chunk-7W4LMPUW.js +186 -0
- package/dist/chunk-7W4LMPUW.js.map +1 -0
- package/dist/chunk-BYQUGDD3.js +16 -0
- package/dist/chunk-BYQUGDD3.js.map +1 -0
- package/dist/{chunk-PYBQREFU.js → chunk-DILMKOZ7.js} +22 -26
- package/dist/chunk-DILMKOZ7.js.map +1 -0
- package/dist/chunk-EHIX7NYZ.js +93 -0
- package/dist/chunk-EHIX7NYZ.js.map +1 -0
- package/dist/chunk-HCEVHFJ7.js +54 -0
- package/dist/chunk-HCEVHFJ7.js.map +1 -0
- package/dist/chunk-HJO6VNQI.js +15 -0
- package/dist/chunk-HJO6VNQI.js.map +1 -0
- package/dist/chunk-IN33Q4VJ.js +52 -0
- package/dist/chunk-IN33Q4VJ.js.map +1 -0
- package/dist/chunk-J6527VRQ.js +56 -0
- package/dist/chunk-J6527VRQ.js.map +1 -0
- package/dist/chunk-JELSZN25.js +47 -0
- package/dist/chunk-JELSZN25.js.map +1 -0
- package/dist/chunk-K575PT4S.js +58 -0
- package/dist/chunk-K575PT4S.js.map +1 -0
- package/dist/chunk-K7Q23EQ4.js +20 -0
- package/dist/chunk-K7Q23EQ4.js.map +1 -0
- package/dist/chunk-LEC4U6CX.js +91 -0
- package/dist/chunk-LEC4U6CX.js.map +1 -0
- package/dist/{chunk-PYOCUBQJ.js → chunk-LP7VQBRG.js} +22 -15
- package/dist/chunk-LP7VQBRG.js.map +1 -0
- package/dist/{chunk-CMG2KMWX.js → chunk-MMH7CW5K.js} +220 -189
- package/dist/chunk-MMH7CW5K.js.map +1 -0
- package/dist/{chunk-Y5JM3UUH.js → chunk-NSDFA6M6.js} +75 -3
- package/dist/chunk-NSDFA6M6.js.map +1 -0
- package/dist/chunk-ON24RKSE.js +60 -0
- package/dist/chunk-ON24RKSE.js.map +1 -0
- package/dist/chunk-PS23H6KQ.js +87 -0
- package/dist/chunk-PS23H6KQ.js.map +1 -0
- package/dist/chunk-QLA76GIT.js +162 -0
- package/dist/chunk-QLA76GIT.js.map +1 -0
- package/dist/chunk-S3FQN4SL.js +506 -0
- package/dist/chunk-S3FQN4SL.js.map +1 -0
- package/dist/chunk-SC3FAFZN.js +53 -0
- package/dist/chunk-SC3FAFZN.js.map +1 -0
- package/dist/chunk-SINXE47Y.js +55 -0
- package/dist/chunk-SINXE47Y.js.map +1 -0
- package/dist/chunk-SQTCS44W.js +33 -0
- package/dist/chunk-SQTCS44W.js.map +1 -0
- package/dist/chunk-TGG26XMK.js +52 -0
- package/dist/chunk-TGG26XMK.js.map +1 -0
- package/dist/chunk-UV6UWGEO.js +147 -0
- package/dist/chunk-UV6UWGEO.js.map +1 -0
- package/dist/chunk-UYWUVEP6.js +158 -0
- package/dist/chunk-UYWUVEP6.js.map +1 -0
- package/dist/chunk-VLGARN6H.js +202 -0
- package/dist/chunk-VLGARN6H.js.map +1 -0
- package/dist/circuit-breaker/index.js +1 -51
- package/dist/circuit-breaker/index.js.map +1 -1
- package/dist/core-BoB6t7dB.d.ts +188 -0
- package/dist/deadline/index.d.ts +24 -5
- package/dist/deadline/index.js +4 -18
- package/dist/deadline/index.js.map +1 -1
- package/dist/debounce/index.d.ts +6 -0
- package/dist/debounce/index.js +1 -54
- package/dist/debounce/index.js.map +1 -1
- package/dist/devtools/index.d.ts +1 -1
- package/dist/devtools/index.js +18 -6
- package/dist/devtools/index.js.map +1 -1
- package/dist/do/index.d.ts +1723 -92
- package/dist/do/index.js +673 -31
- package/dist/do/index.js.map +1 -1
- package/dist/durable-effects-CO9Uh971.d.ts +180 -0
- package/dist/extension/index.d.ts +31 -13
- package/dist/extension/index.js +5 -3
- package/dist/extension/index.js.map +1 -1
- package/dist/extension/react.js +8 -3
- package/dist/extension/react.js.map +1 -1
- package/dist/extension/subs/index.d.ts +2 -2
- package/dist/extension/subs/index.js.map +1 -1
- package/dist/extension/test-utils.js +11 -3
- package/dist/extension/test-utils.js.map +1 -1
- package/dist/fan-out/index.d.ts +239 -0
- package/dist/fan-out/index.js +5 -0
- package/dist/fan-out/index.js.map +1 -0
- package/dist/idempotency/adapter.d.ts +62 -0
- package/dist/idempotency/adapter.js +4 -0
- package/dist/idempotency/adapter.js.map +1 -0
- package/dist/idempotency/index.js +1 -56
- package/dist/idempotency/index.js.map +1 -1
- package/dist/idempotent-intake/index.d.ts +186 -0
- package/dist/idempotent-intake/index.js +7 -0
- package/dist/idempotent-intake/index.js.map +1 -0
- package/dist/index.d.ts +341 -191
- package/dist/index.js +2 -1
- package/dist/llm-call/index.d.ts +309 -0
- package/dist/llm-call/index.js +14 -0
- package/dist/llm-call/index.js.map +1 -0
- package/dist/machine-viz/index.d.ts +4 -4
- package/dist/machine-viz/index.js +5 -9
- package/dist/machine-viz/index.js.map +1 -1
- package/dist/mem/index.d.ts +1 -0
- package/dist/monitored-run/index.d.ts +253 -0
- package/dist/monitored-run/index.js +10 -0
- package/dist/monitored-run/index.js.map +1 -0
- package/dist/node/index.d.ts +5 -5
- package/dist/node/index.js.map +1 -1
- package/dist/paginated-walk/index.d.ts +234 -0
- package/dist/paginated-walk/index.js +15 -0
- package/dist/paginated-walk/index.js.map +1 -0
- package/dist/paginator/index.d.ts +271 -0
- package/dist/paginator/index.js +3 -0
- package/dist/paginator/index.js.map +1 -0
- package/dist/parity/index.d.ts +92 -0
- package/dist/parity/index.js +78 -0
- package/dist/parity/index.js.map +1 -0
- package/dist/pbt/arbitraries/index.d.ts +1 -1
- package/dist/pbt/arbitraries/index.js +1 -1
- package/dist/pbt/index.d.ts +15 -8
- package/dist/pbt/index.js +6 -11
- package/dist/pbt/index.js.map +1 -1
- package/dist/pbt/runners/index.d.ts +1 -1
- package/dist/pbt/runners/index.js +3 -2
- package/dist/poller/index.d.ts +288 -0
- package/dist/poller/index.js +79 -0
- package/dist/poller/index.js.map +1 -0
- package/dist/prediction/index.d.ts +130 -0
- package/dist/prediction/index.js +4 -0
- package/dist/prediction/index.js.map +1 -0
- package/dist/protocol-B5YmJr0d.d.ts +45 -0
- package/dist/pure/index.d.ts +2 -0
- package/dist/pure/index.js +4 -0
- package/dist/pure/index.js.map +1 -0
- package/dist/raft/demo.d.ts +105 -0
- package/dist/raft/demo.js +172 -0
- package/dist/raft/demo.js.map +1 -0
- package/dist/raft/index.d.ts +548 -0
- package/dist/raft/index.js +7 -0
- package/dist/raft/index.js.map +1 -0
- package/dist/raft/sim.d.ts +222 -0
- package/dist/raft/sim.js +8 -0
- package/dist/raft/sim.js.map +1 -0
- package/dist/raft/timeline.d.ts +46 -0
- package/dist/raft/timeline.js +62 -0
- package/dist/raft/timeline.js.map +1 -0
- package/dist/rate-limit/index.js +1 -43
- package/dist/rate-limit/index.js.map +1 -1
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +43 -20
- package/dist/react/index.js.map +1 -1
- package/dist/reconciler/index.d.ts +192 -0
- package/dist/reconciler/index.js +166 -0
- package/dist/reconciler/index.js.map +1 -0
- package/dist/recorder/index.d.ts +7 -3
- package/dist/recorder/index.js +1 -155
- package/dist/recorder/index.js.map +1 -1
- package/dist/resilient-call/index.d.ts +294 -0
- package/dist/resilient-call/index.js +13 -0
- package/dist/resilient-call/index.js.map +1 -0
- package/dist/retry-backoff/index.d.ts +42 -5
- package/dist/retry-backoff/index.js +1 -35
- package/dist/retry-backoff/index.js.map +1 -1
- package/dist/retry-to-success/index.d.ts +109 -0
- package/dist/retry-to-success/index.js +35 -0
- package/dist/retry-to-success/index.js.map +1 -0
- package/dist/saga/index.d.ts +238 -0
- package/dist/saga/index.js +150 -0
- package/dist/saga/index.js.map +1 -0
- package/dist/snapshot/index.d.ts +334 -0
- package/dist/snapshot/index.js +5 -0
- package/dist/snapshot/index.js.map +1 -0
- package/dist/subs/index.d.ts +48 -10
- package/dist/subs/index.js +1 -1
- package/dist/testing/index.d.ts +107 -2
- package/dist/testing/index.js +204 -6
- package/dist/testing/index.js.map +1 -1
- package/dist/throttle/index.d.ts +9 -0
- package/dist/throttle/index.js +1 -52
- package/dist/throttle/index.js.map +1 -1
- package/dist/throttled-input/index.d.ts +380 -0
- package/dist/throttled-input/index.js +88 -0
- package/dist/throttled-input/index.js.map +1 -0
- package/dist/token-refresh/index.d.ts +215 -0
- package/dist/token-refresh/index.js +6 -0
- package/dist/token-refresh/index.js.map +1 -0
- package/dist/trace-replay/index.d.ts +11 -2
- package/dist/trace-replay/index.js +3 -55
- package/dist/trace-replay/index.js.map +1 -1
- package/dist/{types-CwBC5wMn.d.ts → types-B-8bcAp8.d.ts} +1 -1
- package/dist/with-deadline/index.d.ts +196 -0
- package/dist/with-deadline/index.js +111 -0
- package/dist/with-deadline/index.js.map +1 -0
- package/dist/with-resilience/index.d.ts +205 -0
- package/dist/with-resilience/index.js +251 -0
- package/dist/with-resilience/index.js.map +1 -0
- package/dist/with-telemetry/index.d.ts +149 -0
- package/dist/with-telemetry/index.js +70 -0
- package/dist/with-telemetry/index.js.map +1 -0
- package/dist/work-queue/adapter.d.ts +82 -0
- package/dist/work-queue/adapter.js +4 -0
- package/dist/work-queue/adapter.js.map +1 -0
- package/dist/work-queue/index.d.ts +25 -11
- package/dist/work-queue/index.js +1 -46
- package/dist/work-queue/index.js.map +1 -1
- package/dist/work-queue/ops.d.ts +69 -0
- package/dist/work-queue/ops.js +3 -0
- package/dist/work-queue/ops.js.map +1 -0
- package/dist/workflow/index.d.ts +450 -0
- package/dist/workflow/index.js +225 -0
- package/dist/workflow/index.js.map +1 -0
- package/package.json +168 -25
- package/dist/chunk-CMG2KMWX.js.map +0 -1
- package/dist/chunk-PYBQREFU.js.map +0 -1
- package/dist/chunk-PYOCUBQJ.js.map +0 -1
- package/dist/chunk-Y5JM3UUH.js.map +0 -1
- package/dist/chunk-ZRUST6BK.js.map +0 -1
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { C as Cmd, I as Interpret } from '../core-BoB6t7dB.js';
|
|
2
|
+
import { M as MsgType } from '../protocol-B5YmJr0d.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @demlik/tea/token-refresh — a credential's lifecycle as pure state + verbs,
|
|
6
|
+
* with the actual token fetch deferred to a single injected port.
|
|
7
|
+
*
|
|
8
|
+
* This is the L1 brick `authed-call` (#10 in the composition specs) stacks on:
|
|
9
|
+
* a token slice `{ token, stale }` plus pure transitions that decide *when* a
|
|
10
|
+
* credential must be re-minted, and one `handlers(ports)` splice that performs
|
|
11
|
+
* the re-mint. It follows the uniform knob contract every composition wears —
|
|
12
|
+
* `createTokenRefresh(config) → { init, <verbs>, handlers }` — so a consumer
|
|
13
|
+
* spreads ~3 hooks into their machine instead of hand-wiring the refresh dance.
|
|
14
|
+
*
|
|
15
|
+
* It is a LEAF module in the same mold as `@demlik/tea/retry-backoff`: it
|
|
16
|
+
* depends only on the core (`../index`), never on a sibling brick. The two
|
|
17
|
+
* non-negotiables of every knob hold here by construction:
|
|
18
|
+
*
|
|
19
|
+
* - **Durable** — the token slice is plain JSON-serializable data, so it
|
|
20
|
+
* survives a Durable Object eviction / page reload. A rehydrated machine
|
|
21
|
+
* resumes knowing exactly which token it holds and whether it is stale.
|
|
22
|
+
* - **Replayable** — every transition is a pure verb returning
|
|
23
|
+
* `readonly [State, Cmd[]]`. No verb reads the wall clock or performs I/O,
|
|
24
|
+
* so `replay` reconstructs the slice exactly. Time enters ONLY as an `at`
|
|
25
|
+
* parameter the caller stamps; the network fetch enters ONLY at the
|
|
26
|
+
* `handlers` boundary. This keeps the substrate's purity invariants intact
|
|
27
|
+
* (invariant 2: pure transitions; invariant 4: I/O is an effect, not a
|
|
28
|
+
* reducer body).
|
|
29
|
+
*
|
|
30
|
+
* ## The staleness model — two independent reasons to refresh
|
|
31
|
+
*
|
|
32
|
+
* A token becomes unusable for two unrelated reasons, and the slice tracks both
|
|
33
|
+
* without conflating them:
|
|
34
|
+
*
|
|
35
|
+
* 1. **Expiry** — the token has a known `expiresAt` (epoch ms). It is "near
|
|
36
|
+
* expiry" once `at >= expiresAt - skewMs`. The `skewMs` skew refreshes
|
|
37
|
+
* *ahead* of the hard deadline so an in-flight call never races a token
|
|
38
|
+
* that expires mid-request. This is the proactive, time-driven reason and
|
|
39
|
+
* is computed purely from the injected `at` — no clock in the verb.
|
|
40
|
+
*
|
|
41
|
+
* 2. **Server rejection (401)** — the server rejected a request the client
|
|
42
|
+
* believed was still valid (clock skew, a server-side revocation, an early
|
|
43
|
+
* rotation). `on401` flips a `stale` flag regardless of `expiresAt`. This
|
|
44
|
+
* is the reactive reason: the client's belief about validity was wrong, so
|
|
45
|
+
* the next `needsRefresh` returns true even though the clock says the
|
|
46
|
+
* token is fine.
|
|
47
|
+
*
|
|
48
|
+
* `needsRefresh` is the union of the two (plus "no token at all"). One source
|
|
49
|
+
* of truth for "should I fetch a fresh credential before the next call?", read
|
|
50
|
+
* by the consumer's reducer at the call boundary.
|
|
51
|
+
*
|
|
52
|
+
* Typical wiring (inside a consumer machine):
|
|
53
|
+
*
|
|
54
|
+
* const tr = createTokenRefresh({ skewMs: 30_000 });
|
|
55
|
+
*
|
|
56
|
+
* // in init:
|
|
57
|
+
* init: (loaded) => [loaded ?? { auth: tr.init() }, Cmd.none],
|
|
58
|
+
*
|
|
59
|
+
* // in a reducer cell that is about to make an authed call:
|
|
60
|
+
* before_call: (s, msg) => {
|
|
61
|
+
* const [auth, cmds] = tr.ensureFresh(s.auth, msg.at);
|
|
62
|
+
* return [{ ...s, auth }, cmds]; // emits a refresh Cmd iff a refresh is due
|
|
63
|
+
* },
|
|
64
|
+
*
|
|
65
|
+
* // on a 401 from a guarded call:
|
|
66
|
+
* got_401: (s) => [{ ...s, auth: tr.on401(s.auth) }, []],
|
|
67
|
+
*
|
|
68
|
+
* // when the refresh port resolves:
|
|
69
|
+
* token_refreshed: (s, msg) => [{ ...s, auth: tr.refreshed(s.auth, msg.token) }, []],
|
|
70
|
+
*
|
|
71
|
+
* // splice the I/O:
|
|
72
|
+
* interpret: { ...tr.handlers({ refresh: () => mySdk.mintToken() }) },
|
|
73
|
+
*
|
|
74
|
+
* NOT a substrate primitive: from `../index` it reuses only the `Cmd` /
|
|
75
|
+
* `Interpret` types and the `tryInterpret` Railway helper (a runtime function,
|
|
76
|
+
* not a type) that every effectful brick uses — and no sibling brick. It is a
|
|
77
|
+
* leaf utility consumers reach via the `@demlik/tea/token-refresh` subpath.
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A minted credential: the opaque `value` to send on the wire, and the absolute
|
|
82
|
+
* `expiresAt` (epoch milliseconds — the `Date.now()` scale) the issuer stamped
|
|
83
|
+
* it with. Both fields are plain data so a `Token` is trivially serializable
|
|
84
|
+
* into a consumer's `Store<S>`.
|
|
85
|
+
*
|
|
86
|
+
* `value` is `string` because that is the universal shape of a bearer token /
|
|
87
|
+
* API key / session cookie on the wire. A consumer whose refresh port yields a
|
|
88
|
+
* richer object can JSON-encode it into `value`, or keep this module's slice as
|
|
89
|
+
* the "is it fresh?" gate and hold the richer object elsewhere in their Model.
|
|
90
|
+
*/
|
|
91
|
+
interface Token {
|
|
92
|
+
/** The opaque credential to present on each request (bearer token, API key). */
|
|
93
|
+
readonly value: string;
|
|
94
|
+
/** Absolute expiry instant, epoch milliseconds. The `Date.now()` scale. */
|
|
95
|
+
readonly expiresAt: number;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Pure configuration — the knob's only field. Carries no mutable state;
|
|
99
|
+
* `TokenState` does. One config is shared across all the refreshes of one
|
|
100
|
+
* logical credential (often a module-scope const).
|
|
101
|
+
*/
|
|
102
|
+
interface TokenRefreshConfig {
|
|
103
|
+
/**
|
|
104
|
+
* Refresh-ahead skew, in milliseconds. A token is treated as needing a
|
|
105
|
+
* refresh once `at >= expiresAt - skewMs`, i.e. `skewMs` BEFORE its hard
|
|
106
|
+
* expiry. This buys headroom so a call that starts just under the wire never
|
|
107
|
+
* races a token that lapses mid-flight, and absorbs small client/server clock
|
|
108
|
+
* disagreement. `0` means "refresh exactly at expiry"; a negative value is
|
|
109
|
+
* clamped to `0` (a skew that pushes the trigger past expiry is meaningless).
|
|
110
|
+
*
|
|
111
|
+
* @default 0
|
|
112
|
+
*/
|
|
113
|
+
readonly skewMs?: number;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The token slice this knob owns — the Model field a consumer spreads in via
|
|
117
|
+
* `init()`. A two-field record rather than a phase union because the two
|
|
118
|
+
* dimensions are orthogonal: a token can be present-and-fresh, present-but-stale
|
|
119
|
+
* (a 401 came back), absent, or absent-and-stale (cleared after a failed
|
|
120
|
+
* refresh). Every combination is meaningful, so neither field implies the
|
|
121
|
+
* other's value and a discriminated union would only manufacture impossible
|
|
122
|
+
* arms.
|
|
123
|
+
*
|
|
124
|
+
* - `token` — the currently held credential, or `null` when none has been
|
|
125
|
+
* minted yet (fresh boot) or the last refresh failed.
|
|
126
|
+
* - `stale` — set by `on401` when the server rejected a request the client
|
|
127
|
+
* believed valid. Forces the next `needsRefresh` to true regardless of
|
|
128
|
+
* `expiresAt`; cleared by `refreshed` once a new credential lands.
|
|
129
|
+
*
|
|
130
|
+
* Both fields `readonly` so the whole slice threads safely through a pure
|
|
131
|
+
* reducer and serializes into a `Store<S>`.
|
|
132
|
+
*/
|
|
133
|
+
interface TokenState {
|
|
134
|
+
/** The held credential, or `null` if none is available (boot / failed refresh). */
|
|
135
|
+
readonly token: Token | null;
|
|
136
|
+
/** Whether a 401 has marked the held token unusable ahead of its `expiresAt`. */
|
|
137
|
+
readonly stale: boolean;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The starting slice: no token held, not stale. The first `needsRefresh` after
|
|
141
|
+
* this returns `true` (no token), so a consumer's first guarded call mints a
|
|
142
|
+
* credential before going out.
|
|
143
|
+
*/
|
|
144
|
+
declare function initTokenRefresh(): TokenState;
|
|
145
|
+
/**
|
|
146
|
+
* The Cmd this knob emits to ask the runtime to mint a fresh token. Plain data
|
|
147
|
+
* (no closure — invariant 1) carrying nothing: the refresh port takes no input,
|
|
148
|
+
* and the slice the result installs is read off the follow-up Msg, not this
|
|
149
|
+
* Cmd. The consumer unions this into their machine's Cmd type and lets
|
|
150
|
+
* `handlers` interpret it.
|
|
151
|
+
*/
|
|
152
|
+
interface RefreshTokenCmd extends Cmd<"refresh_token"> {
|
|
153
|
+
}
|
|
154
|
+
/** Construct a `refresh_token` Cmd. */
|
|
155
|
+
declare function refreshTokenCmd(): RefreshTokenCmd;
|
|
156
|
+
/**
|
|
157
|
+
* The Msg the refresh port dispatches on success — carries the freshly minted
|
|
158
|
+
* `Token`. The consumer unions this into their Msg type and handles it by
|
|
159
|
+
* folding `refreshed(state, msg.token)` into the slice.
|
|
160
|
+
*/
|
|
161
|
+
interface TokenRefreshedMsg {
|
|
162
|
+
readonly type: typeof MsgType.TokenRefreshed;
|
|
163
|
+
readonly token: Token;
|
|
164
|
+
}
|
|
165
|
+
/** Construct a `token_refreshed` Msg carrying the new token. */
|
|
166
|
+
declare function tokenRefreshedMsg(token: Token): TokenRefreshedMsg;
|
|
167
|
+
/**
|
|
168
|
+
* The Msg the refresh port dispatches on failure — carries the rejection so the
|
|
169
|
+
* consumer's reducer can decide whether to back off, surface an error, or give
|
|
170
|
+
* up (errors are data: it is dispatched, never swallowed). The slice itself is
|
|
171
|
+
* left untouched by a failed refresh — the held token (if any) stays held and
|
|
172
|
+
* stale stays set, so the consumer can retry; clearing it is the consumer's
|
|
173
|
+
* policy, expressed in their reducer.
|
|
174
|
+
*/
|
|
175
|
+
interface TokenRefreshFailedMsg {
|
|
176
|
+
readonly type: typeof MsgType.TokenRefreshFailed;
|
|
177
|
+
readonly error: unknown;
|
|
178
|
+
}
|
|
179
|
+
/** Construct a `token_refresh_failed` Msg carrying the rejection. */
|
|
180
|
+
declare function tokenRefreshFailedMsg(error: unknown): TokenRefreshFailedMsg;
|
|
181
|
+
/** The two Msgs the refresh port can produce. Union for a consumer's Msg type. */
|
|
182
|
+
type TokenRefreshMsg = TokenRefreshedMsg | TokenRefreshFailedMsg;
|
|
183
|
+
/** The ports this knob's `handlers` splice needs — the single refresh DI seam. */
|
|
184
|
+
interface TokenRefreshPorts {
|
|
185
|
+
/**
|
|
186
|
+
* Mint a fresh credential. Called ONLY from the `refresh_token` interpret
|
|
187
|
+
* handler — never from a verb — so the network I/O and any clock the issuer
|
|
188
|
+
* reads stay at the effect boundary. May reject; the rejection is routed to
|
|
189
|
+
* `token_refresh_failed` (errors are data), never thrown into the runtime.
|
|
190
|
+
*/
|
|
191
|
+
readonly refresh: () => Promise<Token>;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* The knob: hand it a config, get back the slice initializer, the pure verbs,
|
|
195
|
+
* and the `handlers` splice. Mirrors every L2 composition's uniform contract so
|
|
196
|
+
* the call sites read identically across bricks.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* const tr = createTokenRefresh({ skewMs: 30_000 });
|
|
200
|
+
* const s0 = tr.init();
|
|
201
|
+
* const [s1, cmds] = tr.ensureFresh(s0, Date.now()); // cmds = [refreshTokenCmd()]
|
|
202
|
+
*/
|
|
203
|
+
declare function createTokenRefresh(config?: TokenRefreshConfig): {
|
|
204
|
+
/** The slice initializer — spread into the consumer's `init`. */
|
|
205
|
+
init: typeof initTokenRefresh;
|
|
206
|
+
needsRefresh: (state: TokenState, at: number) => boolean;
|
|
207
|
+
on401: (state: TokenState) => TokenState;
|
|
208
|
+
refreshed: (state: TokenState, token: Token) => TokenState;
|
|
209
|
+
ensureFresh: (state: TokenState, at: number) => readonly [TokenState, readonly Cmd[]];
|
|
210
|
+
handlers: (ports: TokenRefreshPorts) => Interpret<TokenRefreshMsg, RefreshTokenCmd, unknown>;
|
|
211
|
+
};
|
|
212
|
+
/** The shape `createTokenRefresh` returns — useful for typing a held knob. */
|
|
213
|
+
type TokenRefresh = ReturnType<typeof createTokenRefresh>;
|
|
214
|
+
|
|
215
|
+
export { type RefreshTokenCmd, type Token, type TokenRefresh, type TokenRefreshConfig, type TokenRefreshFailedMsg, type TokenRefreshMsg, type TokenRefreshPorts, type TokenRefreshedMsg, type TokenState, createTokenRefresh, initTokenRefresh, refreshTokenCmd, tokenRefreshFailedMsg, tokenRefreshedMsg };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { createTokenRefresh, initTokenRefresh, refreshTokenCmd, tokenRefreshFailedMsg, tokenRefreshedMsg } from '../chunk-3XJWC7VW.js';
|
|
2
|
+
import '../chunk-JELSZN25.js';
|
|
3
|
+
import '../chunk-MMH7CW5K.js';
|
|
4
|
+
import '../chunk-QLA76GIT.js';
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Cmd, Sub, Machine } from '../
|
|
1
|
+
import { C as Cmd, S as Sub, M as Machine } from '../core-BoB6t7dB.js';
|
|
2
2
|
import { Trace } from '../recorder/index.js';
|
|
3
|
+
import '../index.js';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* The outcome of {@link replayTrace}.
|
|
@@ -48,5 +49,13 @@ interface ReplayResult<S> {
|
|
|
48
49
|
declare function replayTrace<S, M extends {
|
|
49
50
|
type: string;
|
|
50
51
|
}, C extends Cmd, U extends Sub, Ctx>(machine: Machine<S, M, C, U, Ctx>, trace: Trace<S, M>, ctx: Ctx): ReplayResult<S>;
|
|
52
|
+
/**
|
|
53
|
+
* Order-insensitive structural deep-equality — the single comparator this
|
|
54
|
+
* package owns, exposed so consumers (e.g. `@demlik/tea/parity`'s `diff`)
|
|
55
|
+
* reuse it rather than adding a second deep-compare. `true` iff `a` and `b`
|
|
56
|
+
* are structurally equal under {@link firstDivergence}'s walk (object keys
|
|
57
|
+
* compared order-insensitively, arrays index-by-index).
|
|
58
|
+
*/
|
|
59
|
+
declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
51
60
|
|
|
52
|
-
export { type ReplayResult, replayTrace };
|
|
61
|
+
export { type ReplayResult, deepEqual, replayTrace };
|
|
@@ -1,57 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
function replayTrace(machine, trace, ctx) {
|
|
5
|
-
const result = replay(machine, {
|
|
6
|
-
msgs: trace.msgs,
|
|
7
|
-
ctx,
|
|
8
|
-
loaded: trace.loaded
|
|
9
|
-
});
|
|
10
|
-
const actual = result.state;
|
|
11
|
-
const expected = trace.finalState;
|
|
12
|
-
const divergence = firstDivergence(expected, actual, "state");
|
|
13
|
-
if (divergence === null) {
|
|
14
|
-
return { matches: true, expected, actual };
|
|
15
|
-
}
|
|
16
|
-
return { matches: false, expected, actual, divergence };
|
|
17
|
-
}
|
|
18
|
-
function firstDivergence(expected, actual, path) {
|
|
19
|
-
if (Object.is(expected, actual)) return null;
|
|
20
|
-
const te = typeOf(expected);
|
|
21
|
-
const ta = typeOf(actual);
|
|
22
|
-
if (te !== ta) return { path, expected, actual };
|
|
23
|
-
if (te === "array") {
|
|
24
|
-
const ae = expected;
|
|
25
|
-
const aa = actual;
|
|
26
|
-
if (ae.length !== aa.length) {
|
|
27
|
-
return { path, expected, actual };
|
|
28
|
-
}
|
|
29
|
-
for (let i = 0; i < ae.length; i++) {
|
|
30
|
-
const d = firstDivergence(ae[i], aa[i], `${path}[${i}]`);
|
|
31
|
-
if (d !== null) return d;
|
|
32
|
-
}
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
if (te === "object") {
|
|
36
|
-
const oe = expected;
|
|
37
|
-
const oa = actual;
|
|
38
|
-
const keys = [.../* @__PURE__ */ new Set([...Object.keys(oe), ...Object.keys(oa)])].sort();
|
|
39
|
-
for (const key of keys) {
|
|
40
|
-
const d = firstDivergence(oe[key], oa[key], `${path}.${key}`);
|
|
41
|
-
if (d !== null) return d;
|
|
42
|
-
}
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
return { path, expected, actual };
|
|
46
|
-
}
|
|
47
|
-
function typeOf(v) {
|
|
48
|
-
if (v === null) return "null";
|
|
49
|
-
if (v === void 0) return "undefined";
|
|
50
|
-
if (Array.isArray(v)) return "array";
|
|
51
|
-
if (typeof v === "object") return "object";
|
|
52
|
-
return "primitive";
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export { replayTrace };
|
|
1
|
+
export { deepEqual, replayTrace } from '../chunk-ON24RKSE.js';
|
|
2
|
+
import '../chunk-MMH7CW5K.js';
|
|
3
|
+
import '../chunk-QLA76GIT.js';
|
|
56
4
|
//# sourceMappingURL=index.js.map
|
|
57
5
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { S as Sub, C as Cmd, M as Machine } from '../core-BoB6t7dB.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @demlik/tea/with-deadline — auto-fail a wrapped machine at T+N, re-arm on
|
|
5
|
+
* progress. A **pure function over machine data**: `withDeadline(base, config)`
|
|
6
|
+
* takes any `Machine<S, M, C, U, Ctx>` and returns a NEW `Machine` over the
|
|
7
|
+
* composed Model `{ base, $deadline }`. You `run()` / `replay()` the result
|
|
8
|
+
* exactly like any other machine — there is no new runtime, no privileged
|
|
9
|
+
* interception path.
|
|
10
|
+
*
|
|
11
|
+
* ## What it does
|
|
12
|
+
*
|
|
13
|
+
* The wrapper watches the base Msg stream. While the deadline is `armed`, a
|
|
14
|
+
* RELATIVE timeout Sub counts down `config.ms`. Every base Msg the
|
|
15
|
+
* `config.progress` predicate accepts (default: every base Msg) is treated as
|
|
16
|
+
* activity: the wrapper bumps a numeric `seq`, which CHANGES the timer Sub's id,
|
|
17
|
+
* so the substrate's reconcile pass retires the old countdown and starts a fresh
|
|
18
|
+
* one — "re-arm on progress". If `config.ms` elapses with NO accepted progress,
|
|
19
|
+
* the timeout Sub dispatches `$deadline:exceeded`; the merged `update` folds it
|
|
20
|
+
* by flipping `phase` to `exceeded`, and from then on `subscriptions` returns no
|
|
21
|
+
* timer (the machine has auto-failed). The expiry is a slice transition visible
|
|
22
|
+
* in the Msg log — the auto-fail is never an off-ledger side effect.
|
|
23
|
+
*
|
|
24
|
+
* This is an OBSERVE-ONLY wrapper in the same sense as `withTelemetry`: it never
|
|
25
|
+
* intercepts a base Cmd and never gates a base Msg. `state.base` after wrapping
|
|
26
|
+
* is BYTE-IDENTICAL to running the base alone, so the shared
|
|
27
|
+
* `assertWrapperFaithful` gate passes with the deadline bolted on.
|
|
28
|
+
*
|
|
29
|
+
* ## The 5 rules it satisfies (from the wrapper-tier contract)
|
|
30
|
+
*
|
|
31
|
+
* 1. **Named, serializable slice.** Wrapper state lives at `model.$deadline`,
|
|
32
|
+
* a plain `{ phase, seq }` — JSON-serializable, never a closure. There is
|
|
33
|
+
* NO absolute timestamp in the slice: the clock is never read in `update`.
|
|
34
|
+
* 2. **Retag-and-re-emit, never swallow.** The merged `update` runs the base
|
|
35
|
+
* reducer, passes its Cmds through UNCHANGED, and APPENDS its own
|
|
36
|
+
* `$deadline:rearm` / `$deadline:expire` decision Cmd. No base Cmd ever
|
|
37
|
+
* vanishes; nothing is gated.
|
|
38
|
+
* 3. **Every decision is a Msg/Cmd.** Re-arm is a `$deadline:rearm` Cmd; the
|
|
39
|
+
* auto-fail expiry is the `$deadline:exceeded` Msg the Sub delivers, folded
|
|
40
|
+
* into the slice and echoed as a `$deadline:expire` Cmd. "Why did this
|
|
41
|
+
* fire" is answerable from reducer + log alone.
|
|
42
|
+
* 4. **Subs merge by id.** `subscriptions = [...base, ...deadlineSubs]`; the
|
|
43
|
+
* timeout id is `$deadline:timeout:{seq}`, so a progress bump (seq+1)
|
|
44
|
+
* yields a NEW id — the substrate retires the old timer and arms a fresh
|
|
45
|
+
* one. When `exceeded`, no deadline sub is returned.
|
|
46
|
+
* 5. **Clock out of update.** The countdown is a relative-timeout Sub; the
|
|
47
|
+
* clock is read at subscribe time inside the Sub (`setTimeout`), NEVER in a
|
|
48
|
+
* verb. The slice carries `seq`, not a deadline timestamp.
|
|
49
|
+
*
|
|
50
|
+
* ## Typical wiring
|
|
51
|
+
*
|
|
52
|
+
* const guarded = withDeadline(baseMachine, {
|
|
53
|
+
* ms: 900_000, // auto-fail after 15 minutes of inactivity
|
|
54
|
+
* progress: (msg) => msg.type !== "heartbeat", // heartbeats don't re-arm
|
|
55
|
+
* });
|
|
56
|
+
* const runtime = run(guarded, { ctx: baseCtx });
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The wrapper's Model slice. `phase` is the lifecycle (`armed` while the
|
|
61
|
+
* countdown runs, `exceeded` once the deadline fired); `seq` is the monotonic
|
|
62
|
+
* re-arm counter that keys the timer Sub's id so each accepted progress Msg
|
|
63
|
+
* retires the old timer and starts a fresh one.
|
|
64
|
+
*
|
|
65
|
+
* PURE and SERIALIZABLE: there is deliberately NO absolute timestamp here. A
|
|
66
|
+
* deadline timestamp would force the merged `update` to read the wall clock to
|
|
67
|
+
* compute it, which the conformance gate (property 2 — two replays under
|
|
68
|
+
* distinct ambient clocks) is built to catch. Instead, the timer arms a fresh
|
|
69
|
+
* full `ms` countdown per generation; no remaining-delay computation and no
|
|
70
|
+
* clock read occur in the slice or `update` — the only clock is `setTimeout`
|
|
71
|
+
* inside the Sub.
|
|
72
|
+
*/
|
|
73
|
+
interface DeadlineSlice {
|
|
74
|
+
/** `armed` while the countdown is live; `exceeded` once the deadline fired. */
|
|
75
|
+
readonly phase: "armed" | "exceeded";
|
|
76
|
+
/**
|
|
77
|
+
* Monotonic re-arm counter. Starts at 0; each accepted progress Msg (while
|
|
78
|
+
* armed) bumps it by one. Folds into the timer Sub's id, so a bump = a new id
|
|
79
|
+
* = the substrate retires the old timer and arms a fresh `ms` countdown.
|
|
80
|
+
*/
|
|
81
|
+
readonly seq: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The composed Model. The base machine's state is nested under `base`; the
|
|
85
|
+
* wrapper's state lives in the NAMED `$deadline` slice — never hidden in a
|
|
86
|
+
* closure. Both fields are plain data, so the whole composed Model round-trips
|
|
87
|
+
* through `JSON.parse(JSON.stringify(...))` iff the base state does.
|
|
88
|
+
*/
|
|
89
|
+
interface DeadlineModel<S> {
|
|
90
|
+
readonly base: S;
|
|
91
|
+
readonly $deadline: DeadlineSlice;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The relative-timeout Sub the wrapper arms while `phase === "armed"`. `delayMs`
|
|
95
|
+
* is the window `config.ms`; the id is `$deadline:timeout:{seq}`, so each
|
|
96
|
+
* progress bump produces a distinct id and the reconcile pass churns the timer.
|
|
97
|
+
* Same `delayMs`-on-the-Sub shape `fromTimeout` consumes.
|
|
98
|
+
*/
|
|
99
|
+
type DeadlineTimeoutSub = Sub<"$deadline:timeout"> & {
|
|
100
|
+
readonly delayMs: number;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* The Msg the timeout Sub dispatches when `config.ms` elapses with no accepted
|
|
104
|
+
* progress. `seq` echoes the re-arm generation the timer was armed against, so a
|
|
105
|
+
* stale timer's Msg (one whose id was already retired) is identifiable; the
|
|
106
|
+
* merged `update` only honors it when it matches the current slice `seq`.
|
|
107
|
+
*/
|
|
108
|
+
interface DeadlineExceededMsg {
|
|
109
|
+
readonly type: "$deadline:exceeded";
|
|
110
|
+
/** The re-arm generation this timer was armed against. */
|
|
111
|
+
readonly seq: number;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Construct the deadline-exceeded Msg. Exported so the subscribe cell and tests
|
|
115
|
+
* share one constructor rather than two literals that can drift.
|
|
116
|
+
*/
|
|
117
|
+
declare function deadlineExceededMsg(seq: number): DeadlineExceededMsg;
|
|
118
|
+
/**
|
|
119
|
+
* The fire-and-forget decision Cmd the merged `update` appends on every
|
|
120
|
+
* transition, so the wrapper's choice is visible in the replayed Cmd log
|
|
121
|
+
* (rule 3) — `rearm` when an accepted progress Msg bumped `seq`, `expire` when
|
|
122
|
+
* the deadline-exceeded Msg flipped the phase, `idle` otherwise (a non-progress
|
|
123
|
+
* Msg, or a Msg after the machine already auto-failed). Plain data; its
|
|
124
|
+
* interpret handler is a no-op (the decision is the Cmd's presence in the log,
|
|
125
|
+
* not a side effect).
|
|
126
|
+
*/
|
|
127
|
+
type DeadlineDecisionCmd = Cmd<"$deadline:decision"> & {
|
|
128
|
+
readonly decision: "rearm" | "expire" | "idle";
|
|
129
|
+
/** The slice `seq` AFTER this transition (the re-arm generation now armed). */
|
|
130
|
+
readonly seq: number;
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* A predicate over a Msg that decides whether it re-arms the deadline.
|
|
134
|
+
*
|
|
135
|
+
* The argument is `Readonly<M>` and the result is `boolean`: this is the
|
|
136
|
+
* STRONGEST purity TS can express here. `Readonly<M>` removes the most common
|
|
137
|
+
* impurity at compile time — the predicate cannot reassign the Msg's own
|
|
138
|
+
* fields — and `boolean` pins the return so a side-effecting `(msg) => { … }`
|
|
139
|
+
* with an implicit `undefined`/`void` return is rejected.
|
|
140
|
+
*
|
|
141
|
+
* What the type system CANNOT encode (TS has no effect tracking): observational
|
|
142
|
+
* purity. A body that reads `Date.now()`, calls `Math.random()`, performs IO,
|
|
143
|
+
* or closes over and mutates external state still type-checks. Those remain a
|
|
144
|
+
* CONVENTION enforced by review, not the compiler — the predicate runs inside
|
|
145
|
+
* the pure merged `update` (invariant 2), so it must be a pure function of the
|
|
146
|
+
* Msg alone: no clock, no RNG, no IO, no captured mutable state.
|
|
147
|
+
*/
|
|
148
|
+
type ProgressPredicate<M extends {
|
|
149
|
+
type: string;
|
|
150
|
+
}> = (msg: Readonly<M>) => boolean;
|
|
151
|
+
/**
|
|
152
|
+
* The deadline knob. `ms` is the inactivity window; `progress` is an OPTIONAL
|
|
153
|
+
* predicate picking which base Msgs count as activity (default: EVERY base Msg
|
|
154
|
+
* re-arms). The predicate runs INSIDE the pure merged `update`, so it must be a
|
|
155
|
+
* pure function of the Msg — see {@link ProgressPredicate} for what the type
|
|
156
|
+
* enforces vs. what stays a convention.
|
|
157
|
+
*/
|
|
158
|
+
interface DeadlineConfig<M extends {
|
|
159
|
+
type: string;
|
|
160
|
+
}> {
|
|
161
|
+
/** The inactivity window in milliseconds. Auto-fail after `ms` with no progress. */
|
|
162
|
+
readonly ms: number;
|
|
163
|
+
/**
|
|
164
|
+
* Predicate picking which base Msgs re-arm the timer. Default: every base Msg
|
|
165
|
+
* counts as progress. Pure — a function of the Msg alone (see
|
|
166
|
+
* {@link ProgressPredicate}).
|
|
167
|
+
*/
|
|
168
|
+
readonly progress?: ProgressPredicate<M>;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Wrap `base` with an inactivity deadline. Returns a NEW `Machine` over the
|
|
172
|
+
* composed Model `{ base, $deadline }`, with the base's Msgs/Cmds/Subs extended
|
|
173
|
+
* by the wrapper's own (`$deadline:exceeded` Msg, `$deadline:decision` Cmd,
|
|
174
|
+
* `$deadline:timeout` Sub).
|
|
175
|
+
*
|
|
176
|
+
* The composed machine:
|
|
177
|
+
* - `init` rehydrates the base (honoring the `[loaded, []]` contract) and
|
|
178
|
+
* seeds `$deadline: { phase: "armed", seq: 0 }`. Base init Cmds pass
|
|
179
|
+
* through unchanged.
|
|
180
|
+
* - `update` (Reducer form) runs the base reducer for base Msgs — base Cmds
|
|
181
|
+
* pass through UNCHANGED — and on an accepted progress Msg bumps `seq`
|
|
182
|
+
* (re-arm). The `$deadline:exceeded` Msg cell flips `phase` to `exceeded`.
|
|
183
|
+
* Every transition appends a `$deadline:decision` marker Cmd.
|
|
184
|
+
* - `subscriptions` is the base's, plus a `$deadline:timeout` Sub keyed on
|
|
185
|
+
* `seq` while `phase === "armed"`.
|
|
186
|
+
* - `subscribe` is the base's, plus the `$deadline:timeout` cell.
|
|
187
|
+
* - `interpret` is the base's, plus a no-op `$deadline:decision` handler.
|
|
188
|
+
*
|
|
189
|
+
* @param base any `Machine<S, M, C, U, Ctx>`.
|
|
190
|
+
* @param config the deadline knob (`ms` window + optional `progress` predicate).
|
|
191
|
+
*/
|
|
192
|
+
declare function withDeadline<S, M extends {
|
|
193
|
+
type: string;
|
|
194
|
+
}, C extends Cmd, U extends Sub, Ctx>(base: Machine<S, M, C, U, Ctx>, config: DeadlineConfig<M>): Machine<DeadlineModel<S>, M | DeadlineExceededMsg, C | DeadlineDecisionCmd, U | DeadlineTimeoutSub, Ctx>;
|
|
195
|
+
|
|
196
|
+
export { type DeadlineConfig, type DeadlineDecisionCmd, type DeadlineExceededMsg, type DeadlineModel, type DeadlineSlice, type DeadlineTimeoutSub, type ProgressPredicate, deadlineExceededMsg, withDeadline };
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { fromTimeout } from '../chunk-2YFLOCWX.js';
|
|
2
|
+
import '../chunk-MMH7CW5K.js';
|
|
3
|
+
import { msgKeysOf, applyCell, subId } from '../chunk-QLA76GIT.js';
|
|
4
|
+
|
|
5
|
+
// src/with-deadline/index.ts
|
|
6
|
+
function deadlineExceededMsg(seq) {
|
|
7
|
+
return { type: "$deadline:exceeded", seq };
|
|
8
|
+
}
|
|
9
|
+
function withDeadline(base, config) {
|
|
10
|
+
const windowMs = config.ms;
|
|
11
|
+
const isProgress = config.progress;
|
|
12
|
+
const update = {};
|
|
13
|
+
const baseMsgKeys = msgKeysOf(base);
|
|
14
|
+
for (const key of baseMsgKeys) {
|
|
15
|
+
if (key.startsWith("$deadline:")) {
|
|
16
|
+
throw new Error(
|
|
17
|
+
`withDeadline: the base machine declares a reserved "${key}" update cell \u2014 the wrapper owns the "$deadline:" Msg namespace. Rename the base Msg.`
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
for (const key of baseMsgKeys) {
|
|
22
|
+
update[key] = (state, msg) => {
|
|
23
|
+
const [nextBase, baseCmds] = applyCell(
|
|
24
|
+
base,
|
|
25
|
+
state.base,
|
|
26
|
+
msg
|
|
27
|
+
);
|
|
28
|
+
const armed = state.$deadline.phase === "armed";
|
|
29
|
+
const counts = armed && (isProgress === void 0 || isProgress(msg));
|
|
30
|
+
const $deadline = counts ? { phase: "armed", seq: state.$deadline.seq + 1 } : state.$deadline;
|
|
31
|
+
const next = { base: nextBase, $deadline };
|
|
32
|
+
const decision = {
|
|
33
|
+
type: "$deadline:decision",
|
|
34
|
+
decision: counts ? "rearm" : "idle",
|
|
35
|
+
seq: $deadline.seq
|
|
36
|
+
};
|
|
37
|
+
return [next, [...baseCmds, decision]];
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
update["$deadline:exceeded"] = (state, msg) => {
|
|
41
|
+
const m = msg;
|
|
42
|
+
const fires = state.$deadline.phase === "armed" && m.seq === state.$deadline.seq;
|
|
43
|
+
const $deadline = fires ? { phase: "exceeded", seq: state.$deadline.seq } : state.$deadline;
|
|
44
|
+
const next = { base: state.base, $deadline };
|
|
45
|
+
const decision = {
|
|
46
|
+
type: "$deadline:decision",
|
|
47
|
+
decision: fires ? "expire" : "idle",
|
|
48
|
+
seq: $deadline.seq
|
|
49
|
+
};
|
|
50
|
+
return [next, [decision]];
|
|
51
|
+
};
|
|
52
|
+
const baseInterpret = base.interpret ?? {};
|
|
53
|
+
if (Object.hasOwn(baseInterpret, "$deadline:decision")) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
'withDeadline: the base machine declares a reserved "$deadline:decision" interpret handler \u2014 the wrapper owns the "$deadline:" Cmd namespace. Rename the base handler.'
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
const interpret = {
|
|
59
|
+
...baseInterpret,
|
|
60
|
+
"$deadline:decision": async () => {
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const baseSubscriptions = base.subscriptions;
|
|
64
|
+
const subscriptions = (state) => {
|
|
65
|
+
const baseSubs = baseSubscriptions ? baseSubscriptions(state.base) : [];
|
|
66
|
+
if (state.$deadline.phase !== "armed") return baseSubs;
|
|
67
|
+
const timeout = {
|
|
68
|
+
id: subId(`$deadline:timeout:${state.$deadline.seq}`),
|
|
69
|
+
type: "$deadline:timeout",
|
|
70
|
+
delayMs: windowMs
|
|
71
|
+
};
|
|
72
|
+
return [...baseSubs, timeout];
|
|
73
|
+
};
|
|
74
|
+
const baseSubscribe = base.subscribe ?? {};
|
|
75
|
+
if (Object.hasOwn(baseSubscribe, "$deadline:timeout")) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
'withDeadline: the base machine declares a reserved "$deadline:timeout" subscribe handler \u2014 the wrapper owns the "$deadline:" Sub namespace. Rename the base handler.'
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
const timeoutCell = fromTimeout(
|
|
81
|
+
(sub) => {
|
|
82
|
+
const seq = Number(sub.id.slice(sub.id.lastIndexOf(":") + 1));
|
|
83
|
+
return deadlineExceededMsg(seq);
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
const subscribe = {
|
|
87
|
+
...baseSubscribe,
|
|
88
|
+
"$deadline:timeout": timeoutCell
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
init: (loaded, ctx) => {
|
|
92
|
+
if (loaded !== null) {
|
|
93
|
+
return [loaded, []];
|
|
94
|
+
}
|
|
95
|
+
const [base0, cmds] = base.init(null, ctx);
|
|
96
|
+
const model = {
|
|
97
|
+
base: base0,
|
|
98
|
+
$deadline: { phase: "armed", seq: 0 }
|
|
99
|
+
};
|
|
100
|
+
return [model, cmds];
|
|
101
|
+
},
|
|
102
|
+
update,
|
|
103
|
+
subscriptions,
|
|
104
|
+
subscribe,
|
|
105
|
+
interpret
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { deadlineExceededMsg, withDeadline };
|
|
110
|
+
//# sourceMappingURL=index.js.map
|
|
111
|
+
//# sourceMappingURL=index.js.map
|