@smthrs/plan 0.0.0-stage → 1.0.0-rc.13
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/CHANGELOG.md +189 -0
- package/LICENSE +21 -0
- package/README.md +230 -2
- package/THIRD_PARTY_NOTICES.md +48 -0
- package/dist/cjs/CachePolicy.d.ts +131 -0
- package/dist/cjs/CachePolicy.d.ts.map +1 -0
- package/dist/cjs/CachePolicy.js +50 -0
- package/dist/cjs/CachePolicy.js.map +7 -0
- package/dist/cjs/Effects.d.ts +258 -0
- package/dist/cjs/Effects.d.ts.map +1 -0
- package/dist/cjs/Effects.js +76 -0
- package/dist/cjs/Effects.js.map +7 -0
- package/dist/cjs/FileSet.d.ts +366 -0
- package/dist/cjs/FileSet.d.ts.map +1 -0
- package/dist/cjs/FileSet.js +196 -0
- package/dist/cjs/FileSet.js.map +7 -0
- package/dist/cjs/GraphBuildError.d.ts +120 -0
- package/dist/cjs/GraphBuildError.d.ts.map +1 -0
- package/dist/cjs/GraphBuildError.js +71 -0
- package/dist/cjs/GraphBuildError.js.map +7 -0
- package/dist/cjs/KeyMaterial.d.ts +107 -0
- package/dist/cjs/KeyMaterial.d.ts.map +1 -0
- package/dist/cjs/KeyMaterial.js +64 -0
- package/dist/cjs/KeyMaterial.js.map +7 -0
- package/dist/cjs/Node.d.ts +685 -0
- package/dist/cjs/Node.d.ts.map +1 -0
- package/dist/cjs/Node.js +230 -0
- package/dist/cjs/Node.js.map +7 -0
- package/dist/cjs/Placement.d.ts +117 -0
- package/dist/cjs/Placement.d.ts.map +1 -0
- package/dist/cjs/Placement.js +46 -0
- package/dist/cjs/Placement.js.map +7 -0
- package/dist/cjs/Plan.d.ts +489 -0
- package/dist/cjs/Plan.d.ts.map +1 -0
- package/dist/cjs/Plan.js +511 -0
- package/dist/cjs/Plan.js.map +7 -0
- package/dist/cjs/PlanDiff.d.ts +77 -0
- package/dist/cjs/PlanDiff.d.ts.map +1 -0
- package/dist/cjs/PlanDiff.js +149 -0
- package/dist/cjs/PlanDiff.js.map +7 -0
- package/dist/cjs/Planned.d.ts +121 -0
- package/dist/cjs/Planned.d.ts.map +1 -0
- package/dist/cjs/Planned.js +72 -0
- package/dist/cjs/Planned.js.map +7 -0
- package/dist/cjs/Repetition.d.ts +31 -0
- package/dist/cjs/Repetition.d.ts.map +1 -0
- package/dist/cjs/Repetition.js +36 -0
- package/dist/cjs/Repetition.js.map +7 -0
- package/dist/cjs/Scheduling.d.ts +89 -0
- package/dist/cjs/Scheduling.d.ts.map +1 -0
- package/dist/cjs/Scheduling.js +76 -0
- package/dist/cjs/Scheduling.js.map +7 -0
- package/dist/cjs/StepKey.d.ts +354 -0
- package/dist/cjs/StepKey.d.ts.map +1 -0
- package/dist/cjs/StepKey.js +313 -0
- package/dist/cjs/StepKey.js.map +7 -0
- package/dist/cjs/index.d.ts +91 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +59 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/internal/ConflictAnnotation.d.ts +59 -0
- package/dist/cjs/internal/ConflictAnnotation.d.ts.map +1 -0
- package/dist/cjs/internal/ConflictAnnotation.js +247 -0
- package/dist/cjs/internal/ConflictAnnotation.js.map +7 -0
- package/dist/cjs/internal/EffectCandidates.d.ts +19 -0
- package/dist/cjs/internal/EffectCandidates.d.ts.map +1 -0
- package/dist/cjs/internal/EffectCandidates.js +92 -0
- package/dist/cjs/internal/EffectCandidates.js.map +7 -0
- package/dist/cjs/internal/JsonMirror.d.ts +34 -0
- package/dist/cjs/internal/JsonMirror.d.ts.map +1 -0
- package/dist/cjs/internal/JsonMirror.js +195 -0
- package/dist/cjs/internal/JsonMirror.js.map +7 -0
- package/dist/cjs/internal/SchemaIdentity.d.ts +140 -0
- package/dist/cjs/internal/SchemaIdentity.d.ts.map +1 -0
- package/dist/cjs/internal/SchemaIdentity.js +718 -0
- package/dist/cjs/internal/SchemaIdentity.js.map +7 -0
- package/dist/cjs/internal/effects.d.ts +245 -0
- package/dist/cjs/internal/effects.d.ts.map +1 -0
- package/dist/cjs/internal/effects.js +290 -0
- package/dist/cjs/internal/effects.js.map +7 -0
- package/dist/cjs/internal/node.d.ts +526 -0
- package/dist/cjs/internal/node.d.ts.map +1 -0
- package/dist/cjs/internal/node.js +313 -0
- package/dist/cjs/internal/node.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/test/PlanFixtures.d.ts +127 -0
- package/dist/cjs/test/PlanFixtures.d.ts.map +1 -0
- package/dist/cjs/test/PlanFixtures.js +59 -0
- package/dist/cjs/test/PlanFixtures.js.map +7 -0
- package/dist/esm/CachePolicy.d.ts +131 -0
- package/dist/esm/CachePolicy.d.ts.map +1 -0
- package/dist/esm/CachePolicy.js +117 -0
- package/dist/esm/CachePolicy.js.map +1 -0
- package/dist/esm/Effects.d.ts +258 -0
- package/dist/esm/Effects.d.ts.map +1 -0
- package/dist/esm/Effects.js +178 -0
- package/dist/esm/Effects.js.map +1 -0
- package/dist/esm/FileSet.d.ts +366 -0
- package/dist/esm/FileSet.d.ts.map +1 -0
- package/dist/esm/FileSet.js +398 -0
- package/dist/esm/FileSet.js.map +1 -0
- package/dist/esm/GraphBuildError.d.ts +120 -0
- package/dist/esm/GraphBuildError.d.ts.map +1 -0
- package/dist/esm/GraphBuildError.js +145 -0
- package/dist/esm/GraphBuildError.js.map +1 -0
- package/dist/esm/KeyMaterial.d.ts +107 -0
- package/dist/esm/KeyMaterial.d.ts.map +1 -0
- package/dist/esm/KeyMaterial.js +92 -0
- package/dist/esm/KeyMaterial.js.map +1 -0
- package/dist/esm/Node.d.ts +685 -0
- package/dist/esm/Node.d.ts.map +1 -0
- package/dist/esm/Node.js +591 -0
- package/dist/esm/Node.js.map +1 -0
- package/dist/esm/Placement.d.ts +117 -0
- package/dist/esm/Placement.d.ts.map +1 -0
- package/dist/esm/Placement.js +65 -0
- package/dist/esm/Placement.js.map +1 -0
- package/dist/esm/Plan.d.ts +489 -0
- package/dist/esm/Plan.d.ts.map +1 -0
- package/dist/esm/Plan.js +718 -0
- package/dist/esm/Plan.js.map +1 -0
- package/dist/esm/PlanDiff.d.ts +77 -0
- package/dist/esm/PlanDiff.d.ts.map +1 -0
- package/dist/esm/PlanDiff.js +205 -0
- package/dist/esm/PlanDiff.js.map +1 -0
- package/dist/esm/Planned.d.ts +121 -0
- package/dist/esm/Planned.d.ts.map +1 -0
- package/dist/esm/Planned.js +147 -0
- package/dist/esm/Planned.js.map +1 -0
- package/dist/esm/Repetition.d.ts +31 -0
- package/dist/esm/Repetition.d.ts.map +1 -0
- package/dist/esm/Repetition.js +24 -0
- package/dist/esm/Repetition.js.map +1 -0
- package/dist/esm/Scheduling.d.ts +89 -0
- package/dist/esm/Scheduling.d.ts.map +1 -0
- package/dist/esm/Scheduling.js +82 -0
- package/dist/esm/Scheduling.js.map +1 -0
- package/dist/esm/StepKey.d.ts +354 -0
- package/dist/esm/StepKey.d.ts.map +1 -0
- package/dist/esm/StepKey.js +503 -0
- package/dist/esm/StepKey.js.map +1 -0
- package/dist/esm/index.d.ts +91 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +91 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internal/ConflictAnnotation.d.ts +59 -0
- package/dist/esm/internal/ConflictAnnotation.d.ts.map +1 -0
- package/dist/esm/internal/ConflictAnnotation.js +332 -0
- package/dist/esm/internal/ConflictAnnotation.js.map +1 -0
- package/dist/esm/internal/EffectCandidates.d.ts +19 -0
- package/dist/esm/internal/EffectCandidates.d.ts.map +1 -0
- package/dist/esm/internal/EffectCandidates.js +86 -0
- package/dist/esm/internal/EffectCandidates.js.map +1 -0
- package/dist/esm/internal/JsonMirror.d.ts +34 -0
- package/dist/esm/internal/JsonMirror.d.ts.map +1 -0
- package/dist/esm/internal/JsonMirror.js +204 -0
- package/dist/esm/internal/JsonMirror.js.map +1 -0
- package/dist/esm/internal/SchemaIdentity.d.ts +140 -0
- package/dist/esm/internal/SchemaIdentity.d.ts.map +1 -0
- package/dist/esm/internal/SchemaIdentity.js +1002 -0
- package/dist/esm/internal/SchemaIdentity.js.map +1 -0
- package/dist/esm/internal/effects.d.ts +245 -0
- package/dist/esm/internal/effects.d.ts.map +1 -0
- package/dist/esm/internal/effects.js +459 -0
- package/dist/esm/internal/effects.js.map +1 -0
- package/dist/esm/internal/node.d.ts +526 -0
- package/dist/esm/internal/node.d.ts.map +1 -0
- package/dist/esm/internal/node.js +565 -0
- package/dist/esm/internal/node.js.map +1 -0
- package/dist/esm/test/PlanFixtures.d.ts +127 -0
- package/dist/esm/test/PlanFixtures.d.ts.map +1 -0
- package/dist/esm/test/PlanFixtures.js +60 -0
- package/dist/esm/test/PlanFixtures.js.map +1 -0
- package/docs/README.md +167 -0
- package/docs/api.md +1485 -0
- package/docs/concepts/authoring-ast.md +204 -0
- package/docs/concepts/effects-and-conflicts.md +135 -0
- package/docs/concepts/plan-value.md +168 -0
- package/docs/concepts/step-keys.md +179 -0
- package/docs/guides/author-a-node-graph.md +182 -0
- package/docs/guides/compile-a-plan.md +146 -0
- package/docs/guides/declare-file-effects.md +143 -0
- package/docs/guides/diff-two-plans.md +310 -0
- package/docs/installation.md +75 -0
- package/docs/quickstart.md +128 -0
- package/docs/testing.md +73 -0
- package/docs/troubleshooting.md +314 -0
- package/package.json +255 -3
- package/src/CachePolicy.ts +143 -0
- package/src/Effects.ts +302 -0
- package/src/FileSet.ts +493 -0
- package/src/GraphBuildError.ts +156 -0
- package/src/KeyMaterial.ts +111 -0
- package/src/Node.ts +974 -0
- package/src/Placement.ts +99 -0
- package/src/Plan.ts +911 -0
- package/src/PlanDiff.ts +244 -0
- package/src/Planned.ts +208 -0
- package/src/Repetition.ts +33 -0
- package/src/Scheduling.ts +154 -0
- package/src/StepKey.ts +736 -0
- package/src/index.ts +103 -0
- package/src/internal/ConflictAnnotation.ts +374 -0
- package/src/internal/EffectCandidates.ts +89 -0
- package/src/internal/JsonMirror.ts +255 -0
- package/src/internal/SchemaIdentity.ts +1096 -0
- package/src/internal/effects.ts +543 -0
- package/src/internal/node.ts +859 -0
- package/src/test/PlanFixtures.ts +76 -0
package/src/Node.ts
ADDED
|
@@ -0,0 +1,974 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure, pipeable nodes: the shape a flow body describes and nothing more.
|
|
3
|
+
*
|
|
4
|
+
* Building a node records an inspectable AST and executes nothing — the same
|
|
5
|
+
* split as Bazel's `ctx.actions.run`, which declares an action rather than
|
|
6
|
+
* running one. A plan is
|
|
7
|
+
* always a DAG, so there is no loop node here and never will be: repetition
|
|
8
|
+
* lives one level up, in what a flow settles with.
|
|
9
|
+
*
|
|
10
|
+
* A node also carries Effect's requirement channel, `R`, and carries it as a
|
|
11
|
+
* PHANTOM: building a plan demands nothing, and every combinator here unions
|
|
12
|
+
* `R` without ever reading it. What fills the channel is a call to something
|
|
13
|
+
* whose code lives elsewhere — an action — so the type of a plan states which
|
|
14
|
+
* implementations executing it will need, and the place that executes it is
|
|
15
|
+
* where the compiler asks for them.
|
|
16
|
+
*
|
|
17
|
+
* Control flow is structure. {@link branch} takes both arms and evaluates each
|
|
18
|
+
* ONCE, symbolically, so the exit condition and the handoff site are visible
|
|
19
|
+
* topology before anything runs; its predicate is digested and runs later on
|
|
20
|
+
* the real value. {@link map} is transformation only. The rule the authoring
|
|
21
|
+
* note states, and this module encodes: **map transforms; branch decides.**
|
|
22
|
+
*
|
|
23
|
+
* Adapted from `@smthrs/core` (`packages/smithers/flows/core`) `Node.ts`.
|
|
24
|
+
* `Dynamic` is gone — a model call is an ordinary action, and nothing
|
|
25
|
+
* model-shaped belongs in this package — and node annotations are gone with
|
|
26
|
+
* it, because the AST has to stay JSON serializable.
|
|
27
|
+
*
|
|
28
|
+
* @since 0.1.0
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { dual } from "effect/Function"
|
|
32
|
+
import type * as Pipeable from "effect/Pipeable"
|
|
33
|
+
import type * as Schema from "effect/Schema"
|
|
34
|
+
import type * as SchemaAST from "effect/SchemaAST"
|
|
35
|
+
import type * as Types from "effect/Types"
|
|
36
|
+
import { GraphBuildError } from "./GraphBuildError.ts"
|
|
37
|
+
import * as internal from "./internal/node.ts"
|
|
38
|
+
import * as internalSchemaIdentity from "./internal/SchemaIdentity.ts"
|
|
39
|
+
import * as Planned from "./Planned.ts"
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The runtime type identifier carried by every node.
|
|
43
|
+
*
|
|
44
|
+
* @since 0.1.0
|
|
45
|
+
* @category type ids
|
|
46
|
+
* @slop
|
|
47
|
+
*/
|
|
48
|
+
export const TypeId: TypeId = internal.TypeId
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The type-level form of {@link TypeId}.
|
|
52
|
+
*
|
|
53
|
+
* @since 0.1.0
|
|
54
|
+
* @category type ids
|
|
55
|
+
* @slop
|
|
56
|
+
*/
|
|
57
|
+
export type TypeId = "~@smthrs/plan/Node"
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The inspectable AST a node stores: closure-free, and JSON serializable for
|
|
61
|
+
* every JSON payload an author puts in it.
|
|
62
|
+
*
|
|
63
|
+
* @since 0.1.0
|
|
64
|
+
* @category models
|
|
65
|
+
* @slop
|
|
66
|
+
*/
|
|
67
|
+
export type Ast = internal.NodeAst
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* How a flow call joins the caller's plan: `inline` splices the callee's body
|
|
71
|
+
* in, `boundary` makes it one child execution, and `handoff` names the next
|
|
72
|
+
* trampoline round.
|
|
73
|
+
*
|
|
74
|
+
* This is the ONE call-mode vocabulary. {@link flowCall} takes it and
|
|
75
|
+
* `@smthrs/flow`'s graph builder switches on it, so a second spelling in
|
|
76
|
+
* either package would fork the moment one gained a mode.
|
|
77
|
+
*
|
|
78
|
+
* @since 0.1.0
|
|
79
|
+
* @category models
|
|
80
|
+
* @slop
|
|
81
|
+
*/
|
|
82
|
+
export const CallMode = internal.CallMode
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The value form of {@link CallMode}.
|
|
86
|
+
*
|
|
87
|
+
* @since 0.1.0
|
|
88
|
+
* @category models
|
|
89
|
+
* @slop
|
|
90
|
+
*/
|
|
91
|
+
export type CallMode = internal.CallMode
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The serializable stand-in an AST keeps for a plan-time function: a digest of
|
|
95
|
+
* its normalized source, hashed in place of a closure that could not be
|
|
96
|
+
* shipped, stored, or compared.
|
|
97
|
+
*
|
|
98
|
+
* @since 0.1.0
|
|
99
|
+
* @category models
|
|
100
|
+
* @slop
|
|
101
|
+
*/
|
|
102
|
+
export type FunctionIdentity = Extract<Ast, { readonly _tag: "Map" }>["mapper"]
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Reads the inert AST reference created for a planned value.
|
|
106
|
+
*
|
|
107
|
+
* Structural lookalikes remain ordinary payload data. This accessor is the
|
|
108
|
+
* only recognition path because the marker itself is deliberately private to
|
|
109
|
+
* the AST cloner.
|
|
110
|
+
*
|
|
111
|
+
* @since 1.0.0
|
|
112
|
+
* @category accessors
|
|
113
|
+
*/
|
|
114
|
+
export const plannedReference = internal.plannedReference
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A pure graph-building value, covariant in what it will succeed and fail
|
|
118
|
+
* with and in what it will need to run. It is a description: holding one has
|
|
119
|
+
* run nothing.
|
|
120
|
+
*
|
|
121
|
+
* `R` is Effect's requirement channel, and it is PHANTOM here. Nothing at plan
|
|
122
|
+
* time reads it: the AST, the graph built from it, its key material, and every
|
|
123
|
+
* digest are identical whatever `R` says. It exists so a value that names an
|
|
124
|
+
* implementation it does not carry — an action call — can say so in its type,
|
|
125
|
+
* and so the place that finally has to run that implementation can demand it.
|
|
126
|
+
* Building a plan therefore stays requirement-free; only executing one is not.
|
|
127
|
+
*
|
|
128
|
+
* @since 0.1.0
|
|
129
|
+
* @category models
|
|
130
|
+
* @slop
|
|
131
|
+
*/
|
|
132
|
+
export interface Node<out A, out E = never, out R = never> extends Pipeable.Pipeable {
|
|
133
|
+
readonly [TypeId]: {
|
|
134
|
+
readonly _A: Types.Covariant<A>
|
|
135
|
+
readonly _E: Types.Covariant<E>
|
|
136
|
+
readonly _R: Types.Covariant<R>
|
|
137
|
+
}
|
|
138
|
+
readonly ast: Ast
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Any node, whatever it succeeds or fails with and whatever it requires.
|
|
143
|
+
*
|
|
144
|
+
* @since 0.1.0
|
|
145
|
+
* @category models
|
|
146
|
+
* @slop
|
|
147
|
+
*/
|
|
148
|
+
export type Any = Node<unknown, unknown, any>
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The success type of a node.
|
|
152
|
+
*
|
|
153
|
+
* @since 0.1.0
|
|
154
|
+
* @category models
|
|
155
|
+
* @slop
|
|
156
|
+
*/
|
|
157
|
+
export type Success<N> = N extends Node<infer A, infer _E, infer _R> ? A : never
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The error type of a node.
|
|
161
|
+
*
|
|
162
|
+
* @since 0.1.0
|
|
163
|
+
* @category models
|
|
164
|
+
* @slop
|
|
165
|
+
*/
|
|
166
|
+
export type Error<N> = N extends Node<infer _A, infer E, infer _R> ? E : never
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The requirements of a node: what has to be provided wherever it is finally
|
|
170
|
+
* executed, and nothing that has to be provided to build it.
|
|
171
|
+
*
|
|
172
|
+
* @since 0.1.0
|
|
173
|
+
* @category models
|
|
174
|
+
* @slop
|
|
175
|
+
*/
|
|
176
|
+
export type Services<N> = N extends Node<infer _A, infer _E, infer R> ? R : never
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The node reference a branch arm's symbolic subject carries. Arms are built
|
|
180
|
+
* before the graph assigns ids, so every reference an arm records names this
|
|
181
|
+
* placeholder, and graph building rewrites it to the branch's own upstream
|
|
182
|
+
* node — which is `first`, structurally, and therefore not a guess.
|
|
183
|
+
*
|
|
184
|
+
* @since 0.1.0
|
|
185
|
+
* @category constants
|
|
186
|
+
* @slop
|
|
187
|
+
*/
|
|
188
|
+
export const branchSubject = "branch/subject"
|
|
189
|
+
|
|
190
|
+
/** @private */
|
|
191
|
+
const subjectsKey = Symbol.for("@smthrs/plan/Node/subjects/v1")
|
|
192
|
+
const globals = globalThis as typeof globalThis & { [subjectsKey]?: { branch: number; catch: number } }
|
|
193
|
+
const subjects = globals[subjectsKey] ??= { branch: 0, catch: 0 }
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The prefix of the node reference a catch failure arm's symbolic error
|
|
197
|
+
* carries. Each {@link catch_} mints its own token under this prefix, so an
|
|
198
|
+
* outer error captured inside a nested failure arm keeps naming the outer
|
|
199
|
+
* catch, and graph building rewrites each token to the node that catch
|
|
200
|
+
* protects.
|
|
201
|
+
*
|
|
202
|
+
* @since 0.1.0
|
|
203
|
+
* @category constants
|
|
204
|
+
* @slop
|
|
205
|
+
*/
|
|
206
|
+
export const catchSubject = "catch/subject"
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The two arms of a decision plus the predicate that chooses between them.
|
|
210
|
+
*
|
|
211
|
+
* `if` runs at RUN time on the real value. `then` and `else` run at PLAN time,
|
|
212
|
+
* once each, against a {@link module:Planned.Planned} placeholder — they
|
|
213
|
+
* describe topology, so they may pass the value on but never compute on it.
|
|
214
|
+
*
|
|
215
|
+
* @since 0.1.0
|
|
216
|
+
* @category models
|
|
217
|
+
* @slop
|
|
218
|
+
*/
|
|
219
|
+
export interface BranchOptions<A, B1, E1, R1, B2, E2, R2> {
|
|
220
|
+
readonly if: (value: A) => boolean
|
|
221
|
+
readonly then: (value: Planned.Planned<A>) => Node<B1, E1, R1>
|
|
222
|
+
readonly else: (value: Planned.Planned<A>) => Node<B2, E2, R2>
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The statically planned recovery arm and optional schema selecting which
|
|
227
|
+
* typed failures it handles.
|
|
228
|
+
*
|
|
229
|
+
* @since 0.1.0
|
|
230
|
+
* @category models
|
|
231
|
+
* @slop
|
|
232
|
+
*/
|
|
233
|
+
export interface CatchOptions<E, B, E2, R2 = never, Handled = E> {
|
|
234
|
+
readonly error?: Schema.Schema<Handled> | undefined
|
|
235
|
+
readonly onFailure: (error: Planned.Planned<Handled>) => Node<B, E2, R2>
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Checks whether a value is a node.
|
|
240
|
+
*
|
|
241
|
+
* A node this package built is recognized by registration at construction. A
|
|
242
|
+
* node that crossed a serialization boundary, an object sharing the node
|
|
243
|
+
* prototype whose own `ast` is a well-formed AST, is recognized by that shape,
|
|
244
|
+
* because `@smthrs/flow` hands a rehydrated AST back as a node and its side
|
|
245
|
+
* tables are all a round trip loses. The {@link TypeId} marker is a public
|
|
246
|
+
* string any object can carry, so it counts for nothing on its own: every
|
|
247
|
+
* combinator that admits a node reads its `ast` as trusted topology, and an
|
|
248
|
+
* object carrying the marker on any other prototype, one inheriting it from a
|
|
249
|
+
* node, and one whose `ast` is missing, malformed, cyclic, or an accessor are
|
|
250
|
+
* all refused with the same `GraphBuildError` as any other non-node. A proxy
|
|
251
|
+
* is judged by the shape it forwards: one that forwards a node unchanged
|
|
252
|
+
* passes, and one that diverges from it does not.
|
|
253
|
+
*
|
|
254
|
+
* @since 0.1.0
|
|
255
|
+
* @category guards
|
|
256
|
+
* @slop
|
|
257
|
+
*/
|
|
258
|
+
export const isNode = (value: unknown): value is Any => internal.isNode(value)
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* The inert value delivered by {@link succeed}. Planned references resolve to
|
|
262
|
+
* their result type; other values follow the payload's JSON projection.
|
|
263
|
+
* Invalid dates serialize to null. Members without a JSON representation are
|
|
264
|
+
* omitted from objects and replaced with null in arrays.
|
|
265
|
+
*
|
|
266
|
+
* @since 1.0.0
|
|
267
|
+
* @category models
|
|
268
|
+
*/
|
|
269
|
+
export type Succeed<A> = A extends Planned.Planned<infer Value> ? Value
|
|
270
|
+
: A extends Date ? string | null
|
|
271
|
+
: A extends { toJSON: (...args: any) => infer Value } ? Succeed<Value>
|
|
272
|
+
: A extends (...args: any) => any ? undefined
|
|
273
|
+
: A extends symbol ? undefined
|
|
274
|
+
: A extends ReadonlyArray<unknown> ? { [K in keyof A]: SucceedArrayMember<A[K]> }
|
|
275
|
+
: A extends object ? {
|
|
276
|
+
[K in keyof A as K extends string | number ? [Succeed<A[K]>] extends [undefined] ? never : K : never]: Succeed<
|
|
277
|
+
A[K]
|
|
278
|
+
>
|
|
279
|
+
}
|
|
280
|
+
: A
|
|
281
|
+
|
|
282
|
+
/** @private */
|
|
283
|
+
type SucceedArrayMember<A> = A extends Planned.Planned<infer Value> ? Value
|
|
284
|
+
: A extends unknown ? Succeed<A> extends infer Value ? Value extends undefined ? null : Value
|
|
285
|
+
: never
|
|
286
|
+
: never
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* A node that succeeds with the constant's inert JSON projection.
|
|
290
|
+
*
|
|
291
|
+
* Dates produce strings (or null for invalid dates), URLs produce strings,
|
|
292
|
+
* and callable object members are omitted. Use {@link map} to reconstruct a
|
|
293
|
+
* domain value explicitly after this serialization boundary.
|
|
294
|
+
*
|
|
295
|
+
* @since 0.1.0
|
|
296
|
+
* @category constructors
|
|
297
|
+
*/
|
|
298
|
+
export const succeed = <A>(value: A): Node<Succeed<A>> => internal.makeNode<Succeed<A>>(internal.succeed(value))
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* The inert error delivered by {@link fail}: the same JSON projection
|
|
302
|
+
* {@link Succeed} describes, because a plan stores an error exactly as it
|
|
303
|
+
* stores a value.
|
|
304
|
+
*
|
|
305
|
+
* @since 1.0.0
|
|
306
|
+
* @category models
|
|
307
|
+
*/
|
|
308
|
+
export type Fail<E> = Succeed<E>
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* A node that fails with the constant's inert JSON projection, in the typed
|
|
312
|
+
* error channel.
|
|
313
|
+
*
|
|
314
|
+
* It is how a plan states a refusal it already knows about — an unsupported
|
|
315
|
+
* input, an exhausted budget, a compensation that has nothing left to undo —
|
|
316
|
+
* without an action that exists only to fail. {@link catch} recovers it exactly
|
|
317
|
+
* as it recovers an action's failure, and an uncaught one is the flow's typed
|
|
318
|
+
* failure.
|
|
319
|
+
*
|
|
320
|
+
* @since 1.0.0
|
|
321
|
+
* @category constructors
|
|
322
|
+
*/
|
|
323
|
+
export const fail = <E>(error: E): Node<never, Fail<E>> => internal.makeNode<never, Fail<E>>(internal.fail(error))
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Combines independent children into one node, keyed by name.
|
|
327
|
+
*
|
|
328
|
+
* Width is fixed here, at plan time. Fanning out over something a step
|
|
329
|
+
* discovered is not this: end the round and carry the list in the next flow's
|
|
330
|
+
* payload, where it is real data.
|
|
331
|
+
*
|
|
332
|
+
* @since 0.1.0
|
|
333
|
+
* @category constructors
|
|
334
|
+
* @slop
|
|
335
|
+
*/
|
|
336
|
+
export const all = <const Nodes extends Readonly<Record<string, Any>>>(
|
|
337
|
+
nodes: Nodes
|
|
338
|
+
): Node<
|
|
339
|
+
Types.Simplify<{ readonly [K in keyof Nodes]: Success<Nodes[K]> }>,
|
|
340
|
+
Error<Nodes[keyof Nodes]>,
|
|
341
|
+
Services<Nodes[keyof Nodes]>
|
|
342
|
+
> => internal.makeNode(internal.all(memberAsts(nodes, "all", "invalid_all_member")))
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* The member ASTs of a combination, keyed by name, refusing a non-node member.
|
|
346
|
+
*
|
|
347
|
+
* @private
|
|
348
|
+
*/
|
|
349
|
+
const memberAsts = (
|
|
350
|
+
nodes: Readonly<Record<string, unknown>>,
|
|
351
|
+
combinator: string,
|
|
352
|
+
code: "invalid_all_member" | "invalid_join"
|
|
353
|
+
): Record<string, Ast> => {
|
|
354
|
+
const asts: Record<string, Ast> = Object.create(null) as Record<string, Ast>
|
|
355
|
+
for (const [member, node] of Object.entries(nodes)) {
|
|
356
|
+
if (!isNode(node)) {
|
|
357
|
+
throw new GraphBuildError({
|
|
358
|
+
code,
|
|
359
|
+
node: member,
|
|
360
|
+
path: [],
|
|
361
|
+
message: `Node.${combinator} expected a Node at member "${member}"`
|
|
362
|
+
})
|
|
363
|
+
}
|
|
364
|
+
Object.defineProperty(asts, member, {
|
|
365
|
+
configurable: true,
|
|
366
|
+
enumerable: true,
|
|
367
|
+
value: node.ast,
|
|
368
|
+
writable: true
|
|
369
|
+
})
|
|
370
|
+
}
|
|
371
|
+
return asts
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Builds a join that settles early, refusing a member set it could never
|
|
376
|
+
* decide on.
|
|
377
|
+
*
|
|
378
|
+
* @private
|
|
379
|
+
*/
|
|
380
|
+
const join = (mode: internal.RaceMode, count: number, nodes: Readonly<Record<string, unknown>>): Ast => {
|
|
381
|
+
const asts = memberAsts(nodes, mode, "invalid_join")
|
|
382
|
+
const width = Object.keys(asts).length
|
|
383
|
+
if (!internal.isRaceCount(mode, count, width)) {
|
|
384
|
+
throw new GraphBuildError({
|
|
385
|
+
code: "invalid_join",
|
|
386
|
+
node: mode,
|
|
387
|
+
path: [],
|
|
388
|
+
message: mode === "quorum"
|
|
389
|
+
? `Node.quorum needs a whole number of successes between 1 and its ${width} members, got ${String(count)}`
|
|
390
|
+
: `Node.${mode} needs at least one member`
|
|
391
|
+
})
|
|
392
|
+
}
|
|
393
|
+
return internal.race(mode, count, asts)
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* The members of an early-settling join, keyed by name.
|
|
398
|
+
*
|
|
399
|
+
* @since 1.0.0
|
|
400
|
+
* @category models
|
|
401
|
+
*/
|
|
402
|
+
export type Members = Readonly<Record<string, Any>>
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Settles with the first member to settle, success or typed failure.
|
|
406
|
+
*
|
|
407
|
+
* The other members are interrupted. The winner is journaled, so a resumed run
|
|
408
|
+
* settles the same member and never starts the others again.
|
|
409
|
+
*
|
|
410
|
+
* @since 1.0.0
|
|
411
|
+
* @category constructors
|
|
412
|
+
*/
|
|
413
|
+
export const race = <const Nodes extends Members>(
|
|
414
|
+
nodes: Nodes
|
|
415
|
+
): Node<Success<Nodes[keyof Nodes]>, Error<Nodes[keyof Nodes]>, Services<Nodes[keyof Nodes]>> =>
|
|
416
|
+
internal.makeNode(join("race", 1, nodes))
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Settles with the first member to succeed, ignoring typed failures while a
|
|
420
|
+
* member is still running.
|
|
421
|
+
*
|
|
422
|
+
* It fails with the last failure when every member fails. The other members
|
|
423
|
+
* are interrupted, and the winner is journaled as {@link race} journals it.
|
|
424
|
+
*
|
|
425
|
+
* @since 1.0.0
|
|
426
|
+
* @category constructors
|
|
427
|
+
*/
|
|
428
|
+
export const any = <const Nodes extends Members>(
|
|
429
|
+
nodes: Nodes
|
|
430
|
+
): Node<Success<Nodes[keyof Nodes]>, Error<Nodes[keyof Nodes]>, Services<Nodes[keyof Nodes]>> =>
|
|
431
|
+
internal.makeNode(join("any", 1, nodes))
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Succeeds once `count` members succeed, with exactly those members' values
|
|
435
|
+
* keyed by name.
|
|
436
|
+
*
|
|
437
|
+
* It fails with the failure that leaves fewer than `count` members able to
|
|
438
|
+
* succeed. Either way the members still running are interrupted, and the
|
|
439
|
+
* members that decided are journaled as {@link race} journals its winner.
|
|
440
|
+
*
|
|
441
|
+
* @since 1.0.0
|
|
442
|
+
* @category constructors
|
|
443
|
+
*/
|
|
444
|
+
export const quorum = <const Nodes extends Members>(
|
|
445
|
+
count: number,
|
|
446
|
+
nodes: Nodes
|
|
447
|
+
): Node<
|
|
448
|
+
Types.Simplify<{ readonly [K in keyof Nodes]?: Success<Nodes[K]> }>,
|
|
449
|
+
Error<Nodes[keyof Nodes]>,
|
|
450
|
+
Services<Nodes[keyof Nodes]>
|
|
451
|
+
> => internal.makeNode(join("quorum", count, nodes))
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Transforms an eventual success value with a deferred pure function.
|
|
455
|
+
*
|
|
456
|
+
* The function is digested, not run: it executes later, on the real value.
|
|
457
|
+
* This is where computation on a step result belongs — and only computation. A
|
|
458
|
+
* `map` that decides what happens next is a `branch` written wrongly.
|
|
459
|
+
*
|
|
460
|
+
* @since 0.1.0
|
|
461
|
+
* @category mapping
|
|
462
|
+
* @slop
|
|
463
|
+
*/
|
|
464
|
+
export const map: {
|
|
465
|
+
<A, B>(f: (a: A) => B): <E, R>(self: Node<A, E, R>) => Node<B, E, R>
|
|
466
|
+
<A, E, R, B>(self: Node<A, E, R>, f: (a: A) => B): Node<B, E, R>
|
|
467
|
+
} = dual(
|
|
468
|
+
2,
|
|
469
|
+
<A, E, R, B>(self: Node<A, E, R>, f: (a: A) => B): Node<B, E, R> =>
|
|
470
|
+
internal.makeNode<B, E, R>(internal.map(self.ast, (value) => f(value as A), f))
|
|
471
|
+
)
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Starts the entire next subtree only after this node succeeds, without
|
|
475
|
+
* consuming its result. Failure or interruption prevents that subtree from
|
|
476
|
+
* starting, including actions nested inside combinations or inline flows.
|
|
477
|
+
*
|
|
478
|
+
* Use {@link bindPlanned} to build a dependency from a symbolic result, or
|
|
479
|
+
* {@link map} to compute on the eventual value.
|
|
480
|
+
*
|
|
481
|
+
* @since 0.1.0
|
|
482
|
+
* @category sequencing
|
|
483
|
+
* @slop
|
|
484
|
+
*/
|
|
485
|
+
export const andThen: {
|
|
486
|
+
<B, E2, R2>(next: Node<B, E2, R2>): <A, E, R>(self: Node<A, E, R>) => Node<B, E | E2, R | R2>
|
|
487
|
+
<A, E, R, B, E2, R2>(self: Node<A, E, R>, next: Node<B, E2, R2>): Node<B, E | E2, R | R2>
|
|
488
|
+
} = dual(
|
|
489
|
+
2,
|
|
490
|
+
<A, E, R, B, E2, R2>(
|
|
491
|
+
self: Node<A, E, R>,
|
|
492
|
+
next: Node<B, E2, R2>
|
|
493
|
+
): Node<B, E | E2, R | R2> => {
|
|
494
|
+
if (!isNode(next)) {
|
|
495
|
+
throw new GraphBuildError({
|
|
496
|
+
code: "invalid_continuation",
|
|
497
|
+
node: "andThen/next",
|
|
498
|
+
path: [],
|
|
499
|
+
message:
|
|
500
|
+
"Node.andThen expects a Node; use Node.bindPlanned for a symbolic builder or Node.map for value computation"
|
|
501
|
+
})
|
|
502
|
+
}
|
|
503
|
+
return internal.makeNode<B, E | E2, R | R2>(internal.andThenNode(self.ast, next.ast))
|
|
504
|
+
}
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Builds a dependency using a symbolic reference to this node's future result.
|
|
509
|
+
*
|
|
510
|
+
* The builder runs during planning. Its argument is a {@link module:Planned.Planned}
|
|
511
|
+
* reference, not the value the node will eventually produce. Pass references
|
|
512
|
+
* into action payloads; use {@link branch} for decisions and {@link map} for
|
|
513
|
+
* computation on real results. JavaScript truthiness and reference equality
|
|
514
|
+
* cannot inspect a planned result. Enable type-aware ESLint's
|
|
515
|
+
* `@typescript-eslint/strict-boolean-expressions` to reject planned conditions.
|
|
516
|
+
* Independent descendants of the built subtree can start while this producer
|
|
517
|
+
* is running; consumers of its reference wait for its value. Use
|
|
518
|
+
* {@link andThen} when the whole next subtree must wait for success.
|
|
519
|
+
*
|
|
520
|
+
* @since 1.0.0
|
|
521
|
+
* @category sequencing
|
|
522
|
+
*/
|
|
523
|
+
export const bindPlanned: {
|
|
524
|
+
<A, B, E2, R2>(
|
|
525
|
+
build: (reference: Planned.Planned<A>) => Node<B, E2, R2>
|
|
526
|
+
): <E, R>(self: Node<A, E, R>) => Node<B, E | E2, R | R2>
|
|
527
|
+
<A, E, R, B, E2, R2>(
|
|
528
|
+
self: Node<A, E, R>,
|
|
529
|
+
build: (reference: Planned.Planned<A>) => Node<B, E2, R2>
|
|
530
|
+
): Node<B, E | E2, R | R2>
|
|
531
|
+
} = dual(
|
|
532
|
+
2,
|
|
533
|
+
<A, E, R, B, E2, R2>(
|
|
534
|
+
self: Node<A, E, R>,
|
|
535
|
+
build: (reference: Planned.Planned<A>) => Node<B, E2, R2>
|
|
536
|
+
): Node<B, E | E2, R | R2> =>
|
|
537
|
+
internal.makeNode<B, E | E2, R | R2>(
|
|
538
|
+
internal.andThen(self.ast, (value) => build(value as Planned.Planned<A>), build)
|
|
539
|
+
)
|
|
540
|
+
)
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Maximum eagerly nested branch arms. Arm construction precedes graph walking;
|
|
544
|
+
* this bound leaves native stack headroom for a typed refusal.
|
|
545
|
+
*
|
|
546
|
+
* @since 1.0.0
|
|
547
|
+
* @category constants
|
|
548
|
+
*/
|
|
549
|
+
export const maximumArmDepth = 512
|
|
550
|
+
let armDepth = 0
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Builds one arm, once, against the symbolic subject.
|
|
554
|
+
*
|
|
555
|
+
* @private
|
|
556
|
+
*/
|
|
557
|
+
const arm = <A, B, E, R>(
|
|
558
|
+
build: (value: Planned.Planned<A>) => Node<B, E, R>,
|
|
559
|
+
subject: Planned.Planned<A>,
|
|
560
|
+
side: string
|
|
561
|
+
): Ast => {
|
|
562
|
+
if (armDepth >= maximumArmDepth) {
|
|
563
|
+
throw new GraphBuildError({
|
|
564
|
+
code: "graph_too_deep",
|
|
565
|
+
node: `${branchSubject}/${side}`,
|
|
566
|
+
path: [],
|
|
567
|
+
message: "Node.branch recursion needs a plan-time bound, such as a payload-derived round count. " +
|
|
568
|
+
"Both arms are built before the run-time predicate is evaluated."
|
|
569
|
+
})
|
|
570
|
+
}
|
|
571
|
+
armDepth++
|
|
572
|
+
try {
|
|
573
|
+
const node = build(subject)
|
|
574
|
+
if (!isNode(node)) {
|
|
575
|
+
throw new GraphBuildError({
|
|
576
|
+
code: "invalid_continuation",
|
|
577
|
+
node: `${branchSubject}/${side}`,
|
|
578
|
+
path: [],
|
|
579
|
+
message: `Node.branch expected its "${side}" arm to return a Node`
|
|
580
|
+
})
|
|
581
|
+
}
|
|
582
|
+
return node.ast
|
|
583
|
+
} finally {
|
|
584
|
+
armDepth--
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Decides between two arms, both of them static topology.
|
|
590
|
+
*
|
|
591
|
+
* Each arm builder is evaluated exactly once, here, against a
|
|
592
|
+
* {@link module:Planned.Planned} placeholder, and the resulting ASTs are what
|
|
593
|
+
* the node stores — so a plan shows the exit condition and both continuations
|
|
594
|
+
* before it runs. The predicate is digested and evaluated at run time on the
|
|
595
|
+
* real value, which is why none of the plan-time placeholder machinery leaks
|
|
596
|
+
* into control flow.
|
|
597
|
+
*
|
|
598
|
+
* Both arms contribute their requirements, because both arms are topology the
|
|
599
|
+
* plan carries. A run takes one of them, but which one is not known until the
|
|
600
|
+
* predicate sees the real value, so an execution has to be able to take either.
|
|
601
|
+
*
|
|
602
|
+
* @since 0.1.0
|
|
603
|
+
* @category sequencing
|
|
604
|
+
* @slop
|
|
605
|
+
*/
|
|
606
|
+
export const branch: {
|
|
607
|
+
<A, B1, E1, R1, B2, E2, R2>(
|
|
608
|
+
options: BranchOptions<A, B1, E1, R1, B2, E2, R2>
|
|
609
|
+
): <E, R>(self: Node<A, E, R>) => Node<B1 | B2, E | E1 | E2, R | R1 | R2>
|
|
610
|
+
<A, E, R, B1, E1, R1, B2, E2, R2>(
|
|
611
|
+
self: Node<A, E, R>,
|
|
612
|
+
options: BranchOptions<A, B1, E1, R1, B2, E2, R2>
|
|
613
|
+
): Node<B1 | B2, E | E1 | E2, R | R1 | R2>
|
|
614
|
+
} = dual(
|
|
615
|
+
2,
|
|
616
|
+
<A, E, R, B1, E1, R1, B2, E2, R2>(
|
|
617
|
+
self: Node<A, E, R>,
|
|
618
|
+
options: BranchOptions<A, B1, E1, R1, B2, E2, R2>
|
|
619
|
+
): Node<B1 | B2, E | E1 | E2, R | R1 | R2> => {
|
|
620
|
+
const predicate = options.if
|
|
621
|
+
const subjectToken = `${branchSubject}/${subjects.branch++}`
|
|
622
|
+
const subject = Planned.make<A>(subjectToken)
|
|
623
|
+
return internal.makeNode<B1 | B2, E | E1 | E2, R | R1 | R2>(
|
|
624
|
+
internal.branch(
|
|
625
|
+
subjectToken,
|
|
626
|
+
self.ast,
|
|
627
|
+
(value) => predicate(value as A),
|
|
628
|
+
predicate,
|
|
629
|
+
arm(options.then, subject, "then"),
|
|
630
|
+
arm(options.else, subject, "else")
|
|
631
|
+
)
|
|
632
|
+
)
|
|
633
|
+
}
|
|
634
|
+
)
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Recovers from matching typed failures with static failure topology.
|
|
638
|
+
*
|
|
639
|
+
* The protected graph and failure arm are both stored in the AST. The arm is
|
|
640
|
+
* built once at plan time against a strict planned error placeholder. With no
|
|
641
|
+
* schema the whole typed error channel is handled. A schema handles only the
|
|
642
|
+
* values it accepts, so its overloads retain E: a refinement can reject values
|
|
643
|
+
* without narrowing its TypeScript type. Runtime schema callbacks enter the
|
|
644
|
+
* filter identity along with its JSON Schema. Uncaptured callbacks are local
|
|
645
|
+
* to this process, just like uncaptured mappers.
|
|
646
|
+
*
|
|
647
|
+
* The failure arm contributes its requirements to the node's, for the same
|
|
648
|
+
* reason a branch's arms do: it is topology the plan carries, and a run reaches
|
|
649
|
+
* it whenever the protected graph fails.
|
|
650
|
+
*
|
|
651
|
+
* @since 0.1.0
|
|
652
|
+
* @category sequencing
|
|
653
|
+
* @slop
|
|
654
|
+
*/
|
|
655
|
+
const catch_: {
|
|
656
|
+
<Handled, B, E2, R2>(
|
|
657
|
+
options: CatchOptions<unknown, B, E2, R2, Handled> & {
|
|
658
|
+
readonly error: Schema.Schema<Handled>
|
|
659
|
+
}
|
|
660
|
+
): <A, E, R>(self: Node<A, E, R>) => Node<A | B, E | E2, R | R2>
|
|
661
|
+
<E, B, E2, R2>(
|
|
662
|
+
options: CatchOptions<E, B, E2, R2> & {
|
|
663
|
+
readonly error?: undefined
|
|
664
|
+
}
|
|
665
|
+
): <A, R>(self: Node<A, E, R>) => Node<A | B, E2, R | R2>
|
|
666
|
+
<A, E, R, Handled, B, E2, R2>(
|
|
667
|
+
self: Node<A, E, R>,
|
|
668
|
+
options: CatchOptions<E, B, E2, R2, Handled> & {
|
|
669
|
+
readonly error: Schema.Schema<Handled>
|
|
670
|
+
}
|
|
671
|
+
): Node<A | B, E | E2, R | R2>
|
|
672
|
+
<A, E, R, B, E2, R2>(
|
|
673
|
+
self: Node<A, E, R>,
|
|
674
|
+
options: CatchOptions<E, B, E2, R2> & {
|
|
675
|
+
readonly error?: undefined
|
|
676
|
+
}
|
|
677
|
+
): Node<A | B, E2, R | R2>
|
|
678
|
+
} = dual(
|
|
679
|
+
2,
|
|
680
|
+
<A, E, R, Handled, B, E2, R2>(
|
|
681
|
+
self: Node<A, E, R>,
|
|
682
|
+
options: CatchOptions<E, B, E2, R2, Handled>
|
|
683
|
+
): Node<A | B, E | E2, R | R2> => {
|
|
684
|
+
const subjectToken = `${catchSubject}/${subjects.catch++}`
|
|
685
|
+
const failure = options.onFailure(Planned.make<Handled>(subjectToken))
|
|
686
|
+
if (!isNode(failure)) {
|
|
687
|
+
throw new GraphBuildError({
|
|
688
|
+
code: "invalid_continuation",
|
|
689
|
+
node: catchSubject,
|
|
690
|
+
path: [],
|
|
691
|
+
message: "Node.catch expected its failure arm to return a Node"
|
|
692
|
+
})
|
|
693
|
+
}
|
|
694
|
+
return internal.makeNode(internal.catch_(subjectToken, self.ast, failure.ast, options.error))
|
|
695
|
+
}
|
|
696
|
+
)
|
|
697
|
+
|
|
698
|
+
export { catch_ as catch }
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* Attaches a scheduling priority to a node, leaving the original unchanged.
|
|
702
|
+
*
|
|
703
|
+
* The scheduler runs ready work with a higher number first, so a priority
|
|
704
|
+
* changes LATENCY and nothing else. It never enters key material: a node
|
|
705
|
+
* ordered ahead of another still computes the same result, so re-keying it
|
|
706
|
+
* would throw away a legitimate cache hit. Children inherit the value
|
|
707
|
+
* lexically when the graph is built, and a child that states its own keeps it.
|
|
708
|
+
*
|
|
709
|
+
* @since 0.1.0
|
|
710
|
+
* @category annotations
|
|
711
|
+
* @slop
|
|
712
|
+
*/
|
|
713
|
+
export const priority: {
|
|
714
|
+
(value: number): <A, E, R>(self: Node<A, E, R>) => Node<A, E, R>
|
|
715
|
+
<A, E, R>(self: Node<A, E, R>, value: number): Node<A, E, R>
|
|
716
|
+
} = dual(
|
|
717
|
+
2,
|
|
718
|
+
<A, E, R>(self: Node<A, E, R>, value: number): Node<A, E, R> => {
|
|
719
|
+
if (!Number.isSafeInteger(value)) {
|
|
720
|
+
throw new GraphBuildError({
|
|
721
|
+
code: "invalid_priority",
|
|
722
|
+
node: self.ast._tag,
|
|
723
|
+
path: [],
|
|
724
|
+
message: `Node.priority expects a safe integer, received ${value}`
|
|
725
|
+
})
|
|
726
|
+
}
|
|
727
|
+
return internal.makeNode<A, E, R>(internal.withPriority(self.ast, value))
|
|
728
|
+
}
|
|
729
|
+
)
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Reads the scheduling priority a node carries, or `undefined` when it states
|
|
733
|
+
* none and inherits from whatever encloses it.
|
|
734
|
+
*
|
|
735
|
+
* @since 0.1.0
|
|
736
|
+
* @category accessors
|
|
737
|
+
* @slop
|
|
738
|
+
*/
|
|
739
|
+
export const declaredPriority = (ast: Ast): number | undefined => ast.priority
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Constructs the flow-call node used by `@smthrs/flow` without making flow
|
|
743
|
+
* calls part of the public authoring surface of this package.
|
|
744
|
+
*
|
|
745
|
+
* Reserved for `@smthrs/flow`, which owns flow authoring. It is not part
|
|
746
|
+
* of this package's authoring surface, and it validates nothing: an unknown
|
|
747
|
+
* flow tag becomes a call node the graph keeps as a leaf.
|
|
748
|
+
*
|
|
749
|
+
* @since 0.1.0
|
|
750
|
+
* @category engine
|
|
751
|
+
* @slop
|
|
752
|
+
*/
|
|
753
|
+
export const flowCall = <A = unknown, E = never, R = never>(
|
|
754
|
+
declaration: unknown,
|
|
755
|
+
flow: string,
|
|
756
|
+
mode: CallMode,
|
|
757
|
+
payload: unknown
|
|
758
|
+
): Node<A, E, R> => internal.makeNode<A, E, R>(internal.flowCall(declaration, flow, mode, payload))
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Constructs the action-call node used by `@smthrs/flow` without making
|
|
762
|
+
* action calls part of the public authoring surface of this package.
|
|
763
|
+
*
|
|
764
|
+
* Reserved for `@smthrs/flow`, which owns action authoring. It is not part
|
|
765
|
+
* of this package's authoring surface, and an unknown action tag becomes a
|
|
766
|
+
* call node the graph keeps as a leaf.
|
|
767
|
+
*
|
|
768
|
+
* @since 0.1.0
|
|
769
|
+
* @category engine
|
|
770
|
+
* @slop
|
|
771
|
+
*/
|
|
772
|
+
export const actionCall = <A = unknown, E = never, R = never>(
|
|
773
|
+
declaration: unknown,
|
|
774
|
+
action: string,
|
|
775
|
+
payload: unknown
|
|
776
|
+
): Node<A, E, R> => internal.makeNode<A, E, R>(internal.actionCall(declaration, action, payload))
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* Reads the flow or action declaration a call node names, so `@smthrs/flow`
|
|
780
|
+
* can expand a call it recorded. It is `undefined` for an AST that was
|
|
781
|
+
* rehydrated from JSON, because the declaration lives beside the AST rather
|
|
782
|
+
* than inside it — a graph built from such an AST keeps the call as a leaf.
|
|
783
|
+
*
|
|
784
|
+
* Reserved for `@smthrs/flow`, which files the declaration when it builds
|
|
785
|
+
* the call. Authors never call it.
|
|
786
|
+
*
|
|
787
|
+
* @since 0.1.0
|
|
788
|
+
* @category engine
|
|
789
|
+
* @slop
|
|
790
|
+
*/
|
|
791
|
+
export const declaration = (
|
|
792
|
+
ast: Extract<Ast, { readonly _tag: "ActionCall" | "FlowCall" }>
|
|
793
|
+
): unknown => internal.declaration(ast)
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* Reads the continuation builder of a sequenced node, which graph building
|
|
797
|
+
* evaluates once against a placeholder. It is `undefined` when the author
|
|
798
|
+
* supplied a node directly — the topology is already in `next` — and for a
|
|
799
|
+
* rehydrated AST, whose side table did not survive serialization.
|
|
800
|
+
*
|
|
801
|
+
* Reserved for `@smthrs/flow`'s graph walk. Authors never call it.
|
|
802
|
+
*
|
|
803
|
+
* @since 0.1.0
|
|
804
|
+
* @category engine
|
|
805
|
+
* @slop
|
|
806
|
+
*/
|
|
807
|
+
export const continuation = (
|
|
808
|
+
ast: Extract<Ast, { readonly _tag: "AndThen" }>
|
|
809
|
+
): ((value: Planned.Planned<unknown>) => unknown) | undefined => internal.operation(ast)
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* Reads the deferred mapper of a {@link map} node, which a driver applies to
|
|
813
|
+
* the real upstream value once it has one. It is `undefined` for every other
|
|
814
|
+
* variant, and for a rehydrated AST whose side table did not survive
|
|
815
|
+
* serialization.
|
|
816
|
+
*
|
|
817
|
+
* Reserved for `@smthrs/flow`'s interpreter. Authors never call it.
|
|
818
|
+
*
|
|
819
|
+
* @since 0.1.0
|
|
820
|
+
* @category engine
|
|
821
|
+
* @slop
|
|
822
|
+
*/
|
|
823
|
+
export const mapper = (ast: Ast): ((value: unknown) => unknown) | undefined =>
|
|
824
|
+
ast._tag === "Map" ? internal.operation(ast) : undefined
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Reads the run-time predicate of a {@link branch} node, which a driver
|
|
828
|
+
* evaluates on the real subject value to choose an arm. It is `undefined` for
|
|
829
|
+
* every other variant, and for a rehydrated AST whose side table did not
|
|
830
|
+
* survive serialization.
|
|
831
|
+
*
|
|
832
|
+
* Reserved for `@smthrs/flow`'s interpreter. Authors never call it.
|
|
833
|
+
*
|
|
834
|
+
* @since 0.1.0
|
|
835
|
+
* @category engine
|
|
836
|
+
* @slop
|
|
837
|
+
*/
|
|
838
|
+
export const predicate = (ast: Ast): ((value: unknown) => boolean) | undefined =>
|
|
839
|
+
ast._tag === "Branch" ? internal.predicate(ast) : undefined
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* Reads the optional schema selecting failures handled by a {@link catch_}.
|
|
843
|
+
*
|
|
844
|
+
* Reserved for `@smthrs/flow`'s interpreter. It is `undefined` for every
|
|
845
|
+
* variant but `catch_`, and for a rehydrated AST whose side table did not
|
|
846
|
+
* survive serialization.
|
|
847
|
+
*
|
|
848
|
+
* @since 0.1.0
|
|
849
|
+
* @category engine
|
|
850
|
+
* @slop
|
|
851
|
+
*/
|
|
852
|
+
export const catchFilter = (ast: Ast): Schema.Top | undefined => ast._tag === "Catch" ? internal.filter(ast) : undefined
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* Identifies a plan-time function the AST does NOT store, a flow's `body`,
|
|
856
|
+
* exactly as the AST identifies the mapper and continuation it does store.
|
|
857
|
+
* A call that keeps its callee as a leaf still has to re-key when that
|
|
858
|
+
* callee's body is edited, and this is the identity it folds in. Unannotated
|
|
859
|
+
* functions fail closed with process-local identity; use {@link capture} to
|
|
860
|
+
* declare inert captures and obtain deterministic identity.
|
|
861
|
+
*
|
|
862
|
+
* Reserved for `@smthrs/flow`, which folds a callee body's identity into a
|
|
863
|
+
* call it keeps as a leaf. Authors never call it; it throws a `TypeError`
|
|
864
|
+
* when handed anything but a function.
|
|
865
|
+
*
|
|
866
|
+
* @since 0.1.0
|
|
867
|
+
* @category engine
|
|
868
|
+
* @slop
|
|
869
|
+
*/
|
|
870
|
+
export const functionIdentity = (operation: unknown): FunctionIdentity => internal.functionIdentity(operation)
|
|
871
|
+
|
|
872
|
+
/**
|
|
873
|
+
* The document a schema's identity is: JSON-Schema-shaped, and the same bytes
|
|
874
|
+
* under every `effect` release this package supports.
|
|
875
|
+
*
|
|
876
|
+
* @since 1.0.0
|
|
877
|
+
* @category engine
|
|
878
|
+
*/
|
|
879
|
+
export type SchemaIdentity = internalSchemaIdentity.Document
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* The one option a schema's identity is taken under: whether an object closes
|
|
883
|
+
* over the keys it declares (`"error"`) or leaves others open (`"ignore"`,
|
|
884
|
+
* the default). It has the meaning it has in `Schema.toJsonSchemaDocument`.
|
|
885
|
+
*
|
|
886
|
+
* @since 1.0.0
|
|
887
|
+
* @category engine
|
|
888
|
+
*/
|
|
889
|
+
export type SchemaIdentityOptions = internalSchemaIdentity.Options
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* The refusal `schemaIdentity` and `representationIdentity` raise for a schema
|
|
893
|
+
* a step key cannot be derived from. Its message is the one
|
|
894
|
+
* `effect@4.0.0-rc.115` gave for the same schema, where it gave one.
|
|
895
|
+
*
|
|
896
|
+
* @since 1.0.0
|
|
897
|
+
* @category errors
|
|
898
|
+
*/
|
|
899
|
+
export const SchemaIdentityError = internalSchemaIdentity.SchemaIdentityError
|
|
900
|
+
|
|
901
|
+
/**
|
|
902
|
+
* The type of {@link SchemaIdentityError}.
|
|
903
|
+
*
|
|
904
|
+
* @since 1.0.0
|
|
905
|
+
* @category errors
|
|
906
|
+
*/
|
|
907
|
+
export type SchemaIdentityError = internalSchemaIdentity.SchemaIdentityError
|
|
908
|
+
|
|
909
|
+
/**
|
|
910
|
+
* Identifies a schema by the constraints it documents, so a call re-keys when
|
|
911
|
+
* what its callee accepts or produces changes, and a catch re-keys when its
|
|
912
|
+
* filter does.
|
|
913
|
+
*
|
|
914
|
+
* The document is what `Schema.toJsonSchemaDocument(schema, options)`
|
|
915
|
+
* returned under `effect@4.0.0-rc.115`. It is printed by this package, not by
|
|
916
|
+
* the installed `effect`, because a later release prints a different document
|
|
917
|
+
* for the same schema and a moved identity re-keys every recorded step. Use
|
|
918
|
+
* it wherever the document is hashed, compared or pinned; a JSON Schema shown
|
|
919
|
+
* to a model or a person comes from `Schema.toJsonSchemaDocument`.
|
|
920
|
+
*
|
|
921
|
+
* Reserved for the packages that derive identities. Authors never call it; it
|
|
922
|
+
* throws for a schema that has no JSON Schema form.
|
|
923
|
+
*
|
|
924
|
+
* @since 1.0.0
|
|
925
|
+
* @category engine
|
|
926
|
+
*/
|
|
927
|
+
export const schemaIdentity = (schema: Schema.Top, options?: SchemaIdentityOptions): SchemaIdentity =>
|
|
928
|
+
internalSchemaIdentity.schemaIdentity(schema, options)
|
|
929
|
+
|
|
930
|
+
/**
|
|
931
|
+
* Identifies a schema AST by its encoded representation, the material a
|
|
932
|
+
* sealed action's step key folds in for its success and error schemas.
|
|
933
|
+
*
|
|
934
|
+
* The value is what `SchemaRepresentation.toJson(SchemaRepresentation.toRepresentation(ast))`
|
|
935
|
+
* returned under `effect@4.0.0-rc.115`, so a later `effect` release does not
|
|
936
|
+
* re-key recorded steps. The one exception is `Schema.brand`: from
|
|
937
|
+
* `effect@4.0.4` on a brand is not part of the AST, so a declaration with a
|
|
938
|
+
* brand re-keys once.
|
|
939
|
+
*
|
|
940
|
+
* Reserved for the packages that derive step keys. Authors never call it; it
|
|
941
|
+
* throws for an AST that has no representation.
|
|
942
|
+
*
|
|
943
|
+
* @since 1.0.0
|
|
944
|
+
* @category engine
|
|
945
|
+
*/
|
|
946
|
+
export const representationIdentity = (ast: SchemaAST.AST): Schema.Json =>
|
|
947
|
+
internalSchemaIdentity.representationIdentity(ast)
|
|
948
|
+
|
|
949
|
+
/**
|
|
950
|
+
* Binds a frozen plain copy of inert captures as the callback's this receiver.
|
|
951
|
+
*
|
|
952
|
+
* Use a function expression to read the snapshot while keeping the ordinary
|
|
953
|
+
* callback arguments: Node.capture({ factor: 3 }, function(value: number) {
|
|
954
|
+
* return value * this.factor
|
|
955
|
+
* }). Caller objects remain unchanged; lexical aliases still name originals
|
|
956
|
+
* and must not be used as mutable semantic inputs. Nested captures retain the
|
|
957
|
+
* inner callback's snapshot and compose the two identities.
|
|
958
|
+
*
|
|
959
|
+
* Admission walks original descriptors once, rejects built-in brands and
|
|
960
|
+
* non-plain prototypes without evaluating getters, then uses structuredClone
|
|
961
|
+
* to refuse Proxies. Hosts without that API refuse object capture. Only the
|
|
962
|
+
* owned frozen copy enters identity and the wrapper. Sealed, non-extensible,
|
|
963
|
+
* frozen and Immer ordinary data are supported equally. Unsupported values
|
|
964
|
+
* raise a TypeError naming the path. See core docs/api.md#nodecapture for the
|
|
965
|
+
* complete algorithm and the migration from original-object locking.
|
|
966
|
+
*
|
|
967
|
+
* @category constructors
|
|
968
|
+
* @since 0.1.0
|
|
969
|
+
* @slop
|
|
970
|
+
*/
|
|
971
|
+
export const capture = <C extends Readonly<Record<string, unknown>>, Args extends ReadonlyArray<unknown>, A>(
|
|
972
|
+
captures: C,
|
|
973
|
+
operation: (this: Readonly<C>, ...args: Args) => A
|
|
974
|
+
): (...args: Args) => A => internal.capture(captures, operation)
|