@typeonce/effect-machine 0.16.0 → 0.18.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 (68) hide show
  1. package/README.md +157 -150
  2. package/dist/Machine.d.ts +416 -592
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +55 -136
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/atom.d.ts +7 -7
  7. package/dist/internal/machine/atom.d.ts.map +1 -1
  8. package/dist/internal/machine/atom.js.map +1 -1
  9. package/dist/internal/machine/cluster.d.ts +2 -2
  10. package/dist/internal/machine/cluster.d.ts.map +1 -1
  11. package/dist/internal/machine/cluster.js +6 -5
  12. package/dist/internal/machine/cluster.js.map +1 -1
  13. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  14. package/dist/internal/machine/executionPlan.js +10 -3
  15. package/dist/internal/machine/executionPlan.js.map +1 -1
  16. package/dist/internal/machine/invocation.d.ts.map +1 -1
  17. package/dist/internal/machine/invocation.js +1 -1
  18. package/dist/internal/machine/invocation.js.map +1 -1
  19. package/dist/internal/machine/machine.d.ts +8 -6
  20. package/dist/internal/machine/machine.d.ts.map +1 -1
  21. package/dist/internal/machine/machine.js +239 -35
  22. package/dist/internal/machine/machine.js.map +1 -1
  23. package/dist/internal/machine/planner.d.ts +19 -4
  24. package/dist/internal/machine/planner.d.ts.map +1 -1
  25. package/dist/internal/machine/planner.js +56 -20
  26. package/dist/internal/machine/planner.js.map +1 -1
  27. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  28. package/dist/internal/machine/stateDefinition.js +14 -0
  29. package/dist/internal/machine/stateDefinition.js.map +1 -1
  30. package/dist/internal/machine/topology.d.ts +9 -0
  31. package/dist/internal/machine/topology.d.ts.map +1 -1
  32. package/dist/internal/machine/topology.js +16 -0
  33. package/dist/internal/machine/topology.js.map +1 -1
  34. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  35. package/dist/internal/testing/machine/finiteModel.js +15 -20
  36. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  37. package/dist/internal/testing/machine/transitionCoverage.d.ts.map +1 -1
  38. package/dist/internal/testing/machine/transitionCoverage.js +2 -0
  39. package/dist/internal/testing/machine/transitionCoverage.js.map +1 -1
  40. package/dist/testing/MachineTest.d.ts +15 -15
  41. package/dist/testing/MachineTest.d.ts.map +1 -1
  42. package/dist/testing/MachineTest.js +5 -8
  43. package/dist/testing/MachineTest.js.map +1 -1
  44. package/dist/unstable/cluster/ClusterMachine.d.ts +2 -2
  45. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  46. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  47. package/dist/unstable/reactivity/AtomMachine.d.ts +13 -13
  48. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  49. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  50. package/docs/agent-guide.md +229 -193
  51. package/package.json +1 -1
  52. package/src/Machine.ts +1686 -2234
  53. package/src/internal/machine/atom.ts +20 -15
  54. package/src/internal/machine/cluster.ts +14 -9
  55. package/src/internal/machine/executionPlan.ts +8 -3
  56. package/src/internal/machine/invocation.ts +1 -1
  57. package/src/internal/machine/machine.ts +365 -48
  58. package/src/internal/machine/planner.ts +89 -27
  59. package/src/internal/machine/stateDefinition.ts +39 -0
  60. package/src/internal/machine/topology.ts +28 -0
  61. package/src/internal/testing/machine/exploration.ts +1 -1
  62. package/src/internal/testing/machine/finiteModel.ts +18 -21
  63. package/src/internal/testing/machine/trace.ts +1 -1
  64. package/src/internal/testing/machine/transitionCoverage.ts +2 -0
  65. package/src/internal/testing/machine/verification.ts +1 -1
  66. package/src/testing/MachineTest.ts +18 -15
  67. package/src/unstable/cluster/ClusterMachine.ts +8 -4
  68. package/src/unstable/reactivity/AtomMachine.ts +20 -13
package/src/Machine.ts CHANGED
@@ -53,6 +53,9 @@ declare const MachineTypeId: unique symbol
53
53
  declare const EventConstructionTypeId: unique symbol
54
54
  declare const EmittedEventConstructionTypeId: unique symbol
55
55
  declare const EventProtocolTypeId: unique symbol
56
+ declare const ParentModeTypeId: unique symbol
57
+
58
+ const ParentTypeId = "~effect/Machine/Parent"
56
59
 
57
60
  const ChildMachineLogicTypeId: typeof internal.ChildMachineLogicTypeId = internal.ChildMachineLogicTypeId
58
61
 
@@ -184,16 +187,16 @@ export interface Machine<
184
187
  readonly emittedEvents: Machine.EventProtocol<"emitted", Emits>
185
188
 
186
189
  /**
187
- * Public input events accepted by an owning machine when this machine is
188
- * running as a child.
190
+ * Declared owning-machine protocol, or `undefined` when this machine does
191
+ * not communicate with its owner.
189
192
  *
190
- * The protocol types the optional `parent` machine target exposed to
191
- * handlers and is checked against a concrete parent's public events at
192
- * composition boundaries.
193
+ * Required parents make the machine child-only and expose a non-optional
194
+ * `parent` target to handlers. Optional parents keep root execution valid
195
+ * and expose `parent` as possibly absent.
193
196
  *
194
- * @since 0.10.0
197
+ * @since 0.17.0
195
198
  */
196
- readonly parentEvents: Machine.EventProtocol<"public", ParentEvents>
199
+ readonly parent: Machine.ParentDeclarationOf<ParentEvents>
197
200
 
198
201
  /**
199
202
  * Optional schema used to decode the machine input before initialization.
@@ -220,15 +223,6 @@ export interface Machine<
220
223
  /** @internal */
221
224
  readonly handlers: Machine.StateConfigs<States, Events, Emits, UnhandledStates, Machine.TagOf<Events[number]>, E, R>
222
225
 
223
- /**
224
- * Machine-bound equivalent of {@link invoke}. Both forms preserve this
225
- * machine's public input and parent protocols in invocation callbacks;
226
- * `Machine.invoke(...)` inside `handle(...)` is the canonical inline form.
227
- *
228
- * @since 0.11.0
229
- */
230
- readonly invoke: Invoker<States, Events, Emits, InputEvents, ParentEvents>
231
-
232
226
  /** @internal */
233
227
  readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>
234
228
  /** @internal */
@@ -471,22 +465,61 @@ export interface MachineTarget<in Event> {
471
465
  readonly send: (event: Event) => Effect.Effect<void, StoppedError>
472
466
  }
473
467
 
468
+ /**
469
+ * Availability declared for an owning-machine target.
470
+ *
471
+ * @category models
472
+ * @since 0.17.0
473
+ */
474
+ export type ParentMode = "required" | "optional"
475
+
476
+ /**
477
+ * Owning-machine protocol declared by {@link parent} or
478
+ * {@link optionalParent}.
479
+ *
480
+ * @category models
481
+ * @since 0.17.0
482
+ */
483
+ export interface Parent<Mode extends ParentMode, Events extends ReadonlyArray<Machine.TaggedSchema>> {
484
+ readonly [ParentTypeId]: typeof ParentTypeId
485
+ readonly mode: Mode
486
+ readonly events: Machine.EventProtocol<"public", Events>
487
+ }
488
+
489
+ /**
490
+ * Namespace containing type-level members associated with {@link Parent}.
491
+ *
492
+ * @category models
493
+ * @since 0.17.0
494
+ */
495
+ export declare namespace Parent {
496
+ /** Any owning-machine protocol declaration. */
497
+ export type Any = Parent<ParentMode, ReadonlyArray<Machine.TaggedSchema>>
498
+ }
499
+
474
500
  /**
475
501
  * Machine targets available while evaluating machine behavior.
476
502
  *
477
503
  * @category models
478
504
  * @since 0.12.0
479
505
  */
480
- export interface MachineReferences<
506
+ export type MachineReferences<
481
507
  InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
482
508
  ParentEvents extends ReadonlyArray<Machine.TaggedSchema>
483
- > {
484
- /** Target for the current machine. Sending queues a later mailbox event. */
485
- readonly self: MachineTarget<Machine.EventInputOf<InputEvents>>
486
-
487
- /** Target for the owning machine, or `undefined` when running as a root. */
488
- readonly parent: MachineTarget<Machine.EventInputOf<ParentEvents>> | undefined
489
- }
509
+ > =
510
+ & {
511
+ /** Target for the current machine. Sending queues a later mailbox event. */
512
+ readonly self: MachineTarget<Machine.EventInputOf<InputEvents>>
513
+ }
514
+ & (Machine.ParentModeOf<ParentEvents> extends "required" ? {
515
+ /** Required target for the machine that owns this child. */
516
+ readonly parent: MachineTarget<Machine.EventInputOf<ParentEvents>>
517
+ } :
518
+ Machine.ParentModeOf<ParentEvents> extends "optional" ? {
519
+ /** Target for the owning machine, or `undefined` when running as a root. */
520
+ readonly parent: MachineTarget<Machine.EventInputOf<ParentEvents>> | undefined
521
+ } :
522
+ {})
490
523
 
491
524
  /**
492
525
  * Synchronous commands available while a machine transition is being
@@ -600,6 +633,9 @@ type IncompatibleRuntime<Requirements, Events, Emits> = Requirements extends Run
600
633
 
601
634
  const InvokeTypeId: typeof internal.InvokeTypeId = internal.InvokeTypeId
602
635
  const TransitionTypeId: typeof internal.TransitionTypeId = internal.TransitionTypeId
636
+ declare const TransitionBuilderTypeId: unique symbol
637
+ declare const InvokeBuilderTypeId: unique symbol
638
+ declare const InitialBuilderTypeId: unique symbol
603
639
 
604
640
  type StateDefinitionError<
605
641
  Message extends string,
@@ -725,6 +761,29 @@ type ValidateOutputSchema<Node> = "output" extends keyof Node ? Node extends { r
725
761
  : StateDefinitionError<"State output must be a schema">
726
762
  : unknown
727
763
 
764
+ type SelectorChildKey<Children extends Machine.StateSchemas> = ActiveStateKey<Children> | ChoiceStateKey<Children>
765
+
766
+ type ValidateInitialSelectorChild<Children extends Machine.StateSchemas, Path extends PropertyKey> = "initial" extends
767
+ SelectorChildKey<Children> ? StateDefinitionError<
768
+ "Active and choice child states cannot use the reserved target selector key \"initial\"",
769
+ StateDefinitionPath<Extract<Path, string>, "initial">,
770
+ "initial"
771
+ >
772
+ : unknown
773
+
774
+ type ValidateWithSelectorChild<
775
+ Node extends Machine.StateNodeConfig,
776
+ Children extends Machine.StateSchemas,
777
+ Path extends PropertyKey
778
+ > = Node extends { readonly schema: Machine.TaggedSchema } ?
779
+ "with" extends SelectorChildKey<Children> ? StateDefinitionError<
780
+ "Schema-backed compound child states cannot use the reserved local target selector key \"with\"",
781
+ StateDefinitionPath<Extract<Path, string>, "with">,
782
+ "with"
783
+ >
784
+ : unknown
785
+ : unknown
786
+
728
787
  type ValidateStateNodeWithChildren<
729
788
  Node extends Machine.StateNodeConfig,
730
789
  Children,
@@ -732,8 +791,9 @@ type ValidateStateNodeWithChildren<
732
791
  > = Children extends Machine.StateSchemas ?
733
792
  Node extends { readonly type: "final" } ? StateDefinitionError<"Final states cannot declare child states">
734
793
  : Node extends { readonly type: "parallel" } ?
735
- "initial" extends keyof Node ? StateDefinitionError<"Parallel states cannot declare an initial child">
736
- : { readonly states: ValidateStateTree<Children, true, Extract<Path, string>> } & ValidateOutputSchema<Node>
794
+ & ValidateInitialSelectorChild<Children, Path>
795
+ & ("initial" extends keyof Node ? StateDefinitionError<"Parallel states cannot declare an initial child">
796
+ : { readonly states: ValidateStateTree<Children, true, Extract<Path, string>> } & ValidateOutputSchema<Node>)
737
797
  : "output" extends keyof Node ? StateDefinitionError<"Only final and parallel states can declare output">
738
798
  : ValidateCompoundStateNode<Node, Children, Path>
739
799
  : StateDefinitionError<"Child states must be a state tree">
@@ -743,9 +803,12 @@ type ValidateCompoundStateNode<
743
803
  Children extends Machine.StateSchemas,
744
804
  Path extends PropertyKey
745
805
  > = Node extends { readonly initial: infer Initial } ?
746
- Initial extends ActiveStateKey<Children> | ChoiceStateKey<Children> ? {
747
- readonly states: ValidateStateTree<Children, true, Extract<Path, string>>
748
- }
806
+ Initial extends ActiveStateKey<Children> | ChoiceStateKey<Children> ?
807
+ & ValidateInitialSelectorChild<Children, Path>
808
+ & ValidateWithSelectorChild<Node, Children, Path>
809
+ & {
810
+ readonly states: ValidateStateTree<Children, true, Extract<Path, string>>
811
+ }
749
812
  : StateDefinitionError<
750
813
  "Compound initial must be one of its direct child keys",
751
814
  Path,
@@ -2130,7 +2193,7 @@ export interface MachineRef<out State, in Event, out Error = never, out Output =
2130
2193
  }
2131
2194
 
2132
2195
  /**
2133
- * Machine-specific process logic used by `spawn` and `invoke`.
2196
+ * Machine-specific process logic used by `spawn` and state-owned invocations.
2134
2197
  *
2135
2198
  * @category models
2136
2199
  * @since 0.4.0
@@ -2465,6 +2528,35 @@ export declare namespace Machine {
2465
2528
  readonly parentEvents: ParentEvents
2466
2529
  }
2467
2530
 
2531
+ /** @internal Carries parent availability without widening the event tuple. */
2532
+ export type ParentEventSchemas<
2533
+ Mode extends ParentMode,
2534
+ Events extends ReadonlyArray<TaggedSchema>
2535
+ > = Events & { readonly [ParentModeTypeId]: Mode }
2536
+
2537
+ /** Extracts the parent availability encoded in a machine event tuple. */
2538
+ export type ParentModeOf<Events extends ReadonlyArray<TaggedSchema>> = Events extends
2539
+ { readonly [ParentModeTypeId]: infer Mode extends ParentMode } ? Mode
2540
+ : Events extends readonly [] ? "none"
2541
+ : "optional"
2542
+
2543
+ /** Extracts the event schemas declared by an owning-machine protocol. */
2544
+ export type ParentEventsOf<Declaration extends Parent.Any | undefined> = Declaration extends Parent<
2545
+ infer Mode,
2546
+ infer Events
2547
+ > ? ParentEventSchemas<Mode, Events>
2548
+ : readonly []
2549
+
2550
+ /** Extracts the owning-machine declaration carried by a machine. */
2551
+ export type ParentDeclarationOf<Events extends ReadonlyArray<TaggedSchema>> = ParentModeOf<Events> extends
2552
+ infer Mode ? Mode extends ParentMode ? Parent<Mode, Events> : undefined
2553
+ : never
2554
+
2555
+ /** Constraint applied to APIs that create an independent root runtime. */
2556
+ export type RootCompatible<Events extends ReadonlyArray<TaggedSchema>> = ParentModeOf<Events> extends "required"
2557
+ ? never
2558
+ : unknown
2559
+
2468
2560
  /**
2469
2561
  * Any schema-first machine.
2470
2562
  *
@@ -2485,7 +2577,7 @@ export declare namespace Machine {
2485
2577
  readonly events: EventProtocol.Any<"public">
2486
2578
  readonly internalEvents: EventProtocol.Any<"internal">
2487
2579
  readonly emittedEvents: EventProtocol.Any<"emitted">
2488
- readonly parentEvents: EventProtocol.Any<"public">
2580
+ readonly parent: Parent.Any | undefined
2489
2581
  readonly input: Schema.Top | undefined
2490
2582
  readonly id: string | undefined
2491
2583
  /** @internal */
@@ -2495,8 +2587,6 @@ export declare namespace Machine {
2495
2587
  /** @internal */
2496
2588
  readonly handlers: any
2497
2589
  /** @internal */
2498
- readonly invoke: any
2499
- /** @internal */
2500
2590
  readonly initial: any
2501
2591
  /** @internal */
2502
2592
  readonly initialDefinition: InitialDefinition
@@ -2519,12 +2609,26 @@ export declare namespace Machine {
2519
2609
  export type Events<M extends Any> = M[typeof MachineTypeId]["events"]
2520
2610
 
2521
2611
  /**
2522
- * Extracts the input schema carried by a machine definition.
2612
+ * Extracts the startup input schema carried by a machine definition.
2523
2613
  *
2524
2614
  * @category utility types
2525
- * @since 0.4.0
2615
+ * @since 0.18.0
2616
+ */
2617
+ export type InputSchema<M extends Any> = M[typeof MachineTypeId]["input"]
2618
+
2619
+ /**
2620
+ * Extracts the decoded startup input accepted by a machine definition.
2621
+ *
2622
+ * Machines declared with `Schema.Void` do not accept a startup input, so
2623
+ * their extracted input type is `never`.
2624
+ *
2625
+ * @category utility types
2626
+ * @since 0.18.0
2526
2627
  */
2527
- export type Input<M extends Any> = M[typeof MachineTypeId]["input"]
2628
+ export type Input<M extends Any> = InputSchema<M> extends infer Input extends Schema.Top
2629
+ ? Input extends typeof Schema.Void ? never
2630
+ : Input["Type"]
2631
+ : never
2528
2632
 
2529
2633
  /**
2530
2634
  * Extracts state paths that do not yet have handlers.
@@ -2612,6 +2716,9 @@ export declare namespace Machine {
2612
2716
  /** Extracts the public input protocol required from an owning machine. */
2613
2717
  export type ParentEvents<M extends Any> = M[typeof MachineTypeId]["parentEvents"]
2614
2718
 
2719
+ /** Extracts whether a machine requires or optionally accepts an owner. */
2720
+ export type ParentAvailability<M extends Any> = ParentModeOf<M[typeof MachineTypeId]["parentEvents"]>
2721
+
2615
2722
  /** Extracts an internal event schema tuple from the complete and public protocols. */
2616
2723
  export type InternalEventSchemas<
2617
2724
  Events extends ReadonlyArray<TaggedSchema>,
@@ -3306,6 +3413,7 @@ export declare namespace Machine {
3306
3413
  readonly source: SourcePath
3307
3414
  readonly trigger: TransitionTrigger<EventTag>
3308
3415
  readonly reenter: boolean
3416
+ readonly acceptance: TransitionAcceptance
3309
3417
  readonly branches: ReadonlyArray<TransitionBranch<TargetPath>>
3310
3418
  }
3311
3419
 
@@ -4184,6 +4292,24 @@ export declare namespace Machine {
4184
4292
  readonly [Topology.NoTargetTypeId]: typeof Topology.NoTargetTypeId
4185
4293
  }
4186
4294
 
4295
+ /**
4296
+ * Opaque result returned when a declinable transition does not accept the
4297
+ * current event or lifecycle outcome.
4298
+ *
4299
+ * Declining selects no transition and discards operations enqueued by that
4300
+ * resolver. Hierarchical event and eventless dispatch continues with the
4301
+ * next eligible ancestor candidate.
4302
+ *
4303
+ * @category models
4304
+ * @since 0.17.0
4305
+ */
4306
+ export interface Declined {
4307
+ readonly [Topology.DeclinedTypeId]: typeof Topology.DeclinedTypeId
4308
+ }
4309
+
4310
+ /** Static acceptance contract of one transition definition. */
4311
+ export type TransitionAcceptance = "required" | "declinable"
4312
+
4187
4313
  /**
4188
4314
  * Transition instruction that restores a history pseudo-state's parent.
4189
4315
  *
@@ -4393,11 +4519,14 @@ export declare namespace Machine {
4393
4519
  readonly "~effect/Machine/TargetSelectionResult"?: Types.Covariant<Result>
4394
4520
  }
4395
4521
 
4396
- type SelectionMethod<Builder, Path extends string, Kind extends Topology.TargetSelectionKind = "state"> = () =>
4522
+ type SelectionValue<Builder, Path extends string, Kind extends Topology.TargetSelectionKind = "state"> =
4397
4523
  TargetSelection<Builder, Path, Kind>
4398
4524
 
4525
+ type SelectionMethod<Builder, Path extends string, Kind extends Topology.TargetSelectionKind = "state"> = () =>
4526
+ SelectionValue<Builder, Path, Kind>
4527
+
4399
4528
  type InitialSelectionMethod<Builder, Path extends string> = Builder extends { readonly initial: infer Initial } ? {
4400
- readonly initial: SelectionMethod<Initial, Path, "initial">
4529
+ readonly initial: SelectionValue<Initial, Path, "initial">
4401
4530
  }
4402
4531
  : {}
4403
4532
 
@@ -4477,7 +4606,7 @@ export declare namespace Machine {
4477
4606
  LocalTargetBuilder<States, Source> extends infer Builder ?
4478
4607
  & SelectionTreeWithPrefix<States, Children, Scope, "local", Builder>
4479
4608
  & ("with" extends keyof Builder ? {
4480
- readonly with: SelectionMethod<Builder["with"], Scope>
4609
+ readonly with: SelectionValue<Builder["with"], Scope>
4481
4610
  }
4482
4611
  : {})
4483
4612
  : {}
@@ -4491,7 +4620,7 @@ export declare namespace Machine {
4491
4620
  Builder
4492
4621
  > = {
4493
4622
  readonly [Key in Extract<HistoryContainingKey<States>, keyof Builder>]: States[Key] extends HistoryStateNodeConfig ?
4494
- SelectionMethod<
4623
+ SelectionValue<
4495
4624
  Builder[Key],
4496
4625
  JoinPath<Prefix, Key>,
4497
4626
  "history"
@@ -4505,12 +4634,17 @@ export declare namespace Machine {
4505
4634
  : never
4506
4635
  }
4507
4636
 
4508
- /** Definition-time topology selector available to an ordinary transition. */
4637
+ /**
4638
+ * Definition-time topology selector available to an ordinary transition.
4639
+ * Topology-only instructions (`none`, declared `initial` and history
4640
+ * selections, and `local.with`) are values. State and choice destinations
4641
+ * remain callable selection methods.
4642
+ */
4509
4643
  export interface TargetSelector<
4510
4644
  States extends StateSchemas,
4511
4645
  Source extends StateNodeIdentifier<States>
4512
4646
  > {
4513
- readonly none: SelectionMethod<TargetBuilder<States, Source>["none"], never, "none">
4647
+ readonly none: SelectionValue<TargetBuilder<States, Source>["none"], never, "none">
4514
4648
  readonly local: LocalTargetSelector<States, Source>
4515
4649
  readonly branch: BranchTargetSelector<States, Source>
4516
4650
  readonly full: FullTargetSelector<States>
@@ -4518,10 +4652,10 @@ export declare namespace Machine {
4518
4652
  }
4519
4653
 
4520
4654
  /** Definition-time selector that can choose only a valid top-level initial entry. */
4521
- export type InitialSelector<States extends StateSchemas> = {
4655
+ type InitialTargetSelector<States extends StateSchemas> = {
4522
4656
  readonly [Key in Extract<ActiveStateKey<States>, keyof InitialBuilder<States>>]: States[Key] extends
4523
4657
  { readonly states: StateSchemas } ? {
4524
- readonly initial: SelectionMethod<InitialBuilder<States>[Key], Key, "initial">
4658
+ readonly initial: SelectionValue<InitialBuilder<States>[Key], Key, "initial">
4525
4659
  }
4526
4660
  : SelectionMethod<InitialBuilder<States>[Key], Key>
4527
4661
  }
@@ -4532,7 +4666,7 @@ export declare namespace Machine {
4532
4666
  * @category models
4533
4667
  * @since 0.4.0
4534
4668
  */
4535
- export interface HandlerContext<
4669
+ export type HandlerContext<
4536
4670
  States extends StateSchemas,
4537
4671
  Events extends ReadonlyArray<TaggedSchema>,
4538
4672
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -4542,7 +4676,7 @@ export declare namespace Machine {
4542
4676
  R,
4543
4677
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4544
4678
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4545
- > extends MachineReferences<InputEvents, ParentEvents> {
4679
+ > = MachineReferences<InputEvents, ParentEvents> & {
4546
4680
  readonly state: StateByIdentifier<States, StateId>
4547
4681
  readonly containingState: ParentStateValue<States, StateId>
4548
4682
  readonly ancestors: ParentStateValues<States, StateId>
@@ -4565,14 +4699,14 @@ export declare namespace Machine {
4565
4699
  * @category models
4566
4700
  * @since 0.4.0
4567
4701
  */
4568
- export interface StateActionContext<
4702
+ export type StateActionContext<
4569
4703
  States extends StateSchemas,
4570
4704
  Events extends ReadonlyArray<TaggedSchema>,
4571
4705
  Emits extends ReadonlyArray<TaggedSchema>,
4572
4706
  StateId extends StateIdentifier<States>,
4573
4707
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4574
4708
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4575
- > extends MachineReferences<InputEvents, ParentEvents> {
4709
+ > = MachineReferences<InputEvents, ParentEvents> & {
4576
4710
  readonly state: StateByIdentifier<States, StateId>
4577
4711
  readonly containingState: ParentStateValue<States, StateId>
4578
4712
  readonly ancestors: ParentStateValues<States, StateId>
@@ -4585,14 +4719,14 @@ export declare namespace Machine {
4585
4719
  * @category models
4586
4720
  * @since 0.4.0
4587
4721
  */
4588
- export interface InvokeContext<
4722
+ export type InvokeContext<
4589
4723
  States extends StateSchemas,
4590
4724
  Events extends ReadonlyArray<TaggedSchema>,
4591
4725
  Emits extends ReadonlyArray<TaggedSchema>,
4592
4726
  StateId extends StateIdentifier<States>,
4593
4727
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4594
4728
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4595
- > extends MachineReferences<InputEvents, ParentEvents> {
4729
+ > = MachineReferences<InputEvents, ParentEvents> & {
4596
4730
  readonly state: StateByIdentifier<States, StateId>
4597
4731
  readonly containingState: ParentStateValue<States, StateId>
4598
4732
  readonly ancestors: ParentStateValues<States, StateId>
@@ -4605,7 +4739,7 @@ export declare namespace Machine {
4605
4739
  * @category models
4606
4740
  * @since 0.4.0
4607
4741
  */
4608
- export interface InvokeSnapshotContext<
4742
+ export type InvokeSnapshotContext<
4609
4743
  States extends StateSchemas,
4610
4744
  Events extends ReadonlyArray<TaggedSchema>,
4611
4745
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -4615,7 +4749,7 @@ export declare namespace Machine {
4615
4749
  Output,
4616
4750
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4617
4751
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4618
- > extends MachineReferences<InputEvents, ParentEvents> {
4752
+ > = MachineReferences<InputEvents, ParentEvents> & {
4619
4753
  readonly id: string
4620
4754
  readonly state: StateByIdentifier<States, StateId>
4621
4755
  readonly containingState: ParentStateValue<States, StateId>
@@ -4630,7 +4764,7 @@ export declare namespace Machine {
4630
4764
  * @category models
4631
4765
  * @since 0.4.0
4632
4766
  */
4633
- export interface InvokeDoneContext<
4767
+ export type InvokeDoneContext<
4634
4768
  States extends StateSchemas,
4635
4769
  Events extends ReadonlyArray<TaggedSchema>,
4636
4770
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -4638,7 +4772,7 @@ export declare namespace Machine {
4638
4772
  Output,
4639
4773
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4640
4774
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4641
- > extends MachineReferences<InputEvents, ParentEvents> {
4775
+ > = MachineReferences<InputEvents, ParentEvents> & {
4642
4776
  readonly id: string
4643
4777
  readonly state: StateByIdentifier<States, StateId>
4644
4778
  readonly containingState: ParentStateValue<States, StateId>
@@ -4649,7 +4783,7 @@ export declare namespace Machine {
4649
4783
  }
4650
4784
 
4651
4785
  /** Context passed to a Stream invocation's element transition. */
4652
- export interface InvokeElementContext<
4786
+ export type InvokeElementContext<
4653
4787
  States extends StateSchemas,
4654
4788
  Events extends ReadonlyArray<TaggedSchema>,
4655
4789
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -4657,7 +4791,7 @@ export declare namespace Machine {
4657
4791
  Element,
4658
4792
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4659
4793
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4660
- > extends MachineReferences<InputEvents, ParentEvents> {
4794
+ > = MachineReferences<InputEvents, ParentEvents> & {
4661
4795
  readonly id: string
4662
4796
  readonly state: StateByIdentifier<States, StateId>
4663
4797
  readonly containingState: ParentStateValue<States, StateId>
@@ -4668,7 +4802,7 @@ export declare namespace Machine {
4668
4802
  }
4669
4803
 
4670
4804
  /** Context passed to an invocation typed-failure transition. */
4671
- export interface InvokeFailureContext<
4805
+ export type InvokeFailureContext<
4672
4806
  States extends StateSchemas,
4673
4807
  Events extends ReadonlyArray<TaggedSchema>,
4674
4808
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -4676,7 +4810,7 @@ export declare namespace Machine {
4676
4810
  Error,
4677
4811
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4678
4812
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4679
- > extends MachineReferences<InputEvents, ParentEvents> {
4813
+ > = MachineReferences<InputEvents, ParentEvents> & {
4680
4814
  readonly id: string
4681
4815
  readonly state: StateByIdentifier<States, StateId>
4682
4816
  readonly containingState: ParentStateValue<States, StateId>
@@ -4692,14 +4826,14 @@ export declare namespace Machine {
4692
4826
  * @category models
4693
4827
  * @since 0.4.0
4694
4828
  */
4695
- export interface AlwaysContext<
4829
+ export type AlwaysContext<
4696
4830
  States extends StateSchemas,
4697
4831
  Events extends ReadonlyArray<TaggedSchema>,
4698
4832
  Emits extends ReadonlyArray<TaggedSchema>,
4699
4833
  StateId extends StateIdentifier<States>,
4700
4834
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4701
4835
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4702
- > extends MachineReferences<InputEvents, ParentEvents> {
4836
+ > = MachineReferences<InputEvents, ParentEvents> & {
4703
4837
  readonly state: StateByIdentifier<States, StateId>
4704
4838
  readonly containingState: ParentStateValue<States, StateId>
4705
4839
  readonly ancestors: ParentStateValues<States, StateId>
@@ -4723,14 +4857,14 @@ export declare namespace Machine {
4723
4857
  * @category models
4724
4858
  * @since 0.4.0
4725
4859
  */
4726
- export interface DoneContext<
4860
+ export type DoneContext<
4727
4861
  States extends StateSchemas,
4728
4862
  Events extends ReadonlyArray<TaggedSchema>,
4729
4863
  Emits extends ReadonlyArray<TaggedSchema>,
4730
4864
  StateId extends StateIdentifier<States>,
4731
4865
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4732
4866
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4733
- > extends MachineReferences<InputEvents, ParentEvents> {
4867
+ > = MachineReferences<InputEvents, ParentEvents> & {
4734
4868
  readonly state: StateByIdentifier<States, StateId>
4735
4869
  readonly containingState: ParentStateValue<States, StateId>
4736
4870
  readonly ancestors: ParentStateValues<States, StateId>
@@ -4750,14 +4884,14 @@ export declare namespace Machine {
4750
4884
  }
4751
4885
 
4752
4886
  /** Context passed to a transient choice resolver. There is no `state` value. */
4753
- export interface ChoiceContext<
4887
+ export type ChoiceContext<
4754
4888
  States extends StateSchemas,
4755
4889
  Events extends ReadonlyArray<TaggedSchema>,
4756
4890
  Emits extends ReadonlyArray<TaggedSchema>,
4757
4891
  ChoiceId extends ChoiceIdentifier<States>,
4758
4892
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4759
4893
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4760
- > extends MachineReferences<InputEvents, ParentEvents> {
4894
+ > = MachineReferences<InputEvents, ParentEvents> & {
4761
4895
  readonly containingState: StateByIdentifier<
4762
4896
  States,
4763
4897
  Extract<ImmediateParentStateIdentifier<ChoiceId>, StateIdentifier<States>>
@@ -4947,8 +5081,10 @@ export declare namespace Machine {
4947
5081
  * @category utility types
4948
5082
  * @since 0.4.0
4949
5083
  */
4950
- export type EventTransitionReturn<Transition> = Transition extends { readonly resolve?: infer Resolve } ?
4951
- NonNullable<Resolve> extends (...args: any) => infer Ret ? Ret : never
5084
+ export type EventTransitionReturn<Transition> = Transition extends (...args: any) => infer Authored ?
5085
+ Authored extends TransitionBuilderEvidence<infer Result, any> ? Result : never
5086
+ : Transition extends { readonly resolve?: infer Resolve } ?
5087
+ NonNullable<Resolve> extends (...args: any) => infer Ret ? Ret : never
4952
5088
  : never
4953
5089
  /**
4954
5090
  * Extracts the return value from a state's event handlers.
@@ -5092,11 +5228,19 @@ export declare namespace Machine {
5092
5228
  : never
5093
5229
  /** Extracts transition results returned by invocation lifecycle handlers. */
5094
5230
  export type InvokeOutcomeReturn<Invoke> = Invoke extends unknown ?
5231
+ | (Invoke extends {
5232
+ readonly [InvokeTypeId]: { readonly outcomes: Types.Covariant<infer Outcomes> }
5233
+ } ? EventTransitionReturn<Outcomes> :
5234
+ never)
5095
5235
  | (Invoke extends { readonly onDone?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
5096
5236
  | (Invoke extends { readonly onFailure?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
5097
5237
  | (Invoke extends { readonly onElement?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
5098
5238
  | (Invoke extends { readonly onSnapshot?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
5099
5239
  : never
5240
+ type InvokeOutcomeError<Invoke> = IsAny<InvokeOutcomeReturn<Invoke>> extends true ? never
5241
+ : Effect.Error<InvokeOutcomeReturn<Invoke>>
5242
+ type InvokeOutcomeServices<Invoke> = IsAny<InvokeOutcomeReturn<Invoke>> extends true ? never
5243
+ : Effect.Services<InvokeOutcomeReturn<Invoke>>
5100
5244
  /**
5101
5245
  * Extracts the parent transition error contribution from invoked children.
5102
5246
  *
@@ -5107,7 +5251,7 @@ export declare namespace Machine {
5107
5251
  :
5108
5252
  | ChildAlreadyExistsError
5109
5253
  | InvokeInitialError<InvokeReturn<Config>>
5110
- | Effect.Error<InvokeOutcomeReturn<InvokeReturn<Config>>>
5254
+ | InvokeOutcomeError<InvokeReturn<Config>>
5111
5255
  /**
5112
5256
  * Extracts the parent service requirement contribution from invoked children.
5113
5257
  *
@@ -5118,9 +5262,7 @@ export declare namespace Machine {
5118
5262
  :
5119
5263
  | MachineRuntimeRequirement
5120
5264
  | InvokeServices<InvokeReturn<Config>>
5121
- | Effect.Services<
5122
- InvokeOutcomeReturn<InvokeReturn<Config>>
5123
- >
5265
+ | InvokeOutcomeServices<InvokeReturn<Config>>
5124
5266
  /**
5125
5267
  * Extracts the return value from an eventless transition.
5126
5268
  *
@@ -5165,24 +5307,19 @@ export declare namespace Machine {
5165
5307
  | Effect.Services<ChoiceReturn<Config>>
5166
5308
  | InvokeRequirements<Config>
5167
5309
 
5168
- /** Type evidence retained by {@link transition} without affecting runtime data. */
5310
+ /** Type evidence retained by an authored transition without affecting runtime data. */
5169
5311
  export interface TransitionTyped<
5170
5312
  States extends StateSchemas,
5171
5313
  Events extends ReadonlyArray<TaggedSchema>,
5172
5314
  Emits extends ReadonlyArray<TaggedSchema>,
5173
5315
  StateId extends StateNodeIdentifier<States>,
5174
5316
  Context,
5175
- Reenter extends boolean
5317
+ Reenter extends boolean,
5318
+ Acceptance extends TransitionAcceptance = "required"
5176
5319
  > {
5177
5320
  readonly [TransitionTypeId]: {
5178
5321
  readonly owner: Types.Covariant<readonly [States, Events, Emits, StateId, Context]>
5179
- }
5180
- }
5181
-
5182
- /** Type evidence for a targetless transition reusable in every owning context. */
5183
- export interface TargetlessTransitionTyped {
5184
- readonly [TransitionTypeId]: {
5185
- readonly targetless: true
5322
+ readonly acceptance: Types.Covariant<Acceptance>
5186
5323
  }
5187
5324
  }
5188
5325
 
@@ -5193,41 +5330,9 @@ export declare namespace Machine {
5193
5330
  Emits extends ReadonlyArray<TaggedSchema>,
5194
5331
  StateId extends StateNodeIdentifier<States>,
5195
5332
  Context,
5196
- Reenter extends boolean = false
5197
- > =
5198
- & (
5199
- | TransitionTyped<States, Events, Emits, StateId, Context, Reenter>
5200
- | TargetlessTransitionTyped
5201
- | TargetlessTransitionInput<Events, Emits, Context>
5202
- )
5203
- & {
5204
- readonly reenter?: [Reenter] extends [true] ? boolean : never
5205
- }
5206
-
5207
- /** Direct shorthand for a non-reentering targetless transition. */
5208
- // Keep the context generic so omitting `target` is deferred until a resolver
5209
- // is actually authored instead of expanding every TransitionConfig eagerly.
5210
- interface TargetlessTransitionResolver<
5211
- Events extends ReadonlyArray<TaggedSchema>,
5212
- Emits extends ReadonlyArray<TaggedSchema>,
5213
- Context
5214
- > {
5215
- <ResolvedContext extends Omit<Context, "target">>(
5216
- context: ResolvedContext,
5217
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5218
- ): undefined
5219
- }
5220
-
5221
- export type TargetlessTransitionInput<
5222
- Events extends ReadonlyArray<TaggedSchema>,
5223
- Emits extends ReadonlyArray<TaggedSchema>,
5224
- Context
5225
- > = {
5226
- readonly target: TargetlessSelector
5227
- readonly resolve?: TargetlessTransitionResolver<Events, Emits, Context>
5228
- readonly branches?: never
5229
- readonly reenter?: never
5230
- }
5333
+ Reenter extends boolean = false,
5334
+ Acceptance extends TransitionAcceptance = "required"
5335
+ > = TransitionBuilderInput<States, Events, Emits, StateId, Context, Reenter, Acceptance>
5231
5336
 
5232
5337
  export type SelectionBuilder<Selection> = Selection extends TargetSelection<infer Builder, any, any> ? Builder : never
5233
5338
  export type SelectionKind<Selection> = Selection extends TargetSelection<any, any, infer Kind> ? Kind : never
@@ -5239,6 +5344,13 @@ export declare namespace Machine {
5239
5344
  TargetBuilderResult<Builder>
5240
5345
  : never
5241
5346
 
5347
+ type SelectionSupportsDefaultConstruction<Selection> = SelectionKind<Selection> extends "none" ? true
5348
+ : SelectionBuilder<Selection> extends { readonly from: (...args: infer Args) => any } ? [] extends Args ? true
5349
+ : false
5350
+ : SelectionBuilder<Selection> extends (...args: infer Args) => any ? [] extends Args ? true
5351
+ : false
5352
+ : false
5353
+
5242
5354
  export type TransitionResolveContext<
5243
5355
  Context,
5244
5356
  Selection
@@ -5246,6 +5358,11 @@ export declare namespace Machine {
5246
5358
  & Omit<Context, "target">
5247
5359
  & (SelectionKind<Selection> extends "none" ? {} : { readonly target: SelectionBuilder<Selection> })
5248
5360
 
5361
+ /** Context capability available only to explicitly declinable resolvers. */
5362
+ export interface DeclineCapability {
5363
+ readonly decline: () => Declined
5364
+ }
5365
+
5249
5366
  export type TransitionResolver<
5250
5367
  Events extends ReadonlyArray<TaggedSchema>,
5251
5368
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -5256,20 +5373,17 @@ export declare namespace Machine {
5256
5373
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5257
5374
  ) => SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined
5258
5375
 
5259
- export type TransitionDirectInput<
5260
- States extends StateSchemas,
5376
+ export type DeclinableTransitionResolver<
5261
5377
  Events extends ReadonlyArray<TaggedSchema>,
5262
5378
  Emits extends ReadonlyArray<TaggedSchema>,
5263
- StateId extends StateNodeIdentifier<States>,
5264
5379
  Context,
5265
- Reenter extends boolean,
5266
- Selection extends TargetSelection<any, any, any>
5267
- > = {
5268
- readonly target: (to: TargetSelector<States, StateId>) => Selection
5269
- readonly resolve?: TransitionResolver<Events, Emits, Context, Selection>
5270
- readonly branches?: never
5271
- readonly reenter?: Reenter
5272
- }
5380
+ Selection
5381
+ > = (
5382
+ context: TransitionResolveContext<Context, Selection> & DeclineCapability,
5383
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5384
+ ) =>
5385
+ | (SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined)
5386
+ | Declined
5273
5387
 
5274
5388
  /** One named destination declared by a branching transition. */
5275
5389
  export interface TransitionBranchInput<
@@ -5337,805 +5451,992 @@ export declare namespace Machine {
5337
5451
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5338
5452
  ) => BranchSelectionResult<Branches>
5339
5453
 
5340
- export type TransitionBranchesInput<
5341
- States extends StateSchemas,
5454
+ export type DeclinableTransitionBranchesResolver<
5342
5455
  Events extends ReadonlyArray<TaggedSchema>,
5343
5456
  Emits extends ReadonlyArray<TaggedSchema>,
5344
- StateId extends StateNodeIdentifier<States>,
5345
5457
  Context,
5346
- Reenter extends boolean,
5347
5458
  Branches extends Readonly<Record<string, TransitionBranchInput>>
5348
- > = {
5349
- readonly target?: never
5350
- /**
5351
- * Declares every possible destination under a stable semantic key.
5352
- *
5353
- * Branches are captured once in ECMAScript property order. Array-index
5354
- * keys are rejected so reordering ordinary named properties affects only
5355
- * presentation order; the key remains the testing and inspection identity.
5356
- */
5357
- readonly branches: (to: TargetSelector<States, StateId>) => Branches
5358
- readonly resolve: TransitionBranchesResolver<Events, Emits, Context, Branches>
5359
- readonly reenter?: Reenter
5459
+ > = (
5460
+ context: TransitionBranchesResolveContext<Context, Branches> & DeclineCapability,
5461
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5462
+ ) => BranchSelectionResult<Branches> | Declined
5463
+
5464
+ /** @internal Type evidence retained by a transition authored through its bound selector. */
5465
+ export interface TransitionBuilderEvidence<out Result, out Acceptance extends TransitionAcceptance> {
5466
+ readonly [TransitionBuilderTypeId]: {
5467
+ readonly result: Types.Covariant<Result>
5468
+ readonly acceptance: Types.Covariant<Acceptance>
5469
+ }
5360
5470
  }
5361
5471
 
5362
- export type InvokeTransition<
5472
+ type TransitionReenterOption<Reenter extends boolean> = [Reenter] extends [true] ? { readonly reenter?: boolean }
5473
+ : { readonly reenter?: never }
5474
+
5475
+ type TransitionRequiredOptions<Reenter extends boolean> =
5476
+ & TransitionReenterOption<Reenter>
5477
+ & { readonly declinable?: false }
5478
+
5479
+ type TransitionDeclinableOptions<Reenter extends boolean> =
5480
+ & TransitionReenterOption<Reenter>
5481
+ & { readonly declinable: true }
5482
+
5483
+ type BuiltTransition<
5363
5484
  States extends StateSchemas,
5364
5485
  Events extends ReadonlyArray<TaggedSchema>,
5365
5486
  Emits extends ReadonlyArray<TaggedSchema>,
5366
5487
  StateId extends StateNodeIdentifier<States>,
5367
- Context
5368
- > = TransitionConfig<States, Events, Emits, StateId, Context, true>
5369
-
5370
- export type InvokeSource<Value, Context> = Value | ((context: Context) => Value)
5488
+ Context,
5489
+ Reenter extends boolean,
5490
+ Result,
5491
+ Acceptance extends TransitionAcceptance
5492
+ > =
5493
+ & TransitionTyped<States, Events, Emits, StateId, Context, Reenter, Acceptance>
5494
+ & TransitionBuilderEvidence<Result, Acceptance>
5371
5495
 
5372
- /** Inline state-owned work that runs for the lifetime of its active state. */
5373
- export interface InvokeOwned<
5496
+ interface TransitionResolveRequired<
5374
5497
  States extends StateSchemas,
5375
5498
  Events extends ReadonlyArray<TaggedSchema>,
5376
5499
  Emits extends ReadonlyArray<TaggedSchema>,
5377
- StateId extends StateIdentifier<States>
5500
+ StateId extends StateNodeIdentifier<States>,
5501
+ Context,
5502
+ Reenter extends boolean,
5503
+ Selection extends TargetSelection<any, any, any>
5378
5504
  > {
5379
- readonly "~effect/Machine/InvokeOwner"?: Types.Covariant<readonly [States, Events, Emits, StateId]>
5380
- }
5381
-
5382
- /** Type evidence retained by {@link invoke} without affecting runtime data. */
5383
- export interface InvokeTyped<Output, Error, Requirements, InitialError, Emits = never, ParentEvent = never> {
5384
- readonly [InvokeTypeId]: {
5385
- readonly output: Types.Covariant<Output>
5386
- readonly error: Types.Covariant<Error>
5387
- readonly requirements: Types.Covariant<Requirements>
5388
- readonly initialError: Types.Covariant<InitialError>
5389
- readonly emits: Types.Covariant<Emits>
5390
- readonly parentEvents: Types.Covariant<ParentEvent>
5391
- }
5505
+ (
5506
+ resolve: TransitionResolver<Events, Emits, Context, Selection>,
5507
+ options?: TransitionRequiredOptions<Reenter>
5508
+ ): BuiltTransition<
5509
+ States,
5510
+ Events,
5511
+ Emits,
5512
+ StateId,
5513
+ Context,
5514
+ Reenter,
5515
+ SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined,
5516
+ "required"
5517
+ >
5392
5518
  }
5393
5519
 
5394
- export type InvokeConfig<
5520
+ interface TransitionResolveDeclinable<
5395
5521
  States extends StateSchemas,
5396
5522
  Events extends ReadonlyArray<TaggedSchema>,
5397
5523
  Emits extends ReadonlyArray<TaggedSchema>,
5398
- StateId extends StateIdentifier<States>,
5399
- InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5400
- ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5401
- > =
5402
- & InvokeOwned<States, Events, Emits, StateId>
5403
- & (
5404
- | {
5405
- readonly id: string
5406
- readonly effect: (
5407
- context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5408
- ) => Effect.Effect<any, any, any>
5409
- readonly stream?: never
5410
- readonly after?: never
5411
- readonly logic?: never
5412
- readonly child?: never
5413
- readonly address?: never
5414
- readonly onDone?: unknown
5415
- readonly onFailure?: unknown
5416
- readonly onElement?: never
5417
- readonly onSnapshot?: never
5418
- }
5419
- | {
5420
- readonly id: string
5421
- readonly stream: (
5422
- context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5423
- ) => Stream.Stream<any, any, any>
5424
- readonly effect?: never
5425
- readonly after?: never
5426
- readonly logic?: never
5427
- readonly child?: never
5428
- readonly address?: never
5429
- readonly onElement?: unknown
5430
- readonly onDone: unknown
5431
- readonly onFailure?: unknown
5432
- readonly onSnapshot?: never
5433
- }
5434
- | {
5435
- readonly id: string
5436
- readonly after: InvokeSource<
5437
- Duration.Input,
5438
- InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5439
- >
5440
- readonly effect?: never
5441
- readonly stream?: never
5442
- readonly logic?: never
5443
- readonly child?: never
5444
- readonly address?: never
5445
- readonly onDone: unknown
5446
- readonly onFailure?: never
5447
- readonly onElement?: never
5448
- readonly onSnapshot?: never
5449
- }
5450
- | {
5451
- readonly id: string
5452
- readonly address: string
5453
- readonly logic: InvokeSource<
5454
- Logic<any, any, any, any, any, any>,
5455
- InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5456
- >
5457
- readonly effect?: never
5458
- readonly stream?: never
5459
- readonly after?: never
5460
- readonly child?: never
5461
- readonly onDone?: unknown
5462
- readonly onFailure?: unknown
5463
- readonly onElement?: never
5464
- readonly onSnapshot?: unknown
5465
- }
5466
- | {
5467
- readonly child: ChildMachine.Any
5468
- readonly input?:
5469
- | {}
5470
- | null
5471
- | ((context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>) => unknown)
5472
- readonly id?: never
5473
- readonly address?: never
5474
- readonly effect?: never
5475
- readonly stream?: never
5476
- readonly after?: never
5477
- readonly logic?: never
5478
- readonly onDone?: unknown
5479
- readonly onFailure?: unknown
5480
- readonly onElement?: never
5481
- readonly onSnapshot?: unknown
5482
- }
5483
- )
5524
+ StateId extends StateNodeIdentifier<States>,
5525
+ Context,
5526
+ Reenter extends boolean,
5527
+ Selection extends TargetSelection<any, any, any>
5528
+ > {
5529
+ (
5530
+ resolve: DeclinableTransitionResolver<Events, Emits, Context, Selection>,
5531
+ options: TransitionDeclinableOptions<Reenter>
5532
+ ): BuiltTransition<
5533
+ States,
5534
+ Events,
5535
+ Emits,
5536
+ StateId,
5537
+ Context,
5538
+ Reenter,
5539
+ | (SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined)
5540
+ | Declined,
5541
+ "declinable"
5542
+ >
5543
+ }
5484
5544
 
5485
- /** State-bound inline invocation configuration. */
5486
- export type InvokeDefinition<
5545
+ /** A selected transition target with target-specific resolver operations. */
5546
+ export type TransitionTarget<
5487
5547
  States extends StateSchemas,
5488
5548
  Events extends ReadonlyArray<TaggedSchema>,
5489
5549
  Emits extends ReadonlyArray<TaggedSchema>,
5490
- StateId extends StateIdentifier<States>,
5491
- InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5492
- ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5550
+ StateId extends StateNodeIdentifier<States>,
5551
+ Context,
5552
+ Reenter extends boolean,
5553
+ Acceptance extends TransitionAcceptance,
5554
+ Selection extends TargetSelection<any, any, any>
5493
5555
  > =
5494
- | InvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
5495
- | ReadonlyArray<InvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>>
5496
-
5497
- type TypedInvokeDefinition =
5498
- | InvokeTyped<any, any, any, any, any, any>
5499
- | ReadonlyArray<InvokeTyped<any, any, any, any, any, any>>
5556
+ & Selection
5557
+ & (SelectionSupportsDefaultConstruction<Selection> extends true ? BuiltTransition<
5558
+ States,
5559
+ Events,
5560
+ Emits,
5561
+ StateId,
5562
+ Context,
5563
+ Reenter,
5564
+ SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined,
5565
+ "required"
5566
+ >
5567
+ : {})
5568
+ & {
5569
+ readonly resolve:
5570
+ & TransitionResolveRequired<States, Events, Emits, StateId, Context, Reenter, Selection>
5571
+ & ("declinable" extends Acceptance ? TransitionResolveDeclinable<
5572
+ States,
5573
+ Events,
5574
+ Emits,
5575
+ StateId,
5576
+ Context,
5577
+ Reenter,
5578
+ Selection
5579
+ >
5580
+ : {})
5581
+ }
5582
+ & ([Reenter] extends [true] ? SelectionSupportsDefaultConstruction<Selection> extends true ? {
5583
+ readonly reenter: () => BuiltTransition<
5584
+ States,
5585
+ Events,
5586
+ Emits,
5587
+ StateId,
5588
+ Context,
5589
+ Reenter,
5590
+ SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined,
5591
+ "required"
5592
+ >
5593
+ }
5594
+ : {}
5595
+ : {})
5500
5596
 
5501
- type InvokeHandlerRequirement<Value, Handler> = IsAny<Value> extends true ? { readonly handler: Handler }
5502
- : [Value] extends [never] ? { readonly handler?: never }
5503
- : { readonly handler: Handler }
5597
+ /** @internal Type evidence retained by a machine initial-entry declaration. */
5598
+ export interface InitialBuilderEvidence<out Selection> {
5599
+ readonly [InitialBuilderTypeId]: Types.Covariant<Selection>
5600
+ }
5504
5601
 
5505
- export type InvokeDoneRequirement<Value, Handler> = InvokeHandlerRequirement<Value, Handler> extends
5506
- infer Requirement ? Requirement extends { readonly handler: infer Required } ? { readonly onDone: Required }
5507
- : { readonly onDone?: never }
5508
- : never
5602
+ /** A selected machine initial entry with its exact resolver target. */
5603
+ export type InitialTransitionTarget<Input, Selection extends TargetSelection<any, any, any>> =
5604
+ & Selection
5605
+ & (SelectionSupportsDefaultConstruction<Selection> extends true ? InitialBuilderEvidence<Selection> : {})
5606
+ & {
5607
+ readonly resolve: (
5608
+ resolve: (context: {
5609
+ readonly input: Input
5610
+ readonly target: SelectionBuilder<Selection>
5611
+ }) => SelectedTargetResult<Selection>
5612
+ ) => InitialBuilderEvidence<Selection>
5613
+ }
5509
5614
 
5510
- export type InvokeFailureRequirement<Value, Handler> = InvokeHandlerRequirement<Value, Handler> extends
5511
- infer Requirement ? Requirement extends { readonly handler: infer Required } ? { readonly onFailure: Required }
5512
- : { readonly onFailure?: never }
5615
+ type InitialSelectorNode<Input, Node> = Node extends (...args: infer Args) => infer Selection ?
5616
+ Selection extends TargetSelection<any, any, any> ?
5617
+ & ((...args: Args) => InitialTransitionTarget<Input, Selection>)
5618
+ & {
5619
+ readonly [Key in keyof Node]: InitialSelectorNode<Input, Node[Key]>
5620
+ }
5513
5621
  : never
5622
+ : Node extends TargetSelection<any, any, any> ? InitialTransitionTarget<Input, Node>
5623
+ : {
5624
+ readonly [Key in keyof Node]: InitialSelectorNode<Input, Node[Key]>
5625
+ }
5514
5626
 
5515
- export type InvokeElementRequirement<Value, Handler> = InvokeHandlerRequirement<Value, Handler> extends
5516
- infer Requirement ? Requirement extends { readonly handler: infer Required } ? { readonly onElement: Required }
5517
- : { readonly onElement?: never }
5518
- : never
5627
+ /** Definition-time selector for a machine's top-level initial entry. */
5628
+ export type InitialSelector<States extends StateSchemas, Input = void> = InitialSelectorNode<
5629
+ Input,
5630
+ InitialTargetSelector<States>
5631
+ >
5519
5632
 
5520
- export type TimerInvokeArgs<
5521
- States extends StateSchemas,
5522
- Events extends ReadonlyArray<TaggedSchema>,
5523
- Emits extends ReadonlyArray<TaggedSchema>,
5524
- StateId extends StateIdentifier<States>,
5525
- InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5526
- ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5527
- > = {
5528
- readonly id: InvokeLifecycleId
5529
- readonly after: InvokeSource<
5530
- Duration.Input,
5531
- InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5532
- >
5533
- readonly effect?: never
5534
- readonly stream?: never
5535
- readonly logic?: never
5536
- readonly child?: never
5537
- readonly address?: never
5538
- readonly onFailure?: never
5539
- readonly onElement?: never
5540
- readonly onSnapshot?: never
5541
- readonly onDone: InvokeTransition<
5542
- States,
5543
- Events,
5544
- Emits,
5545
- StateId,
5546
- InvokeDoneContext<States, Events, Emits, StateId, void, InputEvents, ParentEvents>
5547
- >
5548
- }
5633
+ /** Target-first initial-entry declaration accepted by {@link make}. */
5634
+ export type InitialBuilderInput<States extends StateSchemas, Input> = (
5635
+ to: InitialSelector<States, Input>
5636
+ ) => InitialBuilderEvidence<TargetSelection<any, any, any>>
5549
5637
 
5550
- export type LogicInvokeArgs<
5638
+ type TransitionSelectorNode<
5551
5639
  States extends StateSchemas,
5552
5640
  Events extends ReadonlyArray<TaggedSchema>,
5553
5641
  Emits extends ReadonlyArray<TaggedSchema>,
5554
- StateId extends StateIdentifier<States>,
5555
- ChildState,
5556
- ChildEvent,
5557
- ChildError,
5558
- ChildRequirements,
5559
- ChildOutput,
5560
- ChildInitialError,
5561
- Address extends ChildAddress<never>,
5562
- Source = Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>,
5563
- InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5564
- ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5565
- > =
5566
- & {
5567
- readonly id: InvokeLifecycleId
5568
- readonly address: Address & ChildAddress.Compatibility<Address, ChildEvent>
5569
- readonly logic: Source
5570
- readonly effect?: never
5571
- readonly stream?: never
5572
- readonly after?: never
5573
- readonly child?: never
5574
- readonly onElement?: never
5575
- readonly onSnapshot?: InvokeTransition<
5576
- States,
5577
- Events,
5578
- Emits,
5579
- StateId,
5580
- InvokeSnapshotContext<
5642
+ StateId extends StateNodeIdentifier<States>,
5643
+ Context,
5644
+ Reenter extends boolean,
5645
+ Acceptance extends TransitionAcceptance,
5646
+ Node
5647
+ > = Node extends (...args: infer Args) => infer Selection ? Selection extends TargetSelection<any, any, any> ?
5648
+ & ((...args: Args) => TransitionTarget<
5581
5649
  States,
5582
5650
  Events,
5583
5651
  Emits,
5584
5652
  StateId,
5585
- NoInfer<ChildState>,
5586
- NoInfer<ChildError>,
5587
- NoInfer<ChildOutput>,
5588
- InputEvents,
5589
- ParentEvents
5590
- >
5591
- >
5592
- }
5593
- & InvokeDoneRequirement<
5594
- NoInfer<ChildOutput>,
5595
- InvokeTransition<
5653
+ Context,
5654
+ Reenter,
5655
+ Acceptance,
5656
+ Selection
5657
+ >)
5658
+ & {
5659
+ readonly [Key in keyof Node]: TransitionSelectorNode<
5660
+ States,
5661
+ Events,
5662
+ Emits,
5663
+ StateId,
5664
+ Context,
5665
+ Reenter,
5666
+ Acceptance,
5667
+ Node[Key]
5668
+ >
5669
+ }
5670
+ : never
5671
+ : Node extends TargetSelection<any, any, any> ? TransitionTarget<
5596
5672
  States,
5597
5673
  Events,
5598
5674
  Emits,
5599
5675
  StateId,
5600
- InvokeDoneContext<States, Events, Emits, StateId, NoInfer<ChildOutput>, InputEvents, ParentEvents>
5676
+ Context,
5677
+ Reenter,
5678
+ Acceptance,
5679
+ Node
5601
5680
  >
5602
- >
5603
- & InvokeFailureRequirement<
5604
- NoInfer<ChildError>,
5605
- InvokeTransition<
5681
+ : {
5682
+ readonly [Key in keyof Node]: TransitionSelectorNode<
5606
5683
  States,
5607
5684
  Events,
5608
5685
  Emits,
5609
5686
  StateId,
5610
- InvokeFailureContext<States, Events, Emits, StateId, NoInfer<ChildError>, InputEvents, ParentEvents>
5687
+ Context,
5688
+ Reenter,
5689
+ Acceptance,
5690
+ Node[Key]
5611
5691
  >
5612
- >
5692
+ }
5613
5693
 
5614
- export type ChildInvokeArgs<
5694
+ interface TransitionBranchesResolveRequired<
5615
5695
  States extends StateSchemas,
5616
5696
  Events extends ReadonlyArray<TaggedSchema>,
5617
5697
  Emits extends ReadonlyArray<TaggedSchema>,
5618
- StateId extends StateIdentifier<States>,
5619
- ChildDefinition extends Machine.Any,
5620
- Child extends ChildMachine<string, ChildDefinition>,
5621
- InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5622
- ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5623
- > =
5624
- & {
5625
- readonly child:
5626
- & Child
5627
- & (ChildDefinition extends EnsureExecutable<
5628
- Machine.States<ChildDefinition>,
5629
- Machine.UnhandledStates<ChildDefinition>,
5630
- Machine.OutputStates<ChildDefinition>
5631
- > ? unknown :
5632
- never)
5633
- readonly id?: never
5634
- readonly address?: never
5635
- readonly effect?: never
5636
- readonly stream?: never
5637
- readonly after?: never
5638
- readonly logic?: never
5639
- readonly onElement?: never
5640
- readonly onSnapshot?: InvokeTransition<
5641
- States,
5642
- Events,
5643
- Emits,
5644
- StateId,
5645
- InvokeSnapshotContext<
5646
- States,
5647
- Events,
5648
- Emits,
5649
- StateId,
5650
- Snapshot<Machine.States<ChildDefinition>>,
5651
- Error<ChildDefinition>,
5652
- Output<ChildDefinition>,
5653
- InputEvents,
5654
- ParentEvents
5655
- >
5656
- >
5657
- }
5658
- & (Input<ChildDefinition> extends typeof Schema.Void ? { readonly input?: never } : {
5659
- readonly input: InvokeSource<
5660
- Input<ChildDefinition>["Type"],
5661
- InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5662
- >
5663
- })
5664
- & InvokeDoneRequirement<
5665
- Output<ChildDefinition>,
5666
- InvokeTransition<
5667
- States,
5668
- Events,
5669
- Emits,
5670
- StateId,
5671
- InvokeDoneContext<
5672
- States,
5673
- Events,
5674
- Emits,
5675
- StateId,
5676
- Output<ChildDefinition>,
5677
- InputEvents,
5678
- ParentEvents
5679
- >
5680
- >
5681
- >
5682
- & InvokeFailureRequirement<
5683
- Error<ChildDefinition> | ActionError<Services<ChildDefinition>>,
5684
- InvokeTransition<
5685
- States,
5686
- Events,
5687
- Emits,
5688
- StateId,
5689
- InvokeFailureContext<
5690
- States,
5691
- Events,
5692
- Emits,
5693
- StateId,
5694
- Error<ChildDefinition> | ActionError<Services<ChildDefinition>>,
5695
- InputEvents,
5696
- ParentEvents
5697
- >
5698
- >
5698
+ StateId extends StateNodeIdentifier<States>,
5699
+ Context,
5700
+ Reenter extends boolean,
5701
+ Branches extends Readonly<Record<string, TransitionBranchInput>>
5702
+ > {
5703
+ (
5704
+ resolve: TransitionBranchesResolver<Events, Emits, Context, Branches>,
5705
+ options?: TransitionRequiredOptions<Reenter>
5706
+ ): BuiltTransition<
5707
+ States,
5708
+ Events,
5709
+ Emits,
5710
+ StateId,
5711
+ Context,
5712
+ Reenter,
5713
+ BranchSelectionResult<Branches>,
5714
+ "required"
5699
5715
  >
5716
+ }
5700
5717
 
5701
- type LogicInitialEffectOf<Value> = Value extends { readonly initial: infer Initial } ?
5702
- Initial extends (...args: ReadonlyArray<any>) => infer Result ? Result : never
5703
- : never
5704
-
5705
- type LogicRunEffectOf<Value> = Value extends { readonly run: infer Run } ?
5706
- Run extends (...args: ReadonlyArray<any>) => infer Result ? Result : never
5707
- : never
5708
-
5709
- export type LogicStateOf<Value> = Effect.Success<LogicInitialEffectOf<Value>>
5710
- export type LogicEventOf<Value> = Value extends { readonly initial: infer Initial } ?
5711
- Initial extends (scope: infer LogicScope, ...args: ReadonlyArray<any>) => any ?
5712
- LogicScope extends Logic.Scope<infer Event> ? Event : never
5713
- : never
5714
- : never
5715
- export type LogicErrorOf<Value> = Effect.Error<LogicRunEffectOf<Value>>
5716
- export type LogicServicesOf<Value> = Effect.Services<LogicInitialEffectOf<Value> | LogicRunEffectOf<Value>>
5717
- export type LogicOutputOf<Value> = Effect.Success<LogicRunEffectOf<Value>>
5718
- export type LogicInitialErrorOf<Value> = Effect.Error<LogicInitialEffectOf<Value>>
5718
+ interface TransitionBranchesResolveDeclinable<
5719
+ States extends StateSchemas,
5720
+ Events extends ReadonlyArray<TaggedSchema>,
5721
+ Emits extends ReadonlyArray<TaggedSchema>,
5722
+ StateId extends StateNodeIdentifier<States>,
5723
+ Context,
5724
+ Reenter extends boolean,
5725
+ Branches extends Readonly<Record<string, TransitionBranchInput>>
5726
+ > {
5727
+ (
5728
+ resolve: DeclinableTransitionBranchesResolver<Events, Emits, Context, Branches>,
5729
+ options: TransitionDeclinableOptions<Reenter>
5730
+ ): BuiltTransition<
5731
+ States,
5732
+ Events,
5733
+ Emits,
5734
+ StateId,
5735
+ Context,
5736
+ Reenter,
5737
+ BranchSelectionResult<Branches> | Declined,
5738
+ "declinable"
5739
+ >
5740
+ }
5719
5741
 
5720
- type ContextualInvokeConfig<
5742
+ /** Selector supplied to inline transition declarations. */
5743
+ export type TransitionSelector<
5721
5744
  States extends StateSchemas,
5722
5745
  Events extends ReadonlyArray<TaggedSchema>,
5723
5746
  Emits extends ReadonlyArray<TaggedSchema>,
5724
- StateId extends StateIdentifier<States>,
5725
- InputEvents extends ReadonlyArray<TaggedSchema>,
5726
- ParentEvents extends ReadonlyArray<TaggedSchema>,
5727
- Raw
5728
- > = Raw extends InvokeTyped<any, any, any, any, any> ? unknown
5729
- : Raw extends { readonly effect: infer Source } ?
5730
- InvokeFactoryResult<Source> extends infer Fx extends Effect.Effect<any, any, any> ?
5731
- & InvokeDoneRequirement<
5732
- Effect.Success<Fx>,
5733
- InvokeTransition<
5734
- States,
5735
- Events,
5736
- Emits,
5737
- StateId,
5738
- InvokeDoneContext<States, Events, Emits, StateId, Effect.Success<Fx>, InputEvents, ParentEvents>
5739
- >
5740
- >
5741
- & InvokeFailureRequirement<
5742
- Effect.Error<Fx>,
5743
- InvokeTransition<
5744
- States,
5745
- Events,
5746
- Emits,
5747
- StateId,
5748
- InvokeFailureContext<States, Events, Emits, StateId, Effect.Error<Fx>, InputEvents, ParentEvents>
5749
- >
5750
- >
5751
- & { readonly onSnapshot?: never }
5752
- : never
5753
- : Raw extends { readonly stream: infer Source } ?
5754
- InvokeFactoryResult<Source> extends infer SourceStream extends Stream.Stream<any, any, any> ?
5755
- & InvokeElementRequirement<
5756
- Stream.Success<SourceStream>,
5757
- InvokeTransition<
5758
- States,
5759
- Events,
5760
- Emits,
5761
- StateId,
5762
- InvokeElementContext<
5763
- States,
5764
- Events,
5765
- Emits,
5766
- StateId,
5767
- Stream.Success<SourceStream>,
5768
- InputEvents,
5769
- ParentEvents
5770
- >
5771
- >
5772
- >
5773
- & {
5774
- readonly onDone: InvokeTransition<
5775
- States,
5776
- Events,
5777
- Emits,
5778
- StateId,
5779
- InvokeDoneContext<States, Events, Emits, StateId, void, InputEvents, ParentEvents>
5780
- >
5781
- }
5782
- & InvokeFailureRequirement<
5783
- Stream.Error<SourceStream>,
5784
- InvokeTransition<
5747
+ StateId extends StateNodeIdentifier<States>,
5748
+ Context,
5749
+ Reenter extends boolean,
5750
+ Acceptance extends TransitionAcceptance
5751
+ > =
5752
+ & TransitionSelectorNode<
5753
+ States,
5754
+ Events,
5755
+ Emits,
5756
+ StateId,
5757
+ Context,
5758
+ Reenter,
5759
+ Acceptance,
5760
+ TargetSelector<States, StateId>
5761
+ >
5762
+ & {
5763
+ readonly branches: <const Branches extends Readonly<Record<string, TransitionBranchInput>>>(
5764
+ branches: Branches & ValidateTransitionBranchRecord<NoInfer<Branches>>
5765
+ ) => {
5766
+ readonly resolve:
5767
+ & TransitionBranchesResolveRequired<States, Events, Emits, StateId, Context, Reenter, Branches>
5768
+ & ("declinable" extends Acceptance ? TransitionBranchesResolveDeclinable<
5785
5769
  States,
5786
5770
  Events,
5787
5771
  Emits,
5788
5772
  StateId,
5789
- InvokeFailureContext<
5790
- States,
5791
- Events,
5792
- Emits,
5793
- StateId,
5794
- Stream.Error<SourceStream>,
5795
- InputEvents,
5796
- ParentEvents
5797
- >
5773
+ Context,
5774
+ Reenter,
5775
+ Branches
5798
5776
  >
5799
- >
5800
- & { readonly onSnapshot?: never }
5801
- : never
5802
- : Raw extends { readonly after: unknown } ? {
5803
- readonly onDone: InvokeTransition<
5804
- States,
5805
- Events,
5806
- Emits,
5807
- StateId,
5808
- InvokeDoneContext<States, Events, Emits, StateId, void, InputEvents, ParentEvents>
5809
- >
5810
- readonly onFailure?: never
5811
- readonly onSnapshot?: never
5777
+ : {})
5812
5778
  }
5813
- : Raw extends { readonly logic: infer Source; readonly address: infer Address } ?
5814
- InvokeResolvedSource<Source> extends infer ChildLogic extends Logic<any, any, any, any, any, any> ?
5815
- & InvokeDoneRequirement<
5816
- InvokeOutput<Raw>,
5817
- InvokeTransition<
5818
- States,
5819
- Events,
5820
- Emits,
5821
- StateId,
5822
- InvokeDoneContext<States, Events, Emits, StateId, InvokeOutput<Raw>, InputEvents, ParentEvents>
5823
- >
5824
- >
5825
- & InvokeFailureRequirement<
5826
- InvokeRuntimeError<Raw>,
5827
- InvokeTransition<
5828
- States,
5829
- Events,
5830
- Emits,
5831
- StateId,
5832
- InvokeFailureContext<
5833
- States,
5834
- Events,
5835
- Emits,
5836
- StateId,
5837
- InvokeRuntimeError<Raw>,
5838
- InputEvents,
5839
- ParentEvents
5840
- >
5841
- >
5842
- >
5843
- & {
5844
- readonly address:
5845
- & ChildAddress<never>
5846
- & ChildAddress.Compatibility<
5847
- Address,
5848
- ChildLogic extends Logic<any, infer ChildEvent, any, any, any, any> ? ChildEvent : never
5849
- >
5850
- readonly onSnapshot?: InvokeTransition<
5851
- States,
5852
- Events,
5853
- Emits,
5854
- StateId,
5855
- InvokeSnapshotContext<
5856
- States,
5857
- Events,
5858
- Emits,
5859
- StateId,
5860
- ChildLogic extends Logic<infer ChildState, any, any, any, any, any> ? ChildState : never,
5861
- InvokeRuntimeError<Raw>,
5862
- InvokeOutput<Raw>,
5863
- InputEvents,
5864
- ParentEvents
5865
- >
5866
- >
5867
- }
5868
- : never
5869
- : Raw extends { readonly child: infer Child extends ChildMachine<string, infer ChildDefinition> } ?
5870
- ChildMachineLogic<Child> extends infer ChildLogic extends Logic<any, any, any, any, any, any> ?
5871
- & InvokeDoneRequirement<
5872
- Output<ChildDefinition>,
5873
- InvokeTransition<
5874
- States,
5875
- Events,
5876
- Emits,
5877
- StateId,
5878
- InvokeDoneContext<
5879
- States,
5880
- Events,
5881
- Emits,
5882
- StateId,
5883
- Output<ChildDefinition>,
5884
- InputEvents,
5885
- ParentEvents
5886
- >
5887
- >
5888
- >
5889
- & InvokeFailureRequirement<
5890
- Error<ChildDefinition> | ActionError<Services<ChildDefinition>>,
5891
- InvokeTransition<
5892
- States,
5893
- Events,
5894
- Emits,
5895
- StateId,
5896
- InvokeFailureContext<
5897
- States,
5898
- Events,
5899
- Emits,
5900
- StateId,
5901
- Error<ChildDefinition> | ActionError<Services<ChildDefinition>>,
5902
- InputEvents,
5903
- ParentEvents
5904
- >
5905
- >
5906
- >
5907
- & (Input<ChildDefinition> extends typeof Schema.Void ? { readonly input?: never } : {
5908
- readonly input: InvokeSource<
5909
- Input<ChildDefinition>["Type"],
5910
- InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5911
- >
5912
- })
5913
- & {
5914
- readonly onSnapshot?: InvokeTransition<
5915
- States,
5916
- Events,
5917
- Emits,
5918
- StateId,
5919
- InvokeSnapshotContext<
5920
- States,
5921
- Events,
5922
- Emits,
5923
- StateId,
5924
- Snapshot<Machine.States<ChildDefinition>>,
5925
- Error<ChildDefinition>,
5926
- Output<ChildDefinition>,
5927
- InputEvents,
5928
- ParentEvents
5929
- >
5930
- >
5931
- }
5932
- : never
5933
- : never
5779
+ }
5934
5780
 
5935
- type ContextualInvokeDefinition<
5781
+ /** Inline transition declaration accepted by state and invocation handlers. */
5782
+ export type TransitionBuilderInput<
5936
5783
  States extends StateSchemas,
5937
5784
  Events extends ReadonlyArray<TaggedSchema>,
5938
5785
  Emits extends ReadonlyArray<TaggedSchema>,
5939
- StateId extends StateIdentifier<States>,
5940
- InputEvents extends ReadonlyArray<TaggedSchema>,
5941
- ParentEvents extends ReadonlyArray<TaggedSchema>,
5942
- Raw
5943
- > = Raw extends ReadonlyArray<any> ? {
5944
- readonly [Index in keyof Raw]: ContextualInvokeConfig<
5945
- States,
5946
- Events,
5947
- Emits,
5948
- StateId,
5949
- InputEvents,
5950
- ParentEvents,
5951
- Raw[Index]
5952
- >
5953
- }
5954
- : ContextualInvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents, Raw>
5786
+ StateId extends StateNodeIdentifier<States>,
5787
+ Context,
5788
+ Reenter extends boolean,
5789
+ Acceptance extends TransitionAcceptance
5790
+ > = (
5791
+ to: TransitionSelector<States, Events, Emits, StateId, Context, Reenter, Acceptance>
5792
+ ) =>
5793
+ & TransitionTyped<States, Events, Emits, StateId, Context, Reenter, Acceptance>
5794
+ & TransitionBuilderEvidence<any, Acceptance>
5955
5795
 
5956
- type OutputHandlerConfig<
5796
+ export type InvokeTransition<
5957
5797
  States extends StateSchemas,
5958
5798
  Events extends ReadonlyArray<TaggedSchema>,
5959
- StateId extends StateIdentifier<States>,
5799
+ Emits extends ReadonlyArray<TaggedSchema>,
5800
+ StateId extends StateNodeIdentifier<States>,
5960
5801
  Context
5961
- > = NodeByIdentifier<States, StateId> extends { readonly output: Schema.Top } ? {
5962
- readonly output: (context: Context) => OutputByIdentifier<States, StateId>
5963
- }
5964
- : {
5965
- readonly output?: never
5966
- }
5802
+ > = TransitionConfig<States, Events, Emits, StateId, Context, true, TransitionAcceptance>
5967
5803
 
5968
- type ActiveOutputHandlerConfig<
5969
- States extends StateSchemas,
5970
- Events extends ReadonlyArray<TaggedSchema>,
5971
- StateId extends StateIdentifier<States>
5972
- > = NodeByIdentifier<States, StateId> extends { readonly type: "parallel" } ? OutputHandlerConfig<
5973
- States,
5974
- Events,
5975
- StateId,
5976
- ParallelOutputContext<States, Events, StateId>
5977
- >
5978
- : {
5979
- readonly output?: never
5980
- }
5804
+ export type InvokeSource<Value, Context> = Value | ((context: Context) => Value)
5981
5805
 
5982
- /**
5983
- * Configuration accepted for a non-final state.
5984
- *
5985
- * @category models
5986
- * @since 0.4.0
5987
- */
5988
- export type ActiveStateConfig<
5806
+ /** Inline state-owned work that runs for the lifetime of its active state. */
5807
+ export interface InvokeOwned<
5989
5808
  States extends StateSchemas,
5990
5809
  Events extends ReadonlyArray<TaggedSchema>,
5991
5810
  Emits extends ReadonlyArray<TaggedSchema>,
5992
5811
  StateId extends StateIdentifier<States>,
5993
- E,
5994
- R,
5995
5812
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5996
5813
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5997
- > = {
5998
- readonly entry?: (
5999
- context: StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
6000
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6001
- ) => StateActionResult<any, any>
6002
- readonly exit?: (
6003
- context: StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
6004
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6005
- ) => StateActionResult<any, any>
6006
- readonly invoke?:
6007
- | InvokeDefinition<States, Events, Emits, StateId, InputEvents, ParentEvents>
6008
- | TypedInvokeDefinition
6009
- readonly always?: TransitionConfig<
6010
- States,
6011
- Events,
6012
- Emits,
6013
- StateId,
6014
- AlwaysContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
6015
- >
6016
- readonly onDone?: TransitionConfig<
6017
- States,
6018
- Events,
6019
- Emits,
6020
- StateId,
6021
- DoneContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5814
+ > {
5815
+ readonly "~effect/Machine/InvokeOwner"?: Types.Covariant<
5816
+ readonly [States, Events, Emits, StateId, InputEvents, ParentEvents]
6022
5817
  >
6023
- readonly on?: {
6024
- readonly [EventTag in TagOf<Events[number]>]?: TransitionConfig<
6025
- States,
6026
- Events,
6027
- Emits,
6028
- StateId,
6029
- HandlerContext<States, Events, Emits, StateId, EventTag, E, R, InputEvents, ParentEvents>,
6030
- true
6031
- >
5818
+ }
5819
+
5820
+ /** Type evidence retained by a completed state-owned invocation. */
5821
+ export interface InvokeTyped<
5822
+ Output,
5823
+ Error,
5824
+ Requirements,
5825
+ InitialError,
5826
+ Emits = never,
5827
+ ParentEvent = never,
5828
+ Outcomes = never
5829
+ > {
5830
+ readonly [InvokeTypeId]: {
5831
+ readonly output: Types.Covariant<Output>
5832
+ readonly error: Types.Covariant<Error>
5833
+ readonly requirements: Types.Covariant<Requirements>
5834
+ readonly initialError: Types.Covariant<InitialError>
5835
+ readonly emits: Types.Covariant<Emits>
5836
+ readonly parentEvents: Types.Covariant<ParentEvent>
5837
+ readonly outcomes: Types.Covariant<Outcomes>
6032
5838
  }
6033
- readonly initialize?: StateInitializeHandler<States, Events, Emits, StateId, InputEvents, ParentEvents>
6034
- } & ActiveOutputHandlerConfig<States, Events, StateId>
5839
+ }
6035
5840
 
6036
- /**
6037
- * Values constructed for a state's schema-valued direct initial children.
6038
- *
6039
- * @category utility types
6040
- * @since 0.13.0
6041
- */
6042
- export type StateInitializeValue<
5841
+ type StoredInvokeConfig<
6043
5842
  States extends StateSchemas,
6044
- StateId extends StateIdentifier<States>
6045
- > = NodeByIdentifier<States, StateId> extends infer Node ?
6046
- Node extends { readonly type: "parallel"; readonly states: infer Children extends StateSchemas } ? {
6047
- readonly [Key in ActiveStateKey<Children> as NodeSchema<Children[Key]> extends never ? never : Key]: NodeValue<
6048
- Children[Key]
5843
+ Events extends ReadonlyArray<TaggedSchema>,
5844
+ Emits extends ReadonlyArray<TaggedSchema>,
5845
+ StateId extends StateIdentifier<States>,
5846
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5847
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5848
+ > =
5849
+ & InvokeOwned<States, Events, Emits, StateId, InputEvents, ParentEvents>
5850
+ & (
5851
+ | {
5852
+ readonly id: string
5853
+ readonly effect: (
5854
+ context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5855
+ ) => Effect.Effect<any, any, any>
5856
+ readonly stream?: never
5857
+ readonly after?: never
5858
+ readonly logic?: never
5859
+ readonly child?: never
5860
+ readonly address?: never
5861
+ readonly onDone?: unknown
5862
+ readonly onFailure?: unknown
5863
+ readonly onElement?: never
5864
+ readonly onSnapshot?: never
5865
+ }
5866
+ | {
5867
+ readonly id: string
5868
+ readonly stream: (
5869
+ context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5870
+ ) => Stream.Stream<any, any, any>
5871
+ readonly effect?: never
5872
+ readonly after?: never
5873
+ readonly logic?: never
5874
+ readonly child?: never
5875
+ readonly address?: never
5876
+ readonly onElement?: unknown
5877
+ readonly onDone: unknown
5878
+ readonly onFailure?: unknown
5879
+ readonly onSnapshot?: never
5880
+ }
5881
+ | {
5882
+ readonly id: string
5883
+ readonly after: InvokeSource<
5884
+ Duration.Input,
5885
+ InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
6049
5886
  >
5887
+ readonly effect?: never
5888
+ readonly stream?: never
5889
+ readonly logic?: never
5890
+ readonly child?: never
5891
+ readonly address?: never
5892
+ readonly onDone: unknown
5893
+ readonly onFailure?: never
5894
+ readonly onElement?: never
5895
+ readonly onSnapshot?: never
6050
5896
  }
6051
- : Node extends { readonly states: infer Children extends StateSchemas; readonly initial: infer Initial } ?
6052
- Initial extends ActiveStateKey<Children> ? NodeSchema<Children[Initial]> extends never ? never
6053
- : { readonly [Key in Initial]: NodeValue<Children[Initial]> }
6054
- : never
5897
+ | {
5898
+ readonly id: string
5899
+ readonly address: string
5900
+ readonly logic: InvokeSource<
5901
+ Logic<any, any, any, any, any, any>,
5902
+ InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5903
+ >
5904
+ readonly effect?: never
5905
+ readonly stream?: never
5906
+ readonly after?: never
5907
+ readonly child?: never
5908
+ readonly onDone?: unknown
5909
+ readonly onFailure?: unknown
5910
+ readonly onElement?: never
5911
+ readonly onSnapshot?: unknown
5912
+ }
5913
+ | {
5914
+ readonly child: ChildMachine.Any
5915
+ readonly input?:
5916
+ | {}
5917
+ | null
5918
+ | ((context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>) => unknown)
5919
+ readonly id?: never
5920
+ readonly address?: never
5921
+ readonly effect?: never
5922
+ readonly stream?: never
5923
+ readonly after?: never
5924
+ readonly logic?: never
5925
+ readonly onDone?: unknown
5926
+ readonly onFailure?: unknown
5927
+ readonly onElement?: never
5928
+ readonly onSnapshot?: unknown
5929
+ }
5930
+ )
5931
+
5932
+ type StoredInvokeDefinition<
5933
+ States extends StateSchemas,
5934
+ Events extends ReadonlyArray<TaggedSchema>,
5935
+ Emits extends ReadonlyArray<TaggedSchema>,
5936
+ StateId extends StateIdentifier<States>,
5937
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5938
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5939
+ > =
5940
+ | StoredInvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
5941
+ | ReadonlyArray<StoredInvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>>
5942
+
5943
+ type LogicInitialEffectOf<Value> = Value extends { readonly initial: infer Initial } ?
5944
+ Initial extends (...args: ReadonlyArray<any>) => infer Result ? Result : never
6055
5945
  : never
5946
+
5947
+ type LogicRunEffectOf<Value> = Value extends { readonly run: infer Run } ?
5948
+ Run extends (...args: ReadonlyArray<any>) => infer Result ? Result : never
6056
5949
  : never
6057
5950
 
6058
- type StateInitializeCompoundBuilder<Node, Initial extends PropertyKey> = Node extends {
6059
- readonly states: infer Children extends StateSchemas
6060
- } ? Initial extends ActiveStateKey<Children> ? NodeSchema<Children[Initial]> extends never ? never
6061
- : NodeBuilderMethod<
6062
- Children[Initial],
6063
- readonly [value: NodeValue<Children[Initial]>],
6064
- SnapshotBuilderComplete<{ readonly [Key in Initial]: NodeValue<Children[Initial]> }>,
6065
- readonly [input: NodeMakeInput<Children[Initial]>],
6066
- SnapshotBuilderComplete<{ readonly [Key in Initial]: NodeValue<Children[Initial]> }, true>
6067
- >
5951
+ export type LogicStateOf<Value> = Effect.Success<LogicInitialEffectOf<Value>>
5952
+ export type LogicEventOf<Value> = Value extends { readonly initial: infer Initial } ?
5953
+ Initial extends (scope: infer LogicScope, ...args: ReadonlyArray<any>) => any ?
5954
+ LogicScope extends Logic.Scope<infer Event> ? Event : never
6068
5955
  : never
6069
5956
  : never
5957
+ export type LogicErrorOf<Value> = Effect.Error<LogicRunEffectOf<Value>>
5958
+ export type LogicServicesOf<Value> = Effect.Services<LogicInitialEffectOf<Value> | LogicRunEffectOf<Value>>
5959
+ export type LogicOutputOf<Value> = Effect.Success<LogicRunEffectOf<Value>>
5960
+ export type LogicInitialErrorOf<Value> = Effect.Error<LogicInitialEffectOf<Value>>
6070
5961
 
6071
- type StateInitializeParallelBuilder<
6072
- Children extends StateSchemas,
6073
- Remaining extends ActiveStateKey<Children> = {
6074
- readonly [Key in ActiveStateKey<Children>]: NodeSchema<Children[Key]> extends never ? never : Key
6075
- }[ActiveStateKey<Children>],
6076
- Values = {},
6077
- Constructed extends boolean = false
5962
+ type RequiredInvokeChannel<Value, Channel extends string> = IsAny<Value> extends true ? Channel
5963
+ : [Value] extends [never] ? never
5964
+ : Channel
5965
+
5966
+ type InvokeBuilderResult<
5967
+ States extends StateSchemas,
5968
+ Events extends ReadonlyArray<TaggedSchema>,
5969
+ Emits extends ReadonlyArray<TaggedSchema>,
5970
+ StateId extends StateIdentifier<States>,
5971
+ InputEvents extends ReadonlyArray<TaggedSchema>,
5972
+ ParentEvents extends ReadonlyArray<TaggedSchema>,
5973
+ Output,
5974
+ Error,
5975
+ Requirements,
5976
+ InitialError,
5977
+ ChildEmits,
5978
+ ChildParentEvent,
5979
+ Outcomes
6078
5980
  > =
6079
- & SnapshotBuilderComplete<Values, Constructed>
6080
- & {
6081
- readonly [Key in Remaining]: NodeBuilderMethod<
6082
- Children[Key],
6083
- readonly [value: NodeValue<Children[Key]>],
6084
- StateInitializeParallelBuilder<
6085
- Children,
6086
- Exclude<Remaining, Key>,
6087
- Values & { readonly [Region in Key]: NodeValue<Children[Key]> },
6088
- Constructed
6089
- >,
6090
- readonly [input: NodeMakeInput<Children[Key]>],
6091
- StateInitializeParallelBuilder<
6092
- Children,
6093
- Exclude<Remaining, Key>,
6094
- Values & { readonly [Region in Key]: NodeValue<Children[Key]> },
6095
- true
5981
+ & InvokeOwned<States, Events, Emits, StateId, InputEvents, ParentEvents>
5982
+ & InvokeTyped<Output, Error, Requirements, InitialError, ChildEmits, ChildParentEvent, Outcomes>
5983
+ & { readonly [InvokeBuilderTypeId]: true }
5984
+
5985
+ type InvokeBuilder<
5986
+ States extends StateSchemas,
5987
+ Events extends ReadonlyArray<TaggedSchema>,
5988
+ Emits extends ReadonlyArray<TaggedSchema>,
5989
+ StateId extends StateIdentifier<States>,
5990
+ InputEvents extends ReadonlyArray<TaggedSchema>,
5991
+ ParentEvents extends ReadonlyArray<TaggedSchema>,
5992
+ Output,
5993
+ Error,
5994
+ Requirements,
5995
+ InitialError,
5996
+ ChildEmits,
5997
+ ChildParentEvent,
5998
+ Element,
5999
+ Pending extends string,
6000
+ SnapshotHandler,
6001
+ Outcomes = never
6002
+ > =
6003
+ & ([Pending] extends [never] ? InvokeBuilderResult<
6004
+ States,
6005
+ Events,
6006
+ Emits,
6007
+ StateId,
6008
+ InputEvents,
6009
+ ParentEvents,
6010
+ Output,
6011
+ Error,
6012
+ Requirements,
6013
+ InitialError,
6014
+ ChildEmits,
6015
+ ChildParentEvent,
6016
+ Outcomes
6017
+ >
6018
+ : {})
6019
+ & ("done" extends Pending ? {
6020
+ readonly onDone: <
6021
+ const Handler extends InvokeTransition<
6022
+ States,
6023
+ Events,
6024
+ Emits,
6025
+ StateId,
6026
+ InvokeDoneContext<States, Events, Emits, StateId, Output, InputEvents, ParentEvents>
6027
+ >
6028
+ >(
6029
+ handler:
6030
+ & Handler
6031
+ & InvokeTransition<
6032
+ States,
6033
+ Events,
6034
+ Emits,
6035
+ StateId,
6036
+ InvokeDoneContext<States, Events, Emits, StateId, Output, InputEvents, ParentEvents>
6037
+ >
6038
+ ) => InvokeBuilder<
6039
+ States,
6040
+ Events,
6041
+ Emits,
6042
+ StateId,
6043
+ InputEvents,
6044
+ ParentEvents,
6045
+ Output,
6046
+ Error,
6047
+ Requirements,
6048
+ InitialError,
6049
+ ChildEmits,
6050
+ ChildParentEvent,
6051
+ Element,
6052
+ Exclude<Pending, "done">,
6053
+ SnapshotHandler,
6054
+ Outcomes | Handler
6055
+ >
6056
+ }
6057
+ : {})
6058
+ & ("failure" extends Pending ? {
6059
+ readonly onFailure: <
6060
+ const Handler extends InvokeTransition<
6061
+ States,
6062
+ Events,
6063
+ Emits,
6064
+ StateId,
6065
+ InvokeFailureContext<States, Events, Emits, StateId, Error, InputEvents, ParentEvents>
6066
+ >
6067
+ >(
6068
+ handler:
6069
+ & Handler
6070
+ & InvokeTransition<
6071
+ States,
6072
+ Events,
6073
+ Emits,
6074
+ StateId,
6075
+ InvokeFailureContext<States, Events, Emits, StateId, Error, InputEvents, ParentEvents>
6076
+ >
6077
+ ) => InvokeBuilder<
6078
+ States,
6079
+ Events,
6080
+ Emits,
6081
+ StateId,
6082
+ InputEvents,
6083
+ ParentEvents,
6084
+ Output,
6085
+ Error,
6086
+ Requirements,
6087
+ InitialError,
6088
+ ChildEmits,
6089
+ ChildParentEvent,
6090
+ Element,
6091
+ Exclude<Pending, "failure">,
6092
+ SnapshotHandler,
6093
+ Outcomes | Handler
6094
+ >
6095
+ }
6096
+ : {})
6097
+ & ("element" extends Pending ? {
6098
+ readonly onElement: <
6099
+ const Handler extends InvokeTransition<
6100
+ States,
6101
+ Events,
6102
+ Emits,
6103
+ StateId,
6104
+ InvokeElementContext<States, Events, Emits, StateId, Element, InputEvents, ParentEvents>
6105
+ >
6106
+ >(
6107
+ handler:
6108
+ & Handler
6109
+ & InvokeTransition<
6110
+ States,
6111
+ Events,
6112
+ Emits,
6113
+ StateId,
6114
+ InvokeElementContext<States, Events, Emits, StateId, Element, InputEvents, ParentEvents>
6115
+ >
6116
+ ) => InvokeBuilder<
6117
+ States,
6118
+ Events,
6119
+ Emits,
6120
+ StateId,
6121
+ InputEvents,
6122
+ ParentEvents,
6123
+ Output,
6124
+ Error,
6125
+ Requirements,
6126
+ InitialError,
6127
+ ChildEmits,
6128
+ ChildParentEvent,
6129
+ Element,
6130
+ Exclude<Pending, "element">,
6131
+ SnapshotHandler,
6132
+ Outcomes | Handler
6096
6133
  >
6134
+ }
6135
+ : {})
6136
+ & ([SnapshotHandler] extends [never] ? {} : {
6137
+ readonly onSnapshot: <const Handler extends SnapshotHandler>(handler: Handler & SnapshotHandler) => InvokeBuilder<
6138
+ States,
6139
+ Events,
6140
+ Emits,
6141
+ StateId,
6142
+ InputEvents,
6143
+ ParentEvents,
6144
+ Output,
6145
+ Error,
6146
+ Requirements,
6147
+ InitialError,
6148
+ ChildEmits,
6149
+ ChildParentEvent,
6150
+ Element,
6151
+ Pending,
6152
+ never,
6153
+ Outcomes | Handler
6097
6154
  >
6098
- }
6155
+ })
6099
6156
 
6100
- /**
6101
- * Builder bound to the direct initial child or regions owned by a state.
6102
- *
6103
- * @category utility types
6104
- * @since 0.13.0
6105
- */
6106
- export type StateInitializeBuilder<
6157
+ type EffectInvokeBuilder<
6107
6158
  States extends StateSchemas,
6159
+ Events extends ReadonlyArray<TaggedSchema>,
6160
+ Emits extends ReadonlyArray<TaggedSchema>,
6108
6161
  StateId extends StateIdentifier<States>,
6109
- Node = NodeByIdentifier<States, StateId>
6110
- > = Node extends { readonly type: "parallel"; readonly states: infer Children extends StateSchemas } ?
6111
- StateInitializeParallelBuilder<Children>
6112
- : Node extends { readonly initial: infer Initial } ? StateInitializeCompoundBuilder<Node, Initial & PropertyKey>
6113
- : never
6162
+ InputEvents extends ReadonlyArray<TaggedSchema>,
6163
+ ParentEvents extends ReadonlyArray<TaggedSchema>,
6164
+ Source extends (...args: ReadonlyArray<any>) => Effect.Effect<any, any, any>
6165
+ > = InvokeBuilder<
6166
+ States,
6167
+ Events,
6168
+ Emits,
6169
+ StateId,
6170
+ InputEvents,
6171
+ ParentEvents,
6172
+ Effect.Success<ReturnType<Source>>,
6173
+ Effect.Error<ReturnType<Source>>,
6174
+ Effect.Services<ReturnType<Source>>,
6175
+ never,
6176
+ never,
6177
+ never,
6178
+ never,
6179
+ | RequiredInvokeChannel<Effect.Success<ReturnType<Source>>, "done">
6180
+ | RequiredInvokeChannel<Effect.Error<ReturnType<Source>>, "failure">,
6181
+ never
6182
+ >
6183
+
6184
+ type StreamInvokeBuilder<
6185
+ States extends StateSchemas,
6186
+ Events extends ReadonlyArray<TaggedSchema>,
6187
+ Emits extends ReadonlyArray<TaggedSchema>,
6188
+ StateId extends StateIdentifier<States>,
6189
+ InputEvents extends ReadonlyArray<TaggedSchema>,
6190
+ ParentEvents extends ReadonlyArray<TaggedSchema>,
6191
+ Source extends (...args: ReadonlyArray<any>) => Stream.Stream<any, any, any>
6192
+ > = InvokeBuilder<
6193
+ States,
6194
+ Events,
6195
+ Emits,
6196
+ StateId,
6197
+ InputEvents,
6198
+ ParentEvents,
6199
+ void,
6200
+ Stream.Error<ReturnType<Source>>,
6201
+ Stream.Services<ReturnType<Source>>,
6202
+ never,
6203
+ never,
6204
+ never,
6205
+ Stream.Success<ReturnType<Source>>,
6206
+ | "done"
6207
+ | RequiredInvokeChannel<Stream.Success<ReturnType<Source>>, "element">
6208
+ | RequiredInvokeChannel<Stream.Error<ReturnType<Source>>, "failure">,
6209
+ never
6210
+ >
6211
+
6212
+ type LogicInvokeBuilder<
6213
+ States extends StateSchemas,
6214
+ Events extends ReadonlyArray<TaggedSchema>,
6215
+ Emits extends ReadonlyArray<TaggedSchema>,
6216
+ StateId extends StateIdentifier<States>,
6217
+ InputEvents extends ReadonlyArray<TaggedSchema>,
6218
+ ParentEvents extends ReadonlyArray<TaggedSchema>,
6219
+ ChildLogic
6220
+ > = InvokeBuilder<
6221
+ States,
6222
+ Events,
6223
+ Emits,
6224
+ StateId,
6225
+ InputEvents,
6226
+ ParentEvents,
6227
+ LogicOutputOf<ChildLogic>,
6228
+ LogicErrorOf<ChildLogic>,
6229
+ LogicServicesOf<ChildLogic>,
6230
+ LogicInitialErrorOf<ChildLogic>,
6231
+ never,
6232
+ never,
6233
+ never,
6234
+ | RequiredInvokeChannel<LogicOutputOf<ChildLogic>, "done">
6235
+ | RequiredInvokeChannel<LogicErrorOf<ChildLogic>, "failure">,
6236
+ InvokeTransition<
6237
+ States,
6238
+ Events,
6239
+ Emits,
6240
+ StateId,
6241
+ InvokeSnapshotContext<
6242
+ States,
6243
+ Events,
6244
+ Emits,
6245
+ StateId,
6246
+ LogicStateOf<ChildLogic>,
6247
+ LogicErrorOf<ChildLogic>,
6248
+ LogicOutputOf<ChildLogic>,
6249
+ InputEvents,
6250
+ ParentEvents
6251
+ >
6252
+ >
6253
+ >
6254
+
6255
+ type ChildInvokeBuilder<
6256
+ States extends StateSchemas,
6257
+ Events extends ReadonlyArray<TaggedSchema>,
6258
+ Emits extends ReadonlyArray<TaggedSchema>,
6259
+ StateId extends StateIdentifier<States>,
6260
+ InputEvents extends ReadonlyArray<TaggedSchema>,
6261
+ ParentEvents extends ReadonlyArray<TaggedSchema>,
6262
+ Child extends ChildMachine.Any,
6263
+ ChildDefinition extends Machine.Any = Child["machine"]
6264
+ > = InvokeBuilder<
6265
+ States,
6266
+ Events,
6267
+ Emits,
6268
+ StateId,
6269
+ InputEvents,
6270
+ ParentEvents,
6271
+ Output<ChildDefinition>,
6272
+ Error<ChildDefinition> | ActionError<Services<ChildDefinition>>,
6273
+ Services<ChildDefinition>,
6274
+ InitialError<ChildDefinition>,
6275
+ Emit<ChildDefinition>,
6276
+ EventOf<Machine.ParentEvents<ChildDefinition>>,
6277
+ never,
6278
+ | RequiredInvokeChannel<Output<ChildDefinition>, "done">
6279
+ | RequiredInvokeChannel<Error<ChildDefinition> | ActionError<Services<ChildDefinition>>, "failure">,
6280
+ InvokeTransition<
6281
+ States,
6282
+ Events,
6283
+ Emits,
6284
+ StateId,
6285
+ InvokeSnapshotContext<
6286
+ States,
6287
+ Events,
6288
+ Emits,
6289
+ StateId,
6290
+ Snapshot<Machine.States<ChildDefinition>>,
6291
+ Error<ChildDefinition>,
6292
+ Output<ChildDefinition>,
6293
+ InputEvents,
6294
+ ParentEvents
6295
+ >
6296
+ >
6297
+ >
6114
6298
 
6115
6299
  /**
6116
- * Context passed to an implicit child-state initializer.
6300
+ * Selects state-owned work and begins its lifecycle-handler chain.
6301
+ *
6302
+ * Each source exposes exactly the lifecycle methods that it can produce. A
6303
+ * chain becomes returnable from `invoke` only after every reachable required
6304
+ * channel has been handled.
6117
6305
  *
6118
6306
  * @category models
6119
- * @since 0.13.0
6307
+ * @since 0.18.0
6120
6308
  */
6121
- export type StateInitializeContext<
6309
+ export interface InvokeSelector<
6122
6310
  States extends StateSchemas,
6123
6311
  Events extends ReadonlyArray<TaggedSchema>,
6124
6312
  Emits extends ReadonlyArray<TaggedSchema>,
6125
6313
  StateId extends StateIdentifier<States>,
6126
6314
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
6127
6315
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
6128
- > = StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents> & {
6129
- readonly builder: StateInitializeBuilder<States, StateId>
6316
+ > {
6317
+ /** Starts a fresh Effect each time the owning state is entered. */
6318
+ readonly effect: <
6319
+ const Source extends (
6320
+ context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
6321
+ ) => Effect.Effect<unknown, unknown, unknown>
6322
+ >(
6323
+ id: InvokeLifecycleId,
6324
+ source: Source
6325
+ ) => EffectInvokeBuilder<States, Events, Emits, StateId, InputEvents, ParentEvents, Source>
6326
+
6327
+ /** Starts a fresh, backpressured Stream each time the owning state is entered. */
6328
+ readonly stream: <
6329
+ const Source extends (
6330
+ context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
6331
+ ) => Stream.Stream<unknown, unknown, any>
6332
+ >(
6333
+ id: InvokeLifecycleId,
6334
+ source: Source
6335
+ ) => StreamInvokeBuilder<States, Events, Emits, StateId, InputEvents, ParentEvents, Source>
6336
+
6337
+ /** Starts a cancellable state-scoped timer. */
6338
+ readonly timer: (
6339
+ id: InvokeLifecycleId,
6340
+ duration: InvokeSource<
6341
+ Duration.Input,
6342
+ InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
6343
+ >
6344
+ ) => InvokeBuilder<
6345
+ States,
6346
+ Events,
6347
+ Emits,
6348
+ StateId,
6349
+ InputEvents,
6350
+ ParentEvents,
6351
+ void,
6352
+ never,
6353
+ never,
6354
+ never,
6355
+ never,
6356
+ never,
6357
+ never,
6358
+ "done",
6359
+ never
6360
+ >
6361
+
6362
+ /** Starts reusable process logic at a typed parent-local address. */
6363
+ readonly logic: {
6364
+ <
6365
+ const Source extends (
6366
+ context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
6367
+ ) => unknown,
6368
+ Address extends ChildAddress<never>
6369
+ >(
6370
+ id: InvokeLifecycleId,
6371
+ options: {
6372
+ readonly address:
6373
+ & Address
6374
+ & ChildAddress.Compatibility<Address, LogicEventOf<ReturnType<NoInfer<Source>>>>
6375
+ readonly logic: Source
6376
+ },
6377
+ ..._validation: ReturnType<Source> extends { readonly initial: unknown; readonly run: unknown } ? [] : [
6378
+ "logic factory must return Machine.Logic"
6379
+ ]
6380
+ ): LogicInvokeBuilder<
6381
+ States,
6382
+ Events,
6383
+ Emits,
6384
+ StateId,
6385
+ InputEvents,
6386
+ ParentEvents,
6387
+ ReturnType<Source>
6388
+ >
6389
+ <const Source, Address extends ChildAddress<never>>(
6390
+ id: InvokeLifecycleId,
6391
+ options: {
6392
+ readonly address: Address & ChildAddress.Compatibility<Address, LogicEventOf<NoInfer<Source>>>
6393
+ readonly logic: Source
6394
+ },
6395
+ ..._validation: Source extends { readonly initial: unknown; readonly run: unknown } ? [] : [
6396
+ "logic must implement Machine.Logic"
6397
+ ]
6398
+ ): LogicInvokeBuilder<
6399
+ States,
6400
+ Events,
6401
+ Emits,
6402
+ StateId,
6403
+ InputEvents,
6404
+ ParentEvents,
6405
+ Source
6406
+ >
6407
+ }
6408
+
6409
+ /** Starts a complete child statechart represented by a reusable descriptor. */
6410
+ readonly child: <const Child extends ChildMachine.Any>(
6411
+ child:
6412
+ & Child
6413
+ & (Child["machine"] extends EnsureExecutable<
6414
+ Machine.States<Child["machine"]>,
6415
+ Machine.UnhandledStates<Child["machine"]>,
6416
+ Machine.OutputStates<Child["machine"]>
6417
+ > ? unknown
6418
+ : never),
6419
+ ...options: InputSchema<Child["machine"]> extends typeof Schema.Void ? [options?: { readonly input?: never }]
6420
+ : [options: {
6421
+ readonly input: InvokeSource<
6422
+ Input<Child["machine"]>,
6423
+ InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
6424
+ >
6425
+ }]
6426
+ ) => ChildInvokeBuilder<States, Events, Emits, StateId, InputEvents, ParentEvents, Child>
6130
6427
  }
6131
6428
 
6132
6429
  /**
6133
- * Initial child value implementation for a compound or parallel state.
6430
+ * Inline invocation declaration accepted by an active state handler.
6431
+ *
6432
+ * Return one completed source chain or an array of completed chains. Source
6433
+ * computations and child descriptors may be extracted, but the chain stays
6434
+ * local to preserve the owning state and machine protocols.
6134
6435
  *
6135
6436
  * @category models
6136
- * @since 0.13.0
6437
+ * @since 0.18.0
6137
6438
  */
6138
- export type StateInitializeHandler<
6439
+ export type InvokeBuilderInput<
6139
6440
  States extends StateSchemas,
6140
6441
  Events extends ReadonlyArray<TaggedSchema>,
6141
6442
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -6143,15 +6444,219 @@ export declare namespace Machine {
6143
6444
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
6144
6445
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
6145
6446
  > = (
6146
- context: StateInitializeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
6147
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6148
- ) => SnapshotBuilderComplete<StateInitializeValue<States, StateId>, boolean>
6447
+ from: InvokeSelector<States, Events, Emits, StateId, InputEvents, ParentEvents>
6448
+ ) =>
6449
+ | (InvokeOwned<States, Events, Emits, StateId, InputEvents, ParentEvents> & {
6450
+ readonly [InvokeBuilderTypeId]: true
6451
+ })
6452
+ | ReadonlyArray<
6453
+ InvokeOwned<States, Events, Emits, StateId, InputEvents, ParentEvents> & {
6454
+ readonly [InvokeBuilderTypeId]: true
6455
+ }
6456
+ >
6149
6457
 
6150
- /** Context used only when a history node has no previously captured record. */
6151
- export interface HistoryDefaultContext<
6458
+ type OutputHandlerConfig<
6152
6459
  States extends StateSchemas,
6153
6460
  Events extends ReadonlyArray<TaggedSchema>,
6154
- Emits extends ReadonlyArray<TaggedSchema>,
6461
+ StateId extends StateIdentifier<States>,
6462
+ Context
6463
+ > = NodeByIdentifier<States, StateId> extends { readonly output: Schema.Top } ? {
6464
+ readonly output: (context: Context) => OutputByIdentifier<States, StateId>
6465
+ }
6466
+ : {
6467
+ readonly output?: never
6468
+ }
6469
+
6470
+ type ActiveOutputHandlerConfig<
6471
+ States extends StateSchemas,
6472
+ Events extends ReadonlyArray<TaggedSchema>,
6473
+ StateId extends StateIdentifier<States>
6474
+ > = NodeByIdentifier<States, StateId> extends { readonly type: "parallel" } ? OutputHandlerConfig<
6475
+ States,
6476
+ Events,
6477
+ StateId,
6478
+ ParallelOutputContext<States, Events, StateId>
6479
+ >
6480
+ : {
6481
+ readonly output?: never
6482
+ }
6483
+
6484
+ /**
6485
+ * Configuration accepted for a non-final state.
6486
+ *
6487
+ * @category models
6488
+ * @since 0.4.0
6489
+ */
6490
+ export type ActiveStateConfig<
6491
+ States extends StateSchemas,
6492
+ Events extends ReadonlyArray<TaggedSchema>,
6493
+ Emits extends ReadonlyArray<TaggedSchema>,
6494
+ StateId extends StateIdentifier<States>,
6495
+ E,
6496
+ R,
6497
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
6498
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
6499
+ > = {
6500
+ readonly entry?: (
6501
+ context: StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
6502
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6503
+ ) => StateActionResult<any, any>
6504
+ readonly exit?: (
6505
+ context: StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
6506
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6507
+ ) => StateActionResult<any, any>
6508
+ readonly invoke?: InvokeBuilderInput<States, Events, Emits, StateId, InputEvents, ParentEvents>
6509
+ readonly always?: TransitionConfig<
6510
+ States,
6511
+ Events,
6512
+ Emits,
6513
+ StateId,
6514
+ AlwaysContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
6515
+ false,
6516
+ TransitionAcceptance
6517
+ >
6518
+ readonly onDone?: TransitionConfig<
6519
+ States,
6520
+ Events,
6521
+ Emits,
6522
+ StateId,
6523
+ DoneContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
6524
+ false,
6525
+ TransitionAcceptance
6526
+ >
6527
+ readonly on?: {
6528
+ readonly [EventTag in TagOf<Events[number]>]?: TransitionConfig<
6529
+ States,
6530
+ Events,
6531
+ Emits,
6532
+ StateId,
6533
+ HandlerContext<States, Events, Emits, StateId, EventTag, E, R, InputEvents, ParentEvents>,
6534
+ true,
6535
+ TransitionAcceptance
6536
+ >
6537
+ }
6538
+ readonly initialize?: StateInitializeHandler<States, Events, Emits, StateId, InputEvents, ParentEvents>
6539
+ } & ActiveOutputHandlerConfig<States, Events, StateId>
6540
+
6541
+ /**
6542
+ * Values constructed for a state's schema-valued direct initial children.
6543
+ *
6544
+ * @category utility types
6545
+ * @since 0.13.0
6546
+ */
6547
+ export type StateInitializeValue<
6548
+ States extends StateSchemas,
6549
+ StateId extends StateIdentifier<States>
6550
+ > = NodeByIdentifier<States, StateId> extends infer Node ?
6551
+ Node extends { readonly type: "parallel"; readonly states: infer Children extends StateSchemas } ? {
6552
+ readonly [Key in ActiveStateKey<Children> as NodeSchema<Children[Key]> extends never ? never : Key]: NodeValue<
6553
+ Children[Key]
6554
+ >
6555
+ }
6556
+ : Node extends { readonly states: infer Children extends StateSchemas; readonly initial: infer Initial } ?
6557
+ Initial extends ActiveStateKey<Children> ? NodeSchema<Children[Initial]> extends never ? never
6558
+ : { readonly [Key in Initial]: NodeValue<Children[Initial]> }
6559
+ : never
6560
+ : never
6561
+ : never
6562
+
6563
+ type StateInitializeCompoundBuilder<Node, Initial extends PropertyKey> = Node extends {
6564
+ readonly states: infer Children extends StateSchemas
6565
+ } ? Initial extends ActiveStateKey<Children> ? NodeSchema<Children[Initial]> extends never ? never
6566
+ : NodeBuilderMethod<
6567
+ Children[Initial],
6568
+ readonly [value: NodeValue<Children[Initial]>],
6569
+ SnapshotBuilderComplete<{ readonly [Key in Initial]: NodeValue<Children[Initial]> }>,
6570
+ readonly [input: NodeMakeInput<Children[Initial]>],
6571
+ SnapshotBuilderComplete<{ readonly [Key in Initial]: NodeValue<Children[Initial]> }, true>
6572
+ >
6573
+ : never
6574
+ : never
6575
+
6576
+ type StateInitializeParallelBuilder<
6577
+ Children extends StateSchemas,
6578
+ Remaining extends ActiveStateKey<Children> = {
6579
+ readonly [Key in ActiveStateKey<Children>]: NodeSchema<Children[Key]> extends never ? never : Key
6580
+ }[ActiveStateKey<Children>],
6581
+ Values = {},
6582
+ Constructed extends boolean = false
6583
+ > =
6584
+ & SnapshotBuilderComplete<Values, Constructed>
6585
+ & {
6586
+ readonly [Key in Remaining]: NodeBuilderMethod<
6587
+ Children[Key],
6588
+ readonly [value: NodeValue<Children[Key]>],
6589
+ StateInitializeParallelBuilder<
6590
+ Children,
6591
+ Exclude<Remaining, Key>,
6592
+ Values & { readonly [Region in Key]: NodeValue<Children[Key]> },
6593
+ Constructed
6594
+ >,
6595
+ readonly [input: NodeMakeInput<Children[Key]>],
6596
+ StateInitializeParallelBuilder<
6597
+ Children,
6598
+ Exclude<Remaining, Key>,
6599
+ Values & { readonly [Region in Key]: NodeValue<Children[Key]> },
6600
+ true
6601
+ >
6602
+ >
6603
+ }
6604
+
6605
+ /**
6606
+ * Builder bound to the direct initial child or regions owned by a state.
6607
+ *
6608
+ * @category utility types
6609
+ * @since 0.13.0
6610
+ */
6611
+ export type StateInitializeBuilder<
6612
+ States extends StateSchemas,
6613
+ StateId extends StateIdentifier<States>,
6614
+ Node = NodeByIdentifier<States, StateId>
6615
+ > = Node extends { readonly type: "parallel"; readonly states: infer Children extends StateSchemas } ?
6616
+ StateInitializeParallelBuilder<Children>
6617
+ : Node extends { readonly initial: infer Initial } ? StateInitializeCompoundBuilder<Node, Initial & PropertyKey>
6618
+ : never
6619
+
6620
+ /**
6621
+ * Context passed to an implicit child-state initializer.
6622
+ *
6623
+ * @category models
6624
+ * @since 0.13.0
6625
+ */
6626
+ export type StateInitializeContext<
6627
+ States extends StateSchemas,
6628
+ Events extends ReadonlyArray<TaggedSchema>,
6629
+ Emits extends ReadonlyArray<TaggedSchema>,
6630
+ StateId extends StateIdentifier<States>,
6631
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
6632
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
6633
+ > = StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents> & {
6634
+ readonly builder: StateInitializeBuilder<States, StateId>
6635
+ }
6636
+
6637
+ /**
6638
+ * Initial child value implementation for a compound or parallel state.
6639
+ *
6640
+ * @category models
6641
+ * @since 0.13.0
6642
+ */
6643
+ export type StateInitializeHandler<
6644
+ States extends StateSchemas,
6645
+ Events extends ReadonlyArray<TaggedSchema>,
6646
+ Emits extends ReadonlyArray<TaggedSchema>,
6647
+ StateId extends StateIdentifier<States>,
6648
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
6649
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
6650
+ > = (
6651
+ context: StateInitializeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
6652
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6653
+ ) => SnapshotBuilderComplete<StateInitializeValue<States, StateId>, boolean>
6654
+
6655
+ /** Context used only when a history node has no previously captured record. */
6656
+ export interface HistoryDefaultContext<
6657
+ States extends StateSchemas,
6658
+ Events extends ReadonlyArray<TaggedSchema>,
6659
+ Emits extends ReadonlyArray<TaggedSchema>,
6155
6660
  ParentId extends StateIdentifier<States>
6156
6661
  > {
6157
6662
  readonly event: LifecycleEvent<Events>
@@ -6304,55 +6809,6 @@ export declare namespace Machine {
6304
6809
  : Path extends keyof Config ? Config[Path]
6305
6810
  : never
6306
6811
 
6307
- type HandlerInvokeContextAtPath<
6308
- AllStates extends StateSchemas,
6309
- Events extends ReadonlyArray<TaggedSchema>,
6310
- InputEvents extends ReadonlyArray<TaggedSchema>,
6311
- Emits extends ReadonlyArray<TaggedSchema>,
6312
- ParentEvents extends ReadonlyArray<TaggedSchema>,
6313
- Config,
6314
- StateId extends StateNodeIdentifier<AllStates>,
6315
- NodeConfig = HandlerConfigAtPath<Config, StateId>
6316
- > = StateId extends StateIdentifier<AllStates> ?
6317
- NodeConfig extends { readonly invoke: infer Invoke } ? HandlerValidationAtPath<
6318
- StateId,
6319
- {
6320
- readonly invoke: ContextualInvokeDefinition<
6321
- AllStates,
6322
- Events,
6323
- Emits,
6324
- StateId,
6325
- InputEvents,
6326
- ParentEvents,
6327
- Invoke
6328
- >
6329
- }
6330
- >
6331
- : unknown
6332
- : unknown
6333
-
6334
- type HandlerInvokeContexts<
6335
- AllStates extends StateSchemas,
6336
- Events extends ReadonlyArray<TaggedSchema>,
6337
- InputEvents extends ReadonlyArray<TaggedSchema>,
6338
- Emits extends ReadonlyArray<TaggedSchema>,
6339
- ParentEvents extends ReadonlyArray<TaggedSchema>,
6340
- Config
6341
- > = Types.UnionToIntersection<
6342
- StateNodeIdentifier<AllStates> extends infer StateId extends StateNodeIdentifier<AllStates> ?
6343
- StateId extends StateNodeIdentifier<AllStates> ? HandlerInvokeContextAtPath<
6344
- AllStates,
6345
- Events,
6346
- InputEvents,
6347
- Emits,
6348
- ParentEvents,
6349
- Config,
6350
- StateId
6351
- >
6352
- : never
6353
- : never
6354
- >
6355
-
6356
6812
  // Rebuild the public nested handler shape so branded validation errors stay
6357
6813
  // attached to the exact property that introduced them.
6358
6814
  type HandlerValidationAtPath<Path extends string, Validation> = Path extends `${infer Head}.${infer Rest}` ? {
@@ -6952,7 +7408,6 @@ export declare namespace Machine {
6952
7408
  >(
6953
7409
  config:
6954
7410
  & Config
6955
- & HandlerInvokeContexts<States, Events, InputEvents, Emits, ParentEvents, NoInfer<Config>>
6956
7411
  & HandlerTreeValidation<
6957
7412
  States,
6958
7413
  Events,
@@ -7022,7 +7477,8 @@ export declare namespace Machine {
7022
7477
  Emits,
7023
7478
  StateId,
7024
7479
  HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
7025
- true
7480
+ true,
7481
+ TransitionAcceptance
7026
7482
  >
7027
7483
  >
7028
7484
  >
@@ -7050,20 +7506,24 @@ export declare namespace Machine {
7050
7506
  context: StateActionContext<States, Events, Emits, StateId>,
7051
7507
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
7052
7508
  ) => StateActionResult<E, R>
7053
- readonly invoke?: InvokeDefinition<States, Events, Emits, StateId>
7509
+ readonly invoke?: StoredInvokeDefinition<States, Events, Emits, StateId>
7054
7510
  readonly always?: TransitionConfig<
7055
7511
  States,
7056
7512
  Events,
7057
7513
  Emits,
7058
7514
  StateId,
7059
- AlwaysContext<States, Events, Emits, StateId>
7515
+ AlwaysContext<States, Events, Emits, StateId>,
7516
+ false,
7517
+ TransitionAcceptance
7060
7518
  >
7061
7519
  readonly onDone?: TransitionConfig<
7062
7520
  States,
7063
7521
  Events,
7064
7522
  Emits,
7065
7523
  StateId,
7066
- DoneContext<States, Events, Emits, StateId>
7524
+ DoneContext<States, Events, Emits, StateId>,
7525
+ false,
7526
+ TransitionAcceptance
7067
7527
  >
7068
7528
  readonly output?:
7069
7529
  | ((context: FinalOutputContext<States, Events, StateId>) => unknown)
@@ -7094,15 +7554,49 @@ export declare namespace Machine {
7094
7554
  >
7095
7555
  }
7096
7556
 
7557
+ type StateSource = Machine.DefinedStates<any> | Machine.Any
7558
+
7559
+ type StateSchemasOf<Source extends StateSource> = Source extends Machine.DefinedStates<infer States> ? States
7560
+ : Source extends Machine.Any ? Machine.States<Source>
7561
+ : never
7562
+
7097
7563
  /**
7098
- * Extracts the complete logical snapshot represented by a state definition.
7564
+ * Extracts the complete logical snapshot represented by a state definition or
7565
+ * machine.
7099
7566
  *
7100
7567
  * @category utility types
7101
7568
  * @since 0.15.0
7102
7569
  */
7103
- export type Snapshot<Defined extends Machine.DefinedStates<any>> = Defined extends Machine.DefinedStates<infer States>
7104
- ? Machine.Snapshot<States>
7105
- : never
7570
+ export type Snapshot<Source extends StateSource> = Machine.Snapshot<StateSchemasOf<Source>>
7571
+
7572
+ /**
7573
+ * Extracts the decoded value owned by a schema-backed state path.
7574
+ *
7575
+ * The source may be the object returned by {@link states} or a machine
7576
+ * definition. Control-only state paths are intentionally excluded.
7577
+ *
7578
+ * @category utility types
7579
+ * @since 0.18.0
7580
+ */
7581
+ export type Value<
7582
+ Source extends StateSource,
7583
+ Path extends Machine.ValuedStateIdentifier<StateSchemasOf<Source>>
7584
+ > = Machine.StateByIdentifier<StateSchemasOf<Source>, Path>
7585
+
7586
+ /**
7587
+ * Extracts the logical snapshot rooted at a state path.
7588
+ *
7589
+ * The source may be the object returned by {@link states} or a machine
7590
+ * definition. This is the type-level counterpart of
7591
+ * `DefinedStates.getSnapshot`.
7592
+ *
7593
+ * @category utility types
7594
+ * @since 0.18.0
7595
+ */
7596
+ export type SnapshotAt<
7597
+ Source extends StateSource,
7598
+ Path extends Machine.StateIdentifier<StateSchemasOf<Source>>
7599
+ > = Machine.SnapshotByIdentifier<StateSchemasOf<Source>, Path>
7106
7600
 
7107
7601
  /**
7108
7602
  * Returns `true` if a value is a `Machine`.
@@ -7220,10 +7714,7 @@ export const state: StateConstructor = internal.state as StateConstructor
7220
7714
  * Machine.make({
7221
7715
  * states: States.states,
7222
7716
  * events: Machine.events(),
7223
- * initial: {
7224
- * target: (to) => to.idle(),
7225
- * resolve: ({ target }) => target.from()
7226
- * }
7717
+ * initial: (to) => to.idle().resolve(({ target }) => target.from())
7227
7718
  * })
7228
7719
  * ```
7229
7720
  *
@@ -7232,20 +7723,6 @@ export const state: StateConstructor = internal.state as StateConstructor
7232
7723
  */
7233
7724
  export const states: StatesConstructor = internal.states
7234
7725
 
7235
- type InitialDirectInput<
7236
- States extends Machine.StateSchemas,
7237
- Input,
7238
- Selection extends Machine.TargetSelection<any, any, any>
7239
- > = {
7240
- readonly target: (to: Machine.InitialSelector<States>) => Selection
7241
- readonly resolve?: (context: {
7242
- readonly input: Input
7243
- readonly target: Machine.SelectionBuilder<Selection>
7244
- }) => Machine.SelectedTargetResult<Selection>
7245
- readonly cases?: never
7246
- readonly otherwise?: never
7247
- }
7248
-
7249
7726
  type MakeConfig<
7250
7727
  States extends Machine.StateSchemas,
7251
7728
  InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
@@ -7254,7 +7731,7 @@ type MakeConfig<
7254
7731
  InitialE,
7255
7732
  InitialR,
7256
7733
  InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
7257
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>
7734
+ ParentDeclaration extends Parent.Any | undefined
7258
7735
  > = {
7259
7736
  readonly id?: string
7260
7737
  readonly states: States & DefineStateTreeInput<NoInfer<States>>
@@ -7268,7 +7745,7 @@ type MakeConfig<
7268
7745
  NoInfer<InternalEvents>
7269
7746
  >
7270
7747
  readonly emittedEvents?: Machine.EventProtocol<"emitted", Emits>
7271
- readonly parentEvents?: Machine.EventProtocol<"public", ParentEvents>
7748
+ readonly parent?: ParentDeclaration
7272
7749
  readonly input?: Input
7273
7750
  readonly initial: unknown
7274
7751
  }
@@ -7281,7 +7758,7 @@ type MakeResult<
7281
7758
  InitialE,
7282
7759
  InitialR,
7283
7760
  InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
7284
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>
7761
+ ParentDeclaration extends Parent.Any | undefined
7285
7762
  > = Definition<
7286
7763
  States,
7287
7764
  readonly [...InputEvents, ...InternalEvents],
@@ -7292,7 +7769,7 @@ type MakeResult<
7292
7769
  Machine.TerminalOutput<States>,
7293
7770
  Emits,
7294
7771
  InputEvents,
7295
- ParentEvents
7772
+ Machine.ParentEventsOf<ParentDeclaration>
7296
7773
  >
7297
7774
 
7298
7775
  interface Make {
@@ -7304,17 +7781,16 @@ interface Make {
7304
7781
  InitialE = never,
7305
7782
  InitialR = never,
7306
7783
  const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
7307
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
7308
- const InitialSelection extends Machine.TargetSelection<any, any, any> = Machine.TargetSelection<any, any, any>
7784
+ const ParentDeclaration extends Parent.Any | undefined = undefined
7309
7785
  >(
7310
7786
  config:
7311
7787
  & Omit<
7312
- MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>,
7788
+ MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentDeclaration>,
7313
7789
  "initial"
7314
7790
  >
7315
- & { readonly initial: InitialDirectInput<States, Input["Type"], InitialSelection> },
7791
+ & { readonly initial: Machine.InitialBuilderInput<States, Input["Type"]> },
7316
7792
  ..._validation: ValidateDefinedStates<NoInfer<States>>
7317
- ): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>
7793
+ ): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentDeclaration>
7318
7794
  <
7319
7795
  const States extends Machine.StateSchemas,
7320
7796
  const InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
@@ -7323,10 +7799,13 @@ interface Make {
7323
7799
  InitialE = never,
7324
7800
  InitialR = never,
7325
7801
  const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
7326
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
7802
+ const ParentDeclaration extends Parent.Any | undefined = undefined
7327
7803
  >(
7328
7804
  config:
7329
- & Omit<MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>, "states">
7805
+ & Omit<
7806
+ MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentDeclaration>,
7807
+ "states"
7808
+ >
7330
7809
  & { readonly states: InvalidDefinedStateTreeInput<States> }
7331
7810
  ): never
7332
7811
  }
@@ -7342,11 +7821,17 @@ interface Make {
7342
7821
  * `states` or is passed inline. Call `handle` on the returned definition
7343
7822
  * to implement state behavior with ordinary TypeScript control flow.
7344
7823
  *
7824
+ * `initial` is a target-first callback. Its outer selector runs once while the
7825
+ * definition is captured; an attached `.resolve(...)` callback remains lazy
7826
+ * until initial planning. Return a bare selected state when its schema supports
7827
+ * default construction.
7828
+ *
7345
7829
  * `Machine.events` defines the public input protocol. `Machine.internalEvents`
7346
7830
  * adds raised events and other machine-local deliveries.
7347
- * `Machine.emittedEvents` defines outward ephemeral notifications, while
7348
- * `parentEvents` declares the inputs a child may explicitly send to its owning
7349
- * machine. All descriptors expose deferred constructors while retaining their
7831
+ * `Machine.emittedEvents` defines outward ephemeral notifications. The
7832
+ * `parent` configuration accepts `Machine.parent(events)` for a required owner
7833
+ * or `Machine.optionalParent(events)` for a root-capable machine. All
7834
+ * descriptors expose deferred constructors while retaining their
7350
7835
  * schemas opaquely for runtime validation. Public and internal tags must be
7351
7836
  * disjoint.
7352
7837
  *
@@ -7370,18 +7855,13 @@ interface Make {
7370
7855
  * const counter = Machine.make({
7371
7856
  * states: States.states,
7372
7857
  * events: Events,
7373
- * initial: {
7374
- * target: (to) => to.Count(),
7375
- * resolve: ({ target }) => target(new Count({ value: 0 }))
7376
- * }
7858
+ * initial: (to) => to.Count().resolve(({ target }) => target(new Count({ value: 0 })))
7377
7859
  * }).handle({
7378
7860
  * Count: {
7379
7861
  * on: {
7380
- * Increment: Machine.transition({
7381
- * target: (to) => to.full.Count(),
7382
- * resolve: ({ event, state, target }) =>
7383
- * target(new Count({ value: state.value + event.by }))
7384
- * })
7862
+ * Increment: (to) =>
7863
+ * to.full.Count().resolve(({ event, state, target }) =>
7864
+ * target(new Count({ value: state.value + event.by })))
7385
7865
  * }
7386
7866
  * }
7387
7867
  * })
@@ -7408,1336 +7888,310 @@ export type EventOf<Protocol extends Machine.EventProtocol.Any> = Machine.EventO
7408
7888
  * statically finite configured event tag.
7409
7889
  *
7410
7890
  * Constructor inputs retain their schema-derived required fields, defaults,
7411
- * and transformations. Construction is deferred until delivery so failures
7412
- * enter the machine's `MachineSchemaDecodeError` channel rather than throwing
7413
- * at the call site.
7414
- *
7415
- * **Example**
7416
- *
7417
- * ```ts
7418
- * export const Event = Machine.events(
7419
- * Schema.TaggedUnion({
7420
- * Increment: { by: Schema.Number },
7421
- * Reset: {}
7422
- * })
7423
- * )
7424
- * const machine = Machine.make({ events: Event, ... })
7425
- * yield* ref.send(Event.Increment({ by: 1 }))
7426
- * ```
7427
- *
7428
- * @category constructors
7429
- * @since 0.10.0
7430
- */
7431
- export const events: {
7432
- <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
7433
- ...schemas: Schemas & ValidateInputEventProtocol<NoInfer<Schemas>>
7434
- ): Machine.EventProtocol<"public", readonly [...Schemas]>
7435
- <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"public">>>(
7436
- ...inputs: Inputs & ValidateEventProtocolBuilder<"public", Inputs>
7437
- ): Machine.EventProtocol<"public", Machine.EventProtocolInputSchemasOf<"public", Inputs>>
7438
- } = internal.events as any
7439
-
7440
- /**
7441
- * Defines an internal event protocol and returns deferred constructors for
7442
- * every statically finite configured event tag.
7443
- *
7444
- * Use these constructors for raised events and other machine-local deliveries.
7445
- * Construction failures are reported through the
7446
- * owning machine's `MachineSchemaDecodeError` channel.
7447
- *
7448
- * **Example**
7449
- *
7450
- * ```ts
7451
- * const Internal = Machine.internalEvents(
7452
- * Schema.TaggedUnion({
7453
- * Loaded: { value: Schema.String },
7454
- * Failed: { message: Schema.String }
7455
- * })
7456
- * )
7457
- * const machine = Machine.make({ internalEvents: Internal, ... })
7458
- *
7459
- * // Inside a transition callback:
7460
- * enqueue.raise(Internal.Loaded({ value }))
7461
- * ```
7462
- *
7463
- * @category constructors
7464
- * @since 0.10.0
7465
- */
7466
- export const internalEvents: {
7467
- <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
7468
- ...schemas: Schemas & ValidateInternalEventProtocol<readonly [], NoInfer<Schemas>>
7469
- ): Machine.EventProtocol<"internal", readonly [...Schemas]>
7470
- <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"internal">>>(
7471
- ...inputs: Inputs & ValidateEventProtocolBuilder<"internal", Inputs>
7472
- ): Machine.EventProtocol<"internal", Machine.EventProtocolInputSchemasOf<"internal", Inputs>>
7473
- } = internal.internalEvents as any
7474
-
7475
- /**
7476
- * Defines the ephemeral notifications a machine may publish to external
7477
- * observers. Emitted events are separate from machine input and are never sent
7478
- * implicitly to a parent machine. Observe them through `MachineRef.emissions` or
7479
- * the AtomMachine emission stream adapters.
7480
- *
7481
- * **Example**
7482
- *
7483
- * ```ts
7484
- * const Emitted = Machine.emittedEvents(
7485
- * Schema.TaggedUnion({
7486
- * Saved: { id: Schema.String }
7487
- * })
7488
- * )
7489
- * const machine = Machine.make({ emittedEvents: Emitted, ... })
7490
- * ```
7491
- *
7492
- * @category constructors
7493
- * @since 0.10.0
7494
- */
7495
- export const emittedEvents: {
7496
- <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
7497
- ...schemas: Schemas & ValidateEmittedEventProtocol<NoInfer<Schemas>>
7498
- ): Machine.EventProtocol<"emitted", readonly [...Schemas]>
7499
- <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"emitted">>>(
7500
- ...inputs: Inputs & ValidateEventProtocolBuilder<"emitted", Inputs>
7501
- ): Machine.EventProtocol<"emitted", Machine.EventProtocolInputSchemasOf<"emitted", Inputs>>
7502
- } = internal.emittedEvents as any
7503
-
7504
- /**
7505
- * Encodes a decoded machine snapshot into a normalized data representation.
7506
- *
7507
- * **When to use**
7508
- *
7509
- * Use when you need to store or transport a statechart snapshot independently
7510
- * of its local machine runtime.
7511
- *
7512
- * **Details**
7513
- *
7514
- * Each active state value and completed output is encoded with the schema
7515
- * declared for its state path. The result contains no process-local runtime
7516
- * state.
7517
- *
7518
- * **Gotchas**
7519
- *
7520
- * The encoded snapshot does not contain the machine definition, machine
7521
- * version, running children, invoked process state, services, or subscriptions.
7522
- * Store machine identity and migration metadata alongside the result when the
7523
- * snapshot crosses deployment versions. Schema encoding does not by itself
7524
- * guarantee JSON-compatible values; schemas used with JSON-backed storage must
7525
- * have JSON-compatible encoded representations.
7526
- *
7527
- * **Example**
7528
- *
7529
- * ```ts
7530
- * import { Effect, Schema } from "effect"
7531
- * import { Machine } from "@typeonce/effect-machine"
7532
- *
7533
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
7534
- * const States = Machine.states({ Idle })
7535
- * const machine = Machine.make({
7536
- * states: States.states,
7537
- * events: Machine.events(),
7538
- * initial: {
7539
- * target: (to) => to.Idle(),
7540
- * resolve: ({ target }) => target.from()
7541
- * }
7542
- * }).handle({ Idle: {} })
7543
- *
7544
- * const encoded = Effect.gen(function*() {
7545
- * const initial = yield* Machine.planInitial(machine)
7546
- * return yield* Machine.encodeSnapshot(machine, initial.state)
7547
- * })
7548
- * ```
7549
- *
7550
- * @see {@link decodeSnapshot} for restoring an encoded snapshot.
7551
- * @category encoding
7552
- * @since 0.4.0
7553
- */
7554
- export const encodeSnapshot: <
7555
- const States extends Machine.StateSchemas,
7556
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
7557
- const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
7558
- const Input extends Schema.Top = typeof Schema.Void,
7559
- UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
7560
- E = never,
7561
- R = never,
7562
- InitialE = never,
7563
- InitialR = never,
7564
- FinalStates extends Machine.StateIdentifier<States> = never,
7565
- Output = never,
7566
- OutputStates extends Machine.StateIdentifier<States> = never,
7567
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
7568
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
7569
- >(
7570
- machine: Machine<
7571
- States,
7572
- Events,
7573
- Input,
7574
- UnhandledStates,
7575
- E,
7576
- R,
7577
- InitialE,
7578
- InitialR,
7579
- FinalStates,
7580
- Output,
7581
- Emits,
7582
- OutputStates,
7583
- InputEvents,
7584
- ParentEvents
7585
- >,
7586
- snapshot: Machine.Snapshot<States>
7587
- ) => Effect.Effect<
7588
- Machine.EncodedSnapshot,
7589
- MachineSchemaEncodeError,
7590
- Machine.SnapshotEncodingServices<States>
7591
- > = internal.encodeSnapshot as any
7592
-
7593
- /**
7594
- * Decodes a normalized data representation into a validated machine snapshot.
7595
- *
7596
- * **When to use**
7597
- *
7598
- * Use when you need to resume planning from a snapshot loaded from storage or
7599
- * received over a transport boundary.
7600
- *
7601
- * **Details**
7602
- *
7603
- * Decoding resolves every path against the supplied machine, decodes values
7604
- * with their state and output schemas, validates compound and parallel state
7605
- * relationships, and rebuilds the recursive in-memory snapshot.
7606
- *
7607
- * **Gotchas**
7608
- *
7609
- * Decoding restores logical statechart data only. It does not restart invoked
7610
- * processes, recreate spawned children, or restore a previous `MachineRef`.
7611
- *
7612
- * **Example**
7613
- *
7614
- * ```ts
7615
- * import { Effect, Schema } from "effect"
7616
- * import { Machine } from "@typeonce/effect-machine"
7617
- *
7618
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
7619
- * const States = Machine.states({ Idle })
7620
- * const machine = Machine.make({
7621
- * states: States.states,
7622
- * events: Machine.events(),
7623
- * initial: {
7624
- * target: (to) => to.Idle(),
7625
- * resolve: ({ target }) => target.from()
7626
- * }
7627
- * }).handle({ Idle: {} })
7628
- *
7629
- * const roundTrip = Effect.gen(function*() {
7630
- * const initial = yield* Machine.planInitial(machine)
7631
- * const encoded = yield* Machine.encodeSnapshot(machine, initial.state)
7632
- * return yield* Machine.decodeSnapshot(machine, encoded)
7633
- * })
7634
- * ```
7635
- *
7636
- * @see {@link encodeSnapshot} for creating the normalized representation.
7637
- * @category decoding
7638
- * @since 0.4.0
7639
- */
7640
- export const decodeSnapshot: <
7641
- const States extends Machine.StateSchemas,
7642
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
7643
- const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
7644
- const Input extends Schema.Top = typeof Schema.Void,
7645
- UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
7646
- E = never,
7647
- R = never,
7648
- InitialE = never,
7649
- InitialR = never,
7650
- FinalStates extends Machine.StateIdentifier<States> = never,
7651
- Output = never,
7652
- OutputStates extends Machine.StateIdentifier<States> = never,
7653
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
7654
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
7655
- >(
7656
- machine: Machine<
7657
- States,
7658
- Events,
7659
- Input,
7660
- UnhandledStates,
7661
- E,
7662
- R,
7663
- InitialE,
7664
- InitialR,
7665
- FinalStates,
7666
- Output,
7667
- Emits,
7668
- OutputStates,
7669
- InputEvents,
7670
- ParentEvents
7671
- >,
7672
- encoded: unknown
7673
- ) => Effect.Effect<
7674
- Machine.Snapshot<States>,
7675
- MachineSchemaDecodeError,
7676
- Machine.SnapshotDecodingServices<States>
7677
- > = internal.decodeSnapshot as any
7678
-
7679
- type EffectInvokeSource<
7680
- States extends Machine.StateSchemas,
7681
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7682
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7683
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7684
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7685
- StateId extends Machine.StateIdentifier<States>,
7686
- Source extends (
7687
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
7688
- ) => Effect.Effect<unknown, unknown, unknown>
7689
- > = {
7690
- readonly id: InvokeLifecycleId
7691
- readonly effect: Source
7692
- readonly stream?: never
7693
- readonly after?: never
7694
- readonly logic?: never
7695
- readonly child?: never
7696
- readonly address?: never
7697
- readonly onElement?: never
7698
- readonly onSnapshot?: never
7699
- }
7700
-
7701
- type EffectDoneHandler<
7702
- States extends Machine.StateSchemas,
7703
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7704
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7705
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7706
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7707
- StateId extends Machine.StateIdentifier<States>,
7708
- Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
7709
- > = Machine.InvokeTransition<
7710
- States,
7711
- Events,
7712
- Emits,
7713
- StateId,
7714
- Machine.InvokeDoneContext<
7715
- States,
7716
- Events,
7717
- Emits,
7718
- StateId,
7719
- Effect.Success<ReturnType<NoInfer<Source>>>,
7720
- InputEvents,
7721
- ParentEvents
7722
- >
7723
- >
7724
-
7725
- type EffectFailureHandler<
7726
- States extends Machine.StateSchemas,
7727
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7728
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7729
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7730
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7731
- StateId extends Machine.StateIdentifier<States>,
7732
- Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
7733
- > = Machine.InvokeTransition<
7734
- States,
7735
- Events,
7736
- Emits,
7737
- StateId,
7738
- Machine.InvokeFailureContext<
7739
- States,
7740
- Events,
7741
- Emits,
7742
- StateId,
7743
- Effect.Error<ReturnType<NoInfer<Source>>>,
7744
- InputEvents,
7745
- ParentEvents
7746
- >
7747
- >
7748
-
7749
- type EffectInvokeResult<
7750
- States extends Machine.StateSchemas,
7751
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7752
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7753
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7754
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7755
- StateId extends Machine.StateIdentifier<States>,
7756
- Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
7757
- > =
7758
- & Machine.InvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
7759
- & Machine.InvokeTyped<
7760
- Effect.Success<ReturnType<Source>>,
7761
- Effect.Error<ReturnType<Source>>,
7762
- Effect.Services<ReturnType<Source>>,
7763
- never
7764
- >
7765
-
7766
- type StreamInvokeSource<
7767
- States extends Machine.StateSchemas,
7768
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7769
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7770
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7771
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7772
- StateId extends Machine.StateIdentifier<States>,
7773
- Source extends (...args: ReadonlyArray<any>) => unknown
7774
- > = {
7775
- readonly id: InvokeLifecycleId
7776
- readonly stream:
7777
- & Source
7778
- & ((context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>) => unknown)
7779
- readonly effect?: never
7780
- readonly after?: never
7781
- readonly logic?: never
7782
- readonly child?: never
7783
- readonly address?: never
7784
- readonly onSnapshot?: never
7785
- }
7786
-
7787
- type StreamSourceResult<Source extends (...args: ReadonlyArray<any>) => unknown> = ReturnType<Source> extends
7788
- infer Result extends Stream.Stream<any, any, any> ? Result : never
7789
-
7790
- type StreamElementHandler<
7791
- States extends Machine.StateSchemas,
7792
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7793
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7794
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7795
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7796
- StateId extends Machine.StateIdentifier<States>,
7797
- Source extends (...args: ReadonlyArray<any>) => unknown
7798
- > = Machine.InvokeTransition<
7799
- States,
7800
- Events,
7801
- Emits,
7802
- StateId,
7803
- Machine.InvokeElementContext<
7804
- States,
7805
- Events,
7806
- Emits,
7807
- StateId,
7808
- Stream.Success<StreamSourceResult<NoInfer<Source>>>,
7809
- InputEvents,
7810
- ParentEvents
7811
- >
7812
- >
7813
-
7814
- type StreamDoneHandler<
7815
- States extends Machine.StateSchemas,
7816
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7817
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7818
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7819
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7820
- StateId extends Machine.StateIdentifier<States>
7821
- > = Machine.InvokeTransition<
7822
- States,
7823
- Events,
7824
- Emits,
7825
- StateId,
7826
- Machine.InvokeDoneContext<States, Events, Emits, StateId, void, InputEvents, ParentEvents>
7827
- >
7828
-
7829
- type StreamFailureHandler<
7830
- States extends Machine.StateSchemas,
7831
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7832
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7833
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7834
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7835
- StateId extends Machine.StateIdentifier<States>,
7836
- Source extends (...args: ReadonlyArray<any>) => unknown
7837
- > = Machine.InvokeTransition<
7838
- States,
7839
- Events,
7840
- Emits,
7841
- StateId,
7842
- Machine.InvokeFailureContext<
7843
- States,
7844
- Events,
7845
- Emits,
7846
- StateId,
7847
- Stream.Error<StreamSourceResult<NoInfer<Source>>>,
7848
- InputEvents,
7849
- ParentEvents
7850
- >
7851
- >
7852
-
7853
- type StreamInvokeResult<
7854
- States extends Machine.StateSchemas,
7855
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7856
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7857
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7858
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7859
- StateId extends Machine.StateIdentifier<States>,
7860
- Source extends (...args: ReadonlyArray<any>) => unknown
7861
- > =
7862
- & Machine.InvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
7863
- & Machine.InvokeTyped<
7864
- void,
7865
- Stream.Error<StreamSourceResult<Source>>,
7866
- Stream.Services<StreamSourceResult<Source>>,
7867
- never
7868
- >
7869
-
7870
- type InvokeChannelIsNever<Value> = IsAny<Value> extends true ? false : [Value] extends [never] ? true : false
7871
-
7872
- type BoundEffectInvokeSource<
7873
- States extends Machine.StateSchemas,
7874
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7875
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7876
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7877
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7878
- StateId extends Machine.StateIdentifier<States>,
7879
- Source extends (
7880
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
7881
- ) => Effect.Effect<unknown, unknown, unknown>
7882
- > = {
7883
- readonly id: InvokeLifecycleId
7884
- readonly effect: Source
7885
- readonly stream?: never
7886
- readonly after?: never
7887
- readonly logic?: never
7888
- readonly child?: never
7889
- readonly address?: never
7890
- readonly onElement?: never
7891
- readonly onSnapshot?: never
7892
- }
7893
-
7894
- type BoundEffectDoneHandler<
7895
- States extends Machine.StateSchemas,
7896
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7897
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7898
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7899
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7900
- StateId extends Machine.StateIdentifier<States>,
7901
- Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
7902
- > = Machine.InvokeTransition<
7903
- States,
7904
- Events,
7905
- Emits,
7906
- StateId,
7907
- Machine.InvokeDoneContext<
7908
- States,
7909
- Events,
7910
- Emits,
7911
- StateId,
7912
- Effect.Success<ReturnType<NoInfer<Source>>>,
7913
- InputEvents,
7914
- ParentEvents
7915
- >
7916
- >
7917
-
7918
- type BoundEffectFailureHandler<
7919
- States extends Machine.StateSchemas,
7920
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7921
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7922
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7923
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7924
- StateId extends Machine.StateIdentifier<States>,
7925
- Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
7926
- > = Machine.InvokeTransition<
7927
- States,
7928
- Events,
7929
- Emits,
7930
- StateId,
7931
- Machine.InvokeFailureContext<
7932
- States,
7933
- Events,
7934
- Emits,
7935
- StateId,
7936
- Effect.Error<ReturnType<NoInfer<Source>>>,
7937
- InputEvents,
7938
- ParentEvents
7939
- >
7940
- >
7941
-
7942
- type BoundEffectInvokeResult<
7943
- States extends Machine.StateSchemas,
7944
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7945
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7946
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7947
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>,
7948
- StateId extends Machine.StateIdentifier<States>,
7949
- Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
7950
- > =
7951
- & Machine.InvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
7952
- & Machine.InvokeTyped<
7953
- Effect.Success<ReturnType<Source>>,
7954
- Effect.Error<ReturnType<Source>>,
7955
- Effect.Services<ReturnType<Source>>,
7956
- never
7957
- >
7958
-
7959
- /** Captures one exact named-branch topology while preserving handler inference. */
7960
- type BranchingTransitionResult<
7961
- States extends Machine.StateSchemas,
7962
- Events extends ReadonlyArray<Machine.TaggedSchema>,
7963
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
7964
- StateId extends Machine.StateNodeIdentifier<States>,
7965
- Context,
7966
- Reenter extends boolean,
7967
- Branches extends Readonly<Record<string, Machine.TransitionBranchInput>>
7968
- > =
7969
- & Machine.TransitionBranchesInput<States, Events, Emits, StateId, Context, Reenter, Branches>
7970
- & Machine.TransitionTyped<States, Events, Emits, StateId, Context, Reenter>
7971
-
7972
- type TransitionBranchRecordError<Message extends string, Key extends PropertyKey = never> = {
7973
- readonly "~effect/Machine/TransitionBranchRecordError": Message
7974
- readonly key: Key
7975
- }
7976
-
7977
- type InvalidStaticTransitionBranchKey<Branches> = Extract<keyof Branches, "" | number | symbol>
7978
-
7979
- type ValidateTransitionBranchRecord<Branches> = [keyof Branches] extends [never] ?
7980
- TransitionBranchRecordError<"Branch records must contain at least one branch">
7981
- : [InvalidStaticTransitionBranchKey<Branches>] extends [never] ? unknown
7982
- : TransitionBranchRecordError<
7983
- "Branch keys must be non-empty, non-index strings",
7984
- InvalidStaticTransitionBranchKey<Branches>
7985
- >
7986
-
7987
- /**
7988
- * Contextually types direct and branching transition definitions.
7891
+ * and transformations. Construction is deferred until delivery so failures
7892
+ * enter the machine's `MachineSchemaDecodeError` channel rather than throwing
7893
+ * at the call site.
7894
+ *
7895
+ * **Example**
7896
+ *
7897
+ * ```ts
7898
+ * export const Event = Machine.events(
7899
+ * Schema.TaggedUnion({
7900
+ * Increment: { by: Schema.Number },
7901
+ * Reset: {}
7902
+ * })
7903
+ * )
7904
+ * const machine = Machine.make({ events: Event, ... })
7905
+ * yield* ref.send(Event.Increment({ by: 1 }))
7906
+ * ```
7989
7907
  *
7990
7908
  * @category constructors
7991
- * @since 0.14.0
7909
+ * @since 0.10.0
7992
7910
  */
7993
- export interface TransitionConstructor {
7994
- <
7995
- const States extends Machine.StateSchemas,
7996
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
7997
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
7998
- StateId extends Machine.StateNodeIdentifier<States>,
7999
- Context,
8000
- const Selection extends Machine.TargetSelection<any, any, any>,
8001
- Reenter extends boolean = never
8002
- >(
8003
- config: Machine.TransitionDirectInput<States, Events, Emits, StateId, Context, Reenter, Selection>
8004
- ):
8005
- & Machine.TransitionDirectInput<States, Events, Emits, StateId, Context, Reenter, Selection>
8006
- & (Machine.SelectionKind<Selection> extends "none" ? Machine.TargetlessTransitionTyped
8007
- : Machine.TransitionTyped<States, Events, Emits, StateId, Context, Reenter>)
8008
- <
8009
- const States extends Machine.StateSchemas,
8010
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8011
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8012
- StateId extends Machine.StateNodeIdentifier<States>,
8013
- Context,
8014
- const Branches extends Readonly<Record<string, Machine.TransitionBranchInput>>,
8015
- Reenter extends boolean = never
8016
- >(
8017
- config:
8018
- & Machine.TransitionBranchesInput<
8019
- States,
8020
- Events,
8021
- Emits,
8022
- StateId,
8023
- Context,
8024
- Reenter,
8025
- Branches
8026
- >
8027
- & ValidateTransitionBranchRecord<NoInfer<Branches>>
8028
- ): BranchingTransitionResult<
8029
- States,
8030
- Events,
8031
- Emits,
8032
- StateId,
8033
- Context,
8034
- Reenter,
8035
- Branches
8036
- >
8037
- }
7911
+ export const events: {
7912
+ <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
7913
+ ...schemas: Schemas & ValidateInputEventProtocol<NoInfer<Schemas>>
7914
+ ): Machine.EventProtocol<"public", readonly [...Schemas]>
7915
+ <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"public">>>(
7916
+ ...inputs: Inputs & ValidateEventProtocolBuilder<"public", Inputs>
7917
+ ): Machine.EventProtocol<"public", Machine.EventProtocolInputSchemasOf<"public", Inputs>>
7918
+ } = internal.events as any
8038
7919
 
8039
7920
  /**
8040
- * Defines a statically inspectable transition.
7921
+ * Requires the machine to run as an owned child whose parent accepts the
7922
+ * supplied public event protocol.
8041
7923
  *
8042
- * It is required for every event, lifecycle, choice, and invocation transition
8043
- * so the destination selected by `target` can contextually type the
8044
- * corresponding resolver. Branching transitions declare every possible
8045
- * destination up front, then select one through ordinary TypeScript control
8046
- * flow in `resolve`.
7924
+ * Required-parent machines expose a non-optional `parent` target in behavior
7925
+ * contexts and are rejected by root execution APIs.
8047
7926
  *
8048
- * ```ts
8049
- * Machine.transition({
8050
- * target: (to) => to.full.Ready(),
8051
- * resolve: ({ target }) => target.from()
8052
- * })
7927
+ * @category constructors
7928
+ * @since 0.17.0
7929
+ */
7930
+ export const parent: <const Events extends ReadonlyArray<Machine.TaggedSchema>>(
7931
+ events: Machine.EventProtocol<"public", Events>
7932
+ ) => Parent<"required", Events> = internal.parent
7933
+
7934
+ /**
7935
+ * Declares public events a machine may send to its owner while preserving the
7936
+ * ability to run that machine as a root.
8053
7937
  *
8054
- * Machine.transition({
8055
- * branches: (to) => ({
8056
- * cached: { target: to.full.Ready() },
8057
- * loading: { target: to.full.Loading() }
8058
- * }),
8059
- * resolve: ({ event, select }) => event.cached
8060
- * ? select.cached.from({ data: event.cached })
8061
- * : select.loading.from()
8062
- * })
8063
- * ```
7938
+ * Optional-parent machines expose `parent` as a possibly absent target.
8064
7939
  *
8065
7940
  * @category constructors
8066
- * @since 0.14.0
7941
+ * @since 0.17.0
8067
7942
  */
8068
- export const transition: TransitionConstructor = ((config: unknown) => config) as TransitionConstructor
7943
+ export const optionalParent: <const Events extends ReadonlyArray<Machine.TaggedSchema>>(
7944
+ events: Machine.EventProtocol<"public", Events>
7945
+ ) => Parent<"optional", Events> = internal.optionalParent
8069
7946
 
8070
7947
  /**
8071
- * Sentinel used by direct targetless transition shorthands.
7948
+ * Defines an internal event protocol and returns deferred constructors for
7949
+ * every statically finite configured event tag.
7950
+ *
7951
+ * Use these constructors for raised events and other machine-local deliveries.
7952
+ * Construction failures are reported through the
7953
+ * owning machine's `MachineSchemaDecodeError` channel.
8072
7954
  *
8073
- * This is the concise form of `target: (to) => to.none()`.
7955
+ * **Example**
8074
7956
  *
8075
7957
  * ```ts
8076
- * onElement: {
8077
- * target: Machine.targetless,
8078
- * resolve: ({ element }, enqueue) => {
8079
- * enqueue.raise(new MeasurementReceived({ value: element }))
8080
- * }
8081
- * }
7958
+ * const Internal = Machine.internalEvents(
7959
+ * Schema.TaggedUnion({
7960
+ * Loaded: { value: Schema.String },
7961
+ * Failed: { message: Schema.String }
7962
+ * })
7963
+ * )
7964
+ * const machine = Machine.make({ internalEvents: Internal, ... })
8082
7965
  *
8083
- * onDone: { target: Machine.targetless }
7966
+ * // Inside a transition callback:
7967
+ * enqueue.raise(Internal.Loaded({ value }))
8084
7968
  * ```
8085
7969
  *
8086
7970
  * @category constructors
8087
- * @since 0.16.0
7971
+ * @since 0.10.0
8088
7972
  */
8089
- export interface TargetlessSelector {
8090
- readonly "~effect/Machine/TargetlessSelector": true
8091
- <
8092
- const States extends Machine.StateSchemas,
8093
- StateId extends Machine.StateNodeIdentifier<States>
8094
- >(to: Machine.TargetSelector<States, StateId>): ReturnType<Machine.TargetSelector<States, StateId>["none"]>
8095
- }
7973
+ export const internalEvents: {
7974
+ <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
7975
+ ...schemas: Schemas & ValidateInternalEventProtocol<readonly [], NoInfer<Schemas>>
7976
+ ): Machine.EventProtocol<"internal", readonly [...Schemas]>
7977
+ <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"internal">>>(
7978
+ ...inputs: Inputs & ValidateEventProtocolBuilder<"internal", Inputs>
7979
+ ): Machine.EventProtocol<"internal", Machine.EventProtocolInputSchemasOf<"internal", Inputs>>
7980
+ } = internal.internalEvents as any
8096
7981
 
8097
7982
  /**
8098
- * Selects no transition target while keeping enqueue operations explicit.
7983
+ * Defines the ephemeral notifications a machine may publish to external
7984
+ * observers. Emitted events are separate from machine input and are never sent
7985
+ * implicitly to a parent machine. Observe them through `MachineRef.emissions` or
7986
+ * the AtomMachine emission stream adapters.
7987
+ *
7988
+ * **Example**
8099
7989
  *
8100
- * Use as the `target` of a direct handler object when the handler should keep
8101
- * the current state configuration and only raise, emit, or send events.
7990
+ * ```ts
7991
+ * const Emitted = Machine.emittedEvents(
7992
+ * Schema.TaggedUnion({
7993
+ * Saved: { id: Schema.String }
7994
+ * })
7995
+ * )
7996
+ * const machine = Machine.make({ emittedEvents: Emitted, ... })
7997
+ * ```
8102
7998
  *
8103
7999
  * @category constructors
8104
- * @since 0.16.0
8000
+ * @since 0.10.0
8105
8001
  */
8106
- export const targetless: TargetlessSelector = Object.assign(
8107
- (to: Machine.TargetSelector<any, any>) => to.none(),
8108
- { "~effect/Machine/TargetlessSelector": true as const }
8109
- )
8002
+ export const emittedEvents: {
8003
+ <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
8004
+ ...schemas: Schemas & ValidateEmittedEventProtocol<NoInfer<Schemas>>
8005
+ ): Machine.EventProtocol<"emitted", readonly [...Schemas]>
8006
+ <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"emitted">>>(
8007
+ ...inputs: Inputs & ValidateEventProtocolBuilder<"emitted", Inputs>
8008
+ ): Machine.EventProtocol<"emitted", Machine.EventProtocolInputSchemasOf<"emitted", Inputs>>
8009
+ } = internal.emittedEvents as any
8110
8010
 
8111
8011
  /**
8112
- * Machine-bound invocation constructor that preserves the owning machine's
8113
- * public input and parent protocols in every invocation callback.
8012
+ * Encodes a decoded machine snapshot into a normalized data representation.
8114
8013
  *
8115
- * @category constructors
8116
- * @since 0.11.0
8117
- */
8118
- export interface Invoker<
8119
- States extends Machine.StateSchemas,
8120
- Events extends ReadonlyArray<Machine.TaggedSchema>,
8121
- Emits extends ReadonlyArray<Machine.TaggedSchema>,
8122
- InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
8123
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema>
8124
- > {
8125
- <
8126
- StateId extends Machine.StateIdentifier<States>,
8127
- const Source extends (
8128
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8129
- ) => Effect.Effect<unknown, unknown, unknown>
8130
- >(
8131
- config:
8132
- & BoundEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8133
- & {
8134
- readonly onDone: BoundEffectDoneHandler<
8135
- States,
8136
- Events,
8137
- Emits,
8138
- InputEvents,
8139
- ParentEvents,
8140
- StateId,
8141
- Source
8142
- >
8143
- readonly onFailure: BoundEffectFailureHandler<
8144
- States,
8145
- Events,
8146
- Emits,
8147
- InputEvents,
8148
- ParentEvents,
8149
- StateId,
8150
- Source
8151
- >
8152
- },
8153
- ..._validation: InvokeChannelIsNever<Effect.Success<ReturnType<Source>>> extends true ? [
8154
- "onDone must be omitted when the Effect output is never"
8155
- ]
8156
- : InvokeChannelIsNever<Effect.Error<ReturnType<Source>>> extends true ? [
8157
- "onFailure must be omitted when the Effect error is never"
8158
- ]
8159
- : []
8160
- ): BoundEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8161
- <
8162
- StateId extends Machine.StateIdentifier<States>,
8163
- const Source extends (
8164
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8165
- ) => Effect.Effect<unknown, never, unknown>
8166
- >(
8167
- config:
8168
- & BoundEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8169
- & {
8170
- readonly onDone: BoundEffectDoneHandler<
8171
- States,
8172
- Events,
8173
- Emits,
8174
- InputEvents,
8175
- ParentEvents,
8176
- StateId,
8177
- Source
8178
- >
8179
- readonly onFailure?: never
8180
- },
8181
- ..._validation: InvokeChannelIsNever<Effect.Success<ReturnType<Source>>> extends true ? [
8182
- "onDone must be omitted when the Effect output is never"
8183
- ]
8184
- : []
8185
- ): BoundEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8186
- <
8187
- StateId extends Machine.StateIdentifier<States>,
8188
- const Source extends (
8189
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8190
- ) => Effect.Effect<never, unknown, unknown>
8191
- >(
8192
- config:
8193
- & BoundEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8194
- & {
8195
- readonly onDone?: never
8196
- readonly onFailure: BoundEffectFailureHandler<
8197
- States,
8198
- Events,
8199
- Emits,
8200
- InputEvents,
8201
- ParentEvents,
8202
- StateId,
8203
- Source
8204
- >
8205
- },
8206
- ..._validation: InvokeChannelIsNever<Effect.Error<ReturnType<Source>>> extends true ? [
8207
- "onFailure must be omitted when the Effect error is never"
8208
- ]
8209
- : []
8210
- ): BoundEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8211
- <
8212
- StateId extends Machine.StateIdentifier<States>,
8213
- const Source extends (
8214
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8215
- ) => Effect.Effect<never, never, unknown>
8216
- >(
8217
- config:
8218
- & BoundEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8219
- & {
8220
- readonly onDone?: never
8221
- readonly onFailure?: never
8222
- }
8223
- ): BoundEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8224
- <
8225
- StateId extends Machine.StateIdentifier<States>,
8226
- const Source extends (
8227
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8228
- ) => Stream.Stream<unknown, unknown, any>
8229
- >(
8230
- config:
8231
- & StreamInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8232
- & {
8233
- readonly onElement: StreamElementHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8234
- readonly onDone: StreamDoneHandler<States, Events, Emits, InputEvents, ParentEvents, StateId>
8235
- readonly onFailure: StreamFailureHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8236
- },
8237
- ..._validation: InvokeChannelIsNever<Stream.Success<ReturnType<Source>>> extends true ? [
8238
- "onElement must be omitted when the Stream element is never"
8239
- ]
8240
- : InvokeChannelIsNever<Stream.Error<ReturnType<Source>>> extends true ? [
8241
- "onFailure must be omitted when the Stream error is never"
8242
- ]
8243
- : []
8244
- ): StreamInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8245
- <
8246
- StateId extends Machine.StateIdentifier<States>,
8247
- const Source extends (
8248
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8249
- ) => Stream.Stream<never, unknown, any>
8250
- >(
8251
- config:
8252
- & StreamInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8253
- & {
8254
- readonly onElement?: never
8255
- readonly onDone: StreamDoneHandler<States, Events, Emits, InputEvents, ParentEvents, StateId>
8256
- readonly onFailure: StreamFailureHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8257
- },
8258
- ..._validation: InvokeChannelIsNever<Stream.Error<ReturnType<Source>>> extends true ? [
8259
- "onFailure must be omitted when the Stream error is never"
8260
- ]
8261
- : []
8262
- ): StreamInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8263
- <
8264
- StateId extends Machine.StateIdentifier<States>,
8265
- const Source extends (
8266
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8267
- ) => Stream.Stream<never, never, any>
8268
- >(
8269
- config:
8270
- & StreamInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8271
- & {
8272
- readonly onElement?: never
8273
- readonly onDone: StreamDoneHandler<States, Events, Emits, InputEvents, ParentEvents, StateId>
8274
- readonly onFailure?: never
8275
- }
8276
- ): StreamInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8277
- <
8278
- StateId extends Machine.StateIdentifier<States>,
8279
- const Source extends (
8280
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8281
- ) => Stream.Stream<unknown, never, any>
8282
- >(
8283
- config:
8284
- & StreamInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8285
- & {
8286
- readonly onElement: StreamElementHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8287
- readonly onDone: StreamDoneHandler<States, Events, Emits, InputEvents, ParentEvents, StateId>
8288
- readonly onFailure?: never
8289
- }
8290
- ): StreamInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8291
- <StateId extends Machine.StateIdentifier<States>, const Config extends object>(
8292
- config: Config & Machine.TimerInvokeArgs<States, Events, Emits, StateId, InputEvents, ParentEvents>
8293
- ): Config & Machine.InvokeOwned<States, Events, Emits, StateId> & Machine.InvokeTyped<void, never, never, never>
8294
- <
8295
- StateId extends Machine.StateIdentifier<States>,
8296
- const Source extends (
8297
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8298
- ) => unknown,
8299
- Address extends ChildAddress<never>
8300
- >(
8301
- config:
8302
- & { readonly logic: Source }
8303
- & Machine.LogicInvokeArgs<
8304
- States,
8305
- Events,
8306
- Emits,
8307
- StateId,
8308
- Machine.LogicStateOf<ReturnType<Source>>,
8309
- Machine.LogicEventOf<ReturnType<Source>>,
8310
- Machine.LogicErrorOf<ReturnType<Source>>,
8311
- Machine.LogicServicesOf<ReturnType<Source>>,
8312
- Machine.LogicOutputOf<ReturnType<Source>>,
8313
- Machine.LogicInitialErrorOf<ReturnType<Source>>,
8314
- Address,
8315
- Source,
8316
- InputEvents,
8317
- ParentEvents
8318
- >,
8319
- ..._validation: ReturnType<Source> extends { readonly initial: unknown; readonly run: unknown } ? [] : [
8320
- "logic factory must return Machine.Logic"
8321
- ]
8322
- ):
8323
- & Machine.InvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
8324
- & Machine.InvokeTyped<
8325
- Machine.LogicOutputOf<ReturnType<Source>>,
8326
- Machine.LogicErrorOf<ReturnType<Source>>,
8327
- Machine.LogicServicesOf<ReturnType<Source>>,
8328
- Machine.LogicInitialErrorOf<ReturnType<Source>>
8329
- >
8330
- <
8331
- StateId extends Machine.StateIdentifier<States>,
8332
- const Source,
8333
- Address extends ChildAddress<never>
8334
- >(
8335
- config:
8336
- & { readonly logic: Source }
8337
- & Machine.LogicInvokeArgs<
8338
- States,
8339
- Events,
8340
- Emits,
8341
- StateId,
8342
- Machine.LogicStateOf<Source>,
8343
- Machine.LogicEventOf<Source>,
8344
- Machine.LogicErrorOf<Source>,
8345
- Machine.LogicServicesOf<Source>,
8346
- Machine.LogicOutputOf<Source>,
8347
- Machine.LogicInitialErrorOf<Source>,
8348
- Address,
8349
- Source,
8350
- InputEvents,
8351
- ParentEvents
8352
- >,
8353
- ..._validation: Source extends { readonly initial: unknown; readonly run: unknown } ? [] : [
8354
- "logic must implement Machine.Logic"
8355
- ]
8356
- ):
8357
- & Machine.InvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
8358
- & Machine.InvokeTyped<
8359
- Machine.LogicOutputOf<Source>,
8360
- Machine.LogicErrorOf<Source>,
8361
- Machine.LogicServicesOf<Source>,
8362
- Machine.LogicInitialErrorOf<Source>
8363
- >
8364
- <
8365
- StateId extends Machine.StateIdentifier<States>,
8366
- const Child extends ChildMachine.Any,
8367
- const Config extends object
8368
- >(
8369
- config:
8370
- & Config
8371
- & Machine.ChildInvokeArgs<
8372
- States,
8373
- Events,
8374
- Emits,
8375
- StateId,
8376
- Child["machine"],
8377
- Child,
8378
- InputEvents,
8379
- ParentEvents
8380
- >
8381
- ):
8382
- & Config
8383
- & Machine.InvokeOwned<States, Events, Emits, StateId>
8384
- & Machine.InvokeTyped<
8385
- Machine.Output<Child["machine"]>,
8386
- Machine.Error<Child["machine"]> | ActionError<Machine.Services<Child["machine"]>>,
8387
- Machine.Services<Child["machine"]>,
8388
- Machine.InitialError<Child["machine"]>,
8389
- Machine.Emit<Child["machine"]>,
8390
- Machine.EventOf<Machine.ParentEvents<Child["machine"]>>
8391
- >
8392
- }
8014
+ * **When to use**
8015
+ *
8016
+ * Use when you need to store or transport a statechart snapshot independently
8017
+ * of its local machine runtime.
8018
+ *
8019
+ * **Details**
8020
+ *
8021
+ * Each active state value and completed output is encoded with the schema
8022
+ * declared for its state path. The result contains no process-local runtime
8023
+ * state.
8024
+ *
8025
+ * **Gotchas**
8026
+ *
8027
+ * The encoded snapshot does not contain the machine definition, machine
8028
+ * version, running children, invoked process state, services, or subscriptions.
8029
+ * Store machine identity and migration metadata alongside the result when the
8030
+ * snapshot crosses deployment versions. Schema encoding does not by itself
8031
+ * guarantee JSON-compatible values; schemas used with JSON-backed storage must
8032
+ * have JSON-compatible encoded representations.
8033
+ *
8034
+ * **Example**
8035
+ *
8036
+ * ```ts
8037
+ * import { Effect, Schema } from "effect"
8038
+ * import { Machine } from "@typeonce/effect-machine"
8039
+ *
8040
+ * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
8041
+ * const States = Machine.states({ Idle })
8042
+ * const machine = Machine.make({
8043
+ * states: States.states,
8044
+ * events: Machine.events(),
8045
+ * initial: (to) => to.Idle().resolve(({ target }) => target.from())
8046
+ * }).handle({ Idle: {} })
8047
+ *
8048
+ * const encoded = Effect.gen(function*() {
8049
+ * const initial = yield* Machine.planInitial(machine)
8050
+ * return yield* Machine.encodeSnapshot(machine, initial.state)
8051
+ * })
8052
+ * ```
8053
+ *
8054
+ * @see {@link decodeSnapshot} for restoring an encoded snapshot.
8055
+ * @category encoding
8056
+ * @since 0.4.0
8057
+ */
8058
+ export const encodeSnapshot: <
8059
+ const States extends Machine.StateSchemas,
8060
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
8061
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8062
+ const Input extends Schema.Top = typeof Schema.Void,
8063
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
8064
+ E = never,
8065
+ R = never,
8066
+ InitialE = never,
8067
+ InitialR = never,
8068
+ FinalStates extends Machine.StateIdentifier<States> = never,
8069
+ Output = never,
8070
+ OutputStates extends Machine.StateIdentifier<States> = never,
8071
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
8072
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8073
+ >(
8074
+ machine: Machine<
8075
+ States,
8076
+ Events,
8077
+ Input,
8078
+ UnhandledStates,
8079
+ E,
8080
+ R,
8081
+ InitialE,
8082
+ InitialR,
8083
+ FinalStates,
8084
+ Output,
8085
+ Emits,
8086
+ OutputStates,
8087
+ InputEvents,
8088
+ ParentEvents
8089
+ >,
8090
+ snapshot: Machine.Snapshot<States>
8091
+ ) => Effect.Effect<
8092
+ Machine.EncodedSnapshot,
8093
+ MachineSchemaEncodeError,
8094
+ Machine.SnapshotEncodingServices<States>
8095
+ > = internal.encodeSnapshot as any
8393
8096
 
8394
8097
  /**
8395
- * Preserves inference for a state-owned invocation configuration.
8396
- *
8397
- * Use `effect` for one-shot work, `stream` for repeated values, `after` for a
8398
- * cancellable timer, `logic` for reusable process logic, or `child` for a
8399
- * complete child machine. Stream elements are handled by `onElement` before
8400
- * the next element is pulled. `onDone` is required whenever the source can
8401
- * complete, while `onFailure` is required only when the source has a typed
8402
- * failure channel.
8403
- *
8404
- * This constructor is an identity at runtime, but preserves lifecycle callback
8405
- * inference through published declarations. Effect and Stream factories run
8406
- * when their owning state is entered and infer the owner context, value,
8407
- * output, error, and service channels together without a return annotation.
8408
- * Durations may be
8409
- * supplied directly or derived from the owning state's entry context. Logic
8410
- * invocations require both a lifecycle `id` and a typed communication
8411
- * `address`. Child descriptors already own their identity, so `id` and
8412
- * `address` must not be repeated.
8413
- *
8414
- * Inside `handle(...)`, the owning definition contextually supplies its public
8415
- * input and `parentEvents` protocols. Invocation sources and lifecycle handlers
8416
- * can therefore send through `self` and `parent` without naming the definition.
8417
- * The bound `definition.invoke(...)` constructor remains an equivalent form.
8098
+ * Decodes a normalized data representation into a validated machine snapshot.
8099
+ *
8100
+ * **When to use**
8101
+ *
8102
+ * Use when you need to resume planning from a snapshot loaded from storage or
8103
+ * received over a transport boundary.
8104
+ *
8105
+ * **Details**
8106
+ *
8107
+ * Decoding resolves every path against the supplied machine, decodes values
8108
+ * with their state and output schemas, validates compound and parallel state
8109
+ * relationships, and rebuilds the recursive in-memory snapshot.
8110
+ *
8111
+ * **Gotchas**
8112
+ *
8113
+ * Decoding restores logical statechart data only. It does not restart invoked
8114
+ * processes, recreate spawned children, or restore a previous `MachineRef`.
8115
+ *
8116
+ * **Example**
8418
8117
  *
8419
8118
  * ```ts
8420
- * invoke: Machine.invoke({
8421
- * id: "load",
8422
- * effect: () =>
8423
- * Effect.tryPromise({
8424
- * try: () => fetch("/api/data").then((response) => response.json()),
8425
- * catch: (cause) => new LoadError({ cause })
8426
- * }),
8427
- * onDone: Machine.transition({
8428
- * target: (to) => to.full.Ready(),
8429
- * resolve: ({ output, target }) => target.from({ data: output })
8430
- * }),
8431
- * onFailure: Machine.transition({
8432
- * target: (to) => to.full.Failed(),
8433
- * resolve: ({ error, target }) => target.from({ error })
8434
- * })
8119
+ * import { Effect, Schema } from "effect"
8120
+ * import { Machine } from "@typeonce/effect-machine"
8121
+ *
8122
+ * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
8123
+ * const States = Machine.states({ Idle })
8124
+ * const machine = Machine.make({
8125
+ * states: States.states,
8126
+ * events: Machine.events(),
8127
+ * initial: (to) => to.Idle().resolve(({ target }) => target.from())
8128
+ * }).handle({ Idle: {} })
8129
+ *
8130
+ * const roundTrip = Effect.gen(function*() {
8131
+ * const initial = yield* Machine.planInitial(machine)
8132
+ * const encoded = yield* Machine.encodeSnapshot(machine, initial.state)
8133
+ * return yield* Machine.decodeSnapshot(machine, encoded)
8435
8134
  * })
8436
8135
  * ```
8437
8136
  *
8438
- * @category constructors
8439
- * @since 0.9.0
8137
+ * @see {@link encodeSnapshot} for creating the normalized representation.
8138
+ * @category decoding
8139
+ * @since 0.4.0
8440
8140
  */
8441
- export const invoke: {
8442
- <
8443
- const States extends Machine.StateSchemas,
8444
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8445
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8446
- StateId extends Machine.StateIdentifier<States>,
8447
- const Source extends (
8448
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8449
- ) => Effect.Effect<unknown, unknown, unknown>,
8450
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8451
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8452
- >(
8453
- config:
8454
- & EffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8455
- & {
8456
- readonly onDone: EffectDoneHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8457
- readonly onFailure: EffectFailureHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8458
- },
8459
- ..._validation: InvokeChannelIsNever<Effect.Success<ReturnType<Source>>> extends true ? [
8460
- "onDone must be omitted when the Effect output is never"
8461
- ]
8462
- : InvokeChannelIsNever<Effect.Error<ReturnType<Source>>> extends true ? [
8463
- "onFailure must be omitted when the Effect error is never"
8464
- ]
8465
- : []
8466
- ): EffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8467
- <
8468
- const States extends Machine.StateSchemas,
8469
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8470
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8471
- StateId extends Machine.StateIdentifier<States>,
8472
- const Source extends (
8473
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8474
- ) => Effect.Effect<unknown, never, unknown>,
8475
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8476
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8477
- >(
8478
- config:
8479
- & EffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8480
- & {
8481
- readonly onDone: EffectDoneHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8482
- readonly onFailure?: never
8483
- },
8484
- ..._validation: InvokeChannelIsNever<Effect.Success<ReturnType<Source>>> extends true ? [
8485
- "onDone must be omitted when the Effect output is never"
8486
- ]
8487
- : []
8488
- ): EffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8489
- <
8490
- const States extends Machine.StateSchemas,
8491
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8492
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8493
- StateId extends Machine.StateIdentifier<States>,
8494
- const Source extends (
8495
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8496
- ) => Effect.Effect<never, unknown, unknown>,
8497
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8498
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8499
- >(
8500
- config:
8501
- & EffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8502
- & {
8503
- readonly onDone?: never
8504
- readonly onFailure: EffectFailureHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8505
- },
8506
- ..._validation: InvokeChannelIsNever<Effect.Error<ReturnType<Source>>> extends true ? [
8507
- "onFailure must be omitted when the Effect error is never"
8508
- ]
8509
- : []
8510
- ): EffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8511
- <
8512
- const States extends Machine.StateSchemas,
8513
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8514
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8515
- StateId extends Machine.StateIdentifier<States>,
8516
- const Source extends (
8517
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8518
- ) => Effect.Effect<never, never, unknown>,
8519
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8520
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8521
- >(
8522
- config:
8523
- & EffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8524
- & {
8525
- readonly onDone?: never
8526
- readonly onFailure?: never
8527
- }
8528
- ): EffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8529
- <
8530
- const States extends Machine.StateSchemas,
8531
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8532
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8533
- StateId extends Machine.StateIdentifier<States>,
8534
- const Source extends (
8535
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8536
- ) => Stream.Stream<unknown, unknown, any>,
8537
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8538
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8539
- >(
8540
- config:
8541
- & StreamInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8542
- & {
8543
- readonly onElement: StreamElementHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8544
- readonly onDone: StreamDoneHandler<States, Events, Emits, InputEvents, ParentEvents, StateId>
8545
- readonly onFailure: StreamFailureHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8546
- },
8547
- ..._validation: InvokeChannelIsNever<Stream.Success<ReturnType<Source>>> extends true ? [
8548
- "onElement must be omitted when the Stream element is never"
8549
- ]
8550
- : InvokeChannelIsNever<Stream.Error<ReturnType<Source>>> extends true ? [
8551
- "onFailure must be omitted when the Stream error is never"
8552
- ]
8553
- : []
8554
- ): StreamInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8555
- <
8556
- const States extends Machine.StateSchemas,
8557
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8558
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8559
- StateId extends Machine.StateIdentifier<States>,
8560
- const Source extends (
8561
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8562
- ) => Stream.Stream<never, unknown, any>,
8563
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8564
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8565
- >(
8566
- config:
8567
- & StreamInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8568
- & {
8569
- readonly onElement?: never
8570
- readonly onDone: StreamDoneHandler<States, Events, Emits, InputEvents, ParentEvents, StateId>
8571
- readonly onFailure: StreamFailureHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8572
- },
8573
- ..._validation: InvokeChannelIsNever<Stream.Error<ReturnType<Source>>> extends true ? [
8574
- "onFailure must be omitted when the Stream error is never"
8575
- ]
8576
- : []
8577
- ): StreamInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8578
- <
8579
- const States extends Machine.StateSchemas,
8580
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8581
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8582
- StateId extends Machine.StateIdentifier<States>,
8583
- const Source extends (
8584
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8585
- ) => Stream.Stream<never, never, any>,
8586
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8587
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8588
- >(
8589
- config:
8590
- & StreamInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8591
- & {
8592
- readonly onElement?: never
8593
- readonly onDone: StreamDoneHandler<States, Events, Emits, InputEvents, ParentEvents, StateId>
8594
- readonly onFailure?: never
8595
- }
8596
- ): StreamInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8597
- <
8598
- const States extends Machine.StateSchemas,
8599
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8600
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8601
- StateId extends Machine.StateIdentifier<States>,
8602
- const Source extends (
8603
- context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
8604
- ) => Stream.Stream<unknown, never, any>,
8605
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8606
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8607
- >(
8608
- config:
8609
- & StreamInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8610
- & {
8611
- readonly onElement: StreamElementHandler<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8612
- readonly onDone: StreamDoneHandler<States, Events, Emits, InputEvents, ParentEvents, StateId>
8613
- readonly onFailure?: never
8614
- }
8615
- ): StreamInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
8616
- <
8617
- const States extends Machine.StateSchemas,
8618
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8619
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8620
- StateId extends Machine.StateIdentifier<States>,
8621
- const Config extends object,
8622
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8623
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8624
- >(
8625
- config: Config & Machine.TimerInvokeArgs<States, Events, Emits, StateId, InputEvents, ParentEvents>
8626
- ): Config & Machine.InvokeOwned<States, Events, Emits, StateId> & Machine.InvokeTyped<void, never, never, never>
8627
- <
8628
- const States extends Machine.StateSchemas,
8629
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8630
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8631
- StateId extends Machine.StateIdentifier<States>,
8632
- ChildState,
8633
- ChildEvent,
8634
- ChildError,
8635
- ChildRequirements,
8636
- ChildOutput,
8637
- ChildInitialError,
8638
- Address extends ChildAddress<never>,
8639
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8640
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8641
- >(
8642
- config: Machine.LogicInvokeArgs<
8643
- States,
8644
- Events,
8645
- Emits,
8646
- StateId,
8647
- ChildState,
8648
- ChildEvent,
8649
- ChildError,
8650
- ChildRequirements,
8651
- ChildOutput,
8652
- ChildInitialError,
8653
- Address,
8654
- (context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>) => Logic<
8655
- ChildState,
8656
- ChildEvent,
8657
- ChildError,
8658
- ChildRequirements,
8659
- ChildOutput,
8660
- ChildInitialError
8661
- >,
8662
- InputEvents,
8663
- ParentEvents
8664
- >
8665
- ):
8666
- & Machine.InvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
8667
- & Machine.InvokeTyped<
8668
- ChildOutput,
8669
- ChildError,
8670
- ChildRequirements,
8671
- ChildInitialError
8672
- >
8673
- <
8674
- const States extends Machine.StateSchemas,
8675
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8676
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8677
- StateId extends Machine.StateIdentifier<States>,
8678
- ChildState,
8679
- ChildEvent,
8680
- ChildError,
8681
- ChildRequirements,
8682
- ChildOutput,
8683
- ChildInitialError,
8684
- Address extends ChildAddress<never>,
8685
- const Config extends object,
8686
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8687
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8688
- >(
8689
- config:
8690
- & Config
8691
- & Machine.LogicInvokeArgs<
8692
- States,
8693
- Events,
8694
- Emits,
8695
- StateId,
8696
- ChildState,
8697
- ChildEvent,
8698
- ChildError,
8699
- ChildRequirements,
8700
- ChildOutput,
8701
- ChildInitialError,
8702
- Address,
8703
- Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>,
8704
- InputEvents,
8705
- ParentEvents
8706
- >
8707
- ):
8708
- & Config
8709
- & Machine.InvokeOwned<States, Events, Emits, StateId>
8710
- & Machine.InvokeTyped<
8711
- ChildOutput,
8712
- ChildError,
8713
- ChildRequirements,
8714
- ChildInitialError
8715
- >
8716
- <
8717
- const States extends Machine.StateSchemas,
8718
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8719
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
8720
- StateId extends Machine.StateIdentifier<States>,
8721
- const Child extends ChildMachine.Any,
8722
- const Config extends object,
8723
- const InputEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8724
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8725
- >(
8726
- config:
8727
- & Config
8728
- & Machine.ChildInvokeArgs<States, Events, Emits, StateId, Child["machine"], Child, InputEvents, ParentEvents>
8729
- ):
8730
- & Config
8731
- & Machine.InvokeOwned<States, Events, Emits, StateId>
8732
- & Machine.InvokeTyped<
8733
- Machine.Output<Child["machine"]>,
8734
- Machine.Error<Child["machine"]> | ActionError<Machine.Services<Child["machine"]>>,
8735
- Machine.Services<Child["machine"]>,
8736
- Machine.InitialError<Child["machine"]>,
8737
- Machine.Emit<Child["machine"]>,
8738
- Machine.EventOf<Machine.ParentEvents<Child["machine"]>>
8739
- >
8740
- } = ((config: unknown) => config) as any
8141
+ export const decodeSnapshot: <
8142
+ const States extends Machine.StateSchemas,
8143
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
8144
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8145
+ const Input extends Schema.Top = typeof Schema.Void,
8146
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
8147
+ E = never,
8148
+ R = never,
8149
+ InitialE = never,
8150
+ InitialR = never,
8151
+ FinalStates extends Machine.StateIdentifier<States> = never,
8152
+ Output = never,
8153
+ OutputStates extends Machine.StateIdentifier<States> = never,
8154
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
8155
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8156
+ >(
8157
+ machine: Machine<
8158
+ States,
8159
+ Events,
8160
+ Input,
8161
+ UnhandledStates,
8162
+ E,
8163
+ R,
8164
+ InitialE,
8165
+ InitialR,
8166
+ FinalStates,
8167
+ Output,
8168
+ Emits,
8169
+ OutputStates,
8170
+ InputEvents,
8171
+ ParentEvents
8172
+ >,
8173
+ encoded: unknown
8174
+ ) => Effect.Effect<
8175
+ Machine.Snapshot<States>,
8176
+ MachineSchemaDecodeError,
8177
+ Machine.SnapshotDecodingServices<States>
8178
+ > = internal.decodeSnapshot as any
8179
+
8180
+ type TransitionBranchRecordError<Message extends string, Key extends PropertyKey = never> = {
8181
+ readonly "~effect/Machine/TransitionBranchRecordError": Message
8182
+ readonly key: Key
8183
+ }
8184
+
8185
+ type InvalidStaticTransitionBranchKey<Branches> = Extract<keyof Branches, "" | number | symbol>
8186
+
8187
+ type ValidateTransitionBranchRecord<Branches> = [keyof Branches] extends [never] ?
8188
+ TransitionBranchRecordError<"Branch records must contain at least one branch">
8189
+ : [InvalidStaticTransitionBranchKey<Branches>] extends [never] ? unknown
8190
+ : TransitionBranchRecordError<
8191
+ "Branch keys must be non-empty, non-index strings",
8192
+ InvalidStaticTransitionBranchKey<Branches>
8193
+ >
8194
+
8741
8195
  /**
8742
8196
  * Plans the initial state for a machine without executing machine commands.
8743
8197
  *
@@ -8767,10 +8221,7 @@ export const invoke: {
8767
8221
  * const machine = Machine.make({
8768
8222
  * states: States.states,
8769
8223
  * events: Machine.events(),
8770
- * initial: {
8771
- * target: (to) => to.Idle(),
8772
- * resolve: ({ target }) => target.from()
8773
- * }
8224
+ * initial: (to) => to.Idle().resolve(({ target }) => target.from())
8774
8225
  * }).handle({ Idle: {} })
8775
8226
  *
8776
8227
  * const initialState = Effect.map(Machine.planInitial(machine), (plan) => plan.state)
@@ -8814,7 +8265,8 @@ export const planInitial: <
8814
8265
  InputEvents,
8815
8266
  ParentEvents
8816
8267
  >
8817
- & EnsureExecutable<States, UnhandledStates, OutputStates>,
8268
+ & EnsureExecutable<States, UnhandledStates, OutputStates>
8269
+ & Machine.RootCompatible<ParentEvents>,
8818
8270
  ...args: [...Machine.InputArgs<Input>]
8819
8271
  ) => Effect.Effect<
8820
8272
  & {
@@ -8899,7 +8351,8 @@ export const initialDefinition: <M extends Machine.Any>(machine: M) => Machine.I
8899
8351
  * Event handlers retain their handler-key order within each source state and
8900
8352
  * are followed by eventless and completion handlers. This function does not
8901
8353
  * execute resolvers. Every direct, named, and targetless branch exposes the
8902
- * destination selected by its required static `target` declaration.
8354
+ * destination selected by its required static `target` declaration, while
8355
+ * `acceptance` reports whether the resolver may decline the transition.
8903
8356
  *
8904
8357
  * @category getters
8905
8358
  * @since 0.4.0
@@ -8952,7 +8405,12 @@ export const configuration: <M extends Machine.Any>(
8952
8405
  > = internal.configuration
8953
8406
 
8954
8407
  /**
8955
- * Returns the event tags handled by the current state snapshot.
8408
+ * Returns event tags with at least one structurally eligible handler in the
8409
+ * current state snapshot.
8410
+ *
8411
+ * A `declinable` handler may still reject a concrete event at planning time,
8412
+ * so this is a static candidate query rather than a guarantee that every value
8413
+ * with the returned tag will be handled.
8956
8414
  *
8957
8415
  * @category getters
8958
8416
  * @since 0.4.0
@@ -9022,17 +8480,12 @@ export const enabled: <
9022
8480
  * const machine = Machine.make({
9023
8481
  * states: States.states,
9024
8482
  * events: Machine.events(Toggle),
9025
- * initial: {
9026
- * target: (to) => to.Off(),
9027
- * resolve: ({ target }) => target.from()
9028
- * }
8483
+ * initial: (to) => to.Off().resolve(({ target }) => target.from())
9029
8484
  * }).handle({
9030
8485
  * Off: {
9031
8486
  * on: {
9032
- * Toggle: Machine.transition({
9033
- * target: (to) => to.full.On(),
9034
- * resolve: ({ target }) => target.from()
9035
- * })
8487
+ * Toggle: (to) =>
8488
+ * to.full.On().resolve(({ target }) => target.from())
9036
8489
  * }
9037
8490
  * },
9038
8491
  * On: {}
@@ -9082,7 +8535,8 @@ export const plan: <
9082
8535
  InputEvents,
9083
8536
  ParentEvents
9084
8537
  >
9085
- & EnsureExecutable<States, UnhandledStates, OutputStates>,
8538
+ & EnsureExecutable<States, UnhandledStates, OutputStates>
8539
+ & Machine.RootCompatible<ParentEvents>,
9086
8540
  state: Machine.Snapshot<States>,
9087
8541
  event: Machine.EventInputOf<InputEvents>
9088
8542
  ) => Effect.Effect<
@@ -9185,7 +8639,7 @@ export const child: <const Id extends string, M extends Machine.Any>(id: Id, mac
9185
8639
  * Creates a typed parent-local address for lower-level child process logic.
9186
8640
  *
9187
8641
  * The default event protocol is `never`; provide an event type before using
9188
- * the address with `spawn`, `invoke`, or `sendTo`.
8642
+ * the address with `spawn`, a state-owned logic invocation, or `sendTo`.
9189
8643
  *
9190
8644
  * @category constructors
9191
8645
  * @since 0.4.0
@@ -9206,7 +8660,8 @@ export const childAddress: <Event = never>(id: string) => ChildAddress<Event> =
9206
8660
  * This Effect requires a managed process runtime. A named child id must be
9207
8661
  * unique for the current parent until that child stops.
9208
8662
  *
9209
- * @see {@link invoke} for children that start and stop with a state.
8663
+ * Use a state's `invoke: (from) => from.logic(...)` declaration for children
8664
+ * that start and stop with that state.
9210
8665
  * @see {@link sendTo} for sending events to named children.
9211
8666
  * @category runtime
9212
8667
  * @since 0.4.0
@@ -9344,7 +8799,8 @@ export const prepare: <
9344
8799
  InputEvents,
9345
8800
  ParentEvents
9346
8801
  >
9347
- & EnsureExecutable<States, UnhandledStates, OutputStates>,
8802
+ & EnsureExecutable<States, UnhandledStates, OutputStates>
8803
+ & Machine.RootCompatible<ParentEvents>,
9348
8804
  ...args: [...Machine.InputArgs<Input>]
9349
8805
  ) => Effect.Effect<
9350
8806
  Prepared<
@@ -9405,10 +8861,7 @@ export const prepare: <
9405
8861
  * const machine = Machine.make({
9406
8862
  * states: States.states,
9407
8863
  * events: Machine.events(),
9408
- * initial: {
9409
- * target: (to) => to.Idle(),
9410
- * resolve: ({ target }) => target.from()
9411
- * }
8864
+ * initial: (to) => to.Idle().resolve(({ target }) => target.from())
9412
8865
  * }).handle({ Idle: {} })
9413
8866
  *
9414
8867
  * const state = Effect.gen(function*() {
@@ -9455,7 +8908,8 @@ export const start: <
9455
8908
  InputEvents,
9456
8909
  ParentEvents
9457
8910
  >
9458
- & EnsureExecutable<States, UnhandledStates, OutputStates>,
8911
+ & EnsureExecutable<States, UnhandledStates, OutputStates>
8912
+ & Machine.RootCompatible<ParentEvents>,
9459
8913
  ...args: [...Machine.InputArgs<Input>]
9460
8914
  ) => Effect.Effect<
9461
8915
  MachineRef<
@@ -9518,10 +8972,7 @@ export const start: <
9518
8972
  * const machine = Machine.make({
9519
8973
  * states: States.states,
9520
8974
  * events: Machine.events(),
9521
- * initial: {
9522
- * target: (to) => to.Idle(),
9523
- * resolve: ({ target }) => target.from()
9524
- * }
8975
+ * initial: (to) => to.Idle().resolve(({ target }) => target.from())
9525
8976
  * }).handle({ Idle: {} })
9526
8977
  *
9527
8978
  * const resumed = Effect.gen(function*() {
@@ -9570,7 +9021,8 @@ export const resume: <
9570
9021
  InputEvents,
9571
9022
  ParentEvents
9572
9023
  >
9573
- & EnsureExecutable<States, UnhandledStates, OutputStates>,
9024
+ & EnsureExecutable<States, UnhandledStates, OutputStates>
9025
+ & Machine.RootCompatible<ParentEvents>,
9574
9026
  snapshot: Machine.Snapshot<States>
9575
9027
  ) => Effect.Effect<
9576
9028
  MachineRef<