@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
|
@@ -6,23 +6,27 @@ import { hasProperty } from "effect/Predicate";
|
|
|
6
6
|
import * as Schema from "effect/Schema";
|
|
7
7
|
import * as Activities from "./activities.js";
|
|
8
8
|
import * as Configuration from "./configuration.js";
|
|
9
|
+
import * as Declaration from "./declaration.js";
|
|
10
|
+
import * as InitialDeclaration from "./initialDeclaration.js";
|
|
9
11
|
import * as InvocationDefinition from "./invocationDefinition.js";
|
|
10
12
|
import * as internalPlanner from "./planner.js";
|
|
11
13
|
import * as internalProcess from "./process.js";
|
|
12
14
|
import * as Protocol from "./protocol.js";
|
|
13
|
-
import * as internalRuntime from "./
|
|
15
|
+
import * as internalRuntime from "./runtimeProtocol.js";
|
|
14
16
|
import * as Serialization from "./serialization.js";
|
|
15
17
|
import * as StateDefinition from "./stateDefinition.js";
|
|
16
18
|
import { ChildMachineLogicTypeId } from "./symbols.js";
|
|
17
|
-
import { getLocalTargetScope,
|
|
19
|
+
import { getLocalTargetScope, makeTargetBuilder, withFrom } from "./targetBuilder.js";
|
|
20
|
+
import * as TargetReference from "./targetReference.js";
|
|
18
21
|
import * as Topology from "./topology.js";
|
|
19
22
|
export { ChildAlreadyExistsError, InfiniteTransitionError, MachineSchemaDecodeError, MachineSchemaEncodeError, ProcessLocalError, StartupError, StoppedError } from "./errors.js";
|
|
20
23
|
export { ChildMachineLogicTypeId, InitialEventTypeId, SnapshotBuilderStateTypeId } from "./symbols.js";
|
|
24
|
+
/** Internal seam for the public target-reference factory and its opaque brand. */
|
|
25
|
+
export const TargetReferenceTypeId = TargetReference.TypeId;
|
|
26
|
+
export const targets = TargetReference.make;
|
|
21
27
|
const TypeId = "~effect/Machine";
|
|
22
28
|
const ParentTypeId = "~effect/Machine/Parent";
|
|
23
29
|
export const InvokeTypeId = Symbol.for("effect/Machine/Invoke");
|
|
24
|
-
export const TransitionTypeId = Symbol.for("effect/Machine/Transition");
|
|
25
|
-
const InvokeBuilderDescriptorTypeId = Symbol("effect/Machine/InvokeBuilderDescriptor");
|
|
26
30
|
const ChildMachineTypeId = "~effect/Machine/ChildMachine";
|
|
27
31
|
const Proto = {
|
|
28
32
|
...Inspectable.BaseProto,
|
|
@@ -52,32 +56,9 @@ const makeWithHandlers = (self, handlers) => {
|
|
|
52
56
|
Protocol.copyProtocol(self, machine);
|
|
53
57
|
return machine;
|
|
54
58
|
};
|
|
55
|
-
const TransitionBuilderDescriptorTypeId = Symbol("effect/Machine/TransitionBuilderDescriptor");
|
|
56
|
-
const InitialBuilderDescriptorTypeId = Symbol("effect/Machine/InitialBuilderDescriptor");
|
|
57
59
|
const plainTargetSelection = (selection) => selection.kind === "none"
|
|
58
60
|
? Topology.noneTargetSelection
|
|
59
61
|
: Topology.makeTargetSelection(selection.kind, selection.path, selection.scope, selection.updatePath);
|
|
60
|
-
const transitionOptions = (options) => {
|
|
61
|
-
const configuration = typeof options === "object" && options !== null
|
|
62
|
-
? options
|
|
63
|
-
: {};
|
|
64
|
-
if (hasProperty(configuration, "reenter")) {
|
|
65
|
-
throw new Error("Use .reenter() before construction instead of the resolver reenter option");
|
|
66
|
-
}
|
|
67
|
-
return { declinable: configuration.declinable === true };
|
|
68
|
-
};
|
|
69
|
-
const makeDirectTransitionDescriptor = (selection, resolve, options, reenterSource = false) => {
|
|
70
|
-
const shared = {
|
|
71
|
-
[TransitionBuilderDescriptorTypeId]: TransitionBuilderDescriptorTypeId,
|
|
72
|
-
type: "direct",
|
|
73
|
-
selection: plainTargetSelection(selection),
|
|
74
|
-
reenterSource,
|
|
75
|
-
...transitionOptions(options)
|
|
76
|
-
};
|
|
77
|
-
return resolve === undefined
|
|
78
|
-
? Object.freeze(shared)
|
|
79
|
-
: Object.freeze({ ...shared, resolver: resolve });
|
|
80
|
-
};
|
|
81
62
|
// The authored callback crosses an erased schema boundary here. Each builder
|
|
82
63
|
// retains its construction mode, and planning validates both resulting values.
|
|
83
64
|
const constructSelectionValue = (selection, context, method, value) => {
|
|
@@ -88,131 +69,6 @@ const constructSelectionValue = (selection, context, method, value) => {
|
|
|
88
69
|
const values = value;
|
|
89
70
|
return context.target[method](values.target).update(context.owner[method](values.update));
|
|
90
71
|
};
|
|
91
|
-
const guardedSelection = (selection, predicate, reenterSource) => {
|
|
92
|
-
const wrap = (resolve, options) => makeDirectTransitionDescriptor(selection, (context, enqueue) => predicate(context) ? resolve(context, enqueue) : Topology.makeDeclined(), { ...transitionOptions(options), declinable: true }, reenterSource);
|
|
93
|
-
return Object.freeze({
|
|
94
|
-
...wrap((context) => selection.kind === "none" ? undefined : constructSelectedTarget(context.target)),
|
|
95
|
-
from: (value) => wrap((context) => constructSelectionValue(selection, context, "from", value(context))),
|
|
96
|
-
decoded: (value) => wrap((context) => constructSelectionValue(selection, context, "decoded", value(context))),
|
|
97
|
-
resolve: wrap
|
|
98
|
-
});
|
|
99
|
-
};
|
|
100
|
-
const decorateTransitionSelection = (selection, reenterSource = false) => Object.freeze({
|
|
101
|
-
...selection,
|
|
102
|
-
...(reenterSource ? makeDirectTransitionDescriptor(selection, undefined, undefined, true) : {}),
|
|
103
|
-
guard: (predicate) => guardedSelection(selection, predicate, reenterSource),
|
|
104
|
-
from: (value) => makeDirectTransitionDescriptor(selection, (context) => constructSelectionValue(selection, context, "from", value(context)), undefined, reenterSource),
|
|
105
|
-
decoded: (value) => makeDirectTransitionDescriptor(selection, (context) => constructSelectionValue(selection, context, "decoded", value(context)), undefined, reenterSource),
|
|
106
|
-
resolve: (resolve, options) => makeDirectTransitionDescriptor(selection, resolve, options, reenterSource),
|
|
107
|
-
reenter: () => decorateTransitionSelection(selection, true),
|
|
108
|
-
updating: (owner) => {
|
|
109
|
-
if (typeof owner !== "function") {
|
|
110
|
-
throw new Error("Machine updating owner must be a state selector");
|
|
111
|
-
}
|
|
112
|
-
const ownerSelection = owner();
|
|
113
|
-
if (!Topology.isTargetSelection(ownerSelection) || ownerSelection.kind !== "state" ||
|
|
114
|
-
ownerSelection.scope !== "branch") {
|
|
115
|
-
throw new Error("Machine updating owner must be addressed by one branch state selector");
|
|
116
|
-
}
|
|
117
|
-
return decorateTransitionSelection(Topology.makeTargetSelection(selection.kind, selection.path, selection.scope, ownerSelection.path), reenterSource);
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
const noneTransitionSelection = decorateTransitionSelection(Topology.noneTargetSelection);
|
|
121
|
-
const makeInitialBuilderDescriptor = (selection, resolve) => Object.freeze({
|
|
122
|
-
[InitialBuilderDescriptorTypeId]: InitialBuilderDescriptorTypeId,
|
|
123
|
-
selection: plainTargetSelection(selection),
|
|
124
|
-
resolve
|
|
125
|
-
});
|
|
126
|
-
const decorateInitialSelection = (selection) => Object.freeze({
|
|
127
|
-
...selection,
|
|
128
|
-
from: (value) => makeInitialBuilderDescriptor(selection, (context) => context.target.from(value(context))),
|
|
129
|
-
decoded: (value) => makeInitialBuilderDescriptor(selection, (context) => context.target.decoded(value(context))),
|
|
130
|
-
resolve: (resolve) => makeInitialBuilderDescriptor(selection, resolve)
|
|
131
|
-
});
|
|
132
|
-
const decorateInitialSelectorNode = (node) => {
|
|
133
|
-
if (Topology.isTargetSelection(node))
|
|
134
|
-
return decorateInitialSelection(node);
|
|
135
|
-
if (typeof node === "function") {
|
|
136
|
-
const wrapped = ((...args) => decorateInitialSelection(node(...args)));
|
|
137
|
-
for (const key of Object.keys(node)) {
|
|
138
|
-
wrapped[key] = decorateInitialSelectorNode(node[key]);
|
|
139
|
-
}
|
|
140
|
-
return Object.freeze(wrapped);
|
|
141
|
-
}
|
|
142
|
-
if (typeof node === "object" && node !== null) {
|
|
143
|
-
const wrapped = {};
|
|
144
|
-
for (const key of Object.keys(node)) {
|
|
145
|
-
wrapped[key] = decorateInitialSelectorNode(node[key]);
|
|
146
|
-
}
|
|
147
|
-
return Object.freeze(wrapped);
|
|
148
|
-
}
|
|
149
|
-
return node;
|
|
150
|
-
};
|
|
151
|
-
const decorateTransitionSelectorNode = (node) => {
|
|
152
|
-
if (Topology.isTargetSelection(node)) {
|
|
153
|
-
if (node.kind === "state" && node.scope === "full" && hasProperty(node, "initial")) {
|
|
154
|
-
return Object.freeze({
|
|
155
|
-
...decorateTransitionSelection(node),
|
|
156
|
-
initial: decorateTransitionSelection(node.initial)
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
return node === Topology.noneTargetSelection ? noneTransitionSelection : decorateTransitionSelection(node);
|
|
160
|
-
}
|
|
161
|
-
if (typeof node === "function") {
|
|
162
|
-
const wrapped = ((...args) => decorateTransitionSelection(node(...args)));
|
|
163
|
-
for (const key of Object.keys(node)) {
|
|
164
|
-
wrapped[key] = decorateTransitionSelectorNode(node[key]);
|
|
165
|
-
}
|
|
166
|
-
return Object.freeze(wrapped);
|
|
167
|
-
}
|
|
168
|
-
if (typeof node === "object" && node !== null) {
|
|
169
|
-
const wrapped = {};
|
|
170
|
-
for (const key of Object.keys(node)) {
|
|
171
|
-
wrapped[key] = decorateTransitionSelectorNode(node[key]);
|
|
172
|
-
}
|
|
173
|
-
return Object.freeze(wrapped);
|
|
174
|
-
}
|
|
175
|
-
return node;
|
|
176
|
-
};
|
|
177
|
-
const decorateBranches = (declarations, reenterSource = false) => Object.freeze({
|
|
178
|
-
reenter: () => decorateBranches(declarations, true),
|
|
179
|
-
resolve: (resolve, options) => Object.freeze({
|
|
180
|
-
[TransitionBuilderDescriptorTypeId]: TransitionBuilderDescriptorTypeId,
|
|
181
|
-
type: "branches",
|
|
182
|
-
declarations,
|
|
183
|
-
resolve,
|
|
184
|
-
reenterSource,
|
|
185
|
-
...transitionOptions(options)
|
|
186
|
-
})
|
|
187
|
-
});
|
|
188
|
-
const makeTransitionSelector = (stateNodes, source) => Object.freeze({
|
|
189
|
-
...decorateTransitionSelectorNode(makeTargetSelector(stateNodes, source)),
|
|
190
|
-
branches: (declarations) => decorateBranches(declarations)
|
|
191
|
-
});
|
|
192
|
-
const normalizeTransitionBuilder = (transition, stateNodes, path) => {
|
|
193
|
-
const result = transition(makeTransitionSelector(stateNodes, path));
|
|
194
|
-
if (Topology.isTargetSelection(result) && !hasProperty(result, TransitionBuilderDescriptorTypeId)) {
|
|
195
|
-
const selection = plainTargetSelection(result);
|
|
196
|
-
return { target: () => selection };
|
|
197
|
-
}
|
|
198
|
-
if (!hasProperty(result, TransitionBuilderDescriptorTypeId))
|
|
199
|
-
return result;
|
|
200
|
-
const descriptor = result;
|
|
201
|
-
if (descriptor.type === "branches") {
|
|
202
|
-
return {
|
|
203
|
-
branches: () => descriptor.declarations,
|
|
204
|
-
resolve: descriptor.resolve,
|
|
205
|
-
reenter: descriptor.reenterSource,
|
|
206
|
-
declinable: descriptor.declinable
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
return {
|
|
210
|
-
target: () => descriptor.selection,
|
|
211
|
-
resolve: descriptor.resolver,
|
|
212
|
-
reenter: descriptor.reenterSource,
|
|
213
|
-
declinable: descriptor.declinable
|
|
214
|
-
};
|
|
215
|
-
};
|
|
216
72
|
const transitionTargetSelection = (selection) => Object.freeze({
|
|
217
73
|
path: selection.path,
|
|
218
74
|
kind: selection.kind,
|
|
@@ -221,90 +77,7 @@ const transitionTargetSelection = (selection) => Object.freeze({
|
|
|
221
77
|
const selectionUpdates = (selection) => selection.updatePath === undefined ?
|
|
222
78
|
selection.kind === "update" && selection.path !== undefined ? [selection.path] : []
|
|
223
79
|
: [selection.updatePath];
|
|
224
|
-
const makeSelectionMethod = (kind, path, scope) => () => Topology.makeTargetSelection(kind, path, scope);
|
|
225
|
-
const makeSelectionValue = (kind, path, scope) => Topology.makeTargetSelection(kind, path, scope);
|
|
226
80
|
const makeStateUpdateSelection = (path, scope) => Topology.makeTargetSelection("update", path, scope);
|
|
227
|
-
const addSelectionChildren = (builder, stateNodes, parent, scope, source) => {
|
|
228
|
-
for (const node of stateNodes.byPath.values()) {
|
|
229
|
-
if (node.parent !== parent || node.type === "history")
|
|
230
|
-
continue;
|
|
231
|
-
builder[node.key] = makeSelectionNode(stateNodes, node.path, scope, source);
|
|
232
|
-
}
|
|
233
|
-
};
|
|
234
|
-
const makeSelectionNode = (stateNodes, path, scope, source) => {
|
|
235
|
-
const node = getTargetBuilderNode(stateNodes, path);
|
|
236
|
-
const kind = node.type === "choice" ? "choice" : "state";
|
|
237
|
-
const method = makeSelectionMethod(kind, path, scope);
|
|
238
|
-
if (node.type !== "atomic" && node.type !== "final" && node.type !== "choice" && node.type !== "history") {
|
|
239
|
-
Object.defineProperty(method, "initial", {
|
|
240
|
-
value: makeSelectionValue("initial", path, scope),
|
|
241
|
-
enumerable: true
|
|
242
|
-
});
|
|
243
|
-
if (scope === "local" || scope === "branch") {
|
|
244
|
-
addSelectionChildren(method, stateNodes, path, scope, source);
|
|
245
|
-
}
|
|
246
|
-
if (scope === "branch" && source !== undefined && node.schema !== undefined &&
|
|
247
|
-
(source === path || Configuration.isDescendantOf(source, path)) &&
|
|
248
|
-
getTargetBuilderNode(stateNodes, source).type !== "choice") {
|
|
249
|
-
Object.defineProperty(method, "update", {
|
|
250
|
-
value: makeStateUpdateSelection(path, "branch"),
|
|
251
|
-
enumerable: true
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
if (node.type === "atomic" && scope === "branch" && source === path && node.schema !== undefined) {
|
|
256
|
-
Object.defineProperty(method, "update", { value: makeStateUpdateSelection(path, "branch"), enumerable: true });
|
|
257
|
-
}
|
|
258
|
-
return method;
|
|
259
|
-
};
|
|
260
|
-
const makeHistorySelectionTree = (stateNodes, parent) => {
|
|
261
|
-
const builder = {};
|
|
262
|
-
for (const node of stateNodes.byPath.values()) {
|
|
263
|
-
if (node.parent !== parent)
|
|
264
|
-
continue;
|
|
265
|
-
if (node.type === "history") {
|
|
266
|
-
builder[node.key] = makeSelectionValue("history", node.path, "full");
|
|
267
|
-
}
|
|
268
|
-
else if (node.type !== "choice") {
|
|
269
|
-
const children = makeHistorySelectionTree(stateNodes, node.path);
|
|
270
|
-
if (Object.keys(children).length > 0)
|
|
271
|
-
builder[node.key] = children;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
return builder;
|
|
275
|
-
};
|
|
276
|
-
const makeTargetSelector = (stateNodes, source) => {
|
|
277
|
-
const full = Object.assign({}, makeSelectionValue("state", "", "full"), {
|
|
278
|
-
initial: makeSelectionValue("initial", "", "full")
|
|
279
|
-
});
|
|
280
|
-
const branch = {};
|
|
281
|
-
const root = getTargetBuilderNode(stateNodes, stateNodes.roots[0]);
|
|
282
|
-
addSelectionChildren(branch, stateNodes, root.path, "branch", source);
|
|
283
|
-
const local = {};
|
|
284
|
-
const localScope = getLocalTargetScope(stateNodes, source);
|
|
285
|
-
if (localScope !== undefined) {
|
|
286
|
-
const localScopeNode = getTargetBuilderNode(stateNodes, localScope);
|
|
287
|
-
if (localScopeNode.schema !== undefined) {
|
|
288
|
-
local.with = makeSelectionValue("state", localScope, "local");
|
|
289
|
-
if (getTargetBuilderNode(stateNodes, source).type !== "choice") {
|
|
290
|
-
local.update = makeStateUpdateSelection(localScope, "local");
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
addSelectionChildren(local, stateNodes, localScope, "local");
|
|
294
|
-
}
|
|
295
|
-
return {
|
|
296
|
-
none: Topology.noneTargetSelection,
|
|
297
|
-
self: getTargetBuilderNode(stateNodes, source).schema === undefined ||
|
|
298
|
-
getTargetBuilderNode(stateNodes, source).type === "final"
|
|
299
|
-
? {}
|
|
300
|
-
: { update: makeStateUpdateSelection(source, "branch") },
|
|
301
|
-
root: makeSelectionNode(stateNodes, root.path, "branch", source),
|
|
302
|
-
local,
|
|
303
|
-
branch,
|
|
304
|
-
full,
|
|
305
|
-
history: makeHistorySelectionTree(stateNodes, "")
|
|
306
|
-
};
|
|
307
|
-
};
|
|
308
81
|
const captureDefinitionBranch = (branch, selector, stateNodes, path, trigger) => {
|
|
309
82
|
if (typeof branch !== "object" || branch === null || !hasProperty(branch, "target") ||
|
|
310
83
|
typeof branch.target !== "function") {
|
|
@@ -436,7 +209,12 @@ const runCapturedBranch = (branch, context, enqueue, stateNodes, source) => {
|
|
|
436
209
|
const selectedTarget = getSelectionBuilder(context.target, branch.selection, stateNodes, source);
|
|
437
210
|
if (branch.resolve === undefined)
|
|
438
211
|
return constructSelectedTarget(selectedTarget);
|
|
439
|
-
|
|
212
|
+
// This fresh context is owned by this evaluation and can be retained by user callbacks.
|
|
213
|
+
const resolverContext = {
|
|
214
|
+
...context,
|
|
215
|
+
root: source === "" ? context.state : context.ancestors[""],
|
|
216
|
+
decline: Topology.makeDeclined
|
|
217
|
+
};
|
|
440
218
|
if (branch.selection.kind === "none")
|
|
441
219
|
delete resolverContext.target;
|
|
442
220
|
else
|
|
@@ -452,8 +230,6 @@ const runCapturedBranch = (branch, context, enqueue, stateNodes, source) => {
|
|
|
452
230
|
resolverContext.current = context.ancestors[ownerPath];
|
|
453
231
|
resolverContext.owner = getSelectionBuilder(context.target, makeStateUpdateSelection(ownerPath, "branch"), stateNodes, source);
|
|
454
232
|
}
|
|
455
|
-
if (branch.declinable === true)
|
|
456
|
-
resolverContext.decline = Topology.makeDeclined;
|
|
457
233
|
const resolved = branch.resolve(resolverContext, enqueue);
|
|
458
234
|
if (Topology.isDeclined(resolved)) {
|
|
459
235
|
if (branch.declinable !== true) {
|
|
@@ -492,21 +268,23 @@ const captureNamedBranches = (declarations, path, trigger) => {
|
|
|
492
268
|
if (!Topology.isTargetSelection(target)) {
|
|
493
269
|
throw new Error(`Machine transition branch "${key}" must select exactly one target`);
|
|
494
270
|
}
|
|
495
|
-
if (target.updatePath !== undefined) {
|
|
496
|
-
throw new Error(`Machine transition branch "${key}" cannot declare an updating target`);
|
|
497
|
-
}
|
|
498
|
-
if (hasProperty(target, TransitionBuilderDescriptorTypeId)) {
|
|
499
|
-
throw new Error(`Machine transition branch "${key}" requires a topology selection; apply .reenter() to .branches(...)`);
|
|
500
|
-
}
|
|
501
271
|
if (title !== undefined && (typeof title !== "string" || title.length === 0)) {
|
|
502
272
|
throw new Error(`Machine transition branch "${key}" title must be a non-empty string`);
|
|
503
273
|
}
|
|
504
274
|
return Object.freeze({ key, title: title ?? key, selection: plainTargetSelection(target) });
|
|
505
275
|
}));
|
|
506
276
|
};
|
|
507
|
-
const wrapSelectedBranchBuilder = (builder, owner, branchIndex, branchKey) => {
|
|
277
|
+
const wrapSelectedBranchBuilder = (builder, owner, branchIndex, branchKey, updateBuilder) => {
|
|
508
278
|
if (typeof builder === "function") {
|
|
509
|
-
const wrapped = (...args) =>
|
|
279
|
+
const wrapped = (...args) => {
|
|
280
|
+
const result = builder(...args);
|
|
281
|
+
if (updateBuilder !== undefined) {
|
|
282
|
+
return Object.freeze({
|
|
283
|
+
update: Object.freeze(Object.fromEntries(Object.getOwnPropertyNames(updateBuilder).map((method) => [method, (value) => Topology.makeSelectedBranch(owner, branchIndex, branchKey, result.update(updateBuilder[method](value)))])))
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
return Topology.makeSelectedBranch(owner, branchIndex, branchKey, result);
|
|
287
|
+
};
|
|
510
288
|
for (const property of Reflect.ownKeys(builder)) {
|
|
511
289
|
if (property === "length" || property === "name" || property === "prototype" || property === "caller" ||
|
|
512
290
|
property === "arguments")
|
|
@@ -515,7 +293,7 @@ const wrapSelectedBranchBuilder = (builder, owner, branchIndex, branchKey) => {
|
|
|
515
293
|
if (descriptor === undefined)
|
|
516
294
|
continue;
|
|
517
295
|
if ("value" in descriptor && typeof descriptor.value === "function") {
|
|
518
|
-
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey);
|
|
296
|
+
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder);
|
|
519
297
|
}
|
|
520
298
|
Object.defineProperty(wrapped, property, descriptor);
|
|
521
299
|
}
|
|
@@ -528,7 +306,7 @@ const wrapSelectedBranchBuilder = (builder, owner, branchIndex, branchKey) => {
|
|
|
528
306
|
if (descriptor === undefined)
|
|
529
307
|
continue;
|
|
530
308
|
if ("value" in descriptor && typeof descriptor.value === "function") {
|
|
531
|
-
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey);
|
|
309
|
+
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder);
|
|
532
310
|
}
|
|
533
311
|
Object.defineProperty(wrapped, property, descriptor);
|
|
534
312
|
}
|
|
@@ -540,7 +318,9 @@ const makeBranchSelectors = (context, branches, owner, stateNodes, source) => {
|
|
|
540
318
|
const select = Object.create(null);
|
|
541
319
|
for (let branchIndex = 0; branchIndex < branches.length; branchIndex++) {
|
|
542
320
|
const branch = branches[branchIndex];
|
|
543
|
-
select[branch.key] = wrapSelectedBranchBuilder(getSelectionBuilder(context.target, branch.selection, stateNodes, source), owner, branchIndex, branch.key
|
|
321
|
+
select[branch.key] = wrapSelectedBranchBuilder(getSelectionBuilder(context.target, branch.selection, stateNodes, source), owner, branchIndex, branch.key, branch.selection.updatePath === undefined
|
|
322
|
+
? undefined
|
|
323
|
+
: getSelectionBuilder(context.target, makeStateUpdateSelection(branch.selection.updatePath, "branch"), stateNodes, source));
|
|
544
324
|
}
|
|
545
325
|
return Object.freeze(select);
|
|
546
326
|
};
|
|
@@ -556,16 +336,102 @@ const validateSelectedBranchResult = (result, selection, stateNodes) => {
|
|
|
556
336
|
}
|
|
557
337
|
validateResolvedSelection(result, selection, stateNodes);
|
|
558
338
|
};
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
339
|
+
// Invocation input contexts do not pass through captured transition evaluation.
|
|
340
|
+
const objectContext = (context, path) => ({
|
|
341
|
+
...context,
|
|
342
|
+
root: path === "" ? context.state : context.ancestors[""]
|
|
343
|
+
});
|
|
344
|
+
const normalizeObjectTransition = (raw, declaration, path, stateNodes) => {
|
|
345
|
+
if (typeof raw !== "object" || raw === null)
|
|
346
|
+
throw new Error(`Machine transition for "${path}" must be an object`);
|
|
347
|
+
const config = { ...raw };
|
|
348
|
+
const allowed = [
|
|
349
|
+
"target",
|
|
350
|
+
"update",
|
|
351
|
+
"initial",
|
|
352
|
+
"history",
|
|
353
|
+
"none",
|
|
354
|
+
"branches",
|
|
355
|
+
"data",
|
|
356
|
+
"decoded",
|
|
357
|
+
"resolve",
|
|
358
|
+
"guard",
|
|
359
|
+
"reenter",
|
|
360
|
+
"declinable"
|
|
361
|
+
];
|
|
362
|
+
for (const key of Reflect.ownKeys(config)) {
|
|
363
|
+
if (typeof key !== "string" || !allowed.includes(key)) {
|
|
364
|
+
throw new Error("Machine transition contains an unknown field");
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
for (const key of ["resolve", "guard"]) {
|
|
368
|
+
if (config[key] !== undefined && typeof config[key] !== "function") {
|
|
369
|
+
throw new Error(`Machine transition ${key} must be a function`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
const guard = typeof config.guard === "function" ? config.guard : undefined;
|
|
373
|
+
const resolve = typeof config.resolve === "function" ? config.resolve : undefined;
|
|
374
|
+
if (config.decoded !== undefined && typeof config.decoded !== "boolean") {
|
|
375
|
+
throw new Error("Machine decoded must be a boolean construction flag");
|
|
376
|
+
}
|
|
377
|
+
const hasData = Object.prototype.hasOwnProperty.call(config, "data");
|
|
378
|
+
if (config.decoded === true && !hasData)
|
|
379
|
+
throw new Error("Machine decoded construction requires data");
|
|
380
|
+
if (hasData && resolve !== undefined)
|
|
381
|
+
throw new Error("Machine data and resolve are mutually exclusive");
|
|
382
|
+
const data = config.data;
|
|
383
|
+
const construct = typeof data === "function" ? data : () => data;
|
|
384
|
+
const guarded = guard !== undefined;
|
|
385
|
+
const declinable = guarded || config.declinable === true;
|
|
386
|
+
if (typeof config.branches === "string") {
|
|
387
|
+
const group = declaration.branches.get(config.branches);
|
|
388
|
+
if (group === undefined || resolve === undefined) {
|
|
389
|
+
throw new Error("Machine branching transition requires a registered group and resolver");
|
|
390
|
+
}
|
|
391
|
+
if (["target", "update", "initial", "history", "none", "data", "decoded"].some((key) => config[key] !== undefined))
|
|
392
|
+
throw new Error("Machine branching transition cannot redeclare its destination");
|
|
393
|
+
const entries = Object.fromEntries(Object.entries(group).map(([key, spec]) => [key, {
|
|
394
|
+
title: spec.title,
|
|
395
|
+
target: captureDefinitionBranch({ target: () => Declaration.selection(declaration, spec) }, undefined, stateNodes, path, key).selection
|
|
396
|
+
}]));
|
|
397
|
+
return {
|
|
398
|
+
branches: () => entries,
|
|
399
|
+
reenter: config.reenter,
|
|
400
|
+
declinable,
|
|
401
|
+
resolve: (context, enqueue) => {
|
|
402
|
+
return guarded && !guard(context) ? Topology.makeDeclined() : resolve(context, enqueue);
|
|
403
|
+
}
|
|
404
|
+
};
|
|
562
405
|
}
|
|
563
|
-
const
|
|
406
|
+
const selection = Declaration.selection(declaration, config);
|
|
407
|
+
if (resolve !== undefined && selection.kind !== "none") {
|
|
408
|
+
throw new Error("Machine advanced construction requires a declared branch group");
|
|
409
|
+
}
|
|
410
|
+
const method = hasData ? config.decoded === true ? "decoded" : "from" : undefined;
|
|
411
|
+
return {
|
|
412
|
+
target: () => selection,
|
|
413
|
+
reenter: config.reenter,
|
|
414
|
+
declinable,
|
|
415
|
+
...(!guarded && method === undefined && resolve === undefined ? {} : {
|
|
416
|
+
resolve: (context, enqueue) => {
|
|
417
|
+
if (guarded && !guard(context))
|
|
418
|
+
return Topology.makeDeclined();
|
|
419
|
+
if (method !== undefined && construct !== undefined) {
|
|
420
|
+
return constructSelectionValue(selection, context, method, construct(context));
|
|
421
|
+
}
|
|
422
|
+
if (resolve !== undefined)
|
|
423
|
+
return resolve(context, enqueue);
|
|
424
|
+
return selection.kind === "none" ? undefined : constructSelectedTarget(context.target);
|
|
425
|
+
}
|
|
426
|
+
})
|
|
427
|
+
};
|
|
428
|
+
};
|
|
429
|
+
const captureTransition = (rawTransition, stateNodes, path, declaration, trigger) => {
|
|
430
|
+
const transition = normalizeObjectTransition(rawTransition, declaration, path, stateNodes);
|
|
564
431
|
if (typeof transition !== "object" || transition === null) {
|
|
565
432
|
throw new Error(`Machine transition for state "${path}" on "${String(trigger)}" must be an object`);
|
|
566
433
|
}
|
|
567
434
|
const definition = transition;
|
|
568
|
-
const selector = makeTargetSelector(stateNodes, path);
|
|
569
435
|
const reenter = definition.reenter === true;
|
|
570
436
|
const declinable = definition.declinable === true;
|
|
571
437
|
if (hasProperty(definition, "branches")) {
|
|
@@ -574,14 +440,16 @@ const captureTransition = (rawTransition, stateNodes, path, trigger) => {
|
|
|
574
440
|
throw new Error(`Machine branching transition for state "${path}" on "${String(trigger)}" requires branches and resolve`);
|
|
575
441
|
}
|
|
576
442
|
const resolve = branching.resolve;
|
|
577
|
-
const branches = captureNamedBranches(branching.branches(
|
|
443
|
+
const branches = captureNamedBranches(branching.branches(), path, trigger);
|
|
578
444
|
const owner = Object.freeze({});
|
|
579
445
|
const evaluate = (context, enqueue) => {
|
|
580
|
-
const resolverContext = {
|
|
446
|
+
const resolverContext = {
|
|
447
|
+
...context,
|
|
448
|
+
root: path === "" ? context.state : context.ancestors[""],
|
|
449
|
+
decline: Topology.makeDeclined
|
|
450
|
+
};
|
|
581
451
|
delete resolverContext.target;
|
|
582
452
|
resolverContext.select = makeBranchSelectors(context, branches, owner, stateNodes, path);
|
|
583
|
-
if (declinable)
|
|
584
|
-
resolverContext.decline = Topology.makeDeclined;
|
|
585
453
|
const selected = resolve(resolverContext, enqueue);
|
|
586
454
|
if (Topology.isDeclined(selected)) {
|
|
587
455
|
if (!declinable) {
|
|
@@ -621,7 +489,7 @@ const captureTransition = (rawTransition, stateNodes, path, trigger) => {
|
|
|
621
489
|
transition: (context, enqueue) => evaluate(context, enqueue).result
|
|
622
490
|
};
|
|
623
491
|
}
|
|
624
|
-
const branch = captureDefinitionBranch(transition,
|
|
492
|
+
const branch = captureDefinitionBranch(transition, undefined, stateNodes, path, trigger);
|
|
625
493
|
const evaluate = (context, enqueue) => ({
|
|
626
494
|
result: runCapturedBranch(branch, context, enqueue, stateNodes, path),
|
|
627
495
|
branchIndex: 0,
|
|
@@ -641,67 +509,32 @@ const captureTransition = (rawTransition, stateNodes, path, trigger) => {
|
|
|
641
509
|
transition: (context, enqueue) => evaluate(context, enqueue).result
|
|
642
510
|
};
|
|
643
511
|
};
|
|
644
|
-
const captureEventHandlers = (on, stateNodes, path) => {
|
|
512
|
+
const captureEventHandlers = (on, stateNodes, path, declaration) => {
|
|
645
513
|
// The machine owns its dispatch table. Compiled plans may snapshot these
|
|
646
514
|
// definitions, so retaining caller-owned containers would let strategies
|
|
647
515
|
// observe different handlers after an unsafe external mutation.
|
|
648
516
|
const captured = Object.create(null);
|
|
649
517
|
for (const event of Reflect.ownKeys(on)) {
|
|
650
|
-
captured[event] = captureTransition(on[event], stateNodes, path, event);
|
|
518
|
+
captured[event] = captureTransition(on[event], stateNodes, path, declaration, event);
|
|
651
519
|
}
|
|
652
520
|
return captured;
|
|
653
521
|
};
|
|
654
|
-
const
|
|
655
|
-
const
|
|
656
|
-
[InvokeBuilderDescriptorTypeId]: InvokeBuilderDescriptorTypeId,
|
|
657
|
-
config
|
|
658
|
-
};
|
|
659
|
-
for (const channel of channels) {
|
|
660
|
-
if (!hasProperty(config, channel)) {
|
|
661
|
-
builder[channel] = (handler) => makeInvokeBuilder({ ...config, [channel]: handler }, channels);
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
return Object.freeze(builder);
|
|
665
|
-
};
|
|
666
|
-
const invokeSelector = Object.freeze({
|
|
667
|
-
effect: (id, effect) => makeInvokeBuilder({ id, effect }, ["onDone", "onFailure"]),
|
|
668
|
-
stream: (id, stream) => makeInvokeBuilder({ id, stream }, ["onElement", "onDone", "onFailure"]),
|
|
669
|
-
timer: (id, after) => makeInvokeBuilder({ id, after }, ["onDone"]),
|
|
670
|
-
logic: (id, options) => makeInvokeBuilder({ id, ...options }, ["onSnapshot", "onDone", "onFailure"]),
|
|
671
|
-
child: (child, options) => makeInvokeBuilder(options === undefined ? { child } : { child, ...options }, [
|
|
672
|
-
"onSnapshot",
|
|
673
|
-
"onDone",
|
|
674
|
-
"onFailure"
|
|
675
|
-
])
|
|
676
|
-
});
|
|
677
|
-
const invokeBuilderConfig = (value, path) => {
|
|
678
|
-
if (typeof value !== "object" || value === null ||
|
|
679
|
-
!hasProperty(value, InvokeBuilderDescriptorTypeId) ||
|
|
680
|
-
value[InvokeBuilderDescriptorTypeId] !== InvokeBuilderDescriptorTypeId) {
|
|
681
|
-
throw new Error(`Machine invocation for state "${path}" must be constructed from its source selector`);
|
|
682
|
-
}
|
|
683
|
-
return value.config;
|
|
684
|
-
};
|
|
685
|
-
const captureInvokeDefinition = (invoke, stateNodes, path) => {
|
|
686
|
-
if (typeof invoke !== "function") {
|
|
687
|
-
throw new Error(`Machine invocation for state "${path}" must be a source-first callback`);
|
|
688
|
-
}
|
|
689
|
-
const authored = invoke(invokeSelector);
|
|
690
|
-
const definitions = Array.isArray(authored) ? authored : [authored];
|
|
522
|
+
const captureInvokeDefinition = (invoke, stateNodes, path, declaration) => {
|
|
523
|
+
const definitions = Array.isArray(invoke) ? invoke : [invoke];
|
|
691
524
|
const capturedDefinitions = definitions.map((definition) => {
|
|
692
|
-
const captured =
|
|
525
|
+
const captured = Declaration.invocation(declaration, definition, path, (context) => objectContext(context, path));
|
|
693
526
|
for (const key of ["onElement", "onDone", "onFailure", "onSnapshot"]) {
|
|
694
527
|
if (captured[key] !== undefined) {
|
|
695
|
-
captured[key] = captureTransition(captured[key], stateNodes, path, key);
|
|
528
|
+
captured[key] = captureTransition(captured[key], stateNodes, path, declaration, key);
|
|
696
529
|
}
|
|
697
530
|
}
|
|
698
531
|
return InvocationDefinition.capture(captured, path);
|
|
699
532
|
});
|
|
700
|
-
if (Array.isArray(
|
|
533
|
+
if (Array.isArray(invoke))
|
|
701
534
|
return Object.freeze(capturedDefinitions);
|
|
702
535
|
return capturedDefinitions[0];
|
|
703
536
|
};
|
|
704
|
-
const flattenHandlers = (handlers, stateNodes, states, prefix, config) => {
|
|
537
|
+
const flattenHandlers = (handlers, stateNodes, states, prefix, declaration, config) => {
|
|
705
538
|
for (const key of Object.keys(config)) {
|
|
706
539
|
const path = prefix === "" ? key : `${prefix}.${key}`;
|
|
707
540
|
if (!hasProperty(states, key)) {
|
|
@@ -720,20 +553,20 @@ const flattenHandlers = (handlers, stateNodes, states, prefix, config) => {
|
|
|
720
553
|
}
|
|
721
554
|
const on = stateConfig.on;
|
|
722
555
|
if (typeof on === "object" && on !== null) {
|
|
723
|
-
const capturedOn = captureEventHandlers(on, stateNodes, path);
|
|
556
|
+
const capturedOn = captureEventHandlers(on, stateNodes, path, declaration);
|
|
724
557
|
stateConfig.on = capturedOn;
|
|
725
558
|
}
|
|
726
559
|
if (stateConfig.always !== undefined) {
|
|
727
|
-
stateConfig.always = captureTransition(stateConfig.always, stateNodes, path, "always");
|
|
560
|
+
stateConfig.always = captureTransition(stateConfig.always, stateNodes, path, declaration, "always");
|
|
728
561
|
}
|
|
729
562
|
if (stateConfig.onDone !== undefined) {
|
|
730
|
-
stateConfig.onDone = captureTransition(stateConfig.onDone, stateNodes, path, "done");
|
|
563
|
+
stateConfig.onDone = captureTransition(stateConfig.onDone, stateNodes, path, declaration, "done");
|
|
731
564
|
}
|
|
732
565
|
if (stateConfig.choice !== undefined) {
|
|
733
|
-
stateConfig.choice = captureTransition(stateConfig.choice, stateNodes, path, "choice");
|
|
566
|
+
stateConfig.choice = captureTransition(stateConfig.choice, stateNodes, path, declaration, "choice");
|
|
734
567
|
}
|
|
735
568
|
if (stateConfig.invoke !== undefined) {
|
|
736
|
-
stateConfig.invoke = captureInvokeDefinition(stateConfig.invoke, stateNodes, path);
|
|
569
|
+
stateConfig.invoke = captureInvokeDefinition(stateConfig.invoke, stateNodes, path, declaration);
|
|
737
570
|
}
|
|
738
571
|
const node = stateNodes.byPath.get(path);
|
|
739
572
|
if (node?.type === "choice") {
|
|
@@ -753,84 +586,34 @@ const flattenHandlers = (handlers, stateNodes, states, prefix, config) => {
|
|
|
753
586
|
if (typeof childConfig !== "object" || childConfig === null) {
|
|
754
587
|
throw new Error(`Machine expected state "${path}" child handlers to be an object`);
|
|
755
588
|
}
|
|
756
|
-
flattenHandlers(handlers, stateNodes, node.states, path, childConfig);
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
};
|
|
760
|
-
const makeHandle = (self) => ((config) => {
|
|
589
|
+
flattenHandlers(handlers, stateNodes, node.states, path, declaration, childConfig);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
const makeHandle = (self, declaration) => ((config) => {
|
|
594
|
+
const captured = InitialDeclaration.capture(declaration.root, config);
|
|
595
|
+
const compiled = Object.assign(Object.create(Proto), self);
|
|
596
|
+
Protocol.copyProtocol(self, compiled);
|
|
597
|
+
compiled.states = Object.freeze({ "": captured.node });
|
|
598
|
+
compiled.stateNodes = Topology.compileStateNodes(compiled.states);
|
|
599
|
+
if (compiled.stateNodes.byPath.get("").schema === undefined && config.root !== undefined) {
|
|
600
|
+
throw new Error("Machine schema-less root cannot construct data");
|
|
601
|
+
}
|
|
602
|
+
const constructRoot = compiled.stateNodes.byPath.get("").schema === undefined
|
|
603
|
+
? () => undefined
|
|
604
|
+
: InitialDeclaration.construction(config.root);
|
|
605
|
+
compiled.initial = (input) => Topology.makeInitialTarget("", constructRoot({ input }));
|
|
606
|
+
compiled.initialDefinition = Object.freeze({
|
|
607
|
+
target: "",
|
|
608
|
+
selection: Object.freeze({ path: "", kind: "initial", scope: "initial" })
|
|
609
|
+
});
|
|
610
|
+
compiled.makeTargetBuilder = makeTargetBuilder(compiled.states, compiled.stateNodes);
|
|
761
611
|
const handlers = Object.create(null);
|
|
762
|
-
flattenHandlers(handlers,
|
|
763
|
-
return makeWithHandlers(
|
|
612
|
+
flattenHandlers(handlers, compiled.stateNodes, compiled.states, "", declaration, { "": captured.handlers });
|
|
613
|
+
return makeWithHandlers(compiled, handlers);
|
|
764
614
|
});
|
|
765
615
|
export const isMachine = (u) => hasProperty(u, TypeId) && u[TypeId] === TypeId;
|
|
766
616
|
export const isFinal = (machine, state) => internalPlanner.isFinal(machine, state);
|
|
767
|
-
const getInitialSelectionBuilder = (initialBuilder, selection) => {
|
|
768
|
-
const path = selection.path;
|
|
769
|
-
if (path === undefined || path.includes(".")) {
|
|
770
|
-
throw new Error("Machine initial target must select one top-level state");
|
|
771
|
-
}
|
|
772
|
-
const builder = initialBuilder[path];
|
|
773
|
-
if (typeof builder !== "object" || builder === null || typeof builder.from !== "function") {
|
|
774
|
-
throw new Error(`Machine could not construct selected initial state "${path}"`);
|
|
775
|
-
}
|
|
776
|
-
return builder;
|
|
777
|
-
};
|
|
778
|
-
const captureInitialBranch = (definition, initialBuilder, configuration) => {
|
|
779
|
-
if (definition !== undefined && typeof definition !== "function") {
|
|
780
|
-
throw new Error("Machine initial definition must be a target-first callback");
|
|
781
|
-
}
|
|
782
|
-
const selector = decorateInitialSelection(makeSelectionValue(configuration ? "state" : "initial", "", "initial"));
|
|
783
|
-
const result = definition === undefined ? selector : definition(selector);
|
|
784
|
-
let selection;
|
|
785
|
-
let resolve;
|
|
786
|
-
if (Topology.isTargetSelection(result)) {
|
|
787
|
-
selection = plainTargetSelection(result);
|
|
788
|
-
}
|
|
789
|
-
else if (hasProperty(result, InitialBuilderDescriptorTypeId)) {
|
|
790
|
-
const descriptor = result;
|
|
791
|
-
selection = descriptor.selection;
|
|
792
|
-
resolve = descriptor.resolve;
|
|
793
|
-
}
|
|
794
|
-
else {
|
|
795
|
-
throw new Error("Machine initial definition must select exactly one target");
|
|
796
|
-
}
|
|
797
|
-
if (selection.kind !== "state" && selection.kind !== "initial") {
|
|
798
|
-
throw new Error("Machine initial target must select a top-level state or its declared initial entry");
|
|
799
|
-
}
|
|
800
|
-
const captured = {
|
|
801
|
-
selection,
|
|
802
|
-
builder: getInitialSelectionBuilder(initialBuilder, selection)
|
|
803
|
-
};
|
|
804
|
-
return resolve === undefined ? Object.freeze(captured) : Object.freeze({ ...captured, resolve });
|
|
805
|
-
};
|
|
806
|
-
const validateInitialSelection = (result, selection) => {
|
|
807
|
-
if (typeof result !== "object" || result === null || !hasProperty(result, "path") || result.path !== selection.path) {
|
|
808
|
-
const resultPath = typeof result === "object" && result !== null && hasProperty(result, "path")
|
|
809
|
-
? String(result.path)
|
|
810
|
-
: "<invalid>";
|
|
811
|
-
throw new Error(`Machine initial resolver selected "${selection.path}" but constructed "${resultPath}"`);
|
|
812
|
-
}
|
|
813
|
-
};
|
|
814
|
-
const compileInitial = (definition, states, stateNodes, configuration = false) => {
|
|
815
|
-
const rootNode = getTargetBuilderNode(stateNodes, "");
|
|
816
|
-
const initialBuilder = configuration ?
|
|
817
|
-
makeSnapshotBuilder(states, { mode: "full", prefix: "" })
|
|
818
|
-
: { "": withFrom((value) => Topology.makeInitialTarget("", value), "leaf", rootNode.schema !== undefined) };
|
|
819
|
-
const branch = captureInitialBranch(definition, initialBuilder, configuration);
|
|
820
|
-
return {
|
|
821
|
-
initial: (input) => {
|
|
822
|
-
const result = branch.resolve === undefined
|
|
823
|
-
? constructSelectedTarget(branch.builder)
|
|
824
|
-
: branch.resolve({ input, target: branch.builder });
|
|
825
|
-
validateInitialSelection(result, branch.selection);
|
|
826
|
-
return result;
|
|
827
|
-
},
|
|
828
|
-
definition: Object.freeze({
|
|
829
|
-
target: branch.selection.path,
|
|
830
|
-
selection: transitionTargetSelection(branch.selection)
|
|
831
|
-
})
|
|
832
|
-
};
|
|
833
|
-
};
|
|
834
617
|
export const state = (node) => {
|
|
835
618
|
const captured = StateDefinition.captureRoot(node);
|
|
836
619
|
const access = makeStateHelpers();
|
|
@@ -849,8 +632,11 @@ const makeStateHelpers = () => {
|
|
|
849
632
|
};
|
|
850
633
|
};
|
|
851
634
|
export const make = (config) => {
|
|
635
|
+
if (Object.hasOwn(config, "initial") || Object.hasOwn(config, "initialConfiguration")) {
|
|
636
|
+
throw new Error("Machine initial edges and root data belong in handle");
|
|
637
|
+
}
|
|
852
638
|
const states = Object.freeze({ "": config.root.node });
|
|
853
|
-
const self = Object.create(
|
|
639
|
+
const self = Object.create(PipeablePrototype);
|
|
854
640
|
self.states = states;
|
|
855
641
|
self.root = config.root;
|
|
856
642
|
self.events = config.events;
|
|
@@ -859,13 +645,8 @@ export const make = (config) => {
|
|
|
859
645
|
self.parent = config.parent;
|
|
860
646
|
self.input = config.input;
|
|
861
647
|
self.id = config.id;
|
|
862
|
-
self.stateNodes = Topology.compileStateNodes(states);
|
|
863
|
-
const compiledInitial = compileInitial(config.initialConfiguration ?? config.initial, states, self.stateNodes, config.initialConfiguration !== undefined);
|
|
864
|
-
self.initial = compiledInitial.initial;
|
|
865
|
-
self.initialDefinition = compiledInitial.definition;
|
|
866
|
-
self.makeTargetBuilder = makeTargetBuilder(states, self.stateNodes);
|
|
867
648
|
self.handlers = Object.create(null);
|
|
868
|
-
self.handle = makeHandle(self);
|
|
649
|
+
self.handle = makeHandle(self, Declaration.capture(config.root, config));
|
|
869
650
|
Protocol.setProtocol(self);
|
|
870
651
|
return self;
|
|
871
652
|
};
|