@barnum/barnum 0.2.3 → 0.4.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/artifacts/linux-arm64/barnum +0 -0
- package/artifacts/linux-x64/barnum +0 -0
- package/artifacts/macos-arm64/barnum +0 -0
- package/artifacts/macos-x64/barnum +0 -0
- package/artifacts/win-x64/barnum.exe +0 -0
- package/cli.cjs +33 -0
- package/dist/all.d.ts +43 -0
- package/dist/all.d.ts.map +1 -0
- package/dist/all.js +8 -0
- package/dist/ast.d.ts +476 -0
- package/dist/ast.d.ts.map +1 -0
- package/dist/ast.js +419 -0
- package/dist/bind.d.ts +59 -0
- package/dist/bind.d.ts.map +1 -0
- package/dist/bind.js +69 -0
- package/dist/builtins/array.d.ts +36 -0
- package/dist/builtins/array.d.ts.map +1 -0
- package/dist/builtins/array.js +93 -0
- package/dist/builtins/index.d.ts +6 -0
- package/dist/builtins/index.d.ts.map +1 -0
- package/dist/builtins/index.js +5 -0
- package/dist/builtins/scalar.d.ts +12 -0
- package/dist/builtins/scalar.d.ts.map +1 -0
- package/dist/builtins/scalar.js +41 -0
- package/dist/builtins/struct.d.ts +25 -0
- package/dist/builtins/struct.d.ts.map +1 -0
- package/dist/builtins/struct.js +67 -0
- package/dist/builtins/tagged-union.d.ts +54 -0
- package/dist/builtins/tagged-union.d.ts.map +1 -0
- package/dist/builtins/tagged-union.js +81 -0
- package/dist/builtins/with-resource.d.ts +23 -0
- package/dist/builtins/with-resource.d.ts.map +1 -0
- package/dist/builtins/with-resource.js +35 -0
- package/dist/chain.d.ts +3 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +8 -0
- package/dist/effect-id.d.ts +15 -0
- package/dist/effect-id.d.ts.map +1 -0
- package/dist/effect-id.js +16 -0
- package/dist/handler.d.ts +51 -0
- package/dist/handler.d.ts.map +1 -0
- package/dist/handler.js +130 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/iterator.d.ts +32 -0
- package/dist/iterator.d.ts.map +1 -0
- package/dist/iterator.js +123 -0
- package/dist/option.d.ts +74 -0
- package/dist/option.d.ts.map +1 -0
- package/dist/option.js +141 -0
- package/dist/pipe.d.ts +12 -0
- package/dist/pipe.d.ts.map +1 -0
- package/dist/pipe.js +12 -0
- package/dist/race.d.ts +54 -0
- package/dist/race.d.ts.map +1 -0
- package/dist/race.js +116 -0
- package/dist/recursive.d.ts +40 -0
- package/dist/recursive.d.ts.map +1 -0
- package/dist/recursive.js +58 -0
- package/dist/result.d.ts +50 -0
- package/dist/result.d.ts.map +1 -0
- package/dist/result.js +117 -0
- package/dist/run.d.ts +14 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +160 -0
- package/dist/runtime.d.ts +6 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +7 -0
- package/dist/schema.d.ts +9 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +95 -0
- package/dist/schemas.d.ts +5 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +13 -0
- package/dist/try-catch.d.ts +24 -0
- package/dist/try-catch.d.ts.map +1 -0
- package/dist/try-catch.js +37 -0
- package/dist/values.d.ts +6 -0
- package/dist/values.d.ts.map +1 -0
- package/dist/values.js +12 -0
- package/dist/worker.d.ts +15 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +58 -0
- package/package.json +42 -16
- package/src/all.ts +133 -0
- package/src/ast.ts +1301 -0
- package/src/bind.ts +162 -0
- package/src/builtins/array.ts +121 -0
- package/src/builtins/index.ts +17 -0
- package/src/builtins/scalar.ts +49 -0
- package/src/builtins/struct.ts +111 -0
- package/src/builtins/tagged-union.ts +142 -0
- package/src/builtins/with-resource.ts +69 -0
- package/src/chain.ts +17 -0
- package/src/effect-id.ts +30 -0
- package/src/handler.ts +263 -0
- package/src/index.ts +37 -0
- package/src/iterator.ts +243 -0
- package/src/option.ts +199 -0
- package/src/pipe.ts +138 -0
- package/src/race.ts +173 -0
- package/src/recursive.ts +129 -0
- package/src/result.ts +168 -0
- package/src/run.ts +209 -0
- package/src/runtime.ts +16 -0
- package/src/schema.ts +118 -0
- package/src/schemas.ts +21 -0
- package/src/try-catch.ts +57 -0
- package/src/values.ts +21 -0
- package/src/worker.ts +71 -0
- package/README.md +0 -19
- package/barnum-config-schema.json +0 -408
- package/cli.js +0 -20
- package/index.js +0 -23
package/src/ast.ts
ADDED
|
@@ -0,0 +1,1301 @@
|
|
|
1
|
+
import type { JSONSchema7 } from "json-schema";
|
|
2
|
+
import { chain } from "./chain.js";
|
|
3
|
+
import {
|
|
4
|
+
constant,
|
|
5
|
+
drop,
|
|
6
|
+
extractPrefix,
|
|
7
|
+
flatten as flattenBuiltin,
|
|
8
|
+
getField,
|
|
9
|
+
getIndex,
|
|
10
|
+
identity,
|
|
11
|
+
panic,
|
|
12
|
+
pick,
|
|
13
|
+
splitFirst,
|
|
14
|
+
splitLast,
|
|
15
|
+
tag,
|
|
16
|
+
wrapInField,
|
|
17
|
+
asOption as asOptionStandalone,
|
|
18
|
+
} from "./builtins/index.js";
|
|
19
|
+
import { Option } from "./option.js";
|
|
20
|
+
import { Result } from "./result.js";
|
|
21
|
+
// Lazy import — iterator.ts imports from ast.ts, but these are only called inside
|
|
22
|
+
// methods (after all modules load), so the circular reference is safe at runtime.
|
|
23
|
+
import { Iterator as IteratorNs } from "./iterator.js";
|
|
24
|
+
// Lazy import — bind.ts imports from ast.ts, but these are only called inside
|
|
25
|
+
// methods (after all modules load), so the circular reference is safe at runtime.
|
|
26
|
+
import {
|
|
27
|
+
bind as bindStandalone,
|
|
28
|
+
bindInput as bindInputStandalone,
|
|
29
|
+
type VarRef,
|
|
30
|
+
type InferVarRefs,
|
|
31
|
+
} from "./bind.js";
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Serializable Types — mirror the Rust AST in barnum_ast
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
export type Action =
|
|
38
|
+
| InvokeAction
|
|
39
|
+
| ChainAction
|
|
40
|
+
| ForEachAction
|
|
41
|
+
| AllAction
|
|
42
|
+
| BranchAction
|
|
43
|
+
| ResumeHandleAction
|
|
44
|
+
| ResumePerformAction
|
|
45
|
+
| RestartHandleAction
|
|
46
|
+
| RestartPerformAction;
|
|
47
|
+
|
|
48
|
+
export interface InvokeAction {
|
|
49
|
+
kind: "Invoke";
|
|
50
|
+
handler: HandlerKind;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ChainAction {
|
|
54
|
+
kind: "Chain";
|
|
55
|
+
first: Action;
|
|
56
|
+
rest: Action;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ForEachAction {
|
|
60
|
+
kind: "ForEach";
|
|
61
|
+
action: Action;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface AllAction {
|
|
65
|
+
kind: "All";
|
|
66
|
+
actions: Action[];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface BranchAction {
|
|
70
|
+
kind: "Branch";
|
|
71
|
+
cases: Record<string, Action>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface ResumeHandleAction {
|
|
75
|
+
kind: "ResumeHandle";
|
|
76
|
+
resume_handler_id: ResumeHandlerId;
|
|
77
|
+
body: Action;
|
|
78
|
+
handler: Action;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface ResumePerformAction {
|
|
82
|
+
kind: "ResumePerform";
|
|
83
|
+
resume_handler_id: ResumeHandlerId;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface RestartHandleAction {
|
|
87
|
+
kind: "RestartHandle";
|
|
88
|
+
restart_handler_id: RestartHandlerId;
|
|
89
|
+
body: Action;
|
|
90
|
+
handler: Action;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface RestartPerformAction {
|
|
94
|
+
kind: "RestartPerform";
|
|
95
|
+
restart_handler_id: RestartHandlerId;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
// HandlerKind
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
|
|
102
|
+
export type HandlerKind = TypeScriptHandler | BuiltinHandler;
|
|
103
|
+
|
|
104
|
+
export interface TypeScriptHandler {
|
|
105
|
+
kind: "TypeScript";
|
|
106
|
+
module: string;
|
|
107
|
+
func: string;
|
|
108
|
+
input_schema?: JSONSchema7;
|
|
109
|
+
output_schema?: JSONSchema7;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface BuiltinHandler {
|
|
113
|
+
kind: "Builtin";
|
|
114
|
+
builtin: BuiltinKind;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export type BuiltinKind =
|
|
118
|
+
| { kind: "Constant"; value: unknown }
|
|
119
|
+
| { kind: "Identity" }
|
|
120
|
+
| { kind: "Drop" }
|
|
121
|
+
| { kind: "Merge" }
|
|
122
|
+
| { kind: "Flatten" }
|
|
123
|
+
| { kind: "GetField"; field: string }
|
|
124
|
+
| { kind: "GetIndex"; index: number }
|
|
125
|
+
| { kind: "CollectSome" }
|
|
126
|
+
// Future: Add WrapInArray builtin (T → [T]). Currently done via all(identity()) which
|
|
127
|
+
// works but routes through the All executor for a trivial operation.
|
|
128
|
+
| { kind: "AsOption" }
|
|
129
|
+
| { kind: "SplitFirst" }
|
|
130
|
+
| { kind: "SplitLast" }
|
|
131
|
+
| { kind: "WrapInField"; field: string }
|
|
132
|
+
| { kind: "Sleep"; ms: number }
|
|
133
|
+
| { kind: "Panic"; message: string }
|
|
134
|
+
| { kind: "ExtractPrefix" }
|
|
135
|
+
| { kind: "Slice"; start: number; end?: number };
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* When T is `never` or `void` (handler ignores input / recur doesn't
|
|
139
|
+
* thread state), produce `any` so the combinator can sit in any
|
|
140
|
+
* pipeline position.
|
|
141
|
+
*/
|
|
142
|
+
export type PipeIn<T> = [T] extends [never]
|
|
143
|
+
? any
|
|
144
|
+
: [T] extends [void]
|
|
145
|
+
? any
|
|
146
|
+
: T;
|
|
147
|
+
|
|
148
|
+
// ---------------------------------------------------------------------------
|
|
149
|
+
// Config
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
export interface Config {
|
|
153
|
+
workflow: Action;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
// Type utilities
|
|
158
|
+
// ---------------------------------------------------------------------------
|
|
159
|
+
|
|
160
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
161
|
+
type UnionToIntersection<TUnion> = (
|
|
162
|
+
TUnion extends any ? (x: TUnion) => void : never
|
|
163
|
+
) extends (x: infer TIntersection) => void
|
|
164
|
+
? TIntersection
|
|
165
|
+
: never;
|
|
166
|
+
|
|
167
|
+
/** Merge a tuple of objects into a single intersection type. */
|
|
168
|
+
export type MergeTuple<TTuple> = TTuple extends unknown[]
|
|
169
|
+
? UnionToIntersection<TTuple[number]>
|
|
170
|
+
: never;
|
|
171
|
+
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
// Phantom Types — type-safe input/output tracking
|
|
174
|
+
// ---------------------------------------------------------------------------
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* An action with tracked input/output types. Phantom fields enforce variance
|
|
178
|
+
* and are never set at runtime — they exist only for the TypeScript compiler.
|
|
179
|
+
*
|
|
180
|
+
* In: __in (contravariant) + __in_co (covariant) → invariant
|
|
181
|
+
* Out: __out (covariant only)
|
|
182
|
+
*
|
|
183
|
+
* Input invariance ensures exact type matching at pipeline connection points.
|
|
184
|
+
* Data crosses serialization boundaries to handlers in arbitrary languages
|
|
185
|
+
* (Rust, Python, etc.), so extra/missing fields are runtime errors.
|
|
186
|
+
*
|
|
187
|
+
* Output covariance is safe — a step producing Dog where Animal is expected
|
|
188
|
+
* downstream works. `never` (throwError, recur, done) is assignable to any
|
|
189
|
+
* output slot via standard subtyping.
|
|
190
|
+
*/
|
|
191
|
+
export type TypedAction<In = unknown, Out = unknown> = Action & {
|
|
192
|
+
__in?: (input: In) => void;
|
|
193
|
+
__in_co?: In;
|
|
194
|
+
__out?: () => Out;
|
|
195
|
+
/** Chain this action with another. `a.then(b)` ≡ `chain(a, b)`. */
|
|
196
|
+
then<TNext>(next: Pipeable<Out, TNext>): TypedAction<In, TNext>;
|
|
197
|
+
/** Dispatch on a tagged union output. Auto-unwraps `value` before each case handler. */
|
|
198
|
+
branch<
|
|
199
|
+
TCases extends {
|
|
200
|
+
[K in BranchKeys<Out>]: CaseHandler<BranchPayload<Out, K>, unknown>;
|
|
201
|
+
},
|
|
202
|
+
>(
|
|
203
|
+
cases: [BranchKeys<Out>] extends [never] ? never : TCases,
|
|
204
|
+
): TypedAction<In, ExtractOutput<TCases[keyof TCases & string]>>;
|
|
205
|
+
/** Flatten one level of array nesting. `TElement[][] → TElement[]` */
|
|
206
|
+
flatten<TIn, TElement>(
|
|
207
|
+
this: TypedAction<TIn, TElement[][]>,
|
|
208
|
+
): TypedAction<TIn, TElement[]>;
|
|
209
|
+
/** Discard output. `a.drop()` ≡ `pipe(a, drop)`. */
|
|
210
|
+
drop(): TypedAction<In, void>;
|
|
211
|
+
/** Wrap output as a tagged union member. Requires full variant map TDef so __def is carried. */
|
|
212
|
+
tag<
|
|
213
|
+
TEnumName extends string,
|
|
214
|
+
TDef extends Record<string, unknown>,
|
|
215
|
+
TKind extends keyof TDef & string,
|
|
216
|
+
>(
|
|
217
|
+
kind: TKind,
|
|
218
|
+
enumName: TEnumName,
|
|
219
|
+
): TypedAction<In, TaggedUnion<TEnumName, TDef>>;
|
|
220
|
+
/** Wrap output as `Option.Some`. `T → Option<T>` */
|
|
221
|
+
some(): TypedAction<In, Option<Out>>;
|
|
222
|
+
/** Wrap output as `Result.Ok`. `T → Result<T, never>` */
|
|
223
|
+
ok(): TypedAction<In, Result<Out, never>>;
|
|
224
|
+
/** Wrap output as `Result.Err`. `T → Result<never, T>` */
|
|
225
|
+
err(): TypedAction<In, Result<never, Out>>;
|
|
226
|
+
/** Extract a field from the output object. `a.getField("name")` ≡ `pipe(a, getField("name"))`. */
|
|
227
|
+
getField<TField extends keyof Out & string>(
|
|
228
|
+
field: TField,
|
|
229
|
+
): TypedAction<In, Out[TField]>;
|
|
230
|
+
/** Extract an element from the output array by index. Returns Option. */
|
|
231
|
+
getIndex<TIn, TTuple extends unknown[], TIndex extends number>(
|
|
232
|
+
this: TypedAction<TIn, TTuple>,
|
|
233
|
+
index: TIndex,
|
|
234
|
+
): TypedAction<TIn, Option<TTuple[TIndex]>>;
|
|
235
|
+
/** Wrap output in an object under a field name. `a.wrapInField("foo")` ≡ `pipe(a, wrapInField("foo"))`. */
|
|
236
|
+
wrapInField<TField extends string>(
|
|
237
|
+
field: TField,
|
|
238
|
+
): TypedAction<In, Record<TField, Out>>;
|
|
239
|
+
/** Select fields from the output. `a.pick("x", "y")` ≡ `pipe(a, pick("x", "y"))`. */
|
|
240
|
+
pick<TKeys extends (keyof Out & string)[]>(
|
|
241
|
+
...keys: TKeys
|
|
242
|
+
): TypedAction<In, Pick<Out, TKeys[number]>>;
|
|
243
|
+
/** Head/tail decomposition for Iterator. `Iterator<T> → Option<[T, Iterator<T>]>` */
|
|
244
|
+
splitFirst<TIn, TElement>(
|
|
245
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
246
|
+
): TypedAction<TIn, Option<[TElement, Iterator<TElement>]>>;
|
|
247
|
+
/** Head/tail decomposition. Only callable when Out is TElement[]. */
|
|
248
|
+
splitFirst<TIn, TElement>(
|
|
249
|
+
this: TypedAction<TIn, TElement[]>,
|
|
250
|
+
): TypedAction<TIn, Option<[TElement, TElement[]]>>;
|
|
251
|
+
/** Init/last decomposition for Iterator. `Iterator<T> → Option<[Iterator<T>, T]>` */
|
|
252
|
+
splitLast<TIn, TElement>(
|
|
253
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
254
|
+
): TypedAction<TIn, Option<[Iterator<TElement>, TElement]>>;
|
|
255
|
+
/** Init/last decomposition. Only callable when Out is TElement[]. */
|
|
256
|
+
splitLast<TIn, TElement>(
|
|
257
|
+
this: TypedAction<TIn, TElement[]>,
|
|
258
|
+
): TypedAction<TIn, Option<[TElement[], TElement]>>;
|
|
259
|
+
/**
|
|
260
|
+
* Transform the inner value. Dispatches: Option.map, Result.map.
|
|
261
|
+
*/
|
|
262
|
+
map<TIn, T, U>(
|
|
263
|
+
this: TypedAction<TIn, Option<T>>,
|
|
264
|
+
action: Pipeable<T, U>,
|
|
265
|
+
): TypedAction<TIn, Option<U>>;
|
|
266
|
+
map<TIn, TValue, TOut, TError>(
|
|
267
|
+
this: TypedAction<TIn, Result<TValue, TError>>,
|
|
268
|
+
action: Pipeable<TValue, TOut>,
|
|
269
|
+
): TypedAction<TIn, Result<TOut, TError>>;
|
|
270
|
+
/** Transform each element in Iterator. `Iterator<T> → Iterator<U>` */
|
|
271
|
+
map<TIn, TElement, TOut>(
|
|
272
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
273
|
+
action: Pipeable<TElement, TOut>,
|
|
274
|
+
): TypedAction<TIn, Iterator<TOut>>;
|
|
275
|
+
/**
|
|
276
|
+
* Transform the Err value of a Result output.
|
|
277
|
+
* `Result<TValue, TError> → Result<TValue, TErrorOut>`
|
|
278
|
+
*/
|
|
279
|
+
mapErr<TIn, TValue, TError, TErrorOut>(
|
|
280
|
+
this: TypedAction<TIn, Result<TValue, TError>>,
|
|
281
|
+
action: Pipeable<TError, TErrorOut>,
|
|
282
|
+
): TypedAction<TIn, Result<TValue, TErrorOut>>;
|
|
283
|
+
/**
|
|
284
|
+
* Unwrap or panic. Dispatches: Option.unwrap, Result.unwrap.
|
|
285
|
+
*
|
|
286
|
+
* Option: If Some, pass through value. If None, panic.
|
|
287
|
+
* Result: If Ok, pass through value. If Err, panic.
|
|
288
|
+
*/
|
|
289
|
+
unwrap<TIn, TValue>(
|
|
290
|
+
this: TypedAction<TIn, Option<TValue>>,
|
|
291
|
+
): TypedAction<TIn, TValue>;
|
|
292
|
+
unwrap<TIn, TValue, TError>(
|
|
293
|
+
this: TypedAction<TIn, Result<TValue, TError>>,
|
|
294
|
+
): TypedAction<TIn, TValue>;
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Unwrap a union output. Dispatches: Option.unwrapOr, Result.unwrapOr.
|
|
298
|
+
*
|
|
299
|
+
* Option: If Some, pass through value. If None, apply default.
|
|
300
|
+
* Result: If Ok, pass through value. If Err, apply default.
|
|
301
|
+
*
|
|
302
|
+
* Covariant output makes throw tokens (Out=never) work:
|
|
303
|
+
* `handler.unwrapOr(throwError)`
|
|
304
|
+
*/
|
|
305
|
+
unwrapOr<TIn, TValue>(
|
|
306
|
+
this: TypedAction<TIn, Option<TValue>>,
|
|
307
|
+
defaultAction: Pipeable<void, TValue>,
|
|
308
|
+
): TypedAction<TIn, TValue>;
|
|
309
|
+
unwrapOr<TIn, TValue, TError>(
|
|
310
|
+
this: TypedAction<TIn, Result<TValue, TError>>,
|
|
311
|
+
defaultAction: Pipeable<TError, TValue>,
|
|
312
|
+
): TypedAction<TIn, TValue>;
|
|
313
|
+
|
|
314
|
+
/** Monadic bind. Option: `Option<T> → Option<U>`. Result: `Result<T,E> → Result<U,E>`. */
|
|
315
|
+
andThen<TIn, TValue, TOut>(
|
|
316
|
+
this: TypedAction<TIn, Option<TValue>>,
|
|
317
|
+
action: Pipeable<TValue, Option<TOut>>,
|
|
318
|
+
): TypedAction<TIn, Option<TOut>>;
|
|
319
|
+
andThen<TIn, TValue, TOut, TError>(
|
|
320
|
+
this: TypedAction<TIn, Result<TValue, TError>>,
|
|
321
|
+
action: Pipeable<TValue, Result<TOut, TError>>,
|
|
322
|
+
): TypedAction<TIn, Result<TOut, TError>>;
|
|
323
|
+
|
|
324
|
+
/** Conditional keep. If Some, apply predicate. If None, stay None. */
|
|
325
|
+
filter<TIn, TValue>(
|
|
326
|
+
this: TypedAction<TIn, Option<TValue>>,
|
|
327
|
+
predicate: Pipeable<TValue, Option<TValue>>,
|
|
328
|
+
): TypedAction<TIn, Option<TValue>>;
|
|
329
|
+
/** Keep elements where predicate returns true. `Iterator<T> → Iterator<T>` */
|
|
330
|
+
filter<TIn, TElement>(
|
|
331
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
332
|
+
predicate: Pipeable<TElement, boolean>,
|
|
333
|
+
): TypedAction<TIn, Iterator<TElement>>;
|
|
334
|
+
|
|
335
|
+
/** Test if the value is Some. `Option<T> → boolean` */
|
|
336
|
+
isSome<TIn, TValue>(
|
|
337
|
+
this: TypedAction<TIn, Option<TValue>>,
|
|
338
|
+
): TypedAction<TIn, boolean>;
|
|
339
|
+
|
|
340
|
+
/** Test if the value is None. `Option<T> → boolean` */
|
|
341
|
+
isNone<TIn, TValue>(
|
|
342
|
+
this: TypedAction<TIn, Option<TValue>>,
|
|
343
|
+
): TypedAction<TIn, boolean>;
|
|
344
|
+
|
|
345
|
+
/** Collect Some values from an array, discarding Nones. `Option<T>[] → T[]` */
|
|
346
|
+
collect<TIn, TValue>(
|
|
347
|
+
this: TypedAction<TIn, Option<TValue>[]>,
|
|
348
|
+
): TypedAction<TIn, TValue[]>;
|
|
349
|
+
/** Unwrap Iterator to array. `Iterator<T> → T[]` */
|
|
350
|
+
collect<TIn, TElement>(
|
|
351
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
352
|
+
): TypedAction<TIn, TElement[]>;
|
|
353
|
+
|
|
354
|
+
/** Fallback on Err. `Result<T,E> → Result<T,F>` */
|
|
355
|
+
or<TIn, TValue, TError, TErrorOut>(
|
|
356
|
+
this: TypedAction<TIn, Result<TValue, TError>>,
|
|
357
|
+
fallback: Pipeable<TError, Result<TValue, TErrorOut>>,
|
|
358
|
+
): TypedAction<TIn, Result<TValue, TErrorOut>>;
|
|
359
|
+
|
|
360
|
+
/** Convert Ok to Some, Err to None. `Result<T,E> → Option<T>` */
|
|
361
|
+
asOkOption<TIn, TValue, TError>(
|
|
362
|
+
this: TypedAction<TIn, Result<TValue, TError>>,
|
|
363
|
+
): TypedAction<TIn, Option<TValue>>;
|
|
364
|
+
|
|
365
|
+
/** Convert Err to Some, Ok to None. `Result<T,E> → Option<E>` */
|
|
366
|
+
asErrOption<TIn, TValue, TError>(
|
|
367
|
+
this: TypedAction<TIn, Result<TValue, TError>>,
|
|
368
|
+
): TypedAction<TIn, Option<TError>>;
|
|
369
|
+
|
|
370
|
+
/** Convert boolean to Option<void>. `boolean → Option<void>` */
|
|
371
|
+
asOption<TIn>(
|
|
372
|
+
this: TypedAction<TIn, boolean>,
|
|
373
|
+
): TypedAction<TIn, Option<void>>;
|
|
374
|
+
|
|
375
|
+
/** Test if the value is Ok. `Result<T,E> → boolean` */
|
|
376
|
+
isOk<TIn, TValue, TError>(
|
|
377
|
+
this: TypedAction<TIn, Result<TValue, TError>>,
|
|
378
|
+
): TypedAction<TIn, boolean>;
|
|
379
|
+
|
|
380
|
+
/** Test if the value is Err. `Result<T,E> → boolean` */
|
|
381
|
+
isErr<TIn, TValue, TError>(
|
|
382
|
+
this: TypedAction<TIn, Result<TValue, TError>>,
|
|
383
|
+
): TypedAction<TIn, boolean>;
|
|
384
|
+
|
|
385
|
+
/** Swap nesting. `Option<Result<T,E>> → Result<Option<T>,E>` or `Result<Option<T>,E> → Option<Result<T,E>>`. */
|
|
386
|
+
transpose<TIn, TValue, TError>(
|
|
387
|
+
this: TypedAction<TIn, Option<Result<TValue, TError>>>,
|
|
388
|
+
): TypedAction<TIn, Result<Option<TValue>, TError>>;
|
|
389
|
+
transpose<TIn, TValue, TError>(
|
|
390
|
+
this: TypedAction<TIn, Result<Option<TValue>, TError>>,
|
|
391
|
+
): TypedAction<TIn, Option<Result<TValue, TError>>>;
|
|
392
|
+
|
|
393
|
+
// --- Iterator methods ---
|
|
394
|
+
|
|
395
|
+
/** Enter Iterator from Option. `Option<T> → Iterator<T>` */
|
|
396
|
+
iterate<TIn, TElement>(
|
|
397
|
+
this: TypedAction<TIn, Option<TElement>>,
|
|
398
|
+
): TypedAction<TIn, Iterator<TElement>>;
|
|
399
|
+
/** Enter Iterator from Result. `Result<T,E> → Iterator<T>` */
|
|
400
|
+
iterate<TIn, TElement, TError>(
|
|
401
|
+
this: TypedAction<TIn, Result<TElement, TError>>,
|
|
402
|
+
): TypedAction<TIn, Iterator<TElement>>;
|
|
403
|
+
/** Enter Iterator from array. `T[] → Iterator<T>` */
|
|
404
|
+
iterate<TIn, TElement>(
|
|
405
|
+
this: TypedAction<TIn, TElement[]>,
|
|
406
|
+
): TypedAction<TIn, Iterator<TElement>>;
|
|
407
|
+
|
|
408
|
+
/** Flat-map each element. `f` returns Iterator. `Iterator<T> → Iterator<U>` */
|
|
409
|
+
flatMap<TIn, TElement, TOut>(
|
|
410
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
411
|
+
action: Pipeable<TElement, Iterator<TOut>>,
|
|
412
|
+
): TypedAction<TIn, Iterator<TOut>>;
|
|
413
|
+
/** Flat-map each element. `f` returns Option. `Iterator<T> → Iterator<U>` */
|
|
414
|
+
flatMap<TIn, TElement, TOut>(
|
|
415
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
416
|
+
action: Pipeable<TElement, Option<TOut>>,
|
|
417
|
+
): TypedAction<TIn, Iterator<TOut>>;
|
|
418
|
+
/** Flat-map each element. `f` returns Result. `Iterator<T> → Iterator<U>` */
|
|
419
|
+
flatMap<TIn, TElement, TOut, TError>(
|
|
420
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
421
|
+
action: Pipeable<TElement, Result<TOut, TError>>,
|
|
422
|
+
): TypedAction<TIn, Iterator<TOut>>;
|
|
423
|
+
/** Flat-map each element. `f` returns array. `Iterator<T> → Iterator<U>` */
|
|
424
|
+
flatMap<TIn, TElement, TOut>(
|
|
425
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
426
|
+
action: Pipeable<TElement, TOut[]>,
|
|
427
|
+
): TypedAction<TIn, Iterator<TOut>>;
|
|
428
|
+
|
|
429
|
+
/** Fold elements with accumulator. `Iterator<T> → TAcc` */
|
|
430
|
+
fold<TIn, TElement, TAcc>(
|
|
431
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
432
|
+
init: Pipeable<void, TAcc>,
|
|
433
|
+
body: Pipeable<[TAcc, TElement], TAcc>,
|
|
434
|
+
): TypedAction<TIn, TAcc>;
|
|
435
|
+
|
|
436
|
+
/** Check if iterator is empty. `Iterator<T> → boolean` */
|
|
437
|
+
isEmpty<TIn, TElement>(
|
|
438
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
439
|
+
): TypedAction<TIn, boolean>;
|
|
440
|
+
|
|
441
|
+
/** Slice elements from start to end. `Iterator<T> → Iterator<T>` */
|
|
442
|
+
slice<TIn, TElement>(
|
|
443
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
444
|
+
start: number,
|
|
445
|
+
end?: number,
|
|
446
|
+
): TypedAction<TIn, Iterator<TElement>>;
|
|
447
|
+
|
|
448
|
+
/** First n elements. `Iterator<T> → Iterator<T>` */
|
|
449
|
+
take<TIn, TElement>(
|
|
450
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
451
|
+
n: number,
|
|
452
|
+
): TypedAction<TIn, Iterator<TElement>>;
|
|
453
|
+
|
|
454
|
+
/** Drop first n elements. `Iterator<T> → Iterator<T>` */
|
|
455
|
+
skip<TIn, TElement>(
|
|
456
|
+
this: TypedAction<TIn, Iterator<TElement>>,
|
|
457
|
+
n: number,
|
|
458
|
+
): TypedAction<TIn, Iterator<TElement>>;
|
|
459
|
+
|
|
460
|
+
/** Bind concurrent values as VarRefs available throughout the body. */
|
|
461
|
+
bind<TBindings extends Action[], TOut>(
|
|
462
|
+
bindings: [...TBindings],
|
|
463
|
+
body: (vars: InferVarRefs<TBindings>) => Action & { __out?: () => TOut },
|
|
464
|
+
): TypedAction<In, TOut>;
|
|
465
|
+
/** Capture the pipeline input as a VarRef. */
|
|
466
|
+
bindInput<TOut>(
|
|
467
|
+
body: (input: VarRef<Out>) => Action & { __out?: () => TOut },
|
|
468
|
+
): TypedAction<In, TOut>;
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Parameter type for pipe and combinators. Same phantom fields as TypedAction
|
|
473
|
+
* but without methods.
|
|
474
|
+
*
|
|
475
|
+
* Why no methods: TypedAction's methods (then, branch, etc.) participate in
|
|
476
|
+
* TS assignability checks in complex, recursive ways that interfere with
|
|
477
|
+
* generic inference in pipe overloads. Pipeable strips methods so that only
|
|
478
|
+
* phantom fields drive inference.
|
|
479
|
+
*
|
|
480
|
+
* TypedAction (with methods) is assignable to Pipeable because Pipeable
|
|
481
|
+
* only requires a subset of properties.
|
|
482
|
+
*/
|
|
483
|
+
export type Pipeable<In = unknown, Out = unknown> = Action & {
|
|
484
|
+
__in?: (input: In) => void;
|
|
485
|
+
__in_co?: In;
|
|
486
|
+
__out?: () => Out;
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Strip phantom types from a Pipeable, returning a plain Action.
|
|
491
|
+
*
|
|
492
|
+
* Replaces `x as Action` casts throughout the codebase. The constraint
|
|
493
|
+
* ensures the argument is structurally a Pipeable — unlike a bare cast,
|
|
494
|
+
* `toAction(123)` is a type error.
|
|
495
|
+
*/
|
|
496
|
+
export function toAction<TIn, TOut>(pipeable: Pipeable<TIn, TOut>): Action {
|
|
497
|
+
return pipeable;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Contravariant input + covariant output for branch case handler positions.
|
|
502
|
+
*
|
|
503
|
+
* Omits __in_co (covariant input) compared to Pipeable. This gives:
|
|
504
|
+
* In: contravariant only (via __in)
|
|
505
|
+
* Out: covariant only (via __out)
|
|
506
|
+
*
|
|
507
|
+
* Why contravariant input: a handler that accepts `unknown` (like drop)
|
|
508
|
+
* can handle any variant. (input: unknown) => void is assignable to
|
|
509
|
+
* (input: HasErrors) => void because HasErrors extends unknown.
|
|
510
|
+
* Pipeable's invariant input (__in_co) would reject this.
|
|
511
|
+
*
|
|
512
|
+
* TypedAction is assignable to CaseHandler because CaseHandler only
|
|
513
|
+
* requires a subset of TypedAction's phantom fields.
|
|
514
|
+
*/
|
|
515
|
+
type CaseHandler<TIn = unknown, TOut = unknown> = Action & {
|
|
516
|
+
__in?: (input: TIn) => void;
|
|
517
|
+
__out?: () => TOut;
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
// ---------------------------------------------------------------------------
|
|
521
|
+
// Tagged Union — standard { kind, value } convention with phantom __def
|
|
522
|
+
// ---------------------------------------------------------------------------
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Standard tagged union type. Each variant is `{ kind: K; value: TDef[K] }`
|
|
526
|
+
* with a phantom `__def` field carrying the full variant map. The __def
|
|
527
|
+
* field enables `.branch()` to decompose the union via simple indexing
|
|
528
|
+
* (`keyof ExtractDef<Out>` and `ExtractDef<Out>[K]`) instead of
|
|
529
|
+
* conditional types (`KindOf<Out>` and `Extract<Out, { kind: K }>`).
|
|
530
|
+
*
|
|
531
|
+
* **Void → null mapping:** Variants with `void` payload (e.g. `{ None: void }`)
|
|
532
|
+
* become `{ kind: "None"; value: null }` at runtime. This is handled by
|
|
533
|
+
* `VoidToNull` below — `void` has no runtime representation in JSON, so it
|
|
534
|
+
* serializes as `null`. Use `z.null()` in Zod schemas for void variants.
|
|
535
|
+
*/
|
|
536
|
+
// 0 extends 1 & T detects `any` — preserve as-is to avoid collapsing.
|
|
537
|
+
type VoidToNull<T> = 0 extends 1 & T
|
|
538
|
+
? T
|
|
539
|
+
: [T] extends [never]
|
|
540
|
+
? never
|
|
541
|
+
: [T] extends [void]
|
|
542
|
+
? null
|
|
543
|
+
: T;
|
|
544
|
+
|
|
545
|
+
export type TaggedUnion<
|
|
546
|
+
TEnumName extends string,
|
|
547
|
+
TDef extends Record<string, unknown>,
|
|
548
|
+
> = {
|
|
549
|
+
[K in keyof TDef & string]: {
|
|
550
|
+
kind: `${TEnumName}.${K}`;
|
|
551
|
+
value: VoidToNull<TDef[K]>;
|
|
552
|
+
__def?: TDef;
|
|
553
|
+
};
|
|
554
|
+
}[keyof TDef & string];
|
|
555
|
+
|
|
556
|
+
/** Extract the variant map definition from a tagged union's phantom __def. */
|
|
557
|
+
export type ExtractDef<T> = T extends { __def?: infer D } ? D : never;
|
|
558
|
+
|
|
559
|
+
// ---------------------------------------------------------------------------
|
|
560
|
+
// Option<T> — standard optional value type
|
|
561
|
+
// ---------------------------------------------------------------------------
|
|
562
|
+
|
|
563
|
+
export type OptionDef<T> = { Some: T; None: void };
|
|
564
|
+
export type Option<T> = TaggedUnion<"Option", OptionDef<T>>;
|
|
565
|
+
|
|
566
|
+
// ---------------------------------------------------------------------------
|
|
567
|
+
// Result<TValue, TError> — standard success/error type
|
|
568
|
+
// ---------------------------------------------------------------------------
|
|
569
|
+
|
|
570
|
+
export type ResultDef<TValue, TError> = { Ok: TValue; Err: TError };
|
|
571
|
+
export type Result<TValue, TError> = TaggedUnion<
|
|
572
|
+
"Result",
|
|
573
|
+
ResultDef<TValue, TError>
|
|
574
|
+
>;
|
|
575
|
+
|
|
576
|
+
// ---------------------------------------------------------------------------
|
|
577
|
+
// Iterator<T> — sequence wrapper (single-variant TaggedUnion)
|
|
578
|
+
// ---------------------------------------------------------------------------
|
|
579
|
+
|
|
580
|
+
export type IteratorDef<TElement> = { Iterator: TElement[] };
|
|
581
|
+
export type Iterator<TElement> = TaggedUnion<"Iterator", IteratorDef<TElement>>;
|
|
582
|
+
|
|
583
|
+
/** Extract all `kind` string literals from a discriminated union. */
|
|
584
|
+
type KindOf<T> = T extends { kind: infer K extends string } ? K : never;
|
|
585
|
+
|
|
586
|
+
/** Strip a `"Prefix."` namespace from a dotted kind string. `"Nat.Zero"` → `"Zero"`. */
|
|
587
|
+
type StripKindPrefix<K extends string> = K extends `${string}.${infer Bare}`
|
|
588
|
+
? Bare
|
|
589
|
+
: K;
|
|
590
|
+
|
|
591
|
+
/** Extract the `value` field from a `{ kind, value }` variant. Falls back to T if no `value` field. */
|
|
592
|
+
type UnwrapVariant<T> = T extends { value: infer V } ? V : T;
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Branch case keys: prefer ExtractDef (simple keyof indexing) when the
|
|
596
|
+
* output carries __def. Falls back to KindOf with prefix stripping for
|
|
597
|
+
* outputs without __def (namespaced kinds like "Nat.Zero" → "Zero").
|
|
598
|
+
*/
|
|
599
|
+
type BranchKeys<Out> = [ExtractDef<Out>] extends [never]
|
|
600
|
+
? StripKindPrefix<KindOf<Out>>
|
|
601
|
+
: keyof ExtractDef<Out> & string;
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Branch case payload: prefer ExtractDef[K] (simple indexing) when available.
|
|
605
|
+
* Falls back to UnwrapVariant<Extract<Out, { kind: ... }>> for outputs without __def.
|
|
606
|
+
* In the fallback, matches namespaced kinds (`"Prefix.K"`) against the bare key K.
|
|
607
|
+
*/
|
|
608
|
+
type BranchPayload<Out, K extends string> = [ExtractDef<Out>] extends [never]
|
|
609
|
+
? UnwrapVariant<Extract<Out, { kind: K } | { kind: `${string}.${K}` }>>
|
|
610
|
+
: K extends keyof ExtractDef<Out>
|
|
611
|
+
? VoidToNull<ExtractDef<Out>[K]>
|
|
612
|
+
: never;
|
|
613
|
+
|
|
614
|
+
// ---------------------------------------------------------------------------
|
|
615
|
+
// typedAction — attach .then() and .forEach() as non-enumerable methods
|
|
616
|
+
// ---------------------------------------------------------------------------
|
|
617
|
+
|
|
618
|
+
// Shared implementations (one closure, not per-instance)
|
|
619
|
+
function thenMethod<TIn, TOut, TNext>(
|
|
620
|
+
this: TypedAction<TIn, TOut>,
|
|
621
|
+
next: Pipeable<TOut, TNext>,
|
|
622
|
+
): TypedAction<TIn, TNext> {
|
|
623
|
+
return chain(this, next);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function branchMethod(
|
|
627
|
+
this: TypedAction,
|
|
628
|
+
cases: Record<string, Action>,
|
|
629
|
+
): TypedAction {
|
|
630
|
+
return chain(toAction(this), toAction(branch(cases)));
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function flattenMethod(this: TypedAction): TypedAction {
|
|
634
|
+
return chain(toAction(this), toAction(flattenBuiltin()));
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function dropMethod(this: TypedAction): TypedAction {
|
|
638
|
+
return chain(toAction(this), toAction(drop));
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
function tagMethod(
|
|
642
|
+
this: TypedAction,
|
|
643
|
+
kind: string,
|
|
644
|
+
enumName: string,
|
|
645
|
+
): TypedAction {
|
|
646
|
+
return chain(toAction(this), toAction(tag(kind, enumName)));
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function someMethod(this: TypedAction): TypedAction {
|
|
650
|
+
return chain(toAction(this), toAction(Option.some()));
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
function okMethod(this: TypedAction): TypedAction {
|
|
654
|
+
return chain(toAction(this), toAction(Result.ok()));
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function errMethod(this: TypedAction): TypedAction {
|
|
658
|
+
return chain(toAction(this), toAction(Result.err()));
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function getFieldMethod(this: TypedAction, field: string): TypedAction {
|
|
662
|
+
return chain(toAction(this), toAction(getField(field)));
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
function getIndexMethod(this: TypedAction, index: number): TypedAction {
|
|
666
|
+
return chain(toAction(this), toAction(getIndex(index)));
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
function wrapInFieldMethod(this: TypedAction, field: string): TypedAction {
|
|
670
|
+
return chain(toAction(this), toAction(wrapInField(field)));
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
function pickMethod(this: TypedAction, ...keys: string[]): TypedAction {
|
|
674
|
+
return chain(toAction(this), toAction(pick(...keys)));
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
function splitFirstMethod(this: TypedAction): TypedAction {
|
|
678
|
+
return chain(
|
|
679
|
+
toAction(this),
|
|
680
|
+
toAction(
|
|
681
|
+
branchFamily({
|
|
682
|
+
Iterator: IteratorNs.splitFirst(),
|
|
683
|
+
Array: splitFirst(),
|
|
684
|
+
}),
|
|
685
|
+
),
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
function splitLastMethod(this: TypedAction): TypedAction {
|
|
690
|
+
return chain(
|
|
691
|
+
toAction(this),
|
|
692
|
+
toAction(
|
|
693
|
+
branchFamily({
|
|
694
|
+
Iterator: IteratorNs.splitLast(),
|
|
695
|
+
Array: splitLast(),
|
|
696
|
+
}),
|
|
697
|
+
),
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// --- Shared postfix methods (Option + Result) — dispatch via branchFamily ---
|
|
702
|
+
|
|
703
|
+
function mapMethod(this: TypedAction, action: Action): TypedAction {
|
|
704
|
+
return chain(
|
|
705
|
+
toAction(this),
|
|
706
|
+
toAction(
|
|
707
|
+
branchFamily({
|
|
708
|
+
Result: branch({
|
|
709
|
+
Ok: chain(toAction(action), toAction(Result.ok())),
|
|
710
|
+
Err: Result.err(),
|
|
711
|
+
}),
|
|
712
|
+
Option: branch({
|
|
713
|
+
Some: chain(toAction(action), toAction(Option.some())),
|
|
714
|
+
None: Option.none(),
|
|
715
|
+
}),
|
|
716
|
+
Iterator: IteratorNs.map(action),
|
|
717
|
+
}),
|
|
718
|
+
),
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function unwrapMethod(this: TypedAction): TypedAction {
|
|
723
|
+
return chain(
|
|
724
|
+
toAction(this),
|
|
725
|
+
toAction(
|
|
726
|
+
branchFamily({
|
|
727
|
+
Result: branch({ Ok: identity(), Err: panic("called unwrap on Err") }),
|
|
728
|
+
Option: branch({
|
|
729
|
+
Some: identity(),
|
|
730
|
+
None: panic("called unwrap on None"),
|
|
731
|
+
}),
|
|
732
|
+
}),
|
|
733
|
+
),
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function unwrapOrMethod(this: TypedAction, defaultAction: Action): TypedAction {
|
|
738
|
+
return chain(
|
|
739
|
+
toAction(this),
|
|
740
|
+
toAction(
|
|
741
|
+
branchFamily({
|
|
742
|
+
Result: branch({ Ok: identity(), Err: defaultAction }),
|
|
743
|
+
Option: branch({ Some: identity(), None: defaultAction }),
|
|
744
|
+
}),
|
|
745
|
+
),
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
function andThenMethod(this: TypedAction, action: Action): TypedAction {
|
|
750
|
+
return chain(
|
|
751
|
+
toAction(this),
|
|
752
|
+
toAction(
|
|
753
|
+
branchFamily({
|
|
754
|
+
Result: branch({ Ok: action, Err: Result.err() }),
|
|
755
|
+
Option: branch({ Some: action, None: Option.none() }),
|
|
756
|
+
}),
|
|
757
|
+
),
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
function transposeMethod(this: TypedAction): TypedAction {
|
|
762
|
+
return chain(
|
|
763
|
+
toAction(this),
|
|
764
|
+
toAction(
|
|
765
|
+
branchFamily({
|
|
766
|
+
Option: branch({
|
|
767
|
+
Some: branch({
|
|
768
|
+
Ok: chain(toAction(Option.some()), toAction(Result.ok())),
|
|
769
|
+
Err: Result.err(),
|
|
770
|
+
}),
|
|
771
|
+
None: chain(
|
|
772
|
+
toAction(chain(toAction(drop), toAction(Option.none()))),
|
|
773
|
+
toAction(Result.ok()),
|
|
774
|
+
),
|
|
775
|
+
}),
|
|
776
|
+
Result: branch({
|
|
777
|
+
Ok: branch({
|
|
778
|
+
Some: chain(toAction(Result.ok()), toAction(Option.some())),
|
|
779
|
+
None: chain(toAction(drop), toAction(Option.none())),
|
|
780
|
+
}),
|
|
781
|
+
Err: chain(toAction(Result.err()), toAction(Option.some())),
|
|
782
|
+
}),
|
|
783
|
+
}),
|
|
784
|
+
),
|
|
785
|
+
);
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// --- Result-only postfix methods ---
|
|
789
|
+
|
|
790
|
+
function mapErrMethod(this: TypedAction, action: Action): TypedAction {
|
|
791
|
+
return chain(
|
|
792
|
+
toAction(this),
|
|
793
|
+
toAction(
|
|
794
|
+
branch({
|
|
795
|
+
Ok: Result.ok(),
|
|
796
|
+
Err: chain(toAction(action), toAction(Result.err())),
|
|
797
|
+
}),
|
|
798
|
+
),
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
function orMethod(this: TypedAction, fallback: Action): TypedAction {
|
|
803
|
+
return chain(
|
|
804
|
+
toAction(this),
|
|
805
|
+
toAction(
|
|
806
|
+
branch({
|
|
807
|
+
Ok: Result.ok(),
|
|
808
|
+
Err: fallback,
|
|
809
|
+
}),
|
|
810
|
+
),
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
function asOkOptionMethod(this: TypedAction): TypedAction {
|
|
815
|
+
return chain(
|
|
816
|
+
toAction(this),
|
|
817
|
+
toAction(
|
|
818
|
+
branch({
|
|
819
|
+
Ok: Option.some(),
|
|
820
|
+
Err: chain(toAction(drop), toAction(Option.none())),
|
|
821
|
+
}),
|
|
822
|
+
),
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
function asErrOptionMethod(this: TypedAction): TypedAction {
|
|
827
|
+
return chain(
|
|
828
|
+
toAction(this),
|
|
829
|
+
toAction(
|
|
830
|
+
branch({
|
|
831
|
+
Ok: chain(toAction(drop), toAction(Option.none())),
|
|
832
|
+
Err: Option.some(),
|
|
833
|
+
}),
|
|
834
|
+
),
|
|
835
|
+
);
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
function isOkMethod(this: TypedAction): TypedAction {
|
|
839
|
+
return chain(
|
|
840
|
+
toAction(this),
|
|
841
|
+
toAction(
|
|
842
|
+
branch({
|
|
843
|
+
Ok: constant(true),
|
|
844
|
+
Err: constant(false),
|
|
845
|
+
}),
|
|
846
|
+
),
|
|
847
|
+
);
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
function isErrMethod(this: TypedAction): TypedAction {
|
|
851
|
+
return chain(
|
|
852
|
+
toAction(this),
|
|
853
|
+
toAction(
|
|
854
|
+
branch({
|
|
855
|
+
Ok: constant(false),
|
|
856
|
+
Err: constant(true),
|
|
857
|
+
}),
|
|
858
|
+
),
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// --- Option-only postfix methods ---
|
|
863
|
+
|
|
864
|
+
function filterMethod(this: TypedAction, predicate: Action): TypedAction {
|
|
865
|
+
return chain(
|
|
866
|
+
toAction(this),
|
|
867
|
+
toAction(
|
|
868
|
+
branchFamily({
|
|
869
|
+
Option: branch({
|
|
870
|
+
Some: predicate,
|
|
871
|
+
None: Option.none(),
|
|
872
|
+
}),
|
|
873
|
+
Iterator: IteratorNs.filter(predicate),
|
|
874
|
+
}),
|
|
875
|
+
),
|
|
876
|
+
);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
function isSomeMethod(this: TypedAction): TypedAction {
|
|
880
|
+
return chain(
|
|
881
|
+
toAction(this),
|
|
882
|
+
toAction(
|
|
883
|
+
branch({
|
|
884
|
+
Some: constant(true),
|
|
885
|
+
None: constant(false),
|
|
886
|
+
}),
|
|
887
|
+
),
|
|
888
|
+
);
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
function isNoneMethod(this: TypedAction): TypedAction {
|
|
892
|
+
return chain(
|
|
893
|
+
toAction(this),
|
|
894
|
+
toAction(
|
|
895
|
+
branch({
|
|
896
|
+
Some: constant(false),
|
|
897
|
+
None: constant(true),
|
|
898
|
+
}),
|
|
899
|
+
),
|
|
900
|
+
);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
function asOptionMethod(this: TypedAction): TypedAction {
|
|
904
|
+
return chain(toAction(this), toAction(asOptionStandalone()));
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
// --- Iterator postfix methods ---
|
|
908
|
+
|
|
909
|
+
function iterateMethod(this: TypedAction): TypedAction {
|
|
910
|
+
return chain(
|
|
911
|
+
toAction(this),
|
|
912
|
+
toAction(
|
|
913
|
+
branchFamily({
|
|
914
|
+
Option: IteratorNs.fromOption(),
|
|
915
|
+
Result: IteratorNs.fromResult(),
|
|
916
|
+
Array: IteratorNs.fromArray(),
|
|
917
|
+
}),
|
|
918
|
+
),
|
|
919
|
+
);
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
function flatMapMethod(this: TypedAction, action: Action): TypedAction {
|
|
923
|
+
return chain(toAction(this), toAction(IteratorNs.flatMap(action)));
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
function collectMethod(this: TypedAction): TypedAction {
|
|
927
|
+
return chain(
|
|
928
|
+
toAction(this),
|
|
929
|
+
toAction(
|
|
930
|
+
branchFamily({
|
|
931
|
+
Array: Option.collect(),
|
|
932
|
+
Iterator: IteratorNs.collect(),
|
|
933
|
+
}),
|
|
934
|
+
),
|
|
935
|
+
);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
function foldMethod(
|
|
939
|
+
this: TypedAction,
|
|
940
|
+
init: Action,
|
|
941
|
+
body: Action,
|
|
942
|
+
): TypedAction {
|
|
943
|
+
return chain(toAction(this), toAction(IteratorNs.fold(init, body)));
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
function isEmptyMethod(this: TypedAction): TypedAction {
|
|
947
|
+
return chain(toAction(this), toAction(IteratorNs.isEmpty()));
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
function sliceMethod(
|
|
951
|
+
this: TypedAction,
|
|
952
|
+
start: number,
|
|
953
|
+
end?: number,
|
|
954
|
+
): TypedAction {
|
|
955
|
+
return chain(toAction(this), toAction(IteratorNs.slice(start, end)));
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
function takeMethod(this: TypedAction, n: number): TypedAction {
|
|
959
|
+
return chain(toAction(this), toAction(IteratorNs.take(n)));
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
function skipMethod(this: TypedAction, n: number): TypedAction {
|
|
963
|
+
return chain(toAction(this), toAction(IteratorNs.skip(n)));
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
function bindMethod(
|
|
967
|
+
this: TypedAction,
|
|
968
|
+
bindings: Action[],
|
|
969
|
+
body: (vars: any) => Action,
|
|
970
|
+
): TypedAction {
|
|
971
|
+
return chain(toAction(this), toAction(bindStandalone(bindings, body)));
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
function bindInputMethod(
|
|
975
|
+
this: TypedAction,
|
|
976
|
+
body: (input: any) => Action,
|
|
977
|
+
): TypedAction {
|
|
978
|
+
return chain(toAction(this), toAction(bindInputStandalone(body)));
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
/**
|
|
982
|
+
* Attach `.then()` and `.forEach()` methods to a plain Action object.
|
|
983
|
+
* Methods are non-enumerable: invisible to JSON.stringify and toEqual.
|
|
984
|
+
*/
|
|
985
|
+
export function typedAction<In = unknown, Out = unknown>(
|
|
986
|
+
action: Action,
|
|
987
|
+
): TypedAction<In, Out> {
|
|
988
|
+
if (!("then" in action)) {
|
|
989
|
+
Object.defineProperties(action, {
|
|
990
|
+
then: { value: thenMethod, configurable: true },
|
|
991
|
+
|
|
992
|
+
branch: { value: branchMethod, configurable: true },
|
|
993
|
+
flatten: { value: flattenMethod, configurable: true },
|
|
994
|
+
drop: { value: dropMethod, configurable: true },
|
|
995
|
+
tag: { value: tagMethod, configurable: true },
|
|
996
|
+
some: { value: someMethod, configurable: true },
|
|
997
|
+
ok: { value: okMethod, configurable: true },
|
|
998
|
+
err: { value: errMethod, configurable: true },
|
|
999
|
+
getField: { value: getFieldMethod, configurable: true },
|
|
1000
|
+
getIndex: { value: getIndexMethod, configurable: true },
|
|
1001
|
+
wrapInField: { value: wrapInFieldMethod, configurable: true },
|
|
1002
|
+
|
|
1003
|
+
pick: { value: pickMethod, configurable: true },
|
|
1004
|
+
splitFirst: { value: splitFirstMethod, configurable: true },
|
|
1005
|
+
splitLast: { value: splitLastMethod, configurable: true },
|
|
1006
|
+
map: { value: mapMethod, configurable: true },
|
|
1007
|
+
mapErr: { value: mapErrMethod, configurable: true },
|
|
1008
|
+
unwrap: { value: unwrapMethod, configurable: true },
|
|
1009
|
+
unwrapOr: { value: unwrapOrMethod, configurable: true },
|
|
1010
|
+
andThen: { value: andThenMethod, configurable: true },
|
|
1011
|
+
filter: { value: filterMethod, configurable: true },
|
|
1012
|
+
isSome: { value: isSomeMethod, configurable: true },
|
|
1013
|
+
isNone: { value: isNoneMethod, configurable: true },
|
|
1014
|
+
asOption: { value: asOptionMethod, configurable: true },
|
|
1015
|
+
collect: { value: collectMethod, configurable: true },
|
|
1016
|
+
fold: { value: foldMethod, configurable: true },
|
|
1017
|
+
isEmpty: { value: isEmptyMethod, configurable: true },
|
|
1018
|
+
slice: { value: sliceMethod, configurable: true },
|
|
1019
|
+
take: { value: takeMethod, configurable: true },
|
|
1020
|
+
skip: { value: skipMethod, configurable: true },
|
|
1021
|
+
or: { value: orMethod, configurable: true },
|
|
1022
|
+
iterate: { value: iterateMethod, configurable: true },
|
|
1023
|
+
flatMap: { value: flatMapMethod, configurable: true },
|
|
1024
|
+
|
|
1025
|
+
asOkOption: { value: asOkOptionMethod, configurable: true },
|
|
1026
|
+
asErrOption: { value: asErrOptionMethod, configurable: true },
|
|
1027
|
+
isOk: { value: isOkMethod, configurable: true },
|
|
1028
|
+
isErr: { value: isErrMethod, configurable: true },
|
|
1029
|
+
transpose: { value: transposeMethod, configurable: true },
|
|
1030
|
+
bind: { value: bindMethod, configurable: true },
|
|
1031
|
+
bindInput: { value: bindInputMethod, configurable: true },
|
|
1032
|
+
});
|
|
1033
|
+
}
|
|
1034
|
+
return action as TypedAction<In, Out>;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
// ---------------------------------------------------------------------------
|
|
1038
|
+
// Type extraction utilities
|
|
1039
|
+
// ---------------------------------------------------------------------------
|
|
1040
|
+
|
|
1041
|
+
/**
|
|
1042
|
+
* Extract the input type from a TypedAction.
|
|
1043
|
+
*
|
|
1044
|
+
* Uses direct phantom field extraction (not full TypedAction matching) to
|
|
1045
|
+
* avoid a full `TypedAction<any, any>` constraint which fails for In=never
|
|
1046
|
+
* due to __in contravariance.
|
|
1047
|
+
*/
|
|
1048
|
+
export type ExtractInput<T> = T extends {
|
|
1049
|
+
__in?: (input: infer In) => void;
|
|
1050
|
+
}
|
|
1051
|
+
? In
|
|
1052
|
+
: never;
|
|
1053
|
+
|
|
1054
|
+
/**
|
|
1055
|
+
* Extract the output type from a TypedAction.
|
|
1056
|
+
*
|
|
1057
|
+
* Uses direct phantom field extraction to avoid constraint issues.
|
|
1058
|
+
*/
|
|
1059
|
+
export type ExtractOutput<T> = T extends { __out?: () => infer Out }
|
|
1060
|
+
? Out
|
|
1061
|
+
: never;
|
|
1062
|
+
|
|
1063
|
+
// ---------------------------------------------------------------------------
|
|
1064
|
+
// Combinators
|
|
1065
|
+
// ---------------------------------------------------------------------------
|
|
1066
|
+
|
|
1067
|
+
export { pipe } from "./pipe.js";
|
|
1068
|
+
export { chain } from "./chain.js";
|
|
1069
|
+
export { all } from "./all.js";
|
|
1070
|
+
export { bind, bindInput, type VarRef, type InferVarRefs } from "./bind.js";
|
|
1071
|
+
export { defineRecursiveFunctions } from "./recursive.js";
|
|
1072
|
+
export { resetEffectIdCounter } from "./effect-id.js";
|
|
1073
|
+
import {
|
|
1074
|
+
allocateRestartHandlerId,
|
|
1075
|
+
type RestartHandlerId,
|
|
1076
|
+
type ResumeHandlerId,
|
|
1077
|
+
} from "./effect-id.js";
|
|
1078
|
+
export { tryCatch } from "./try-catch.js";
|
|
1079
|
+
export { race, sleep, withTimeout } from "./race.js";
|
|
1080
|
+
|
|
1081
|
+
export function forEach<In, Out>(
|
|
1082
|
+
action: Pipeable<In, Out>,
|
|
1083
|
+
): TypedAction<In[], Out[]> {
|
|
1084
|
+
return typedAction({ kind: "ForEach", action: toAction(action) });
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
/**
|
|
1088
|
+
* Insert GetField("value") before each case handler in a branch.
|
|
1089
|
+
* This implements auto-unwrapping: the engine dispatches on `kind`, then
|
|
1090
|
+
* extracts `value` before passing to the handler. Case handlers receive
|
|
1091
|
+
* the payload directly, not the full `{ kind, value }` variant.
|
|
1092
|
+
*/
|
|
1093
|
+
function unwrapBranchCases(
|
|
1094
|
+
cases: Record<string, Action>,
|
|
1095
|
+
): Record<string, Action> {
|
|
1096
|
+
const unwrapped: Record<string, Action> = {};
|
|
1097
|
+
for (const key of Object.keys(cases)) {
|
|
1098
|
+
unwrapped[key] = toAction(
|
|
1099
|
+
chain(toAction(getField("value")), toAction(cases[key])),
|
|
1100
|
+
);
|
|
1101
|
+
}
|
|
1102
|
+
return unwrapped;
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
/**
|
|
1106
|
+
* Compute the branch input type from its cases. For each case key K,
|
|
1107
|
+
* wraps the case handler's input type in `{ kind: K; value: T }`.
|
|
1108
|
+
* This ensures the branch input is a proper tagged union matching the
|
|
1109
|
+
* `{ kind, value }` convention.
|
|
1110
|
+
*
|
|
1111
|
+
* Example: `BranchInput<{ Yes: TypedAction<number, ...>, No: TypedAction<string, ...> }>`
|
|
1112
|
+
* = `{ kind: "Yes"; value: number } | { kind: "No"; value: string }`
|
|
1113
|
+
*
|
|
1114
|
+
* When a case handler uses `any` as input, the wrapping produces
|
|
1115
|
+
* `{ kind: K; value: any }`, which is the correct escape hatch.
|
|
1116
|
+
*/
|
|
1117
|
+
export type BranchInput<TCases> = {
|
|
1118
|
+
[K in keyof TCases & string]: { kind: K; value: ExtractInput<TCases[K]> };
|
|
1119
|
+
}[keyof TCases & string];
|
|
1120
|
+
|
|
1121
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1122
|
+
export function branch<TCases extends Record<string, Action>>(
|
|
1123
|
+
cases: TCases,
|
|
1124
|
+
): TypedAction<
|
|
1125
|
+
BranchInput<TCases>,
|
|
1126
|
+
ExtractOutput<TCases[keyof TCases & string]>
|
|
1127
|
+
> {
|
|
1128
|
+
return typedAction({ kind: "Branch", cases: unwrapBranchCases(cases) });
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
/**
|
|
1132
|
+
* Two-level dispatch: extract the enum prefix from a tagged value's `kind`,
|
|
1133
|
+
* then branch on that prefix. Used by postfix methods (`.map()`, `.unwrapOr()`,
|
|
1134
|
+
* etc.) to dispatch across union families (Option, Result) without runtime
|
|
1135
|
+
* metadata.
|
|
1136
|
+
*
|
|
1137
|
+
* `branchFamily({ Result: ..., Option: ... })` ≡ `chain(extractPrefix(), branch(cases))`
|
|
1138
|
+
*/
|
|
1139
|
+
export function branchFamily(cases: Record<string, Action>): TypedAction {
|
|
1140
|
+
return typedAction({
|
|
1141
|
+
kind: "Chain",
|
|
1142
|
+
first: toAction(extractPrefix()),
|
|
1143
|
+
rest: toAction(branch(cases)),
|
|
1144
|
+
});
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
type LoopResultDef<TContinue, TBreak> = {
|
|
1148
|
+
Continue: TContinue;
|
|
1149
|
+
Break: TBreak;
|
|
1150
|
+
};
|
|
1151
|
+
|
|
1152
|
+
export type LoopResult<TContinue, TBreak> = TaggedUnion<
|
|
1153
|
+
"LoopResult",
|
|
1154
|
+
LoopResultDef<TContinue, TBreak>
|
|
1155
|
+
>;
|
|
1156
|
+
|
|
1157
|
+
// ---------------------------------------------------------------------------
|
|
1158
|
+
// recur — restart body primitive
|
|
1159
|
+
// ---------------------------------------------------------------------------
|
|
1160
|
+
|
|
1161
|
+
/**
|
|
1162
|
+
* Restartable scope. The body callback receives `restart`, a TypedAction that
|
|
1163
|
+
* re-executes the body from the beginning with a new input value.
|
|
1164
|
+
*
|
|
1165
|
+
* If the body completes normally → output is TOut.
|
|
1166
|
+
* If restart fires → body re-executes with the restarted value.
|
|
1167
|
+
*
|
|
1168
|
+
* Compiled form: `RestartHandle(id, GetIndex(0), body)`
|
|
1169
|
+
*/
|
|
1170
|
+
export function recur<TIn = void, TOut = any>(
|
|
1171
|
+
bodyFn: (restart: TypedAction<TIn, never>) => Pipeable<TIn, TOut>,
|
|
1172
|
+
): TypedAction<PipeIn<TIn>, TOut> {
|
|
1173
|
+
const restartHandlerId = allocateRestartHandlerId();
|
|
1174
|
+
|
|
1175
|
+
const restartAction = typedAction<TIn, never>({
|
|
1176
|
+
kind: "RestartPerform",
|
|
1177
|
+
restart_handler_id: restartHandlerId,
|
|
1178
|
+
});
|
|
1179
|
+
|
|
1180
|
+
const body = toAction(bodyFn(restartAction));
|
|
1181
|
+
|
|
1182
|
+
return typedAction({
|
|
1183
|
+
kind: "RestartHandle",
|
|
1184
|
+
restart_handler_id: restartHandlerId,
|
|
1185
|
+
body,
|
|
1186
|
+
handler: toAction(getIndex(0).unwrap()),
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
// ---------------------------------------------------------------------------
|
|
1191
|
+
// earlyReturn — exit scope primitive (built on restart + Branch)
|
|
1192
|
+
// ---------------------------------------------------------------------------
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* Early return scope. The body callback receives `earlyReturn`, a TypedAction
|
|
1196
|
+
* that exits the scope immediately with the returned value.
|
|
1197
|
+
*
|
|
1198
|
+
* If the body completes normally → output is TOut.
|
|
1199
|
+
* If earlyReturn fires → output is TEarlyReturn.
|
|
1200
|
+
* Combined output: TEarlyReturn | TOut.
|
|
1201
|
+
*
|
|
1202
|
+
* Built on the restart mechanism: input is tagged Continue, body runs inside
|
|
1203
|
+
* a Branch. earlyReturn tags with Break and performs — the handler restarts
|
|
1204
|
+
* the body, Branch takes the Break path, and the value exits.
|
|
1205
|
+
*/
|
|
1206
|
+
export function earlyReturn<TEarlyReturn = void, TIn = any, TOut = any>(
|
|
1207
|
+
bodyFn: (
|
|
1208
|
+
earlyReturn: TypedAction<TEarlyReturn, never>,
|
|
1209
|
+
) => Pipeable<TIn, TOut>,
|
|
1210
|
+
): TypedAction<TIn, TEarlyReturn | TOut> {
|
|
1211
|
+
const restartHandlerId = allocateRestartHandlerId();
|
|
1212
|
+
|
|
1213
|
+
const earlyReturnAction = typedAction<TEarlyReturn, never>(
|
|
1214
|
+
toAction(
|
|
1215
|
+
chain(toAction(tag("Break", "LoopResult")), {
|
|
1216
|
+
kind: "RestartPerform",
|
|
1217
|
+
restart_handler_id: restartHandlerId,
|
|
1218
|
+
}),
|
|
1219
|
+
),
|
|
1220
|
+
);
|
|
1221
|
+
|
|
1222
|
+
const body = toAction(bodyFn(earlyReturnAction));
|
|
1223
|
+
|
|
1224
|
+
return typedAction(
|
|
1225
|
+
buildRestartBranchAction(restartHandlerId, body, toAction(identity())),
|
|
1226
|
+
);
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
// ---------------------------------------------------------------------------
|
|
1230
|
+
// loop — iterative restart with break
|
|
1231
|
+
// ---------------------------------------------------------------------------
|
|
1232
|
+
|
|
1233
|
+
/**
|
|
1234
|
+
* Build the restart+branch compiled form:
|
|
1235
|
+
* `Chain(Tag("Continue"), RestartHandle(id, GetIndex(0), Branch({ Continue: continueArm, Break: breakArm })))`
|
|
1236
|
+
*
|
|
1237
|
+
* Input is tagged Continue so the Branch enters the continueArm on first execution.
|
|
1238
|
+
* Continue tag → restart → re-enters continueArm. Break tag → restart → runs breakArm, exits `RestartHandle`.
|
|
1239
|
+
*
|
|
1240
|
+
* Used by earlyReturn, loop, tryCatch, and race.
|
|
1241
|
+
*/
|
|
1242
|
+
export function buildRestartBranchAction(
|
|
1243
|
+
restartHandlerId: RestartHandlerId,
|
|
1244
|
+
continueArm: Action,
|
|
1245
|
+
breakArm: Action,
|
|
1246
|
+
): Action {
|
|
1247
|
+
return toAction(
|
|
1248
|
+
chain(toAction(tag("Continue", "LoopResult")), {
|
|
1249
|
+
kind: "RestartHandle",
|
|
1250
|
+
restart_handler_id: restartHandlerId,
|
|
1251
|
+
body: toAction(branch({ Continue: continueArm, Break: breakArm })),
|
|
1252
|
+
handler: toAction(getIndex(0).unwrap()),
|
|
1253
|
+
}),
|
|
1254
|
+
);
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
/**
|
|
1258
|
+
* Iterative loop. The body callback receives `recur` and `done`:
|
|
1259
|
+
* - `recur`: restart the loop with a new input
|
|
1260
|
+
* - `done`: exit the loop with the break value
|
|
1261
|
+
*
|
|
1262
|
+
* Both are TypedAction values (not functions), consistent with throwError in tryCatch.
|
|
1263
|
+
*
|
|
1264
|
+
* Compiles to `RestartHandle`/`RestartPerform`/Branch — same effect substrate as tryCatch and earlyReturn.
|
|
1265
|
+
*/
|
|
1266
|
+
export function loop<TBreak = void, TRecur = void>(
|
|
1267
|
+
bodyFn: (
|
|
1268
|
+
recur: TypedAction<TRecur, never>,
|
|
1269
|
+
done: TypedAction<VoidToNull<TBreak>, never>,
|
|
1270
|
+
) => Pipeable<TRecur, never>,
|
|
1271
|
+
): TypedAction<PipeIn<TRecur>, VoidToNull<TBreak>> {
|
|
1272
|
+
const restartHandlerId = allocateRestartHandlerId();
|
|
1273
|
+
|
|
1274
|
+
const perform: Action = {
|
|
1275
|
+
kind: "RestartPerform",
|
|
1276
|
+
restart_handler_id: restartHandlerId,
|
|
1277
|
+
};
|
|
1278
|
+
|
|
1279
|
+
const recurAction = typedAction<TRecur, never>(
|
|
1280
|
+
toAction(chain(toAction(tag("Continue", "LoopResult")), toAction(perform))),
|
|
1281
|
+
);
|
|
1282
|
+
|
|
1283
|
+
const doneAction = typedAction<VoidToNull<TBreak>, never>(
|
|
1284
|
+
toAction(chain(toAction(tag("Break", "LoopResult")), toAction(perform))),
|
|
1285
|
+
);
|
|
1286
|
+
|
|
1287
|
+
const body = toAction(bodyFn(recurAction, doneAction));
|
|
1288
|
+
|
|
1289
|
+
return typedAction(
|
|
1290
|
+
buildRestartBranchAction(restartHandlerId, body, toAction(identity())),
|
|
1291
|
+
);
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
// ---------------------------------------------------------------------------
|
|
1295
|
+
// Config builders
|
|
1296
|
+
// ---------------------------------------------------------------------------
|
|
1297
|
+
|
|
1298
|
+
/** Simple config factory. */
|
|
1299
|
+
export function config(workflow: Action): Config {
|
|
1300
|
+
return { workflow };
|
|
1301
|
+
}
|