@typeonce/effect-machine 0.11.0 → 0.13.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 (83) hide show
  1. package/README.md +89 -13
  2. package/dist/Machine.d.ts +396 -127
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +21 -19
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/atom.d.ts +1 -0
  7. package/dist/internal/machine/atom.d.ts.map +1 -1
  8. package/dist/internal/machine/atom.js +23 -0
  9. package/dist/internal/machine/atom.js.map +1 -1
  10. package/dist/internal/machine/cluster.js +2 -2
  11. package/dist/internal/machine/cluster.js.map +1 -1
  12. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  13. package/dist/internal/machine/commandRuntime.js +2 -5
  14. package/dist/internal/machine/commandRuntime.js.map +1 -1
  15. package/dist/internal/machine/configuration.d.ts +12 -7
  16. package/dist/internal/machine/configuration.d.ts.map +1 -1
  17. package/dist/internal/machine/configuration.js +9 -5
  18. package/dist/internal/machine/configuration.js.map +1 -1
  19. package/dist/internal/machine/executionPlan.d.ts +4 -3
  20. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  21. package/dist/internal/machine/executionPlan.js +68 -43
  22. package/dist/internal/machine/executionPlan.js.map +1 -1
  23. package/dist/internal/machine/initialization.d.ts +5 -0
  24. package/dist/internal/machine/initialization.d.ts.map +1 -0
  25. package/dist/internal/machine/initialization.js +61 -0
  26. package/dist/internal/machine/initialization.js.map +1 -0
  27. package/dist/internal/machine/inspectionRuntime.d.ts +25 -0
  28. package/dist/internal/machine/inspectionRuntime.d.ts.map +1 -0
  29. package/dist/internal/machine/inspectionRuntime.js +79 -0
  30. package/dist/internal/machine/inspectionRuntime.js.map +1 -0
  31. package/dist/internal/machine/invocation.d.ts +2 -1
  32. package/dist/internal/machine/invocation.d.ts.map +1 -1
  33. package/dist/internal/machine/invocation.js +18 -11
  34. package/dist/internal/machine/invocation.js.map +1 -1
  35. package/dist/internal/machine/machine.d.ts +1 -2
  36. package/dist/internal/machine/machine.d.ts.map +1 -1
  37. package/dist/internal/machine/machine.js +21 -6
  38. package/dist/internal/machine/machine.js.map +1 -1
  39. package/dist/internal/machine/planner.d.ts +17 -2
  40. package/dist/internal/machine/planner.d.ts.map +1 -1
  41. package/dist/internal/machine/planner.js +137 -64
  42. package/dist/internal/machine/planner.js.map +1 -1
  43. package/dist/internal/machine/process.d.ts.map +1 -1
  44. package/dist/internal/machine/process.js +7 -5
  45. package/dist/internal/machine/process.js.map +1 -1
  46. package/dist/internal/machine/runtime.d.ts +29 -5
  47. package/dist/internal/machine/runtime.d.ts.map +1 -1
  48. package/dist/internal/machine/runtime.js +440 -64
  49. package/dist/internal/machine/runtime.js.map +1 -1
  50. package/dist/internal/machine/symbols.d.ts +2 -0
  51. package/dist/internal/machine/symbols.d.ts.map +1 -1
  52. package/dist/internal/machine/symbols.js +2 -0
  53. package/dist/internal/machine/symbols.js.map +1 -1
  54. package/dist/internal/machine/topology.d.ts +13 -0
  55. package/dist/internal/machine/topology.d.ts.map +1 -1
  56. package/dist/internal/machine/topology.js +9 -0
  57. package/dist/internal/machine/topology.js.map +1 -1
  58. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  59. package/dist/internal/testing/machine/finiteModel.js +3 -3
  60. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  61. package/dist/unstable/reactivity/AtomMachine.d.ts +9 -0
  62. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  63. package/dist/unstable/reactivity/AtomMachine.js +9 -0
  64. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  65. package/docs/agent-guide.md +130 -20
  66. package/package.json +5 -5
  67. package/src/Machine.ts +548 -217
  68. package/src/internal/machine/atom.ts +26 -0
  69. package/src/internal/machine/cluster.ts +2 -2
  70. package/src/internal/machine/commandRuntime.ts +2 -7
  71. package/src/internal/machine/configuration.ts +38 -13
  72. package/src/internal/machine/executionPlan.ts +85 -52
  73. package/src/internal/machine/initialization.ts +73 -0
  74. package/src/internal/machine/inspectionRuntime.ts +102 -0
  75. package/src/internal/machine/invocation.ts +38 -13
  76. package/src/internal/machine/machine.ts +31 -6
  77. package/src/internal/machine/planner.ts +166 -66
  78. package/src/internal/machine/process.ts +7 -5
  79. package/src/internal/machine/runtime.ts +663 -91
  80. package/src/internal/machine/symbols.ts +3 -0
  81. package/src/internal/machine/topology.ts +27 -0
  82. package/src/internal/testing/machine/finiteModel.ts +8 -7
  83. package/src/unstable/reactivity/AtomMachine.ts +12 -0
@@ -6,27 +6,28 @@
6
6
  import * as Cause from "effect/Cause";
7
7
  import * as Effect from "effect/Effect";
8
8
  import { getTargetBuilder, makeCollector } from "./command.js";
9
- import { captureHistory, compareDocumentOrder, completeConfigurationEffect, completeConfigurationSync, configurationFromHistoryRecord, getActiveLeafPathFrom, getActiveLeafPaths, getHistoryRecord, getInitialEntryPaths, getLeafPath, getParentValue, getParentValues, getPathToRoot, getRootPath, isActiveFinalConfiguration, isDescendantOf, isPathInSubtree, normalizeConfiguration, normalizeConfigurationEffect, normalizeConfigurationSync, normalizeTargetConfigurationSync, pathDepth, snapshotFromConfiguration, snapshotFromConfigurationAtPath, validateInitialConfiguration } from "./configuration.js";
9
+ import { captureHistory, compareDocumentOrder, completeConfigurationEffect, completeConfigurationSync, configurationFromHistoryRecord, configurationFromInitialTargetSync, getActiveLeafPathFrom, getActiveLeafPaths, getHistoryRecord, getInitialEntryPaths, getLeafPath, getParentValue, getParentValues, getPathToRoot, getRootPath, isActiveFinalConfiguration, isDescendantOf, isPathInSubtree, normalizeConfiguration, normalizeConfigurationEffect, normalizeConfigurationSync, normalizeTargetConfigurationSync, pathDepth, snapshotFromConfiguration, snapshotFromConfigurationAtPath, validateInitialConfiguration } from "./configuration.js";
10
10
  import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError, StoppedError } from "./errors.js";
11
+ import { getStateInitializeValues, makeStateInitializeBuilder } from "./initialization.js";
11
12
  import * as InvocationEvent from "./invocationEvent.js";
12
13
  import { decodeEventSync, decodeInputSync, decodeStateValueSync } from "./protocol.js";
13
14
  import { InitialEventTypeId } from "./symbols.js";
14
- import { getNode, isChoiceTarget, isHistoryTarget, isNoTarget, isSnapshot, isTarget, makeChoiceTarget, makeTarget, TargetSnapshotTypeId } from "./topology.js";
15
- const rootActorScopes = new WeakMap();
16
- const machineActorScopes = new WeakMap();
17
- export const withActorScope = (machine, actorScope) => {
15
+ import { getNode, isChoiceTarget, isHistoryTarget, isInitialTarget, isNoTarget, isSnapshot, isTarget, makeChoiceTarget, makeTarget, TargetSnapshotTypeId } from "./topology.js";
16
+ const rootMachineReferences = new WeakMap();
17
+ const scopedMachineReferences = new WeakMap();
18
+ export const withMachineReferences = (machine, machineReferences) => {
18
19
  const scoped = Object.create(machine);
19
- machineActorScopes.set(scoped, { self: actorScope.self, parent: actorScope.parent });
20
+ scopedMachineReferences.set(scoped, { self: machineReferences.self, parent: machineReferences.parent });
20
21
  return scoped;
21
22
  };
22
- const getActorContext = (machine, configuration) => {
23
- const scope = configuration.actorScope;
23
+ const resolveMachineReferences = (machine, configuration) => {
24
+ const scope = configuration.machineReferences;
24
25
  if (scope !== undefined)
25
26
  return scope;
26
- const machineScope = machineActorScopes.get(machine);
27
+ const machineScope = scopedMachineReferences.get(machine);
27
28
  if (machineScope !== undefined)
28
29
  return machineScope;
29
- const cached = rootActorScopes.get(machine);
30
+ const cached = rootMachineReferences.get(machine);
30
31
  if (cached !== undefined)
31
32
  return cached;
32
33
  const self = {
@@ -35,7 +36,7 @@ const getActorContext = (machine, configuration) => {
35
36
  send: () => Effect.fail(new StoppedError())
36
37
  };
37
38
  const root = { self, parent: undefined };
38
- rootActorScopes.set(machine, root);
39
+ rootMachineReferences.set(machine, root);
39
40
  return root;
40
41
  };
41
42
  export const normalizeTransition = (transition) => {
@@ -94,6 +95,7 @@ const completeHistoryConfiguration = (machine, configuration, event) => {
94
95
  const commands = [];
95
96
  const raisedEvents = [];
96
97
  const emittedEvents = [];
98
+ const transitions = [];
97
99
  let changed = true;
98
100
  while (changed) {
99
101
  changed = false;
@@ -109,22 +111,55 @@ const completeHistoryConfiguration = (machine, configuration, event) => {
109
111
  values,
110
112
  outputs: configuration.outputs,
111
113
  history: configuration.history,
112
- actorScope: configuration.actorScope
114
+ machineReferences: configuration.machineReferences
113
115
  };
116
+ if (child.type === "choice") {
117
+ const choice = resolveChoiceTarget(machine, current, makeChoiceTarget(child.path, path, Object.fromEntries(values)), event);
118
+ let next = current;
119
+ for (const routed of [choice.target, ...choice.additionalTargets]) {
120
+ if (routed === undefined || isHistoryTarget(routed)) {
121
+ throw new Error(`Machine initial choice "${child.path}" must resolve to an active state target`);
122
+ }
123
+ const resolved = isInitialTarget(routed)
124
+ ? resolveInitialTarget(machine, next, routed, event)
125
+ : undefined;
126
+ next = normalizeTargetConfigurationSync(machine, next, (resolved?.target ?? routed));
127
+ if (resolved !== undefined) {
128
+ commands.push(...resolved.commands);
129
+ raisedEvents.push(...resolved.raisedEvents);
130
+ emittedEvents.push(...resolved.emittedEvents);
131
+ transitions.push(...resolved.transitions);
132
+ }
133
+ }
134
+ active.clear();
135
+ values.clear();
136
+ for (const activePath of next.active)
137
+ active.add(activePath);
138
+ for (const [valuePath, stateValue] of next.values)
139
+ values.set(valuePath, stateValue);
140
+ commands.push(...choice.commands);
141
+ raisedEvents.push(...choice.raisedEvents);
142
+ emittedEvents.push(...choice.emittedEvents);
143
+ transitions.push(...choice.transitions);
144
+ changed = true;
145
+ continue;
146
+ }
114
147
  active.add(child.path);
115
148
  if (child.schema !== undefined) {
116
- const initializer = machine.handlers[path]?.initial;
149
+ const initializer = machine.handlers[path]?.initialize;
117
150
  if (initializer === undefined) {
118
151
  throw new Error(`Machine shallow history requires an initial value implementation for state "${path}"`);
119
152
  }
120
153
  const initialized = collectStateInitializer(machine, initializer, {
121
- ...getActorContext(machine, current),
154
+ ...resolveMachineReferences(machine, current),
122
155
  state: current.values.get(path),
123
156
  containingState: getParentValue(machine, current, path),
124
157
  ancestors: getParentValues(machine, current, path),
125
- event
158
+ event,
159
+ builder: makeStateInitializeBuilder(machine, path)
126
160
  });
127
- values.set(child.path, decodeStateValueSync(machine, child, initialized.value));
161
+ const initializedValues = getStateInitializeValues(path, initialized.value);
162
+ values.set(child.path, decodeStateValueSync(machine, child, initializedValues[child.key]));
128
163
  commands.push(...initialized.commands);
129
164
  raisedEvents.push(...initialized.raisedEvents);
130
165
  emittedEvents.push(...initialized.emittedEvents);
@@ -140,31 +175,32 @@ const completeHistoryConfiguration = (machine, configuration, event) => {
140
175
  values,
141
176
  outputs: configuration.outputs,
142
177
  history: configuration.history,
143
- actorScope: configuration.actorScope
178
+ machineReferences: configuration.machineReferences
144
179
  };
145
- const initializer = valuedMissing.length === 0 ? undefined : machine.handlers[path]?.initial;
180
+ const initializer = valuedMissing.length === 0 ? undefined : machine.handlers[path]?.initialize;
146
181
  if (valuedMissing.length > 0 && initializer === undefined) {
147
182
  throw new Error(`Machine shallow history requires an initial value implementation for state "${path}"`);
148
183
  }
149
184
  const initialized = initializer === undefined ? undefined : collectStateInitializer(machine, initializer, {
150
- ...getActorContext(machine, current),
185
+ ...resolveMachineReferences(machine, current),
151
186
  state: current.values.get(path),
152
187
  containingState: getParentValue(machine, current, path),
153
188
  ancestors: getParentValues(machine, current, path),
154
- event
189
+ event,
190
+ builder: makeStateInitializeBuilder(machine, path)
155
191
  });
156
- if (initialized !== undefined && (typeof initialized.value !== "object" || initialized.value === null)) {
157
- throw new Error(`Machine parallel state initializer for "${path}" must return its region values`);
158
- }
192
+ const initializedValues = initialized === undefined
193
+ ? undefined
194
+ : getStateInitializeValues(path, initialized.value);
159
195
  for (const childPath of missing) {
160
196
  const child = getNode(machine, childPath);
161
197
  active.add(child.path);
162
198
  if (child.schema !== undefined) {
163
199
  if (initialized === undefined ||
164
- !Object.prototype.hasOwnProperty.call(initialized.value, child.key)) {
200
+ initializedValues === undefined || !Object.prototype.hasOwnProperty.call(initializedValues, child.key)) {
165
201
  throw new Error(`Machine parallel state initializer for "${path}" must return region "${child.key}"`);
166
202
  }
167
- values.set(child.path, decodeStateValueSync(machine, child, initialized.value[child.key]));
203
+ values.set(child.path, decodeStateValueSync(machine, child, initializedValues[child.key]));
168
204
  }
169
205
  }
170
206
  if (initialized !== undefined) {
@@ -186,10 +222,26 @@ const completeHistoryConfiguration = (machine, configuration, event) => {
186
222
  },
187
223
  commands,
188
224
  raisedEvents,
189
- emittedEvents
225
+ emittedEvents,
226
+ transitions
190
227
  };
191
228
  };
192
- const resolveHistoryTarget = (machine, configuration, target, event) => {
229
+ export function resolveInitialTarget(machine, configuration, target, event) {
230
+ const partial = configurationFromInitialTargetSync(machine, configuration, target);
231
+ const completed = completeHistoryConfiguration(machine, partial, event);
232
+ const snapshot = snapshotFromConfigurationAtPath(machine, completed.configuration, target.path);
233
+ return {
234
+ target: makeTarget(target.path, snapshot.value, {
235
+ snapshot: snapshot,
236
+ values: Object.fromEntries(completed.configuration.values)
237
+ }),
238
+ commands: completed.commands,
239
+ raisedEvents: completed.raisedEvents,
240
+ emittedEvents: completed.emittedEvents,
241
+ transitions: completed.transitions
242
+ };
243
+ }
244
+ function resolveHistoryTarget(machine, configuration, target, event) {
193
245
  const node = getNode(machine, target.path);
194
246
  if (node.type !== "history" || node.parent !== target.parent) {
195
247
  throw new Error(`Machine expected history target "${target.path}" to resolve to its declared parent`);
@@ -211,7 +263,7 @@ const resolveHistoryTarget = (machine, configuration, target, event) => {
211
263
  commands: completed.commands,
212
264
  raisedEvents: completed.raisedEvents,
213
265
  emittedEvents: completed.emittedEvents,
214
- transitions: []
266
+ transitions: completed.transitions
215
267
  };
216
268
  }
217
269
  const key = node.key;
@@ -262,7 +314,7 @@ const resolveHistoryTarget = (machine, configuration, target, event) => {
262
314
  emittedEvents: [...collected.emittedEvents, ...(choiceResolution?.emittedEvents ?? [])],
263
315
  transitions: choiceResolution?.transitions ?? []
264
316
  };
265
- };
317
+ }
266
318
  const getCandidatePaths = (machine, configuration) => Array.from(configuration.active)
267
319
  .sort((left, right) => {
268
320
  const depth = pathDepth(machine, right) - pathDepth(machine, left);
@@ -304,14 +356,14 @@ export const sortEntryPaths = (machine, paths) => Array.from(new Set(paths))
304
356
  return depth === 0 ? compareDocumentOrder(machine, left, right) : depth;
305
357
  });
306
358
  const makeStateActionContext = (machine, configuration, path, event) => ({
307
- ...getActorContext(machine, configuration),
359
+ ...resolveMachineReferences(machine, configuration),
308
360
  state: configuration.values.get(path),
309
361
  containingState: getParentValue(machine, configuration, path),
310
362
  ancestors: getParentValues(machine, configuration, path),
311
363
  event
312
364
  });
313
365
  const makeTransitionContext = (machine, configuration, path, event, snapshot) => ({
314
- ...getActorContext(machine, configuration),
366
+ ...resolveMachineReferences(machine, configuration),
315
367
  state: configuration.values.get(path),
316
368
  containingState: getParentValue(machine, configuration, path),
317
369
  ancestors: getParentValues(machine, configuration, path),
@@ -320,7 +372,7 @@ const makeTransitionContext = (machine, configuration, path, event, snapshot) =>
320
372
  target: getTargetBuilder(machine, path)
321
373
  });
322
374
  const makeDoneContext = (machine, configuration, path, event, output, snapshot) => ({
323
- ...getActorContext(machine, configuration),
375
+ ...resolveMachineReferences(machine, configuration),
324
376
  state: configuration.values.get(path),
325
377
  containingState: getParentValue(machine, configuration, path),
326
378
  ancestors: getParentValues(machine, configuration, path),
@@ -358,7 +410,7 @@ const selectAlwaysTransitions = (machine, configuration, event) => {
358
410
  trigger: { type: "always" },
359
411
  transition: always,
360
412
  context: {
361
- ...getActorContext(machine, configuration),
413
+ ...resolveMachineReferences(machine, configuration),
362
414
  state: configuration.values.get(path),
363
415
  containingState: getParentValue(machine, configuration, path),
364
416
  ancestors: getParentValues(machine, configuration, path),
@@ -439,7 +491,7 @@ const selectInvocationTransition = (machine, configuration, event) => {
439
491
  return [];
440
492
  const snapshot = snapshotFromConfiguration(machine, configuration);
441
493
  const context = {
442
- ...getActorContext(machine, configuration),
494
+ ...resolveMachineReferences(machine, configuration),
443
495
  state: configuration.values.get(event.path),
444
496
  containingState: getParentValue(machine, configuration, event.path),
445
497
  ancestors: getParentValues(machine, configuration, event.path),
@@ -563,7 +615,7 @@ const withChoiceValues = (target, values) => {
563
615
  values: { ...values, ...(target.values ?? {}) }
564
616
  });
565
617
  };
566
- const resolveChoiceTarget = (machine, configuration, initialTarget, event) => {
618
+ function resolveChoiceTarget(machine, configuration, initialTarget, event) {
567
619
  const pending = [initialTarget];
568
620
  const resolvedTargets = [];
569
621
  const commands = [];
@@ -606,10 +658,10 @@ const resolveChoiceTarget = (machine, configuration, initialTarget, event) => {
606
658
  ]),
607
659
  outputs: configuration.outputs,
608
660
  history: configuration.history,
609
- ...(configuration.actorScope === undefined ? {} : { actorScope: configuration.actorScope })
661
+ ...(configuration.machineReferences === undefined ? {} : { machineReferences: configuration.machineReferences })
610
662
  };
611
663
  const collected = collectTransition(machine, choice.transition, {
612
- ...getActorContext(machine, provisional),
664
+ ...resolveMachineReferences(machine, provisional),
613
665
  containingState: getParentValue(machine, provisional, node.path),
614
666
  ancestors: getParentValues(machine, provisional, node.path),
615
667
  event,
@@ -633,7 +685,7 @@ const resolveChoiceTarget = (machine, configuration, initialTarget, event) => {
633
685
  raisedEvents.push(...collected.raisedEvents);
634
686
  emittedEvents.push(...collected.emittedEvents);
635
687
  }
636
- if (!isTarget(current) && !isSnapshot(current) && !isHistoryTarget(current)) {
688
+ if (!isTarget(current) && !isSnapshot(current) && !isHistoryTarget(current) && !isInitialTarget(current)) {
637
689
  throw new Error("Machine choice resolver must return a concrete typed target");
638
690
  }
639
691
  resolvedTargets.push(current);
@@ -646,7 +698,7 @@ const resolveChoiceTarget = (machine, configuration, initialTarget, event) => {
646
698
  emittedEvents,
647
699
  transitions
648
700
  };
649
- };
701
+ }
650
702
  const collectEvaluatedTransition = (machine, state, selection) => {
651
703
  const stateIdentifier = selection.leafPath;
652
704
  const transitionResult = collectTransition(machine, selection.transition.transition, selection.context);
@@ -658,45 +710,58 @@ const collectEvaluatedTransition = (machine, state, selection) => {
658
710
  ? undefined
659
711
  : resolveChoiceTarget(machine, state, unresolvedTarget, selection.context.event);
660
712
  const choiceResolvedTarget = choiceResolution?.target ?? unresolvedTarget;
713
+ const initialResolution = choiceResolvedTarget !== undefined && isInitialTarget(choiceResolvedTarget)
714
+ ? resolveInitialTarget(machine, state, choiceResolvedTarget, selection.context.event)
715
+ : undefined;
716
+ const routedTarget = initialResolution?.target ?? choiceResolvedTarget;
661
717
  let historyResolution;
662
- const reenteredHistoryTarget = isHistoryTarget(choiceResolvedTarget) && selection.transition.reenter &&
663
- state.active.has(choiceResolvedTarget.parent)
664
- ? choiceResolvedTarget
718
+ const reenteredHistoryTarget = isHistoryTarget(routedTarget) && selection.transition.reenter &&
719
+ state.active.has(routedTarget.parent)
720
+ ? routedTarget
665
721
  : undefined;
666
- if (choiceResolvedTarget !== undefined && isHistoryTarget(choiceResolvedTarget)) {
722
+ if (routedTarget !== undefined && isHistoryTarget(routedTarget)) {
667
723
  // A reentering transition may exit the history node's own parent. SCXML
668
724
  // history observes that same exit, so resolve against a provisional
669
725
  // capture rather than an older record (or the default).
670
726
  const provisionalBoundary = selection.transition.reenter
671
727
  ? getNode(machine, selection.sourcePath).parent
672
- : getLeastCommonAncestor(machine, stateIdentifier, choiceResolvedTarget.parent);
728
+ : getLeastCommonAncestor(machine, stateIdentifier, routedTarget.parent);
673
729
  const provisionalExitPaths = reenteredHistoryTarget !== undefined
674
- ? sortExitPaths(machine, Array.from(state.active).filter((path) => isPathInSubtree(path, choiceResolvedTarget.parent)))
730
+ ? sortExitPaths(machine, Array.from(state.active).filter((path) => isPathInSubtree(path, routedTarget.parent)))
675
731
  : getExitPaths(machine, state, provisionalBoundary);
676
- const stateAtHistoryResolution = provisionalExitPaths.includes(choiceResolvedTarget.parent)
732
+ const stateAtHistoryResolution = provisionalExitPaths.includes(routedTarget.parent)
677
733
  ? captureHistory(machine, state, state, provisionalExitPaths)
678
734
  : state;
679
- historyResolution = resolveHistoryTarget(machine, stateAtHistoryResolution, choiceResolvedTarget, selection.context.event);
735
+ historyResolution = resolveHistoryTarget(machine, stateAtHistoryResolution, routedTarget, selection.context.event);
680
736
  }
681
737
  const target = historyResolution === undefined
682
- ? choiceResolvedTarget
738
+ ? routedTarget
683
739
  : historyResolution.target;
684
- const additionalHistoryActions = [];
685
- const additionalHistoryRaisedEvents = [];
686
- const additionalHistoryEmittedEvents = [];
687
- const additionalHistoryChoiceTransitions = [];
740
+ const additionalTargetActions = [];
741
+ const additionalTargetRaisedEvents = [];
742
+ const additionalTargetEmittedEvents = [];
743
+ const additionalTargetChoiceTransitions = [];
688
744
  const additionalChoiceTargets = [];
689
745
  for (const additionalTarget of choiceResolution?.additionalTargets ?? []) {
746
+ if (isInitialTarget(additionalTarget)) {
747
+ const resolved = resolveInitialTarget(machine, state, additionalTarget, selection.context.event);
748
+ additionalChoiceTargets.push(resolved.target);
749
+ additionalTargetActions.push(...resolved.commands);
750
+ additionalTargetRaisedEvents.push(...resolved.raisedEvents);
751
+ additionalTargetEmittedEvents.push(...resolved.emittedEvents);
752
+ additionalTargetChoiceTransitions.push(...resolved.transitions);
753
+ continue;
754
+ }
690
755
  if (!isHistoryTarget(additionalTarget)) {
691
756
  additionalChoiceTargets.push(additionalTarget);
692
757
  continue;
693
758
  }
694
759
  const resolved = resolveHistoryTarget(machine, state, additionalTarget, selection.context.event);
695
760
  additionalChoiceTargets.push(resolved.target);
696
- additionalHistoryActions.push(...resolved.commands);
697
- additionalHistoryRaisedEvents.push(...resolved.raisedEvents);
698
- additionalHistoryEmittedEvents.push(...resolved.emittedEvents);
699
- additionalHistoryChoiceTransitions.push(...resolved.transitions);
761
+ additionalTargetActions.push(...resolved.commands);
762
+ additionalTargetRaisedEvents.push(...resolved.raisedEvents);
763
+ additionalTargetEmittedEvents.push(...resolved.emittedEvents);
764
+ additionalTargetChoiceTransitions.push(...resolved.transitions);
700
765
  }
701
766
  const targetPath = target === undefined
702
767
  ? undefined
@@ -718,28 +783,32 @@ const collectEvaluatedTransition = (machine, state, selection) => {
718
783
  commands: [
719
784
  ...transitionResult.commands,
720
785
  ...(choiceResolution?.commands ?? []),
786
+ ...(initialResolution?.commands ?? []),
721
787
  ...(historyResolution?.commands ?? []),
722
- ...additionalHistoryActions
788
+ ...additionalTargetActions
723
789
  ],
724
790
  raisedEvents: [
725
791
  ...transitionResult.raisedEvents,
726
792
  ...(choiceResolution?.raisedEvents ?? []),
793
+ ...(initialResolution?.raisedEvents ?? []),
727
794
  ...(historyResolution?.raisedEvents ?? []),
728
- ...additionalHistoryRaisedEvents
795
+ ...additionalTargetRaisedEvents
729
796
  ],
730
797
  emittedEvents: [
731
798
  ...transitionResult.emittedEvents,
732
799
  ...(choiceResolution?.emittedEvents ?? []),
800
+ ...(initialResolution?.emittedEvents ?? []),
733
801
  ...(historyResolution?.emittedEvents ?? []),
734
- ...additionalHistoryEmittedEvents
802
+ ...additionalTargetEmittedEvents
735
803
  ],
736
804
  changed,
737
805
  exitPaths: [],
738
806
  entryPaths: [],
739
807
  choiceTransitions: [
740
808
  ...(choiceResolution?.transitions ?? []),
809
+ ...(initialResolution?.transitions ?? []),
741
810
  ...(historyResolution?.transitions ?? []),
742
- ...additionalHistoryChoiceTransitions
811
+ ...additionalTargetChoiceTransitions
743
812
  ]
744
813
  };
745
814
  }
@@ -757,20 +826,23 @@ const collectEvaluatedTransition = (machine, state, selection) => {
757
826
  commands: [
758
827
  ...transitionResult.commands,
759
828
  ...(choiceResolution?.commands ?? []),
829
+ ...(initialResolution?.commands ?? []),
760
830
  ...(historyResolution?.commands ?? []),
761
- ...additionalHistoryActions
831
+ ...additionalTargetActions
762
832
  ],
763
833
  raisedEvents: [
764
834
  ...transitionResult.raisedEvents,
765
835
  ...(choiceResolution?.raisedEvents ?? []),
836
+ ...(initialResolution?.raisedEvents ?? []),
766
837
  ...(historyResolution?.raisedEvents ?? []),
767
- ...additionalHistoryRaisedEvents
838
+ ...additionalTargetRaisedEvents
768
839
  ],
769
840
  emittedEvents: [
770
841
  ...transitionResult.emittedEvents,
771
842
  ...(choiceResolution?.emittedEvents ?? []),
843
+ ...(initialResolution?.emittedEvents ?? []),
772
844
  ...(historyResolution?.emittedEvents ?? []),
773
- ...additionalHistoryEmittedEvents
845
+ ...additionalTargetEmittedEvents
774
846
  ],
775
847
  changed,
776
848
  exitPaths: reenteredHistoryTarget !== undefined
@@ -781,8 +853,9 @@ const collectEvaluatedTransition = (machine, state, selection) => {
781
853
  : getEntryPaths(machine, stateAfterTransition, boundary),
782
854
  choiceTransitions: [
783
855
  ...(choiceResolution?.transitions ?? []),
856
+ ...(initialResolution?.transitions ?? []),
784
857
  ...(historyResolution?.transitions ?? []),
785
- ...additionalHistoryChoiceTransitions
858
+ ...additionalTargetChoiceTransitions
786
859
  ]
787
860
  };
788
861
  };