@typeonce/effect-machine 0.33.0 → 0.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +272 -628
- package/dist/Machine.d.ts +1076 -896
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +141 -90
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/childRegistry.d.ts +1 -1
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/cluster.d.ts +2 -50
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +4 -27
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterProtocol.d.ts +35 -0
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -0
- package/dist/internal/machine/clusterProtocol.js +28 -0
- package/dist/internal/machine/clusterProtocol.js.map +1 -0
- package/dist/internal/machine/commandRuntime.d.ts +1 -1
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/configuration.d.ts +2 -2
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +38 -64
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/declaration.d.ts +17 -0
- package/dist/internal/machine/declaration.d.ts.map +1 -0
- package/dist/internal/machine/declaration.js +120 -0
- package/dist/internal/machine/declaration.js.map +1 -0
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +15 -22
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +34 -0
- package/dist/internal/machine/implementation.d.ts.map +1 -0
- package/dist/internal/machine/implementation.js +3 -0
- package/dist/internal/machine/implementation.js.map +1 -0
- package/dist/internal/machine/initialDeclaration.d.ts +14 -0
- package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
- package/dist/internal/machine/initialDeclaration.js +137 -0
- package/dist/internal/machine/initialDeclaration.js.map +1 -0
- package/dist/internal/machine/invocation.d.ts +1 -1
- package/dist/internal/machine/invocation.d.ts.map +1 -1
- package/dist/internal/machine/invocation.js +1 -1
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
- package/dist/internal/machine/machine.d.ts +13 -4
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +169 -388
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +8 -0
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +37 -78
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +8 -7
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +16 -12
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/protocol.d.ts +1 -1
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +2 -230
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +8 -1857
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.d.ts +5 -0
- package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -0
- package/dist/internal/machine/runtimeCompiled.js +867 -0
- package/dist/internal/machine/runtimeCompiled.js.map +1 -0
- package/dist/internal/machine/runtimeGeneric.d.ts +3 -0
- package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -0
- package/dist/internal/machine/runtimeGeneric.js +471 -0
- package/dist/internal/machine/runtimeGeneric.js.map +1 -0
- package/dist/internal/machine/runtimeProtocol.d.ts +312 -0
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -0
- package/dist/internal/machine/runtimeProtocol.js +561 -0
- package/dist/internal/machine/runtimeProtocol.js.map +1 -0
- package/dist/internal/machine/stateDefinition.d.ts +1 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +1 -13
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/targetReference.d.ts +15 -0
- package/dist/internal/machine/targetReference.d.ts.map +1 -0
- package/dist/internal/machine/targetReference.js +41 -0
- package/dist/internal/machine/targetReference.js.map +1 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +5 -3
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +53 -46
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/probe.js +1 -1
- package/dist/internal/testing/machine/probe.js.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +7 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +105 -73
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +105 -73
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +18 -13
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +18 -25
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +41 -24
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/docs/agent-guide.md +66 -91
- package/docs/machine-review.md +19 -49
- package/docs/root-api.md +62 -62
- package/package.json +1 -1
- package/src/Machine.ts +1836 -2907
- package/src/internal/machine/childRegistry.ts +1 -1
- package/src/internal/machine/cluster.ts +7 -73
- package/src/internal/machine/clusterProtocol.ts +56 -0
- package/src/internal/machine/commandRuntime.ts +1 -1
- package/src/internal/machine/configuration.ts +53 -65
- package/src/internal/machine/declaration.ts +140 -0
- package/src/internal/machine/executionPlan.ts +16 -22
- package/src/internal/machine/implementation.ts +41 -0
- package/src/internal/machine/initialDeclaration.ts +157 -0
- package/src/internal/machine/invocation.ts +3 -3
- package/src/internal/machine/invocationDefinition.ts +1 -1
- package/src/internal/machine/machine.ts +225 -576
- package/src/internal/machine/planner.ts +84 -118
- package/src/internal/machine/process.ts +40 -33
- package/src/internal/machine/protocol.ts +3 -3
- package/src/internal/machine/runtime.ts +20 -3037
- package/src/internal/machine/runtimeCompiled.ts +1166 -0
- package/src/internal/machine/runtimeGeneric.ts +799 -0
- package/src/internal/machine/runtimeProtocol.ts +1213 -0
- package/src/internal/machine/stateDefinition.ts +1 -15
- package/src/internal/machine/targetReference.ts +55 -0
- package/src/internal/machine/topology.ts +5 -3
- package/src/internal/testing/machine/finiteModel.ts +58 -53
- package/src/internal/testing/machine/probe.ts +1 -1
- package/src/internal/testing/machine/verification.ts +7 -1
- package/src/testing/MachineTest.ts +105 -73
- package/src/unstable/cluster/ClusterMachine.ts +22 -25
- package/src/unstable/reactivity/AtomMachine.ts +41 -24
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/** Captures handler-owned initial edges before compiling executable topology. */
|
|
2
|
+
import { hasProperty } from "effect/Predicate"
|
|
3
|
+
import * as Schema from "effect/Schema"
|
|
4
|
+
import type { Machine, State } from "../../Machine.js"
|
|
5
|
+
import { SnapshotBuilderStateTypeId } from "./symbols.js"
|
|
6
|
+
import * as Reference from "./targetReference.js"
|
|
7
|
+
import * as Topology from "./topology.js"
|
|
8
|
+
|
|
9
|
+
const initializers = new WeakSet<object>()
|
|
10
|
+
|
|
11
|
+
export const isDataInitializer = (value: object): boolean => initializers.has(value)
|
|
12
|
+
|
|
13
|
+
type Context = Readonly<Record<string, unknown>>
|
|
14
|
+
type Constructor = (context: Context) => unknown
|
|
15
|
+
const record = (value: unknown, label: string): Readonly<Record<string, unknown>> => {
|
|
16
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
17
|
+
throw new Error(`Machine ${label} must be an object`)
|
|
18
|
+
}
|
|
19
|
+
return value as Readonly<Record<string, unknown>>
|
|
20
|
+
}
|
|
21
|
+
const evaluate = (value: unknown): Constructor => typeof value === "function" ? value as Constructor : () => value
|
|
22
|
+
|
|
23
|
+
/** Only the outer declaration is interpreted; callback results are ordinary data. */
|
|
24
|
+
export const construction = (value: unknown): Constructor => {
|
|
25
|
+
const descriptor = typeof value === "object" && value !== null &&
|
|
26
|
+
hasProperty(value, "decoded") && value.decoded === true && hasProperty(value, "data")
|
|
27
|
+
if (descriptor) {
|
|
28
|
+
const config = record(value, "decoded construction")
|
|
29
|
+
if (Reflect.ownKeys(config).some((key) => key !== "decoded" && key !== "data")) {
|
|
30
|
+
throw new Error("Machine decoded construction accepts only decoded and data")
|
|
31
|
+
}
|
|
32
|
+
return evaluate(config.data)
|
|
33
|
+
}
|
|
34
|
+
const resolve = evaluate(value)
|
|
35
|
+
return (context) => Topology.makeStateInput(value === undefined ? {} : resolve(context))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const edgeConstruction = (config: Readonly<Record<string, unknown>>): Constructor => {
|
|
39
|
+
if (config.decoded !== undefined && typeof config.decoded !== "boolean") {
|
|
40
|
+
throw new Error("Machine decoded construction flag must be a boolean")
|
|
41
|
+
}
|
|
42
|
+
if (config.decoded === true) {
|
|
43
|
+
if (!hasProperty(config, "data")) throw new Error("Machine decoded construction requires data")
|
|
44
|
+
return evaluate(config.data)
|
|
45
|
+
}
|
|
46
|
+
const resolve = evaluate(config.data)
|
|
47
|
+
return (context) => Topology.makeStateInput(hasProperty(config, "data") ? resolve(context) : {})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface Captured {
|
|
51
|
+
readonly node: Machine.StateNodeConfig | Machine.TaggedSchema
|
|
52
|
+
readonly handlers: Record<string, unknown>
|
|
53
|
+
}
|
|
54
|
+
export const capture = (
|
|
55
|
+
root: State<Machine.StateNodeConfig>,
|
|
56
|
+
handler: Readonly<Record<string, unknown>>
|
|
57
|
+
): Captured => {
|
|
58
|
+
const visit = (
|
|
59
|
+
node: Machine.StateNodeConfig | Machine.TaggedSchema,
|
|
60
|
+
raw: Readonly<Record<string, unknown>>,
|
|
61
|
+
path: string
|
|
62
|
+
): Captured => {
|
|
63
|
+
if (hasProperty(raw, "initialize")) throw new Error("Machine child construction belongs in initial declarations")
|
|
64
|
+
if (path !== "" && hasProperty(raw, "root")) {
|
|
65
|
+
throw new Error("Machine root construction belongs at the root handler")
|
|
66
|
+
}
|
|
67
|
+
const { initial, root: _root, states: rawChildren, ...handlers } = raw
|
|
68
|
+
if (Schema.isSchema(node) || !("states" in node)) {
|
|
69
|
+
if (initial !== undefined) throw new Error(`Machine state "${path}" cannot declare initial children`)
|
|
70
|
+
if (rawChildren !== undefined) throw new Error(`Machine state "${path}" has no children`)
|
|
71
|
+
return { node, handlers }
|
|
72
|
+
}
|
|
73
|
+
const childHandlers = rawChildren === undefined ? {} : record(rawChildren, "child handlers")
|
|
74
|
+
for (const key of Object.keys(childHandlers)) {
|
|
75
|
+
if (!Object.prototype.hasOwnProperty.call(node.states, key)) throw new Error(`Machine unknown child "${key}"`)
|
|
76
|
+
}
|
|
77
|
+
const states: Record<string, Machine.StateNodeConfig | Machine.TaggedSchema> = Object.create(null)
|
|
78
|
+
const children: Record<string, unknown> = Object.create(null)
|
|
79
|
+
const constructors: Record<string, Constructor> = Object.create(null)
|
|
80
|
+
const parallel = node.type === "parallel"
|
|
81
|
+
let initialKey: string | undefined
|
|
82
|
+
if (parallel) {
|
|
83
|
+
const regions = initial === undefined ? {} : record(initial, "parallel initial regions")
|
|
84
|
+
for (const [key, value] of Object.entries(regions)) {
|
|
85
|
+
const child = node.states[key]
|
|
86
|
+
if (child === undefined || (!Schema.isSchema(child) && (child.type === "history" || child.type === "choice"))) {
|
|
87
|
+
throw new Error(`Machine invalid initial region "${key}"`)
|
|
88
|
+
}
|
|
89
|
+
if (!Schema.isSchema(child) && (!("schema" in child) || child.schema === undefined)) {
|
|
90
|
+
throw new Error(`Machine schema-less region "${key}" cannot construct data`)
|
|
91
|
+
}
|
|
92
|
+
constructors[key] = construction(value)
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
const edge = record(initial, `initial edge for "${path}"`)
|
|
96
|
+
if (Reflect.ownKeys(edge).some((key) => !["target", "data", "decoded"].includes(String(key)))) {
|
|
97
|
+
throw new Error("Machine initial edge accepts only target, data, and decoded")
|
|
98
|
+
}
|
|
99
|
+
if (!hasProperty(edge.target, Reference.TypeId)) throw new Error("Machine initial requires a target reference")
|
|
100
|
+
const ref = (edge.target as Reference.Reference)[Reference.TypeId]
|
|
101
|
+
const prefix = path === "" ? "" : `${path}.`
|
|
102
|
+
const key = ref.path.slice(prefix.length)
|
|
103
|
+
if (
|
|
104
|
+
ref.root !== root || !ref.path.startsWith(prefix) || key.includes(".") || !(key in node.states) ||
|
|
105
|
+
ref.kind === "history"
|
|
106
|
+
) {
|
|
107
|
+
throw new Error(`Machine initial target must be a direct child of "${path}"`)
|
|
108
|
+
}
|
|
109
|
+
initialKey = key
|
|
110
|
+
if (ref.kind === "choice" && (hasProperty(edge, "data") || hasProperty(edge, "decoded"))) {
|
|
111
|
+
throw new Error("Machine choice initial edges cannot construct data")
|
|
112
|
+
}
|
|
113
|
+
const construct = edgeConstruction(edge)
|
|
114
|
+
const child = node.states[key]!
|
|
115
|
+
if (
|
|
116
|
+
hasProperty(edge, "data") && !Schema.isSchema(child) && (!("schema" in child) || child.schema === undefined)
|
|
117
|
+
) {
|
|
118
|
+
throw new Error(`Machine schema-less initial child "${key}" cannot construct data`)
|
|
119
|
+
}
|
|
120
|
+
if (hasProperty(edge, "data")) constructors[key] = construct
|
|
121
|
+
}
|
|
122
|
+
for (const [key, child] of Object.entries(node.states)) {
|
|
123
|
+
const childPath = path === "" ? key : `${path}.${key}`
|
|
124
|
+
const captured = visit(
|
|
125
|
+
child,
|
|
126
|
+
childHandlers[key] === undefined ? {} : record(childHandlers[key], "state handler"),
|
|
127
|
+
childPath
|
|
128
|
+
)
|
|
129
|
+
states[key] = captured.node
|
|
130
|
+
// History configuration is owned by its parent, not a state handler.
|
|
131
|
+
if (
|
|
132
|
+
(Schema.isSchema(child) || child.type !== "history") &&
|
|
133
|
+
(childHandlers[key] !== undefined || (!Schema.isSchema(child) && "states" in child))
|
|
134
|
+
) children[key] = captured.handlers
|
|
135
|
+
}
|
|
136
|
+
const compiled = Object.freeze({
|
|
137
|
+
...node,
|
|
138
|
+
states: Object.freeze(states),
|
|
139
|
+
...(parallel ? {} : { initial: initialKey })
|
|
140
|
+
})
|
|
141
|
+
const entries = Object.entries(constructors)
|
|
142
|
+
const initialize = (context: Context) => {
|
|
143
|
+
const ancestors = typeof context.ancestors === "object" && context.ancestors !== null
|
|
144
|
+
? context.ancestors as Readonly<Record<string, unknown>> :
|
|
145
|
+
{}
|
|
146
|
+
const scoped = { ...context, root: path === "" ? context.state : ancestors[""] }
|
|
147
|
+
const values = Object.fromEntries(entries.map(([key, construct]) => [key, construct(scoped)]))
|
|
148
|
+
return { [SnapshotBuilderStateTypeId]: values }
|
|
149
|
+
}
|
|
150
|
+
initializers.add(initialize)
|
|
151
|
+
return {
|
|
152
|
+
node: compiled,
|
|
153
|
+
handlers: { ...handlers, ...(entries.length === 0 ? {} : { initialize }), states: children }
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return visit(root.node, handler, "")
|
|
157
|
+
}
|
|
@@ -13,7 +13,7 @@ import { InfiniteTransitionError, MachineSchemaDecodeError, StoppedError } from
|
|
|
13
13
|
import * as InvocationDefinition from "./invocationDefinition.js"
|
|
14
14
|
import * as InvocationEvent from "./invocationEvent.js"
|
|
15
15
|
import * as Planner from "./planner.js"
|
|
16
|
-
import * as Runtime from "./
|
|
16
|
+
import * as Runtime from "./runtimeProtocol.js"
|
|
17
17
|
import { ChildMachineLogicTypeId } from "./symbols.js"
|
|
18
18
|
|
|
19
19
|
/** @internal */
|
|
@@ -61,7 +61,7 @@ const streamLogic = (
|
|
|
61
61
|
}
|
|
62
62
|
})
|
|
63
63
|
|
|
64
|
-
const resolveValue = (value: unknown, context: Machine.InvokeContext<any, any, any, any>): unknown =>
|
|
64
|
+
const resolveValue = (value: unknown, context: Omit<Machine.InvokeContext<any, any, any, any>, "root">): unknown =>
|
|
65
65
|
typeof value === "function" ? value(context) : value
|
|
66
66
|
|
|
67
67
|
const makeChildOwner = (scope: Runtime.ProcessScope<any>): ChildOwner<any> => ({
|
|
@@ -76,7 +76,7 @@ const makeChildOwner = (scope: Runtime.ProcessScope<any>): ChildOwner<any> => ({
|
|
|
76
76
|
|
|
77
77
|
const resolveOne = (
|
|
78
78
|
raw: InvocationDefinition.InvocationDefinition,
|
|
79
|
-
context: Machine.InvokeContext<any, any, any, any>,
|
|
79
|
+
context: Omit<Machine.InvokeContext<any, any, any, any>, "root">,
|
|
80
80
|
path: string
|
|
81
81
|
): AnyConfig => {
|
|
82
82
|
if ("effect" in raw) {
|
|
@@ -6,7 +6,7 @@ import type { EventTransition } from "./transition.js"
|
|
|
6
6
|
|
|
7
7
|
// Source callbacks cross the erased machine-definition boundary here. Their
|
|
8
8
|
// state, event, error and service relationships are checked by the public builder.
|
|
9
|
-
type Context = Machine.InvokeContext<any, any, any, any>
|
|
9
|
+
type Context = Omit<Machine.InvokeContext<any, any, any, any>, "root">
|
|
10
10
|
interface Outcomes {
|
|
11
11
|
readonly onDone?: EventTransition<any, any, any, any>
|
|
12
12
|
readonly onFailure?: EventTransition<any, any, any, any>
|