@typeonce/effect-machine 0.34.0 → 0.36.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.
Files changed (98) hide show
  1. package/README.md +98 -33
  2. package/dist/Machine.d.ts +408 -483
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +126 -87
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/command.d.ts +0 -1
  7. package/dist/internal/machine/command.d.ts.map +1 -1
  8. package/dist/internal/machine/command.js +0 -14
  9. package/dist/internal/machine/command.js.map +1 -1
  10. package/dist/internal/machine/configuration.d.ts +0 -1
  11. package/dist/internal/machine/configuration.d.ts.map +1 -1
  12. package/dist/internal/machine/configuration.js +15 -21
  13. package/dist/internal/machine/configuration.js.map +1 -1
  14. package/dist/internal/machine/construction.d.ts +9 -0
  15. package/dist/internal/machine/construction.d.ts.map +1 -0
  16. package/dist/internal/machine/construction.js +132 -0
  17. package/dist/internal/machine/construction.js.map +1 -0
  18. package/dist/internal/machine/declaration.d.ts +1 -0
  19. package/dist/internal/machine/declaration.d.ts.map +1 -1
  20. package/dist/internal/machine/declaration.js +3 -8
  21. package/dist/internal/machine/declaration.js.map +1 -1
  22. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  23. package/dist/internal/machine/executionPlan.js +21 -27
  24. package/dist/internal/machine/executionPlan.js.map +1 -1
  25. package/dist/internal/machine/implementation.d.ts +2 -3
  26. package/dist/internal/machine/implementation.d.ts.map +1 -1
  27. package/dist/internal/machine/implementation.js.map +1 -1
  28. package/dist/internal/machine/initialDeclaration.d.ts +14 -0
  29. package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
  30. package/dist/internal/machine/initialDeclaration.js +137 -0
  31. package/dist/internal/machine/initialDeclaration.js.map +1 -0
  32. package/dist/internal/machine/machine.d.ts +0 -2
  33. package/dist/internal/machine/machine.d.ts.map +1 -1
  34. package/dist/internal/machine/machine.js +101 -282
  35. package/dist/internal/machine/machine.js.map +1 -1
  36. package/dist/internal/machine/planner.d.ts +8 -0
  37. package/dist/internal/machine/planner.d.ts.map +1 -1
  38. package/dist/internal/machine/planner.js +86 -82
  39. package/dist/internal/machine/planner.js.map +1 -1
  40. package/dist/internal/machine/protocol.d.ts +1 -1
  41. package/dist/internal/machine/protocol.d.ts.map +1 -1
  42. package/dist/internal/machine/protocol.js.map +1 -1
  43. package/dist/internal/machine/stateDefinition.d.ts +1 -1
  44. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  45. package/dist/internal/machine/stateDefinition.js +1 -13
  46. package/dist/internal/machine/stateDefinition.js.map +1 -1
  47. package/dist/internal/machine/targetReference.d.ts.map +1 -1
  48. package/dist/internal/machine/targetReference.js +14 -4
  49. package/dist/internal/machine/targetReference.js.map +1 -1
  50. package/dist/internal/machine/topology.d.ts +6 -0
  51. package/dist/internal/machine/topology.d.ts.map +1 -1
  52. package/dist/internal/machine/topology.js +3 -2
  53. package/dist/internal/machine/topology.js.map +1 -1
  54. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  55. package/dist/internal/testing/machine/finiteModel.js +51 -82
  56. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  57. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  58. package/dist/internal/testing/machine/verification.js +7 -1
  59. package/dist/internal/testing/machine/verification.js.map +1 -1
  60. package/dist/testing/MachineTest.d.ts +105 -73
  61. package/dist/testing/MachineTest.d.ts.map +1 -1
  62. package/dist/testing/MachineTest.js +105 -73
  63. package/dist/testing/MachineTest.js.map +1 -1
  64. package/dist/unstable/cluster/ClusterMachine.d.ts +13 -8
  65. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  66. package/dist/unstable/cluster/ClusterMachine.js +13 -8
  67. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  68. package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
  69. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  70. package/dist/unstable/reactivity/AtomMachine.js +41 -24
  71. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  72. package/docs/agent-guide.md +37 -34
  73. package/docs/root-api.md +143 -190
  74. package/package.json +1 -1
  75. package/src/Machine.ts +631 -872
  76. package/src/internal/machine/command.ts +0 -16
  77. package/src/internal/machine/configuration.ts +13 -30
  78. package/src/internal/machine/construction.ts +147 -0
  79. package/src/internal/machine/declaration.ts +4 -7
  80. package/src/internal/machine/executionPlan.ts +20 -25
  81. package/src/internal/machine/implementation.ts +2 -3
  82. package/src/internal/machine/initialDeclaration.ts +157 -0
  83. package/src/internal/machine/machine.ts +118 -396
  84. package/src/internal/machine/planner.ts +97 -99
  85. package/src/internal/machine/protocol.ts +3 -3
  86. package/src/internal/machine/stateDefinition.ts +1 -15
  87. package/src/internal/machine/targetReference.ts +19 -4
  88. package/src/internal/machine/topology.ts +7 -2
  89. package/src/internal/testing/machine/finiteModel.ts +60 -132
  90. package/src/internal/testing/machine/verification.ts +7 -1
  91. package/src/testing/MachineTest.ts +105 -73
  92. package/src/unstable/cluster/ClusterMachine.ts +13 -8
  93. package/src/unstable/reactivity/AtomMachine.ts +41 -24
  94. package/dist/internal/machine/targetBuilder.d.ts +0 -16
  95. package/dist/internal/machine/targetBuilder.d.ts.map +0 -1
  96. package/dist/internal/machine/targetBuilder.js +0 -333
  97. package/dist/internal/machine/targetBuilder.js.map +0 -1
  98. package/src/internal/machine/targetBuilder.ts +0 -525
@@ -6,7 +6,9 @@ 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 Construction from "./construction.js";
9
10
  import * as Declaration from "./declaration.js";
11
+ import * as InitialDeclaration from "./initialDeclaration.js";
10
12
  import * as InvocationDefinition from "./invocationDefinition.js";
11
13
  import * as internalPlanner from "./planner.js";
12
14
  import * as internalProcess from "./process.js";
@@ -15,7 +17,6 @@ import * as internalRuntime from "./runtimeProtocol.js";
15
17
  import * as Serialization from "./serialization.js";
16
18
  import * as StateDefinition from "./stateDefinition.js";
17
19
  import { ChildMachineLogicTypeId } from "./symbols.js";
18
- import { getLocalTargetScope, getTargetBuilderNode, makeSnapshotBuilder, makeTargetBuilder, withFrom } from "./targetBuilder.js";
19
20
  import * as TargetReference from "./targetReference.js";
20
21
  import * as Topology from "./topology.js";
21
22
  export { ChildAlreadyExistsError, InfiniteTransitionError, MachineSchemaDecodeError, MachineSchemaEncodeError, ProcessLocalError, StartupError, StoppedError } from "./errors.js";
@@ -50,55 +51,13 @@ const makeWithHandlers = (self, handlers) => {
50
51
  machine.initial = self.initial;
51
52
  machine.initialDefinition = self.initialDefinition;
52
53
  machine.stateNodes = self.stateNodes;
53
- machine.makeTargetBuilder = self.makeTargetBuilder;
54
54
  machine.handlers = handlers;
55
55
  Protocol.copyProtocol(self, machine);
56
56
  return machine;
57
57
  };
58
- const InitialBuilderDescriptorTypeId = Symbol("effect/Machine/InitialBuilderDescriptor");
59
58
  const plainTargetSelection = (selection) => selection.kind === "none"
60
59
  ? Topology.noneTargetSelection
61
60
  : Topology.makeTargetSelection(selection.kind, selection.path, selection.scope, selection.updatePath);
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));
71
- };
72
- const makeInitialBuilderDescriptor = (selection, resolve) => Object.freeze({
73
- [InitialBuilderDescriptorTypeId]: InitialBuilderDescriptorTypeId,
74
- selection: plainTargetSelection(selection),
75
- resolve
76
- });
77
- const decorateInitialSelection = (selection) => Object.freeze({
78
- ...selection,
79
- from: (value) => makeInitialBuilderDescriptor(selection, (context) => context.target.from(value(context))),
80
- decoded: (value) => makeInitialBuilderDescriptor(selection, (context) => context.target.decoded(value(context))),
81
- resolve: (resolve) => makeInitialBuilderDescriptor(selection, resolve)
82
- });
83
- const decorateInitialSelectorNode = (node) => {
84
- if (Topology.isTargetSelection(node))
85
- return decorateInitialSelection(node);
86
- if (typeof node === "function") {
87
- const wrapped = ((...args) => decorateInitialSelection(node(...args)));
88
- for (const key of Object.keys(node)) {
89
- wrapped[key] = decorateInitialSelectorNode(node[key]);
90
- }
91
- return Object.freeze(wrapped);
92
- }
93
- if (typeof node === "object" && node !== null) {
94
- const wrapped = {};
95
- for (const key of Object.keys(node)) {
96
- wrapped[key] = decorateInitialSelectorNode(node[key]);
97
- }
98
- return Object.freeze(wrapped);
99
- }
100
- return node;
101
- };
102
61
  const transitionTargetSelection = (selection) => Object.freeze({
103
62
  path: selection.path,
104
63
  kind: selection.kind,
@@ -107,8 +66,6 @@ const transitionTargetSelection = (selection) => Object.freeze({
107
66
  const selectionUpdates = (selection) => selection.updatePath === undefined ?
108
67
  selection.kind === "update" && selection.path !== undefined ? [selection.path] : []
109
68
  : [selection.updatePath];
110
- const makeSelectionValue = (kind, path, scope) => Topology.makeTargetSelection(kind, path, scope);
111
- const makeStateUpdateSelection = (path, scope) => Topology.makeTargetSelection("update", path, scope);
112
69
  const captureDefinitionBranch = (branch, selector, stateNodes, path, trigger) => {
113
70
  if (typeof branch !== "object" || branch === null || !hasProperty(branch, "target") ||
114
71
  typeof branch.target !== "function") {
@@ -130,76 +87,6 @@ const captureDefinitionBranch = (branch, selector, stateNodes, path, trigger) =>
130
87
  }
131
88
  return { ...branch, selection };
132
89
  };
133
- const makeUpdatingConstruction = (target, ownerPath) => Object.freeze({
134
- update: (update) => {
135
- if (!Topology.isStateUpdate(update) || update.path !== ownerPath) {
136
- throw new Error(`Machine combined target must update its declared owner "${ownerPath}"`);
137
- }
138
- return Topology.makeCombinedTarget(target, update);
139
- }
140
- });
141
- const makeUpdatingTargetBuilder = (builder, ownerPath) => {
142
- if (typeof builder !== "object" || builder === null) {
143
- throw new Error("Machine combined target requires a state construction builder");
144
- }
145
- const updating = {};
146
- for (const property of Reflect.ownKeys(builder)) {
147
- const descriptor = Object.getOwnPropertyDescriptor(builder, property);
148
- if (descriptor === undefined)
149
- continue;
150
- if ((property === "from" || property === "decoded") && "value" in descriptor && typeof descriptor.value === "function") {
151
- const construct = descriptor.value;
152
- descriptor.value = (...args) => makeUpdatingConstruction(construct(...args), ownerPath);
153
- }
154
- Object.defineProperty(updating, property, descriptor);
155
- }
156
- return Object.freeze(updating);
157
- };
158
- const getSelectionBuilder = (target, selection, stateNodes, source) => {
159
- if (selection.kind === "none")
160
- return target.none;
161
- if (selection.kind === "update") {
162
- return withFrom((value) => Topology.makeStateUpdate(selection.path, value), "leaf", true);
163
- }
164
- let builder;
165
- let parts = selection.path.split(".");
166
- if (selection.kind === "history") {
167
- builder = target.history;
168
- if (selection.path !== "")
169
- parts.unshift("");
170
- }
171
- else if (selection.scope === "local") {
172
- builder = target.local;
173
- const scope = getLocalTargetScope(stateNodes, source);
174
- if (scope !== undefined) {
175
- if (selection.path === scope) {
176
- builder = builder.with;
177
- parts = [];
178
- }
179
- else {
180
- parts = (scope === "" ? selection.path : selection.path.slice(scope.length + 1)).split(".");
181
- }
182
- }
183
- }
184
- else if (selection.scope === "branch") {
185
- builder = target.branch;
186
- if (selection.path !== "")
187
- parts.unshift("");
188
- }
189
- else {
190
- builder = target.full;
191
- }
192
- for (const part of parts)
193
- builder = builder[part];
194
- if (selection.kind === "initial")
195
- builder = builder.initial;
196
- if (typeof builder !== "function" &&
197
- (typeof builder !== "object" || builder === null || typeof builder.from !== "function")) {
198
- throw new Error(`Machine could not construct selected transition target "${selection.path}"`);
199
- }
200
- return selection.updatePath === undefined ? builder : makeUpdatingTargetBuilder(builder, selection.updatePath);
201
- };
202
- const constructSelectedTarget = (builder) => typeof builder?.from === "function" ? builder.from() : builder();
203
90
  const validateResolvedSelection = (result, selection, stateNodes) => {
204
91
  if (selection.kind === "none") {
205
92
  if (result !== undefined) {
@@ -237,30 +124,12 @@ const validateResolvedSelection = (result, selection, stateNodes) => {
237
124
  }
238
125
  };
239
126
  const runCapturedBranch = (branch, context, enqueue, stateNodes, source) => {
240
- const selectedTarget = getSelectionBuilder(context.target, branch.selection, stateNodes, source);
241
- if (branch.resolve === undefined)
242
- return constructSelectedTarget(selectedTarget);
243
- // This fresh context is owned by this evaluation and can be retained by user callbacks.
244
127
  const resolverContext = {
245
128
  ...context,
246
129
  root: source === "" ? context.state : context.ancestors[""],
247
130
  decline: Topology.makeDeclined
248
131
  };
249
- if (branch.selection.kind === "none")
250
- delete resolverContext.target;
251
- else
252
- resolverContext.target = selectedTarget;
253
- if (branch.selection.kind === "update") {
254
- const ownerPath = branch.selection.path;
255
- delete resolverContext.target;
256
- resolverContext.current = context.ancestors[ownerPath] ?? context.state;
257
- resolverContext.owner = getSelectionBuilder(context.target, makeStateUpdateSelection(ownerPath, branch.selection.scope === "local" ? "local" : "branch"), stateNodes, source);
258
- }
259
- else if (branch.selection.updatePath !== undefined) {
260
- const ownerPath = branch.selection.updatePath;
261
- resolverContext.current = context.ancestors[ownerPath];
262
- resolverContext.owner = getSelectionBuilder(context.target, makeStateUpdateSelection(ownerPath, "branch"), stateNodes, source);
263
- }
132
+ delete resolverContext.target;
264
133
  const resolved = branch.resolve(resolverContext, enqueue);
265
134
  if (Topology.isDeclined(resolved)) {
266
135
  if (branch.declinable !== true) {
@@ -269,7 +138,7 @@ const runCapturedBranch = (branch, context, enqueue, stateNodes, source) => {
269
138
  return resolved;
270
139
  }
271
140
  validateResolvedSelection(resolved, branch.selection, stateNodes);
272
- return resolved === undefined ? constructSelectedTarget(selectedTarget) : resolved;
141
+ return resolved;
273
142
  };
274
143
  const topologyTargetPath = (selection) => selection.kind === "update" ? undefined : selection.path;
275
144
  const isArrayIndexKey = (key) => {
@@ -305,56 +174,37 @@ const captureNamedBranches = (declarations, path, trigger) => {
305
174
  return Object.freeze({ key, title: title ?? key, selection: plainTargetSelection(target) });
306
175
  }));
307
176
  };
308
- const wrapSelectedBranchBuilder = (builder, owner, branchIndex, branchKey, updateBuilder) => {
309
- if (typeof builder === "function") {
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
- };
319
- for (const property of Reflect.ownKeys(builder)) {
320
- if (property === "length" || property === "name" || property === "prototype" || property === "caller" ||
321
- property === "arguments")
322
- continue;
323
- const descriptor = Object.getOwnPropertyDescriptor(builder, property);
324
- if (descriptor === undefined)
325
- continue;
326
- if ("value" in descriptor && typeof descriptor.value === "function") {
327
- descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder);
328
- }
329
- Object.defineProperty(wrapped, property, descriptor);
330
- }
331
- return wrapped;
332
- }
333
- if (typeof builder === "object" && builder !== null) {
334
- const wrapped = {};
335
- for (const property of Reflect.ownKeys(builder)) {
336
- const descriptor = Object.getOwnPropertyDescriptor(builder, property);
337
- if (descriptor === undefined)
338
- continue;
339
- if ("value" in descriptor && typeof descriptor.value === "function") {
340
- descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder);
341
- }
342
- Object.defineProperty(wrapped, property, descriptor);
177
+ const constructBranch = (selection, stateNodes, declaration, raw = {}, source) => {
178
+ const config = Construction.record(raw);
179
+ if (selection.kind === "none" || selection.kind === "history" || selection.kind === "choice") {
180
+ if (Reflect.ownKeys(config).length > 0)
181
+ throw new Error("Machine pseudo-state construction accepts no data");
182
+ if (selection.kind === "none")
183
+ return undefined;
184
+ const node = stateNodes.byPath.get(selection.path);
185
+ return selection.kind === "history" ?
186
+ Topology.makeHistoryTarget(node.path, node.parent)
187
+ : Topology.makeChoiceTarget(node.path, node.parent);
188
+ }
189
+ if (selection.kind === "update")
190
+ return Construction.update(stateNodes, selection.path, config);
191
+ if (selection.path === "") {
192
+ if (Reflect.ownKeys(config).some((key) => key !== "input")) {
193
+ throw new Error("Machine root targets accept input only");
343
194
  }
344
- return wrapped;
195
+ if (declaration.initialize === undefined)
196
+ throw new Error("Machine root initialization is unavailable");
197
+ return declaration.initialize(config.input);
345
198
  }
346
- throw new Error(`Machine could not construct transition branch "${branchKey}"`);
347
- };
348
- const makeBranchSelectors = (context, branches, owner, stateNodes, source) => {
349
- const select = Object.create(null);
350
- for (let branchIndex = 0; branchIndex < branches.length; branchIndex++) {
351
- const branch = branches[branchIndex];
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));
355
- }
356
- return Object.freeze(select);
199
+ if (selection.updatePath !== undefined) {
200
+ const { update, ...state } = config;
201
+ return Topology.makeCombinedTarget(Construction.target(stateNodes, selection.path, state, source), Construction.update(stateNodes, selection.updatePath, update));
202
+ }
203
+ return Construction.target(stateNodes, selection.path, config, source);
357
204
  };
205
+ const makeBranchSelectors = (branches, owner, stateNodes, declaration, source) => Object.freeze(Object.fromEntries(branches.map((branch, index) => [branch.key, (config) => Topology.makeSelectedBranch(owner, index, branch.key, branch.selection.kind === "none"
206
+ ? Topology.makeNoTarget()
207
+ : constructBranch(branch.selection, stateNodes, declaration, config, source))])));
358
208
  const validateSelectedBranchResult = (result, selection, stateNodes) => {
359
209
  if (selection.kind === "none") {
360
210
  if (!Topology.isNoTarget(result)) {
@@ -379,11 +229,11 @@ const normalizeObjectTransition = (raw, declaration, path, stateNodes) => {
379
229
  const allowed = [
380
230
  "target",
381
231
  "update",
382
- "initial",
232
+ "input",
383
233
  "history",
384
234
  "none",
385
235
  "branches",
386
- "from",
236
+ "data",
387
237
  "decoded",
388
238
  "resolve",
389
239
  "guard",
@@ -395,21 +245,23 @@ const normalizeObjectTransition = (raw, declaration, path, stateNodes) => {
395
245
  throw new Error("Machine transition contains an unknown field");
396
246
  }
397
247
  }
398
- for (const key of ["from", "decoded", "resolve", "guard"]) {
248
+ for (const key of ["resolve", "guard"]) {
399
249
  if (config[key] !== undefined && typeof config[key] !== "function") {
400
250
  throw new Error(`Machine transition ${key} must be a function`);
401
251
  }
402
252
  }
403
253
  const guard = typeof config.guard === "function" ? config.guard : undefined;
404
254
  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");
255
+ if (config.decoded !== undefined && typeof config.decoded !== "boolean") {
256
+ throw new Error("Machine decoded must be a boolean construction flag");
257
+ }
258
+ const hasData = Object.prototype.hasOwnProperty.call(config, "data");
259
+ if (config.decoded === true && !hasData)
260
+ throw new Error("Machine decoded construction requires data");
261
+ if (hasData && resolve !== undefined)
262
+ throw new Error("Machine data and resolve are mutually exclusive");
263
+ const data = config.data;
264
+ const construct = typeof data === "function" ? data : () => data;
413
265
  const guarded = guard !== undefined;
414
266
  const declinable = guarded || config.declinable === true;
415
267
  if (typeof config.branches === "string") {
@@ -417,7 +269,7 @@ const normalizeObjectTransition = (raw, declaration, path, stateNodes) => {
417
269
  if (group === undefined || resolve === undefined) {
418
270
  throw new Error("Machine branching transition requires a registered group and resolver");
419
271
  }
420
- if (["target", "update", "initial", "history", "none", "from", "decoded"].some((key) => config[key] !== undefined))
272
+ if (["target", "update", "input", "history", "none", "data", "decoded"].some((key) => config[key] !== undefined))
421
273
  throw new Error("Machine branching transition cannot redeclare its destination");
422
274
  const entries = Object.fromEntries(Object.entries(group).map(([key, spec]) => [key, {
423
275
  title: spec.title,
@@ -436,23 +288,37 @@ const normalizeObjectTransition = (raw, declaration, path, stateNodes) => {
436
288
  if (resolve !== undefined && selection.kind !== "none") {
437
289
  throw new Error("Machine advanced construction requires a declared branch group");
438
290
  }
439
- const method = config.from !== undefined ? "from" : config.decoded !== undefined ? "decoded" : undefined;
291
+ const input = config.input;
292
+ const constructInput = typeof input === "function" ? input : () => input;
293
+ if (selection.path !== "" && Object.hasOwn(config, "input")) {
294
+ throw new Error("Machine input belongs only to root targets");
295
+ }
296
+ if (selection.path === "" && selection.kind !== "update" && (hasData || config.decoded !== undefined)) {
297
+ throw new Error("Machine root targets accept input instead of data");
298
+ }
440
299
  return {
441
300
  target: () => selection,
442
301
  reenter: config.reenter,
443
302
  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);
303
+ resolve: (context, enqueue) => {
304
+ if (guarded && !guard(context))
305
+ return Topology.makeDeclined();
306
+ if (resolve !== undefined)
307
+ return resolve(context, enqueue);
308
+ if (selection.path === "" && selection.kind !== "update") {
309
+ return constructBranch(selection, stateNodes, declaration, { input: constructInput(context) });
454
310
  }
455
- })
311
+ const value = construct(context);
312
+ return constructBranch(selection, stateNodes, declaration, selection.updatePath === undefined
313
+ ? {
314
+ ...(hasData ? { data: value } : {}),
315
+ ...(config.decoded === undefined ? {} : { decoded: config.decoded })
316
+ }
317
+ : {
318
+ ...(value.target === undefined ? {} : { data: value.target, decoded: config.decoded }),
319
+ update: { data: value.update, decoded: config.decoded }
320
+ }, path);
321
+ }
456
322
  };
457
323
  };
458
324
  const captureTransition = (rawTransition, stateNodes, path, declaration, trigger) => {
@@ -478,7 +344,7 @@ const captureTransition = (rawTransition, stateNodes, path, declaration, trigger
478
344
  decline: Topology.makeDeclined
479
345
  };
480
346
  delete resolverContext.target;
481
- resolverContext.select = makeBranchSelectors(context, branches, owner, stateNodes, path);
347
+ resolverContext.select = makeBranchSelectors(branches, owner, stateNodes, declaration, path);
482
348
  const selected = resolve(resolverContext, enqueue);
483
349
  if (Topology.isDeclined(selected)) {
484
350
  if (!declinable) {
@@ -620,79 +486,34 @@ const flattenHandlers = (handlers, stateNodes, states, prefix, declaration, conf
620
486
  }
621
487
  };
622
488
  const makeHandle = (self, declaration) => ((config) => {
489
+ const captured = InitialDeclaration.capture(declaration.root, config);
490
+ const compiled = Object.assign(Object.create(Proto), self);
491
+ Protocol.copyProtocol(self, compiled);
492
+ compiled.states = Object.freeze({ "": captured.node });
493
+ compiled.stateNodes = Topology.compileStateNodes(compiled.states);
494
+ if (compiled.stateNodes.byPath.get("").schema === undefined && config.root !== undefined) {
495
+ throw new Error("Machine schema-less root cannot construct data");
496
+ }
497
+ const constructRoot = compiled.stateNodes.byPath.get("").schema === undefined
498
+ ? () => undefined
499
+ : InitialDeclaration.construction(config.root);
500
+ compiled.initial = (input) => ({
501
+ ...Topology.makeInitialTarget("", constructRoot({ input })),
502
+ input: { value: input }
503
+ });
504
+ compiled.initialDefinition = Object.freeze({
505
+ target: "",
506
+ selection: Object.freeze({ path: "", kind: "initial", scope: "initial" })
507
+ });
623
508
  const handlers = Object.create(null);
624
- flattenHandlers(handlers, self.stateNodes, self.states, "", declaration, { "": config });
625
- return makeWithHandlers(self, handlers);
509
+ flattenHandlers(handlers, compiled.stateNodes, compiled.states, "", {
510
+ ...declaration,
511
+ initialize: (input) => compiled.initial(Protocol.decodeInputSync(compiled, compiled.input, input))
512
+ }, { "": captured.handlers });
513
+ return makeWithHandlers(compiled, handlers);
626
514
  });
627
515
  export const isMachine = (u) => hasProperty(u, TypeId) && u[TypeId] === TypeId;
628
516
  export const isFinal = (machine, state) => internalPlanner.isFinal(machine, state);
629
- const getInitialSelectionBuilder = (initialBuilder, selection) => {
630
- const path = selection.path;
631
- if (path === undefined || path.includes(".")) {
632
- throw new Error("Machine initial target must select one top-level state");
633
- }
634
- const builder = initialBuilder[path];
635
- if (typeof builder !== "object" || builder === null || typeof builder.from !== "function") {
636
- throw new Error(`Machine could not construct selected initial state "${path}"`);
637
- }
638
- return builder;
639
- };
640
- const captureInitialBranch = (definition, initialBuilder, configuration) => {
641
- if (definition !== undefined && typeof definition !== "function") {
642
- throw new Error("Machine initial definition must be a target-first callback");
643
- }
644
- const selector = decorateInitialSelection(makeSelectionValue(configuration ? "state" : "initial", "", "initial"));
645
- const result = definition === undefined ? selector : definition(selector);
646
- let selection;
647
- let resolve;
648
- if (Topology.isTargetSelection(result)) {
649
- selection = plainTargetSelection(result);
650
- }
651
- else if (hasProperty(result, InitialBuilderDescriptorTypeId)) {
652
- const descriptor = result;
653
- selection = descriptor.selection;
654
- resolve = descriptor.resolve;
655
- }
656
- else {
657
- throw new Error("Machine initial definition must select exactly one target");
658
- }
659
- if (selection.kind !== "state" && selection.kind !== "initial") {
660
- throw new Error("Machine initial target must select a top-level state or its declared initial entry");
661
- }
662
- const captured = {
663
- selection,
664
- builder: getInitialSelectionBuilder(initialBuilder, selection)
665
- };
666
- return resolve === undefined ? Object.freeze(captured) : Object.freeze({ ...captured, resolve });
667
- };
668
- const validateInitialSelection = (result, selection) => {
669
- if (typeof result !== "object" || result === null || !hasProperty(result, "path") || result.path !== selection.path) {
670
- const resultPath = typeof result === "object" && result !== null && hasProperty(result, "path")
671
- ? String(result.path)
672
- : "<invalid>";
673
- throw new Error(`Machine initial resolver selected "${selection.path}" but constructed "${resultPath}"`);
674
- }
675
- };
676
- const compileInitial = (definition, states, stateNodes, configuration = false) => {
677
- const rootNode = getTargetBuilderNode(stateNodes, "");
678
- const initialBuilder = configuration ?
679
- makeSnapshotBuilder(states, { mode: "full", prefix: "" })
680
- : { "": withFrom((value) => Topology.makeInitialTarget("", value), "leaf", rootNode.schema !== undefined) };
681
- const branch = captureInitialBranch(definition, initialBuilder, configuration);
682
- return {
683
- initial: (input) => {
684
- const result = branch.resolve === undefined
685
- ? constructSelectedTarget(branch.builder)
686
- : branch.resolve({ input, target: branch.builder });
687
- validateInitialSelection(result, branch.selection);
688
- return result;
689
- },
690
- definition: Object.freeze({
691
- target: branch.selection.path,
692
- selection: transitionTargetSelection(branch.selection)
693
- })
694
- };
695
- };
696
517
  export const state = (node) => {
697
518
  const captured = StateDefinition.captureRoot(node);
698
519
  const access = makeStateHelpers();
@@ -711,8 +532,11 @@ const makeStateHelpers = () => {
711
532
  };
712
533
  };
713
534
  export const make = (config) => {
535
+ if (Object.hasOwn(config, "initial") || Object.hasOwn(config, "initialConfiguration")) {
536
+ throw new Error("Machine initial edges and root data belong in handle");
537
+ }
714
538
  const states = Object.freeze({ "": config.root.node });
715
- const self = Object.create(Proto);
539
+ const self = Object.create(PipeablePrototype);
716
540
  self.states = states;
717
541
  self.root = config.root;
718
542
  self.events = config.events;
@@ -721,11 +545,6 @@ export const make = (config) => {
721
545
  self.parent = config.parent;
722
546
  self.input = config.input;
723
547
  self.id = config.id;
724
- self.stateNodes = Topology.compileStateNodes(states);
725
- const compiledInitial = compileInitial(config.initialConfiguration ?? config.initial, states, self.stateNodes, config.initialConfiguration !== undefined);
726
- self.initial = compiledInitial.initial;
727
- self.initialDefinition = compiledInitial.definition;
728
- self.makeTargetBuilder = makeTargetBuilder(states, self.stateNodes);
729
548
  self.handlers = Object.create(null);
730
549
  self.handle = makeHandle(self, Declaration.capture(config.root, config));
731
550
  Protocol.setProtocol(self);