@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
@@ -8,7 +8,7 @@ import * as Cause from "effect/Cause"
8
8
  import * as Effect from "effect/Effect"
9
9
  import type * as Schema from "effect/Schema"
10
10
  import type { Enqueue, InitialEvent as MachineInitialEvent, Machine, MachineTarget } from "../../Machine.js"
11
- import { getTargetBuilder, makeCollector, type RuntimeCommand } from "./command.js"
11
+ import { makeCollector, type RuntimeCommand } from "./command.js"
12
12
  import {
13
13
  type ActiveConfiguration,
14
14
  captureHistory,
@@ -35,11 +35,12 @@ import {
35
35
  normalizeTargetConfigurationSync,
36
36
  pathDepth,
37
37
  snapshotFromConfiguration,
38
- snapshotFromConfigurationAtPath,
39
- validateInitialConfiguration
38
+ snapshotFromConfigurationAtPath
40
39
  } from "./configuration.js"
40
+ import * as Construction from "./construction.js"
41
41
  import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError, StoppedError } from "./errors.js"
42
42
  import { type CapturedStateConfig, toImpl } from "./implementation.js"
43
+ import { isDataInitializer } from "./initialDeclaration.js"
43
44
  import { getStateInitializeValues, makeStateInitializeBuilder } from "./initialization.js"
44
45
  import * as InvocationDefinition from "./invocationDefinition.js"
45
46
  import * as InvocationEvent from "./invocationEvent.js"
@@ -265,7 +266,8 @@ const collectStateInitializer = (
265
266
  const completeHistoryConfiguration = (
266
267
  machine: Machine.Any,
267
268
  configuration: ActiveConfiguration,
268
- event: unknown
269
+ event: unknown,
270
+ input?: { readonly value: unknown }
269
271
  ) => {
270
272
  const active = new Set(configuration.active)
271
273
  const values = new Map(configuration.values)
@@ -300,11 +302,13 @@ const completeHistoryConfiguration = (
300
302
  )
301
303
  let next = current
302
304
  for (const routed of [choice.target, ...choice.additionalTargets]) {
303
- if (routed === undefined || isHistoryTarget(routed)) {
305
+ if (routed === undefined) {
304
306
  throw new Error(`Machine initial choice "${child.path}" must resolve to an active state target`)
305
307
  }
306
308
  const resolved = isInitialTarget(routed)
307
309
  ? resolveInitialTarget(machine, next, routed, event)
310
+ : isHistoryTarget(routed)
311
+ ? resolveHistoryTarget(machine, next, routed, event)
308
312
  : undefined
309
313
  next = normalizeTargetConfigurationSync(
310
314
  machine,
@@ -343,7 +347,8 @@ const completeHistoryConfiguration = (
343
347
  containingState: getParentValue(machine, current, path),
344
348
  ancestors: getParentValues(machine, current, path),
345
349
  event,
346
- builder: makeStateInitializeBuilder(machine, path)
350
+ ...(path === "" && input !== undefined ? { input: input.value } : {}),
351
+ ...(isDataInitializer(initializer) ? {} : { builder: makeStateInitializeBuilder(machine, path) })
347
352
  })
348
353
  const initializedValues = getStateInitializeValues(path, initialized.value)
349
354
  values.set(child.path, decodeStateValueSync(machine, child, initializedValues[child.key]))
@@ -371,7 +376,8 @@ const completeHistoryConfiguration = (
371
376
  containingState: getParentValue(machine, current, path),
372
377
  ancestors: getParentValues(machine, current, path),
373
378
  event,
374
- builder: makeStateInitializeBuilder(machine, path)
379
+ ...(path === "" && input !== undefined ? { input: input.value } : {}),
380
+ ...(isDataInitializer(initializer) ? {} : { builder: makeStateInitializeBuilder(machine, path) })
375
381
  })
376
382
  const initializedValues = initialized === undefined
377
383
  ? undefined
@@ -387,6 +393,10 @@ const completeHistoryConfiguration = (
387
393
  if (
388
394
  initializedValues === undefined || !Object.prototype.hasOwnProperty.call(initializedValues, child.key)
389
395
  ) {
396
+ if (initializer !== undefined && isDataInitializer(initializer)) {
397
+ values.set(child.path, decodeStateValueSync(machine, child, makeStateInput({})))
398
+ continue
399
+ }
390
400
  throw new Error(`Machine parallel state initializer for "${path}" must return region "${child.key}"`)
391
401
  }
392
402
  values.set(
@@ -419,19 +429,74 @@ const completeHistoryConfiguration = (
419
429
  }
420
430
  }
421
431
 
432
+ /** Resolves initial data into owned configuration without a snapshot round trip. */
433
+ export function resolveInitialConfiguration(
434
+ machine: Machine.Any,
435
+ configuration: ActiveConfiguration,
436
+ target: InitialTargetInstruction,
437
+ event: unknown
438
+ ) {
439
+ let partial = configurationFromInitialTargetSync(machine, configuration, target)
440
+ if (
441
+ target.children === undefined ||
442
+ ![...target.children.keys()].some((path) => getNode(machine, path).type === "choice")
443
+ ) {
444
+ return completeHistoryConfiguration(machine, partial, event, target.input)
445
+ }
446
+ const commands: Array<RuntimeCommand> = []
447
+ const raisedEvents: Array<unknown> = []
448
+ const emittedEvents: Array<unknown> = []
449
+ const transitions: Array<ResolvedChoiceTransition> = []
450
+ for (const path of target.children?.keys() ?? []) {
451
+ const node = getNode(machine, path)
452
+ if (node.type !== "choice") continue
453
+ const choice = resolveChoiceTarget(machine, partial, makeChoiceTarget(path, node.parent!), event)
454
+ for (const routed of [choice.target, ...choice.additionalTargets]) {
455
+ const resolved = isInitialTarget(routed) ?
456
+ resolveInitialTarget(machine, partial, routed, event)
457
+ : isHistoryTarget(routed)
458
+ ? resolveHistoryTarget(machine, partial, routed, event)
459
+ : undefined
460
+ partial = normalizeTargetConfigurationSync(
461
+ machine,
462
+ partial,
463
+ (resolved?.target ?? routed) as Machine.Target<any, any>
464
+ )
465
+ if (resolved !== undefined) {
466
+ commands.push(...resolved.commands)
467
+ raisedEvents.push(...resolved.raisedEvents)
468
+ emittedEvents.push(...resolved.emittedEvents)
469
+ transitions.push(...resolved.transitions)
470
+ }
471
+ }
472
+ commands.push(...choice.commands)
473
+ raisedEvents.push(...choice.raisedEvents)
474
+ emittedEvents.push(...choice.emittedEvents)
475
+ transitions.push(...choice.transitions)
476
+ }
477
+ const completed = completeHistoryConfiguration(machine, partial, event, target.input)
478
+ if (transitions.length === 0) return completed
479
+ return {
480
+ ...completed,
481
+ commands: [...commands, ...completed.commands],
482
+ raisedEvents: [...raisedEvents, ...completed.raisedEvents],
483
+ emittedEvents: [...emittedEvents, ...completed.emittedEvents],
484
+ transitions: [...transitions, ...completed.transitions]
485
+ }
486
+ }
487
+
422
488
  export function resolveInitialTarget(
423
489
  machine: Machine.Any,
424
490
  configuration: ActiveConfiguration,
425
491
  target: InitialTargetInstruction,
426
492
  event: unknown
427
493
  ) {
428
- const partial = configurationFromInitialTargetSync(machine, configuration, target)
429
- const completed = completeHistoryConfiguration(machine, partial, event)
494
+ const completed = resolveInitialConfiguration(machine, configuration, target, event)
430
495
  const snapshot = snapshotFromConfigurationAtPath(machine, completed.configuration, target.path)
431
496
  return {
432
497
  target: makeTarget(target.path as any, snapshot.value as any, {
433
498
  snapshot: snapshot as any,
434
- values: Object.fromEntries(completed.configuration.values) as any
499
+ values: target.values as any
435
500
  }),
436
501
  commands: completed.commands,
437
502
  raisedEvents: completed.raisedEvents,
@@ -479,7 +544,7 @@ function resolveHistoryTarget(
479
544
  }
480
545
  const collected = collectTransition(machine, fallback, {
481
546
  event,
482
- target: getTargetBuilder(machine, target.parent).full[""],
547
+ target: (config: unknown) => Construction.snapshot(machine.stateNodes, config),
483
548
  owner: target.parent
484
549
  })
485
550
  if (collected.state === undefined || isHistoryTarget(collected.state) || !isSnapshot(collected.state)) {
@@ -716,8 +781,7 @@ const makeTransitionContext = <
716
781
  containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
717
782
  ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
718
783
  event,
719
- snapshot,
720
- target: getTargetBuilder(machine, path)
784
+ snapshot
721
785
  })
722
786
 
723
787
  const makeDoneContext = <
@@ -739,8 +803,7 @@ const makeDoneContext = <
739
803
  ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
740
804
  event,
741
805
  output: output as Machine.CompletionOutputByIdentifier<States, StateId>,
742
- snapshot,
743
- target: getTargetBuilder(machine, path)
806
+ snapshot
744
807
  })
745
808
 
746
809
  const collectStateActions = <
@@ -843,8 +906,7 @@ const selectAlwaysTransitions = <
843
906
  Machine.StateIdentifier<States>
844
907
  >,
845
908
  event,
846
- snapshot: capturedSnapshot(),
847
- target: getTargetBuilder(machine, path)
909
+ snapshot: capturedSnapshot()
848
910
  }
849
911
  })
850
912
  evaluatedSources.set(path, candidate)
@@ -1052,7 +1114,6 @@ const selectInvocationTransition = <
1052
1114
  containingState: getParentValue(machine, configuration, event.path),
1053
1115
  ancestors: getParentValues(machine, configuration, event.path),
1054
1116
  snapshot,
1055
- target: getTargetBuilder(machine, event.path),
1056
1117
  id: event.id,
1057
1118
  ...(event.type === "element"
1058
1119
  ? { element: event.element }
@@ -1228,6 +1289,7 @@ const withChoiceValues = (target: unknown, values: Readonly<Record<string, unkno
1228
1289
  if (isChoiceTarget(target)) {
1229
1290
  return makeChoiceTarget(target.path, target.parent, { ...values, ...(target.values ?? {}) })
1230
1291
  }
1292
+ if (isInitialTarget(target)) return { ...target, values: { ...values, ...target.values } }
1231
1293
  if (!isTarget(target) || Object.keys(values).length === 0) return target
1232
1294
  const snapshot = target[TargetSnapshotTypeId]
1233
1295
  return makeTarget(target.path, target.value, {
@@ -1308,8 +1370,7 @@ function resolveChoiceTarget(
1308
1370
  ...resolveMachineReferences(machine, provisional),
1309
1371
  containingState: getParentValue(machine, provisional, node.path),
1310
1372
  ancestors: getParentValues(machine, provisional, node.path),
1311
- event,
1312
- target: getTargetBuilder(machine, node.path)
1373
+ event
1313
1374
  },
1314
1375
  choice.evaluate
1315
1376
  )
@@ -1403,17 +1464,23 @@ const collectEvaluatedTransition = <
1403
1464
  }
1404
1465
  })()
1405
1466
  : undefined
1467
+ // Initial constructors and choices observe the owner value being committed
1468
+ // with this target, while the source configuration remains untouched.
1469
+ const constructionState =
1470
+ update !== undefined && (isInitialTarget(unresolvedTarget) || isChoiceTarget(unresolvedTarget))
1471
+ ? { ...state, values: new Map(state.values).set(update.path, update.value) }
1472
+ : state
1406
1473
  const choiceResolution = unresolvedTarget === undefined
1407
1474
  ? undefined
1408
1475
  : resolveChoiceTarget(
1409
1476
  machine,
1410
- state,
1477
+ constructionState,
1411
1478
  unresolvedTarget,
1412
1479
  (selection.context as any).event
1413
1480
  )
1414
1481
  const choiceResolvedTarget = choiceResolution?.target ?? unresolvedTarget
1415
1482
  const initialResolution = choiceResolvedTarget !== undefined && isInitialTarget(choiceResolvedTarget)
1416
- ? resolveInitialTarget(machine, state, choiceResolvedTarget, (selection.context as any).event)
1483
+ ? resolveInitialTarget(machine, constructionState, choiceResolvedTarget, (selection.context as any).event)
1417
1484
  : undefined
1418
1485
  const routedTarget = initialResolution?.target ?? choiceResolvedTarget
1419
1486
  let historyResolution: {
@@ -1720,77 +1787,13 @@ export const planInitialSync = <
1720
1787
  outputs: new Map(),
1721
1788
  history: new Map()
1722
1789
  }
1723
- const rootResolution = isInitialTarget(initial)
1724
- ? resolveInitialTarget(machine, emptyConfiguration, initial, InitialEvent)
1725
- : undefined
1726
- const state = rootResolution?.target ?? initial
1727
- const initialChoice = choiceFromTarget(state)
1728
- const choiceResolution = initialChoice === undefined
1729
- ? undefined
1730
- : resolveChoiceTarget(
1731
- machine,
1732
- emptyConfiguration,
1733
- state,
1734
- InitialEvent
1735
- )
1736
- const initialHistoryActions: Array<RuntimeCommand> = []
1737
- const initialHistoryRaisedEvents: Array<unknown> = []
1738
- const initialHistoryEmittedEvents: Array<unknown> = []
1739
- const initialHistoryChoiceTransitions: Array<ResolvedChoiceTransition> = []
1740
- const resolvedInitialTargets: Array<unknown> = []
1741
- for (
1742
- const target of choiceResolution !== undefined
1743
- ? [choiceResolution.target, ...choiceResolution.additionalTargets]
1744
- : rootResolution === undefined
1745
- ? []
1746
- : [rootResolution.target]
1747
- ) {
1748
- if (!isHistoryTarget(target)) {
1749
- resolvedInitialTargets.push(target)
1750
- continue
1751
- }
1752
- const history = resolveHistoryTarget(machine, emptyConfiguration, target, InitialEvent)
1753
- resolvedInitialTargets.push(history.target)
1754
- initialHistoryActions.push(...history.commands)
1755
- initialHistoryRaisedEvents.push(...history.raisedEvents)
1756
- initialHistoryEmittedEvents.push(...history.emittedEvents)
1757
- initialHistoryChoiceTransitions.push(...history.transitions)
1758
- }
1759
- let resolvedConfiguration = resolvedInitialTargets.length === 0
1760
- ? normalizeConfigurationSync<States>(machine, state as Machine.Snapshot<States>)
1761
- : normalizeTargetConfigurationSync<States>(
1762
- machine,
1763
- emptyConfiguration,
1764
- resolvedInitialTargets[0] as
1765
- | Machine.Snapshot<States>
1766
- | Machine.Target<States, Machine.StateIdentifier<States>>
1767
- )
1768
- for (const additionalTarget of resolvedInitialTargets.slice(1)) {
1769
- resolvedConfiguration = normalizeTargetConfigurationSync<States>(
1770
- machine,
1771
- resolvedConfiguration,
1772
- additionalTarget as Machine.Snapshot<States> | Machine.Target<States, Machine.StateIdentifier<States>>
1773
- )
1774
- }
1775
- const configuration: ActiveConfiguration = resolvedConfiguration
1776
- if (machine.initialDefinition.selection.kind === "initial") validateInitialConfiguration(machine, configuration)
1790
+ const rootResolution = resolveInitialTarget(machine, emptyConfiguration, initial, InitialEvent)
1791
+ const configuration = normalizeTargetConfigurationSync(machine, emptyConfiguration, rootResolution.target)
1777
1792
  const startingState = snapshotFromConfiguration<States>(machine, configuration)
1778
1793
  const initialEntryPaths = getInitialEntryPaths(machine, configuration)
1779
- const commands = [
1780
- ...(rootResolution?.commands ?? []),
1781
- ...(choiceResolution?.commands ?? []),
1782
- ...initialHistoryActions
1783
- ]
1784
- const raisedEvents = [
1785
- ...(rootResolution?.raisedEvents ?? []),
1786
- ...(choiceResolution?.raisedEvents ?? []),
1787
- ...initialHistoryRaisedEvents
1788
- ]
1789
- const emittedEvents = [
1790
- ...(rootResolution?.emittedEvents ?? []),
1791
- ...(choiceResolution?.emittedEvents ?? []),
1792
- ...initialHistoryEmittedEvents
1793
- ]
1794
+ const commands = rootResolution.commands
1795
+ const raisedEvents = rootResolution.raisedEvents
1796
+ const emittedEvents = rootResolution.emittedEvents
1794
1797
  const entry = collectStateActions<States, Events, Emits, E, R>(
1795
1798
  machine,
1796
1799
  configuration,
@@ -1805,17 +1808,12 @@ export const planInitialSync = <
1805
1808
  [...entry.commands],
1806
1809
  [...raisedEvents, ...entry.raisedEvents] as Array<Machine.EventOf<Events>>,
1807
1810
  [...emittedEvents, ...entry.emittedEvents],
1808
- choiceResolution === undefined && (rootResolution?.transitions.length ?? 0) === 0 &&
1809
- initialHistoryChoiceTransitions.length === 0 ?
1811
+ rootResolution.transitions.length === 0 ?
1810
1812
  [] :
1811
1813
  [{
1812
1814
  next: configuration,
1813
1815
  event: InitialEvent,
1814
- transitions: [
1815
- ...(rootResolution?.transitions ?? []),
1816
- ...(choiceResolution?.transitions ?? []),
1817
- ...initialHistoryChoiceTransitions
1818
- ],
1816
+ transitions: rootResolution.transitions,
1819
1817
  commands,
1820
1818
  raisedEvents: [
1821
1819
  ...raisedEvents
@@ -2374,4 +2372,4 @@ export const planInitial = (
2374
2372
 
2375
2373
  // Captured constructors are private planner inputs. Public callbacks receive root data
2376
2374
  // and the constructors bound to their declared branches during normalization.
2377
- type ConstructionContext<C> = Omit<C, "root"> & { readonly target: unknown }
2375
+ type ConstructionContext<C> = Omit<C, "root">
@@ -82,7 +82,7 @@ const getBoundaryResultDecoder = (schema: Schema.Top): BoundaryResultDecoder =>
82
82
 
83
83
  const MachineProtocolTypeId = Symbol.for("effect/Machine/protocol")
84
84
 
85
- const getProtocolSchemas = (machine: Machine.Any): MachineProtocolSchemas => {
85
+ const getProtocolSchemas = (machine: object): MachineProtocolSchemas => {
86
86
  const protocol = (machine as any)[MachineProtocolTypeId] as MachineProtocolSchemas | undefined
87
87
  if (protocol === undefined) {
88
88
  throw new Error("Machine protocol is unavailable")
@@ -90,7 +90,7 @@ const getProtocolSchemas = (machine: Machine.Any): MachineProtocolSchemas => {
90
90
  return protocol
91
91
  }
92
92
 
93
- const setProtocolSchemas = (machine: Machine.Any, protocol: MachineProtocolSchemas): void => {
93
+ const setProtocolSchemas = (machine: object, protocol: MachineProtocolSchemas): void => {
94
94
  Object.defineProperty(machine, MachineProtocolTypeId, {
95
95
  value: protocol,
96
96
  enumerable: false
@@ -173,7 +173,7 @@ export const setProtocol = (machine: Machine.Any): void => {
173
173
  })
174
174
  }
175
175
 
176
- export const copyProtocol = (source: Machine.Any, target: Machine.Any): void =>
176
+ export const copyProtocol = (source: object, target: object): void =>
177
177
  setProtocolSchemas(target, getProtocolSchemas(source))
178
178
 
179
179
  export const getEventName = (event: unknown): string | undefined =>
@@ -4,7 +4,7 @@ import * as SchemaAST from "effect/SchemaAST"
4
4
  export const StateNodePropertyPolicy = {
5
5
  atomic: ["schema", "type", "annotations"],
6
6
  final: ["schema", "type", "output", "annotations"],
7
- compound: ["schema", "type", "initial", "states", "annotations"],
7
+ compound: ["schema", "type", "states", "annotations"],
8
8
  parallel: ["schema", "type", "output", "states", "annotations"],
9
9
  history: ["type", "history", "annotations"],
10
10
  choice: ["type", "annotations"]
@@ -266,9 +266,6 @@ const validateStateTree = (
266
266
  fail(boundary, `${path}.output`, "state output must be an Effect Schema")
267
267
  }
268
268
  if (kind === "compound") {
269
- if (typeof node.initial !== "string") {
270
- fail(boundary, `${path}.initial`, "compound states must declare an initial child key")
271
- }
272
269
  validateTargetSelectorChildKey(
273
270
  boundary,
274
271
  path,
@@ -285,18 +282,7 @@ const validateStateTree = (
285
282
  "schema-backed compound child states cannot use the reserved local target selector key \"with\""
286
283
  )
287
284
  }
288
- const initialKey = node.initial as string
289
285
  validateStateTree(boundary, node.states, path, true)
290
- if (!hasOwn(node.states as object, initialKey)) {
291
- fail(boundary, `${path}.initial`, `compound initial child "${initialKey}" does not exist`)
292
- }
293
- const initial = (node.states as Readonly<Record<PropertyKey, unknown>>)[initialKey]
294
- if (
295
- !Schema.isSchema(initial) && typeof initial === "object" && initial !== null &&
296
- (initial as Readonly<Record<PropertyKey, unknown>>).type === "history"
297
- ) {
298
- fail(boundary, `${path}.initial`, "compound initial children cannot be history states")
299
- }
300
286
  continue
301
287
  }
302
288
  if (kind === "parallel") {
@@ -1,5 +1,5 @@
1
+ import * as Schema from "effect/Schema"
1
2
  import type { Machine, State } from "../../Machine.js"
2
- import * as Topology from "./topology.js"
3
3
 
4
4
  /** Reference metadata is separate from child names, including names such as `path`. */
5
5
  export const TypeId: unique symbol = Symbol.for("effect/Machine/TargetReference")
@@ -14,9 +14,24 @@ export interface Reference {
14
14
 
15
15
  /** Captures an immutable reference tree without allocating or executing a machine. */
16
16
  export const make = (root: State<Machine.StateNodeConfig>): { readonly root: Reference } => {
17
- const nodes = Topology.compileStateNodes({ "": root.node })
17
+ const nodes: Array<{ path: string; key: string; parent: string | undefined; type: string }> = []
18
+ const visit = (
19
+ node: Machine.StateNodeConfig | Machine.TaggedSchema,
20
+ path: string,
21
+ key: string,
22
+ parent: string | undefined
23
+ ): void => {
24
+ const type = !Schema.isSchema(node) && "type" in node ? node.type ?? "active" : "active"
25
+ nodes.push({ path, key, parent, type })
26
+ if (!Schema.isSchema(node) && "states" in node) {
27
+ for (const [childKey, child] of Object.entries(node.states)) {
28
+ visit(child, path === "" ? childKey : `${path}.${childKey}`, childKey, path)
29
+ }
30
+ }
31
+ }
32
+ visit(root.node, "", "", undefined)
18
33
  const references = new Map<string, Reference>()
19
- for (const node of nodes.byPath.values()) {
34
+ for (const node of nodes) {
20
35
  const reference = Object.create(null)
21
36
  Object.defineProperty(reference, TypeId, {
22
37
  value: Object.freeze({
@@ -27,7 +42,7 @@ export const make = (root: State<Machine.StateNodeConfig>): { readonly root: Ref
27
42
  })
28
43
  references.set(node.path, reference)
29
44
  }
30
- for (const node of nodes.byPath.values()) {
45
+ for (const node of nodes) {
31
46
  if (node.parent !== undefined) {
32
47
  Object.defineProperty(references.get(node.parent), node.key, {
33
48
  value: references.get(node.path),
@@ -55,6 +55,10 @@ export interface HistoryTarget {
55
55
  export interface InitialTarget {
56
56
  readonly [InitialTargetTypeId]: typeof InitialTargetTypeId
57
57
  readonly _tag: "InitialTarget"
58
+ /** Transient startup input, never retained in a configuration. */
59
+ readonly input?: { readonly value: unknown }
60
+ /** Explicitly selected descendants and their construction values. */
61
+ readonly children?: ReadonlyMap<string, unknown>
58
62
  readonly path: string
59
63
  readonly value: unknown
60
64
  readonly values?: Readonly<Record<string, unknown>>
@@ -330,7 +334,8 @@ export const getStateNodeDefinition = (
330
334
  states: definition.states
331
335
  }
332
336
  }
333
- if (typeof definition.initial !== "string") {
337
+ const initial = "initial" in definition ? definition.initial : undefined
338
+ if (typeof initial !== "string") {
334
339
  throw new Error(`Machine.make expected compound state "${path}" to declare an initial child`)
335
340
  }
336
341
  return {
@@ -339,7 +344,7 @@ export const getStateNodeDefinition = (
339
344
  annotations,
340
345
  type: "compound",
341
346
  history: undefined,
342
- initial: definition.initial,
347
+ initial,
343
348
  states: definition.states
344
349
  }
345
350
  }