@typeonce/effect-machine 0.32.0 → 0.34.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 +216 -635
- package/dist/Machine.d.ts +814 -545
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +20 -8
- 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 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +38 -45
- 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 +5 -4
- 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/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 -2
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +149 -280
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +12 -11
- 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/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/targetReference.d.ts +15 -0
- package/dist/internal/machine/targetReference.d.ts.map +1 -0
- package/dist/internal/machine/targetReference.js +31 -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 +2 -1
- 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 +38 -40
- 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/unstable/cluster/ClusterMachine.d.ts +5 -5
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +5 -17
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/docs/agent-guide.md +66 -91
- package/docs/machine-review.md +19 -49
- package/docs/root-api.md +68 -23
- package/package.json +1 -1
- package/src/Machine.ts +1387 -2011
- 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 -45
- package/src/internal/machine/declaration.ts +140 -0
- package/src/internal/machine/executionPlan.ts +6 -5
- package/src/internal/machine/implementation.ts +41 -0
- package/src/internal/machine/invocation.ts +3 -3
- package/src/internal/machine/invocationDefinition.ts +1 -1
- package/src/internal/machine/machine.ts +210 -389
- package/src/internal/machine/planner.ts +54 -35
- package/src/internal/machine/process.ts +40 -33
- 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/targetReference.ts +40 -0
- package/src/internal/machine/topology.ts +2 -1
- package/src/internal/testing/machine/finiteModel.ts +42 -43
- package/src/internal/testing/machine/probe.ts +1 -1
- package/src/unstable/cluster/ClusterMachine.ts +9 -17
|
@@ -6,23 +6,26 @@ 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";
|
|
9
10
|
import * as InvocationDefinition from "./invocationDefinition.js";
|
|
10
11
|
import * as internalPlanner from "./planner.js";
|
|
11
12
|
import * as internalProcess from "./process.js";
|
|
12
13
|
import * as Protocol from "./protocol.js";
|
|
13
|
-
import * as internalRuntime from "./
|
|
14
|
+
import * as internalRuntime from "./runtimeProtocol.js";
|
|
14
15
|
import * as Serialization from "./serialization.js";
|
|
15
16
|
import * as StateDefinition from "./stateDefinition.js";
|
|
16
17
|
import { ChildMachineLogicTypeId } from "./symbols.js";
|
|
17
18
|
import { getLocalTargetScope, getTargetBuilderNode, makeSnapshotBuilder, makeTargetBuilder, withFrom } from "./targetBuilder.js";
|
|
19
|
+
import * as TargetReference from "./targetReference.js";
|
|
18
20
|
import * as Topology from "./topology.js";
|
|
19
21
|
export { ChildAlreadyExistsError, InfiniteTransitionError, MachineSchemaDecodeError, MachineSchemaEncodeError, ProcessLocalError, StartupError, StoppedError } from "./errors.js";
|
|
20
22
|
export { ChildMachineLogicTypeId, InitialEventTypeId, SnapshotBuilderStateTypeId } from "./symbols.js";
|
|
23
|
+
/** Internal seam for the public target-reference factory and its opaque brand. */
|
|
24
|
+
export const TargetReferenceTypeId = TargetReference.TypeId;
|
|
25
|
+
export const targets = TargetReference.make;
|
|
21
26
|
const TypeId = "~effect/Machine";
|
|
22
27
|
const ParentTypeId = "~effect/Machine/Parent";
|
|
23
28
|
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
29
|
const ChildMachineTypeId = "~effect/Machine/ChildMachine";
|
|
27
30
|
const Proto = {
|
|
28
31
|
...Inspectable.BaseProto,
|
|
@@ -52,66 +55,20 @@ const makeWithHandlers = (self, handlers) => {
|
|
|
52
55
|
Protocol.copyProtocol(self, machine);
|
|
53
56
|
return machine;
|
|
54
57
|
};
|
|
55
|
-
const TransitionBuilderDescriptorTypeId = Symbol("effect/Machine/TransitionBuilderDescriptor");
|
|
56
58
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const makeDirectTransitionDescriptor = (selection, resolve, options) => {
|
|
70
|
-
const shared = {
|
|
71
|
-
[TransitionBuilderDescriptorTypeId]: TransitionBuilderDescriptorTypeId,
|
|
72
|
-
type: "direct",
|
|
73
|
-
selection: plainTargetSelection(selection),
|
|
74
|
-
...transitionOptions(options)
|
|
75
|
-
};
|
|
76
|
-
return resolve === undefined
|
|
77
|
-
? Object.freeze(shared)
|
|
78
|
-
: Object.freeze({ ...shared, resolver: resolve });
|
|
79
|
-
};
|
|
80
|
-
const guardedSelection = (selection, predicate) => {
|
|
81
|
-
const wrap = (resolve, options) => makeDirectTransitionDescriptor(selection, (context, enqueue) => predicate(context) ? resolve(context, enqueue) : Topology.makeDeclined(), { ...transitionOptions(options), declinable: true });
|
|
82
|
-
return Object.freeze({
|
|
83
|
-
...wrap((context) => (selection.kind === "none" ? Topology.makeNoTarget() : constructSelectedTarget(context.target))),
|
|
84
|
-
from: (value) => wrap((context) => context.target.from(value(context))),
|
|
85
|
-
decoded: (value) => wrap((context) => context.target.decoded(value(context))),
|
|
86
|
-
resolve: wrap
|
|
87
|
-
});
|
|
62
|
+
// The authored callback crosses an erased schema boundary here. Each builder
|
|
63
|
+
// retains its construction mode, and planning validates both resulting values.
|
|
64
|
+
const constructSelectionValue = (selection, context, method, value) => {
|
|
65
|
+
if (selection.kind === "update")
|
|
66
|
+
return context.owner[method](value);
|
|
67
|
+
if (selection.updatePath === undefined)
|
|
68
|
+
return context.target[method](value);
|
|
69
|
+
const values = value;
|
|
70
|
+
return context.target[method](values.target).update(context.owner[method](values.update));
|
|
88
71
|
};
|
|
89
|
-
const decorateTransitionSelection = (selection) => Object.freeze({
|
|
90
|
-
...selection,
|
|
91
|
-
guard: (predicate) => guardedSelection(selection, predicate),
|
|
92
|
-
from: (value) => makeDirectTransitionDescriptor(selection, (context) => context.target.from(value(context)), undefined),
|
|
93
|
-
decoded: (value) => makeDirectTransitionDescriptor(selection, (context) => context.target.decoded(value(context)), undefined),
|
|
94
|
-
resolve: (resolve, options) => makeDirectTransitionDescriptor(selection, resolve, options),
|
|
95
|
-
reenter: () => makeDirectTransitionDescriptor(selection, undefined, { reenter: true }),
|
|
96
|
-
updating: (owner) => {
|
|
97
|
-
if (typeof owner !== "function") {
|
|
98
|
-
throw new Error("Machine updating owner must be a state selector");
|
|
99
|
-
}
|
|
100
|
-
const ownerSelection = owner();
|
|
101
|
-
if (!Topology.isTargetSelection(ownerSelection) || ownerSelection.kind !== "state" ||
|
|
102
|
-
ownerSelection.scope !== "branch") {
|
|
103
|
-
throw new Error("Machine updating owner must be addressed by one branch state selector");
|
|
104
|
-
}
|
|
105
|
-
return decorateTransitionSelection(Topology.makeTargetSelection(selection.kind, selection.path, selection.scope, ownerSelection.path));
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
const decorateStateUpdateSelection = (selection) => Object.freeze({
|
|
109
|
-
...selection,
|
|
110
|
-
from: (value) => makeDirectTransitionDescriptor(selection, (context) => context.owner.from(value(context)), undefined),
|
|
111
|
-
decoded: (value) => makeDirectTransitionDescriptor(selection, (context) => context.owner.decoded(value(context)), undefined),
|
|
112
|
-
resolve: (resolve, options) => makeDirectTransitionDescriptor(selection, resolve, options)
|
|
113
|
-
});
|
|
114
|
-
const noneTransitionSelection = decorateTransitionSelection(Topology.noneTargetSelection);
|
|
115
72
|
const makeInitialBuilderDescriptor = (selection, resolve) => Object.freeze({
|
|
116
73
|
[InitialBuilderDescriptorTypeId]: InitialBuilderDescriptorTypeId,
|
|
117
74
|
selection: plainTargetSelection(selection),
|
|
@@ -142,73 +99,6 @@ const decorateInitialSelectorNode = (node) => {
|
|
|
142
99
|
}
|
|
143
100
|
return node;
|
|
144
101
|
};
|
|
145
|
-
const decorateTransitionSelectorNode = (node) => {
|
|
146
|
-
if (Topology.isTargetSelection(node)) {
|
|
147
|
-
if (node.kind === "state" && node.scope === "full" && hasProperty(node, "initial")) {
|
|
148
|
-
return Object.freeze({
|
|
149
|
-
...decorateTransitionSelection(node),
|
|
150
|
-
initial: decorateTransitionSelection(node.initial)
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
if (node.kind === "update")
|
|
154
|
-
return decorateStateUpdateSelection(node);
|
|
155
|
-
return node === Topology.noneTargetSelection ? noneTransitionSelection : decorateTransitionSelection(node);
|
|
156
|
-
}
|
|
157
|
-
if (typeof node === "function") {
|
|
158
|
-
const wrapped = ((...args) => decorateTransitionSelection(node(...args)));
|
|
159
|
-
for (const key of Object.keys(node)) {
|
|
160
|
-
wrapped[key] = decorateTransitionSelectorNode(node[key]);
|
|
161
|
-
}
|
|
162
|
-
return Object.freeze(wrapped);
|
|
163
|
-
}
|
|
164
|
-
if (typeof node === "object" && node !== null) {
|
|
165
|
-
const wrapped = {};
|
|
166
|
-
for (const key of Object.keys(node)) {
|
|
167
|
-
wrapped[key] = decorateTransitionSelectorNode(node[key]);
|
|
168
|
-
}
|
|
169
|
-
return Object.freeze(wrapped);
|
|
170
|
-
}
|
|
171
|
-
return node;
|
|
172
|
-
};
|
|
173
|
-
const makeTransitionSelector = (stateNodes, source) => {
|
|
174
|
-
const selector = {
|
|
175
|
-
...decorateTransitionSelectorNode(makeTargetSelector(stateNodes, source))
|
|
176
|
-
};
|
|
177
|
-
selector.branches = (declarations) => Object.freeze({
|
|
178
|
-
resolve: (resolve, options) => Object.freeze({
|
|
179
|
-
[TransitionBuilderDescriptorTypeId]: TransitionBuilderDescriptorTypeId,
|
|
180
|
-
type: "branches",
|
|
181
|
-
declarations,
|
|
182
|
-
resolve,
|
|
183
|
-
...transitionOptions(options)
|
|
184
|
-
})
|
|
185
|
-
});
|
|
186
|
-
return Object.freeze(selector);
|
|
187
|
-
};
|
|
188
|
-
const normalizeTransitionBuilder = (transition, stateNodes, path) => {
|
|
189
|
-
const result = transition(makeTransitionSelector(stateNodes, path));
|
|
190
|
-
if (Topology.isTargetSelection(result)) {
|
|
191
|
-
const selection = plainTargetSelection(result);
|
|
192
|
-
return { target: () => selection };
|
|
193
|
-
}
|
|
194
|
-
if (!hasProperty(result, TransitionBuilderDescriptorTypeId))
|
|
195
|
-
return result;
|
|
196
|
-
const descriptor = result;
|
|
197
|
-
if (descriptor.type === "branches") {
|
|
198
|
-
return {
|
|
199
|
-
branches: () => descriptor.declarations,
|
|
200
|
-
resolve: descriptor.resolve,
|
|
201
|
-
reenter: descriptor.reenter,
|
|
202
|
-
declinable: descriptor.declinable
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
return {
|
|
206
|
-
target: () => descriptor.selection,
|
|
207
|
-
resolve: descriptor.resolver,
|
|
208
|
-
reenter: descriptor.reenter,
|
|
209
|
-
declinable: descriptor.declinable
|
|
210
|
-
};
|
|
211
|
-
};
|
|
212
102
|
const transitionTargetSelection = (selection) => Object.freeze({
|
|
213
103
|
path: selection.path,
|
|
214
104
|
kind: selection.kind,
|
|
@@ -217,90 +107,8 @@ const transitionTargetSelection = (selection) => Object.freeze({
|
|
|
217
107
|
const selectionUpdates = (selection) => selection.updatePath === undefined ?
|
|
218
108
|
selection.kind === "update" && selection.path !== undefined ? [selection.path] : []
|
|
219
109
|
: [selection.updatePath];
|
|
220
|
-
const makeSelectionMethod = (kind, path, scope) => () => Topology.makeTargetSelection(kind, path, scope);
|
|
221
110
|
const makeSelectionValue = (kind, path, scope) => Topology.makeTargetSelection(kind, path, scope);
|
|
222
111
|
const makeStateUpdateSelection = (path, scope) => Topology.makeTargetSelection("update", path, scope);
|
|
223
|
-
const addSelectionChildren = (builder, stateNodes, parent, scope, source) => {
|
|
224
|
-
for (const node of stateNodes.byPath.values()) {
|
|
225
|
-
if (node.parent !== parent || node.type === "history")
|
|
226
|
-
continue;
|
|
227
|
-
builder[node.key] = makeSelectionNode(stateNodes, node.path, scope, source);
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
const makeSelectionNode = (stateNodes, path, scope, source) => {
|
|
231
|
-
const node = getTargetBuilderNode(stateNodes, path);
|
|
232
|
-
const kind = node.type === "choice" ? "choice" : "state";
|
|
233
|
-
const method = makeSelectionMethod(kind, path, scope);
|
|
234
|
-
if (node.type !== "atomic" && node.type !== "final" && node.type !== "choice" && node.type !== "history") {
|
|
235
|
-
Object.defineProperty(method, "initial", {
|
|
236
|
-
value: makeSelectionValue("initial", path, scope),
|
|
237
|
-
enumerable: true
|
|
238
|
-
});
|
|
239
|
-
if (scope === "local" || scope === "branch") {
|
|
240
|
-
addSelectionChildren(method, stateNodes, path, scope, source);
|
|
241
|
-
}
|
|
242
|
-
if (scope === "branch" && source !== undefined && node.schema !== undefined &&
|
|
243
|
-
(source === path || Configuration.isDescendantOf(source, path)) &&
|
|
244
|
-
getTargetBuilderNode(stateNodes, source).type !== "choice") {
|
|
245
|
-
Object.defineProperty(method, "update", {
|
|
246
|
-
value: makeStateUpdateSelection(path, "branch"),
|
|
247
|
-
enumerable: true
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
if (node.type === "atomic" && scope === "branch" && source === path && node.schema !== undefined) {
|
|
252
|
-
Object.defineProperty(method, "update", { value: makeStateUpdateSelection(path, "branch"), enumerable: true });
|
|
253
|
-
}
|
|
254
|
-
return method;
|
|
255
|
-
};
|
|
256
|
-
const makeHistorySelectionTree = (stateNodes, parent) => {
|
|
257
|
-
const builder = {};
|
|
258
|
-
for (const node of stateNodes.byPath.values()) {
|
|
259
|
-
if (node.parent !== parent)
|
|
260
|
-
continue;
|
|
261
|
-
if (node.type === "history") {
|
|
262
|
-
builder[node.key] = makeSelectionValue("history", node.path, "full");
|
|
263
|
-
}
|
|
264
|
-
else if (node.type !== "choice") {
|
|
265
|
-
const children = makeHistorySelectionTree(stateNodes, node.path);
|
|
266
|
-
if (Object.keys(children).length > 0)
|
|
267
|
-
builder[node.key] = children;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
return builder;
|
|
271
|
-
};
|
|
272
|
-
const makeTargetSelector = (stateNodes, source) => {
|
|
273
|
-
const full = Object.assign({}, makeSelectionValue("state", "", "full"), {
|
|
274
|
-
initial: makeSelectionValue("initial", "", "full")
|
|
275
|
-
});
|
|
276
|
-
const branch = {};
|
|
277
|
-
const root = getTargetBuilderNode(stateNodes, stateNodes.roots[0]);
|
|
278
|
-
addSelectionChildren(branch, stateNodes, root.path, "branch", source);
|
|
279
|
-
const local = {};
|
|
280
|
-
const localScope = getLocalTargetScope(stateNodes, source);
|
|
281
|
-
if (localScope !== undefined) {
|
|
282
|
-
const localScopeNode = getTargetBuilderNode(stateNodes, localScope);
|
|
283
|
-
if (localScopeNode.schema !== undefined) {
|
|
284
|
-
local.with = makeSelectionValue("state", localScope, "local");
|
|
285
|
-
if (getTargetBuilderNode(stateNodes, source).type !== "choice") {
|
|
286
|
-
local.update = makeStateUpdateSelection(localScope, "local");
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
addSelectionChildren(local, stateNodes, localScope, "local");
|
|
290
|
-
}
|
|
291
|
-
return {
|
|
292
|
-
none: Topology.noneTargetSelection,
|
|
293
|
-
self: getTargetBuilderNode(stateNodes, source).schema === undefined ||
|
|
294
|
-
getTargetBuilderNode(stateNodes, source).type === "final"
|
|
295
|
-
? {}
|
|
296
|
-
: { update: makeStateUpdateSelection(source, "branch") },
|
|
297
|
-
root: makeSelectionNode(stateNodes, root.path, "branch", source),
|
|
298
|
-
local,
|
|
299
|
-
branch,
|
|
300
|
-
full,
|
|
301
|
-
history: makeHistorySelectionTree(stateNodes, "")
|
|
302
|
-
};
|
|
303
|
-
};
|
|
304
112
|
const captureDefinitionBranch = (branch, selector, stateNodes, path, trigger) => {
|
|
305
113
|
if (typeof branch !== "object" || branch === null || !hasProperty(branch, "target") ||
|
|
306
114
|
typeof branch.target !== "function") {
|
|
@@ -432,7 +240,12 @@ const runCapturedBranch = (branch, context, enqueue, stateNodes, source) => {
|
|
|
432
240
|
const selectedTarget = getSelectionBuilder(context.target, branch.selection, stateNodes, source);
|
|
433
241
|
if (branch.resolve === undefined)
|
|
434
242
|
return constructSelectedTarget(selectedTarget);
|
|
435
|
-
|
|
243
|
+
// This fresh context is owned by this evaluation and can be retained by user callbacks.
|
|
244
|
+
const resolverContext = {
|
|
245
|
+
...context,
|
|
246
|
+
root: source === "" ? context.state : context.ancestors[""],
|
|
247
|
+
decline: Topology.makeDeclined
|
|
248
|
+
};
|
|
436
249
|
if (branch.selection.kind === "none")
|
|
437
250
|
delete resolverContext.target;
|
|
438
251
|
else
|
|
@@ -448,8 +261,6 @@ const runCapturedBranch = (branch, context, enqueue, stateNodes, source) => {
|
|
|
448
261
|
resolverContext.current = context.ancestors[ownerPath];
|
|
449
262
|
resolverContext.owner = getSelectionBuilder(context.target, makeStateUpdateSelection(ownerPath, "branch"), stateNodes, source);
|
|
450
263
|
}
|
|
451
|
-
if (branch.declinable === true)
|
|
452
|
-
resolverContext.decline = Topology.makeDeclined;
|
|
453
264
|
const resolved = branch.resolve(resolverContext, enqueue);
|
|
454
265
|
if (Topology.isDeclined(resolved)) {
|
|
455
266
|
if (branch.declinable !== true) {
|
|
@@ -488,18 +299,23 @@ const captureNamedBranches = (declarations, path, trigger) => {
|
|
|
488
299
|
if (!Topology.isTargetSelection(target)) {
|
|
489
300
|
throw new Error(`Machine transition branch "${key}" must select exactly one target`);
|
|
490
301
|
}
|
|
491
|
-
if (target.updatePath !== undefined) {
|
|
492
|
-
throw new Error(`Machine transition branch "${key}" cannot declare an updating target`);
|
|
493
|
-
}
|
|
494
302
|
if (title !== undefined && (typeof title !== "string" || title.length === 0)) {
|
|
495
303
|
throw new Error(`Machine transition branch "${key}" title must be a non-empty string`);
|
|
496
304
|
}
|
|
497
305
|
return Object.freeze({ key, title: title ?? key, selection: plainTargetSelection(target) });
|
|
498
306
|
}));
|
|
499
307
|
};
|
|
500
|
-
const wrapSelectedBranchBuilder = (builder, owner, branchIndex, branchKey) => {
|
|
308
|
+
const wrapSelectedBranchBuilder = (builder, owner, branchIndex, branchKey, updateBuilder) => {
|
|
501
309
|
if (typeof builder === "function") {
|
|
502
|
-
const wrapped = (...args) =>
|
|
310
|
+
const wrapped = (...args) => {
|
|
311
|
+
const result = builder(...args);
|
|
312
|
+
if (updateBuilder !== undefined) {
|
|
313
|
+
return Object.freeze({
|
|
314
|
+
update: Object.freeze(Object.fromEntries(Object.getOwnPropertyNames(updateBuilder).map((method) => [method, (value) => Topology.makeSelectedBranch(owner, branchIndex, branchKey, result.update(updateBuilder[method](value)))])))
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
return Topology.makeSelectedBranch(owner, branchIndex, branchKey, result);
|
|
318
|
+
};
|
|
503
319
|
for (const property of Reflect.ownKeys(builder)) {
|
|
504
320
|
if (property === "length" || property === "name" || property === "prototype" || property === "caller" ||
|
|
505
321
|
property === "arguments")
|
|
@@ -508,7 +324,7 @@ const wrapSelectedBranchBuilder = (builder, owner, branchIndex, branchKey) => {
|
|
|
508
324
|
if (descriptor === undefined)
|
|
509
325
|
continue;
|
|
510
326
|
if ("value" in descriptor && typeof descriptor.value === "function") {
|
|
511
|
-
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey);
|
|
327
|
+
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder);
|
|
512
328
|
}
|
|
513
329
|
Object.defineProperty(wrapped, property, descriptor);
|
|
514
330
|
}
|
|
@@ -521,7 +337,7 @@ const wrapSelectedBranchBuilder = (builder, owner, branchIndex, branchKey) => {
|
|
|
521
337
|
if (descriptor === undefined)
|
|
522
338
|
continue;
|
|
523
339
|
if ("value" in descriptor && typeof descriptor.value === "function") {
|
|
524
|
-
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey);
|
|
340
|
+
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder);
|
|
525
341
|
}
|
|
526
342
|
Object.defineProperty(wrapped, property, descriptor);
|
|
527
343
|
}
|
|
@@ -533,7 +349,9 @@ const makeBranchSelectors = (context, branches, owner, stateNodes, source) => {
|
|
|
533
349
|
const select = Object.create(null);
|
|
534
350
|
for (let branchIndex = 0; branchIndex < branches.length; branchIndex++) {
|
|
535
351
|
const branch = branches[branchIndex];
|
|
536
|
-
select[branch.key] = wrapSelectedBranchBuilder(getSelectionBuilder(context.target, branch.selection, stateNodes, source), owner, branchIndex, branch.key
|
|
352
|
+
select[branch.key] = wrapSelectedBranchBuilder(getSelectionBuilder(context.target, branch.selection, stateNodes, source), owner, branchIndex, branch.key, branch.selection.updatePath === undefined
|
|
353
|
+
? undefined
|
|
354
|
+
: getSelectionBuilder(context.target, makeStateUpdateSelection(branch.selection.updatePath, "branch"), stateNodes, source));
|
|
537
355
|
}
|
|
538
356
|
return Object.freeze(select);
|
|
539
357
|
};
|
|
@@ -549,16 +367,100 @@ const validateSelectedBranchResult = (result, selection, stateNodes) => {
|
|
|
549
367
|
}
|
|
550
368
|
validateResolvedSelection(result, selection, stateNodes);
|
|
551
369
|
};
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
370
|
+
// Invocation input contexts do not pass through captured transition evaluation.
|
|
371
|
+
const objectContext = (context, path) => ({
|
|
372
|
+
...context,
|
|
373
|
+
root: path === "" ? context.state : context.ancestors[""]
|
|
374
|
+
});
|
|
375
|
+
const normalizeObjectTransition = (raw, declaration, path, stateNodes) => {
|
|
376
|
+
if (typeof raw !== "object" || raw === null)
|
|
377
|
+
throw new Error(`Machine transition for "${path}" must be an object`);
|
|
378
|
+
const config = { ...raw };
|
|
379
|
+
const allowed = [
|
|
380
|
+
"target",
|
|
381
|
+
"update",
|
|
382
|
+
"initial",
|
|
383
|
+
"history",
|
|
384
|
+
"none",
|
|
385
|
+
"branches",
|
|
386
|
+
"from",
|
|
387
|
+
"decoded",
|
|
388
|
+
"resolve",
|
|
389
|
+
"guard",
|
|
390
|
+
"reenter",
|
|
391
|
+
"declinable"
|
|
392
|
+
];
|
|
393
|
+
for (const key of Reflect.ownKeys(config)) {
|
|
394
|
+
if (typeof key !== "string" || !allowed.includes(key)) {
|
|
395
|
+
throw new Error("Machine transition contains an unknown field");
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
for (const key of ["from", "decoded", "resolve", "guard"]) {
|
|
399
|
+
if (config[key] !== undefined && typeof config[key] !== "function") {
|
|
400
|
+
throw new Error(`Machine transition ${key} must be a function`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
const guard = typeof config.guard === "function" ? config.guard : undefined;
|
|
404
|
+
const resolve = typeof config.resolve === "function" ? config.resolve : undefined;
|
|
405
|
+
const construct = typeof config.from === "function"
|
|
406
|
+
? config.from
|
|
407
|
+
: typeof config.decoded === "function"
|
|
408
|
+
? config.decoded
|
|
409
|
+
: undefined;
|
|
410
|
+
const methods = ["from", "decoded", "resolve"].filter((key) => config[key] !== undefined);
|
|
411
|
+
if (methods.length > 1)
|
|
412
|
+
throw new Error("Machine transition construction methods are mutually exclusive");
|
|
413
|
+
const guarded = guard !== undefined;
|
|
414
|
+
const declinable = guarded || config.declinable === true;
|
|
415
|
+
if (typeof config.branches === "string") {
|
|
416
|
+
const group = declaration.branches.get(config.branches);
|
|
417
|
+
if (group === undefined || resolve === undefined) {
|
|
418
|
+
throw new Error("Machine branching transition requires a registered group and resolver");
|
|
419
|
+
}
|
|
420
|
+
if (["target", "update", "initial", "history", "none", "from", "decoded"].some((key) => config[key] !== undefined))
|
|
421
|
+
throw new Error("Machine branching transition cannot redeclare its destination");
|
|
422
|
+
const entries = Object.fromEntries(Object.entries(group).map(([key, spec]) => [key, {
|
|
423
|
+
title: spec.title,
|
|
424
|
+
target: captureDefinitionBranch({ target: () => Declaration.selection(declaration, spec) }, undefined, stateNodes, path, key).selection
|
|
425
|
+
}]));
|
|
426
|
+
return {
|
|
427
|
+
branches: () => entries,
|
|
428
|
+
reenter: config.reenter,
|
|
429
|
+
declinable,
|
|
430
|
+
resolve: (context, enqueue) => {
|
|
431
|
+
return guarded && !guard(context) ? Topology.makeDeclined() : resolve(context, enqueue);
|
|
432
|
+
}
|
|
433
|
+
};
|
|
555
434
|
}
|
|
556
|
-
const
|
|
435
|
+
const selection = Declaration.selection(declaration, config);
|
|
436
|
+
if (resolve !== undefined && selection.kind !== "none") {
|
|
437
|
+
throw new Error("Machine advanced construction requires a declared branch group");
|
|
438
|
+
}
|
|
439
|
+
const method = config.from !== undefined ? "from" : config.decoded !== undefined ? "decoded" : undefined;
|
|
440
|
+
return {
|
|
441
|
+
target: () => selection,
|
|
442
|
+
reenter: config.reenter,
|
|
443
|
+
declinable,
|
|
444
|
+
...(!guarded && method === undefined && resolve === undefined ? {} : {
|
|
445
|
+
resolve: (context, enqueue) => {
|
|
446
|
+
if (guarded && !guard(context))
|
|
447
|
+
return Topology.makeDeclined();
|
|
448
|
+
if (method !== undefined && construct !== undefined) {
|
|
449
|
+
return constructSelectionValue(selection, context, method, construct(context));
|
|
450
|
+
}
|
|
451
|
+
if (resolve !== undefined)
|
|
452
|
+
return resolve(context, enqueue);
|
|
453
|
+
return selection.kind === "none" ? undefined : constructSelectedTarget(context.target);
|
|
454
|
+
}
|
|
455
|
+
})
|
|
456
|
+
};
|
|
457
|
+
};
|
|
458
|
+
const captureTransition = (rawTransition, stateNodes, path, declaration, trigger) => {
|
|
459
|
+
const transition = normalizeObjectTransition(rawTransition, declaration, path, stateNodes);
|
|
557
460
|
if (typeof transition !== "object" || transition === null) {
|
|
558
461
|
throw new Error(`Machine transition for state "${path}" on "${String(trigger)}" must be an object`);
|
|
559
462
|
}
|
|
560
463
|
const definition = transition;
|
|
561
|
-
const selector = makeTargetSelector(stateNodes, path);
|
|
562
464
|
const reenter = definition.reenter === true;
|
|
563
465
|
const declinable = definition.declinable === true;
|
|
564
466
|
if (hasProperty(definition, "branches")) {
|
|
@@ -567,14 +469,16 @@ const captureTransition = (rawTransition, stateNodes, path, trigger) => {
|
|
|
567
469
|
throw new Error(`Machine branching transition for state "${path}" on "${String(trigger)}" requires branches and resolve`);
|
|
568
470
|
}
|
|
569
471
|
const resolve = branching.resolve;
|
|
570
|
-
const branches = captureNamedBranches(branching.branches(
|
|
472
|
+
const branches = captureNamedBranches(branching.branches(), path, trigger);
|
|
571
473
|
const owner = Object.freeze({});
|
|
572
474
|
const evaluate = (context, enqueue) => {
|
|
573
|
-
const resolverContext = {
|
|
475
|
+
const resolverContext = {
|
|
476
|
+
...context,
|
|
477
|
+
root: path === "" ? context.state : context.ancestors[""],
|
|
478
|
+
decline: Topology.makeDeclined
|
|
479
|
+
};
|
|
574
480
|
delete resolverContext.target;
|
|
575
481
|
resolverContext.select = makeBranchSelectors(context, branches, owner, stateNodes, path);
|
|
576
|
-
if (declinable)
|
|
577
|
-
resolverContext.decline = Topology.makeDeclined;
|
|
578
482
|
const selected = resolve(resolverContext, enqueue);
|
|
579
483
|
if (Topology.isDeclined(selected)) {
|
|
580
484
|
if (!declinable) {
|
|
@@ -614,7 +518,7 @@ const captureTransition = (rawTransition, stateNodes, path, trigger) => {
|
|
|
614
518
|
transition: (context, enqueue) => evaluate(context, enqueue).result
|
|
615
519
|
};
|
|
616
520
|
}
|
|
617
|
-
const branch = captureDefinitionBranch(transition,
|
|
521
|
+
const branch = captureDefinitionBranch(transition, undefined, stateNodes, path, trigger);
|
|
618
522
|
const evaluate = (context, enqueue) => ({
|
|
619
523
|
result: runCapturedBranch(branch, context, enqueue, stateNodes, path),
|
|
620
524
|
branchIndex: 0,
|
|
@@ -634,67 +538,32 @@ const captureTransition = (rawTransition, stateNodes, path, trigger) => {
|
|
|
634
538
|
transition: (context, enqueue) => evaluate(context, enqueue).result
|
|
635
539
|
};
|
|
636
540
|
};
|
|
637
|
-
const captureEventHandlers = (on, stateNodes, path) => {
|
|
541
|
+
const captureEventHandlers = (on, stateNodes, path, declaration) => {
|
|
638
542
|
// The machine owns its dispatch table. Compiled plans may snapshot these
|
|
639
543
|
// definitions, so retaining caller-owned containers would let strategies
|
|
640
544
|
// observe different handlers after an unsafe external mutation.
|
|
641
545
|
const captured = Object.create(null);
|
|
642
546
|
for (const event of Reflect.ownKeys(on)) {
|
|
643
|
-
captured[event] = captureTransition(on[event], stateNodes, path, event);
|
|
547
|
+
captured[event] = captureTransition(on[event], stateNodes, path, declaration, event);
|
|
644
548
|
}
|
|
645
549
|
return captured;
|
|
646
550
|
};
|
|
647
|
-
const
|
|
648
|
-
const
|
|
649
|
-
[InvokeBuilderDescriptorTypeId]: InvokeBuilderDescriptorTypeId,
|
|
650
|
-
config
|
|
651
|
-
};
|
|
652
|
-
for (const channel of channels) {
|
|
653
|
-
if (!hasProperty(config, channel)) {
|
|
654
|
-
builder[channel] = (handler) => makeInvokeBuilder({ ...config, [channel]: handler }, channels);
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
return Object.freeze(builder);
|
|
658
|
-
};
|
|
659
|
-
const invokeSelector = Object.freeze({
|
|
660
|
-
effect: (id, effect) => makeInvokeBuilder({ id, effect }, ["onDone", "onFailure"]),
|
|
661
|
-
stream: (id, stream) => makeInvokeBuilder({ id, stream }, ["onElement", "onDone", "onFailure"]),
|
|
662
|
-
timer: (id, after) => makeInvokeBuilder({ id, after }, ["onDone"]),
|
|
663
|
-
logic: (id, options) => makeInvokeBuilder({ id, ...options }, ["onSnapshot", "onDone", "onFailure"]),
|
|
664
|
-
child: (child, options) => makeInvokeBuilder(options === undefined ? { child } : { child, ...options }, [
|
|
665
|
-
"onSnapshot",
|
|
666
|
-
"onDone",
|
|
667
|
-
"onFailure"
|
|
668
|
-
])
|
|
669
|
-
});
|
|
670
|
-
const invokeBuilderConfig = (value, path) => {
|
|
671
|
-
if (typeof value !== "object" || value === null ||
|
|
672
|
-
!hasProperty(value, InvokeBuilderDescriptorTypeId) ||
|
|
673
|
-
value[InvokeBuilderDescriptorTypeId] !== InvokeBuilderDescriptorTypeId) {
|
|
674
|
-
throw new Error(`Machine invocation for state "${path}" must be constructed from its source selector`);
|
|
675
|
-
}
|
|
676
|
-
return value.config;
|
|
677
|
-
};
|
|
678
|
-
const captureInvokeDefinition = (invoke, stateNodes, path) => {
|
|
679
|
-
if (typeof invoke !== "function") {
|
|
680
|
-
throw new Error(`Machine invocation for state "${path}" must be a source-first callback`);
|
|
681
|
-
}
|
|
682
|
-
const authored = invoke(invokeSelector);
|
|
683
|
-
const definitions = Array.isArray(authored) ? authored : [authored];
|
|
551
|
+
const captureInvokeDefinition = (invoke, stateNodes, path, declaration) => {
|
|
552
|
+
const definitions = Array.isArray(invoke) ? invoke : [invoke];
|
|
684
553
|
const capturedDefinitions = definitions.map((definition) => {
|
|
685
|
-
const captured =
|
|
554
|
+
const captured = Declaration.invocation(declaration, definition, path, (context) => objectContext(context, path));
|
|
686
555
|
for (const key of ["onElement", "onDone", "onFailure", "onSnapshot"]) {
|
|
687
556
|
if (captured[key] !== undefined) {
|
|
688
|
-
captured[key] = captureTransition(captured[key], stateNodes, path, key);
|
|
557
|
+
captured[key] = captureTransition(captured[key], stateNodes, path, declaration, key);
|
|
689
558
|
}
|
|
690
559
|
}
|
|
691
560
|
return InvocationDefinition.capture(captured, path);
|
|
692
561
|
});
|
|
693
|
-
if (Array.isArray(
|
|
562
|
+
if (Array.isArray(invoke))
|
|
694
563
|
return Object.freeze(capturedDefinitions);
|
|
695
564
|
return capturedDefinitions[0];
|
|
696
565
|
};
|
|
697
|
-
const flattenHandlers = (handlers, stateNodes, states, prefix, config) => {
|
|
566
|
+
const flattenHandlers = (handlers, stateNodes, states, prefix, declaration, config) => {
|
|
698
567
|
for (const key of Object.keys(config)) {
|
|
699
568
|
const path = prefix === "" ? key : `${prefix}.${key}`;
|
|
700
569
|
if (!hasProperty(states, key)) {
|
|
@@ -713,20 +582,20 @@ const flattenHandlers = (handlers, stateNodes, states, prefix, config) => {
|
|
|
713
582
|
}
|
|
714
583
|
const on = stateConfig.on;
|
|
715
584
|
if (typeof on === "object" && on !== null) {
|
|
716
|
-
const capturedOn = captureEventHandlers(on, stateNodes, path);
|
|
585
|
+
const capturedOn = captureEventHandlers(on, stateNodes, path, declaration);
|
|
717
586
|
stateConfig.on = capturedOn;
|
|
718
587
|
}
|
|
719
588
|
if (stateConfig.always !== undefined) {
|
|
720
|
-
stateConfig.always = captureTransition(stateConfig.always, stateNodes, path, "always");
|
|
589
|
+
stateConfig.always = captureTransition(stateConfig.always, stateNodes, path, declaration, "always");
|
|
721
590
|
}
|
|
722
591
|
if (stateConfig.onDone !== undefined) {
|
|
723
|
-
stateConfig.onDone = captureTransition(stateConfig.onDone, stateNodes, path, "done");
|
|
592
|
+
stateConfig.onDone = captureTransition(stateConfig.onDone, stateNodes, path, declaration, "done");
|
|
724
593
|
}
|
|
725
594
|
if (stateConfig.choice !== undefined) {
|
|
726
|
-
stateConfig.choice = captureTransition(stateConfig.choice, stateNodes, path, "choice");
|
|
595
|
+
stateConfig.choice = captureTransition(stateConfig.choice, stateNodes, path, declaration, "choice");
|
|
727
596
|
}
|
|
728
597
|
if (stateConfig.invoke !== undefined) {
|
|
729
|
-
stateConfig.invoke = captureInvokeDefinition(stateConfig.invoke, stateNodes, path);
|
|
598
|
+
stateConfig.invoke = captureInvokeDefinition(stateConfig.invoke, stateNodes, path, declaration);
|
|
730
599
|
}
|
|
731
600
|
const node = stateNodes.byPath.get(path);
|
|
732
601
|
if (node?.type === "choice") {
|
|
@@ -746,13 +615,13 @@ const flattenHandlers = (handlers, stateNodes, states, prefix, config) => {
|
|
|
746
615
|
if (typeof childConfig !== "object" || childConfig === null) {
|
|
747
616
|
throw new Error(`Machine expected state "${path}" child handlers to be an object`);
|
|
748
617
|
}
|
|
749
|
-
flattenHandlers(handlers, stateNodes, node.states, path, childConfig);
|
|
618
|
+
flattenHandlers(handlers, stateNodes, node.states, path, declaration, childConfig);
|
|
750
619
|
}
|
|
751
620
|
}
|
|
752
621
|
};
|
|
753
|
-
const makeHandle = (self) => ((config) => {
|
|
622
|
+
const makeHandle = (self, declaration) => ((config) => {
|
|
754
623
|
const handlers = Object.create(null);
|
|
755
|
-
flattenHandlers(handlers, self.stateNodes, self.states, "", { "": config });
|
|
624
|
+
flattenHandlers(handlers, self.stateNodes, self.states, "", declaration, { "": config });
|
|
756
625
|
return makeWithHandlers(self, handlers);
|
|
757
626
|
});
|
|
758
627
|
export const isMachine = (u) => hasProperty(u, TypeId) && u[TypeId] === TypeId;
|
|
@@ -858,7 +727,7 @@ export const make = (config) => {
|
|
|
858
727
|
self.initialDefinition = compiledInitial.definition;
|
|
859
728
|
self.makeTargetBuilder = makeTargetBuilder(states, self.stateNodes);
|
|
860
729
|
self.handlers = Object.create(null);
|
|
861
|
-
self.handle = makeHandle(self);
|
|
730
|
+
self.handle = makeHandle(self, Declaration.capture(config.root, config));
|
|
862
731
|
Protocol.setProtocol(self);
|
|
863
732
|
return self;
|
|
864
733
|
};
|