@typeonce/effect-machine 0.8.0 → 0.9.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 (57) hide show
  1. package/README.md +64 -24
  2. package/dist/Machine.d.ts +386 -304
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +66 -171
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/activities.d.ts +5 -12
  7. package/dist/internal/machine/activities.d.ts.map +1 -1
  8. package/dist/internal/machine/activities.js +47 -17
  9. package/dist/internal/machine/activities.js.map +1 -1
  10. package/dist/internal/machine/atom.d.ts +2 -2
  11. package/dist/internal/machine/atom.d.ts.map +1 -1
  12. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  13. package/dist/internal/machine/executionPlan.js +25 -0
  14. package/dist/internal/machine/executionPlan.js.map +1 -1
  15. package/dist/internal/machine/invocation.d.ts +10 -2
  16. package/dist/internal/machine/invocation.d.ts.map +1 -1
  17. package/dist/internal/machine/invocation.js +93 -34
  18. package/dist/internal/machine/invocation.js.map +1 -1
  19. package/dist/internal/machine/invocationEvent.d.ts +39 -0
  20. package/dist/internal/machine/invocationEvent.d.ts.map +1 -0
  21. package/dist/internal/machine/invocationEvent.js +43 -0
  22. package/dist/internal/machine/invocationEvent.js.map +1 -0
  23. package/dist/internal/machine/machine.d.ts +6 -50
  24. package/dist/internal/machine/machine.d.ts.map +1 -1
  25. package/dist/internal/machine/machine.js +8 -65
  26. package/dist/internal/machine/machine.js.map +1 -1
  27. package/dist/internal/machine/planner.d.ts +2 -1
  28. package/dist/internal/machine/planner.d.ts.map +1 -1
  29. package/dist/internal/machine/planner.js +45 -2
  30. package/dist/internal/machine/planner.js.map +1 -1
  31. package/dist/internal/machine/protocol.d.ts +9 -0
  32. package/dist/internal/machine/protocol.d.ts.map +1 -1
  33. package/dist/internal/machine/protocol.js +114 -0
  34. package/dist/internal/machine/protocol.js.map +1 -1
  35. package/dist/internal/machine/symbols.d.ts +2 -0
  36. package/dist/internal/machine/symbols.d.ts.map +1 -1
  37. package/dist/internal/machine/symbols.js +2 -0
  38. package/dist/internal/machine/symbols.js.map +1 -1
  39. package/dist/internal/machine/topology.d.ts.map +1 -1
  40. package/dist/internal/machine/topology.js +23 -0
  41. package/dist/internal/machine/topology.js.map +1 -1
  42. package/dist/unstable/reactivity/AtomMachine.d.ts +3 -3
  43. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  44. package/docs/agent-guide.md +118 -93
  45. package/package.json +2 -2
  46. package/src/Machine.ts +1040 -651
  47. package/src/internal/machine/activities.ts +48 -33
  48. package/src/internal/machine/atom.ts +3 -3
  49. package/src/internal/machine/executionPlan.ts +29 -0
  50. package/src/internal/machine/invocation.ts +179 -48
  51. package/src/internal/machine/invocationEvent.ts +72 -0
  52. package/src/internal/machine/machine.ts +20 -346
  53. package/src/internal/machine/planner.ts +61 -10
  54. package/src/internal/machine/protocol.ts +149 -0
  55. package/src/internal/machine/symbols.ts +3 -0
  56. package/src/internal/machine/topology.ts +23 -0
  57. package/src/unstable/reactivity/AtomMachine.ts +3 -3
package/src/Machine.ts CHANGED
@@ -49,6 +49,9 @@ export const TypeId: TypeId = "~effect/Machine"
49
49
 
50
50
  declare const MachineOutputStatesTypeId: unique symbol
51
51
  declare const MachineTypeId: unique symbol
52
+ declare const EventConstructionTypeId: unique symbol
53
+
54
+ const ChildMachineLogicTypeId: typeof internal.ChildMachineLogicTypeId = internal.ChildMachineLogicTypeId
52
55
 
53
56
  /**
54
57
  * Type identifier used for the synthetic event passed to startup lifecycle
@@ -158,7 +161,8 @@ export interface Machine<
158
161
  readonly events: InputEvents
159
162
 
160
163
  /**
161
- * Events reserved for invokes, child emissions, and other machine-local work.
164
+ * Events reserved for raised events, child emissions, and other machine-local
165
+ * work.
162
166
  *
163
167
  * @since 0.4.0
164
168
  */
@@ -350,7 +354,7 @@ export interface Runtime<in Events, in Emits> {
350
354
  *
351
355
  * @since 0.4.0
352
356
  */
353
- readonly raise: (event: Events) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>
357
+ readonly raise: (event: Machine.EventInput<Events>) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>
354
358
 
355
359
  /**
356
360
  * Emits an event through the running machine's parent boundary.
@@ -370,7 +374,7 @@ export interface Runtime<in Events, in Emits> {
370
374
  */
371
375
  export interface Enqueue<in Events, in Emits> {
372
376
  /** Raises an event inside the current macrostep. */
373
- readonly raise: (event: Events) => void
377
+ readonly raise: (event: Machine.EventInput<Events>) => void
374
378
 
375
379
  /** Emits an event through the machine's parent boundary. */
376
380
  readonly emit: (event: Emits) => void
@@ -1767,7 +1771,8 @@ export declare namespace Logic {
1767
1771
  *
1768
1772
  * `sendParent` accepts `unknown` because process logic is independent from
1769
1773
  * the parent that eventually owns it. Prefer typed process output or an
1770
- * invoke snapshot mapper when either can represent the communication.
1774
+ * invocation lifecycle transition when either can represent the
1775
+ * communication.
1771
1776
  *
1772
1777
  * @category models
1773
1778
  * @since 0.4.0
@@ -1829,8 +1834,9 @@ type InvokeLifecycleId = string & { readonly [ChildAddressTypeId]?: never }
1829
1834
  * **Details**
1830
1835
  *
1831
1836
  * The descriptor carries the child's address and complete machine type. Pass
1832
- * the same value to `invokeMachine`, `sendTo`, and child lookup APIs so state,
1833
- * event, error, and output types are inferred without separate annotations.
1837
+ * a descriptor for the same id and machine to inline `invoke`, `sendTo`, and
1838
+ * child lookup APIs so state, event, error, and output types are inferred
1839
+ * without separate annotations.
1834
1840
  *
1835
1841
  * @category models
1836
1842
  * @since 0.4.0
@@ -1843,6 +1849,9 @@ export interface ChildMachine<Id extends string, M extends Machine.Any> {
1843
1849
 
1844
1850
  /** Complete machine definition carried by this descriptor. */
1845
1851
  readonly machine: M
1852
+
1853
+ /** @internal */
1854
+ readonly [ChildMachineLogicTypeId]: (input?: unknown) => Logic<any, any, any, any, any, any>
1846
1855
  }
1847
1856
 
1848
1857
  /**
@@ -1868,7 +1877,7 @@ export declare namespace ChildMachine {
1868
1877
  */
1869
1878
  export type Ref<Child> = Child extends ChildMachine<string, infer M> ? MachineRef<
1870
1879
  Machine.Snapshot<Machine.States<M>>,
1871
- Machine.InputEvent<M>,
1880
+ Machine.EventInput<Machine.InputEvent<M>>,
1872
1881
  | Machine.Error<M>
1873
1882
  | ActionError<Machine.Services<M>>
1874
1883
  | InfiniteTransitionError
@@ -2152,6 +2161,13 @@ export declare namespace Machine {
2152
2161
  */
2153
2162
  export type InputEvents<M extends Any> = M[typeof MachineTypeId]["inputEvents"]
2154
2163
 
2164
+ /** Extracts the internal event schema tuple carried by a machine definition. */
2165
+ export type InternalEvents<M extends Any> = Events<M> extends readonly [
2166
+ ...InputEvents<M>,
2167
+ ...infer Internal extends ReadonlyArray<TaggedSchema>
2168
+ ] ? Internal
2169
+ : readonly []
2170
+
2155
2171
  /**
2156
2172
  * Extracts the complete event protocol handled inside a machine.
2157
2173
  *
@@ -2168,6 +2184,53 @@ export declare namespace Machine {
2168
2184
  */
2169
2185
  export type InputEvent<M extends Any> = EventOf<InputEvents<M>>
2170
2186
 
2187
+ /**
2188
+ * Opaque event construction returned by {@link events} and
2189
+ * {@link internalEvents}.
2190
+ *
2191
+ * The machine resolves the instruction through its event schema when it is
2192
+ * delivered. Only the discriminator is available before decoding succeeds.
2193
+ */
2194
+ export interface EventConstruction<out Event extends { readonly _tag: PropertyKey }> {
2195
+ readonly [EventConstructionTypeId]: Event
2196
+ readonly _tag: Event["_tag"]
2197
+ }
2198
+
2199
+ /** A decoded event or a deferred machine-bound construction of that event. */
2200
+ export type EventInput<Event> =
2201
+ | Event
2202
+ | (Event extends { readonly _tag: PropertyKey } ? EventConstruction<Event> : never)
2203
+
2204
+ /** Event inputs accepted for a schema tuple at machine delivery boundaries. */
2205
+ export type EventInputOf<Events extends ReadonlyArray<TaggedSchema>> = EventInput<EventOf<Events>>
2206
+
2207
+ type EventConstructorInput<EventSchema extends TaggedSchema> = Omit<EventSchema["~type.make.in"], "_tag">
2208
+
2209
+ type EventConstructor<
2210
+ EventSchema extends TaggedSchema,
2211
+ Tag extends EventSchema["Type"]["_tag"]
2212
+ > = {} extends EventConstructorInput<EventSchema> ?
2213
+ (input?: EventConstructorInput<EventSchema>) => EventConstruction<EventByTag<readonly [EventSchema], Tag>>
2214
+ : (input: EventConstructorInput<EventSchema>) => EventConstruction<EventByTag<readonly [EventSchema], Tag>>
2215
+
2216
+ type EventConstructorsForSchema<EventSchema extends TaggedSchema> = EventSchema extends {
2217
+ readonly cases: infer Cases extends Readonly<Record<PropertyKey, TaggedSchema>>
2218
+ } ? {
2219
+ readonly [Tag in keyof Cases]: Tag extends Cases[Tag]["Type"]["_tag"] ? EventConstructor<Cases[Tag], Tag>
2220
+ : never
2221
+ }
2222
+ : EventSchema extends { readonly members: infer Members extends ReadonlyArray<TaggedSchema> } ?
2223
+ Types.UnionToIntersection<EventConstructorsForSchema<Members[number]>>
2224
+ : {
2225
+ readonly [Tag in EventSchema["Type"]["_tag"]]: EventConstructor<EventSchema, Tag>
2226
+ }
2227
+
2228
+ /** Protocol-bound constructors keyed by each configured event tag. */
2229
+ export type EventConstructors<Events extends ReadonlyArray<TaggedSchema>> = {
2230
+ readonly [Tag in keyof Types.UnionToIntersection<EventConstructorsForSchema<Events[number]>>]:
2231
+ Types.UnionToIntersection<EventConstructorsForSchema<Events[number]>>[Tag]
2232
+ }
2233
+
2171
2234
  /**
2172
2235
  * Extracts the event protocol emitted by a machine.
2173
2236
  *
@@ -2636,6 +2699,11 @@ export declare namespace Machine {
2636
2699
  | {
2637
2700
  readonly type: "choice"
2638
2701
  }
2702
+ | {
2703
+ readonly type: "invoke"
2704
+ readonly id: string
2705
+ readonly outcome: "done" | "failure" | "snapshot"
2706
+ }
2639
2707
 
2640
2708
  /**
2641
2709
  * Statically inspectable destination paths for a transition handler.
@@ -2658,10 +2726,10 @@ export declare namespace Machine {
2658
2726
  *
2659
2727
  * **Details**
2660
2728
  *
2661
- * Event, eventless, and completion handlers may declare an upper bound of
2662
- * possible target paths. A handler without that declaration is explicitly
2663
- * reported as dynamic. The source, trigger, and reentry behavior are
2664
- * available without executing the handler.
2729
+ * Event, eventless, completion, and invocation lifecycle handlers may
2730
+ * declare an upper bound of possible target paths. A handler without that
2731
+ * declaration is explicitly reported as dynamic. The source, trigger, and
2732
+ * reentry behavior are available without executing the handler.
2665
2733
  *
2666
2734
  * @category models
2667
2735
  * @since 0.4.0
@@ -2680,9 +2748,9 @@ export declare namespace Machine {
2680
2748
  /**
2681
2749
  * Serializable description of state-owned work.
2682
2750
  *
2683
- * Static invoke descriptors expose their lifecycle id and kind without
2684
- * retaining Effects, closures, services, or child runtimes. A function-valued
2685
- * invoke factory is reported as dynamic and is never evaluated by inspection.
2751
+ * Static invoke definitions expose their lifecycle id and kind without
2752
+ * retaining Effects, closures, services, or child runtimes. Function-valued
2753
+ * sources are reported as dynamic and are never evaluated by inspection.
2686
2754
  *
2687
2755
  * @category models
2688
2756
  * @since 0.4.0
@@ -3738,7 +3806,7 @@ export declare namespace Machine {
3738
3806
  }
3739
3807
 
3740
3808
  /**
3741
- * Context passed to an invoked child process source.
3809
+ * Context passed to a function-valued invocation source.
3742
3810
  *
3743
3811
  * @category models
3744
3812
  * @since 0.4.0
@@ -3756,27 +3824,67 @@ export declare namespace Machine {
3756
3824
  }
3757
3825
 
3758
3826
  /**
3759
- * Context passed to an invoked child process active snapshot mapper.
3827
+ * Context passed to an invocation's active-snapshot transition.
3760
3828
  *
3761
3829
  * @category models
3762
3830
  * @since 0.4.0
3763
3831
  */
3764
- export interface InvokeSnapshotContext<State, Error, Output> {
3832
+ export interface InvokeSnapshotContext<
3833
+ States extends StateSchemas,
3834
+ Events extends ReadonlyArray<TaggedSchema>,
3835
+ Emits extends ReadonlyArray<TaggedSchema>,
3836
+ StateId extends StateIdentifier<States>,
3837
+ State,
3838
+ Error,
3839
+ Output
3840
+ > {
3765
3841
  readonly id: string
3842
+ readonly state: StateByIdentifier<States, StateId>
3843
+ readonly parent: ParentStateValue<States, StateId>
3844
+ readonly parents: ParentStateValues<States, StateId>
3845
+ readonly target: TargetBuilder<States, StateId>
3766
3846
  readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "active" }>
3767
3847
  }
3768
3848
 
3769
3849
  /**
3770
- * Context passed to an invoked machine terminal output mapper.
3850
+ * Context passed to an invocation's successful completion transition.
3771
3851
  *
3772
3852
  * @category models
3773
3853
  * @since 0.4.0
3774
3854
  */
3775
- export interface InvokeDoneContext<Output> {
3855
+ export interface InvokeDoneContext<
3856
+ States extends StateSchemas,
3857
+ Events extends ReadonlyArray<TaggedSchema>,
3858
+ Emits extends ReadonlyArray<TaggedSchema>,
3859
+ StateId extends StateIdentifier<States>,
3860
+ Output
3861
+ > {
3776
3862
  readonly id: string
3863
+ readonly state: StateByIdentifier<States, StateId>
3864
+ readonly parent: ParentStateValue<States, StateId>
3865
+ readonly parents: ParentStateValues<States, StateId>
3866
+ readonly snapshot: Snapshot<States>
3867
+ readonly target: TargetBuilder<States, StateId>
3777
3868
  readonly output: Output
3778
3869
  }
3779
3870
 
3871
+ /** Context passed to an invocation typed-failure transition. */
3872
+ export interface InvokeFailureContext<
3873
+ States extends StateSchemas,
3874
+ Events extends ReadonlyArray<TaggedSchema>,
3875
+ Emits extends ReadonlyArray<TaggedSchema>,
3876
+ StateId extends StateIdentifier<States>,
3877
+ Error
3878
+ > {
3879
+ readonly id: string
3880
+ readonly state: StateByIdentifier<States, StateId>
3881
+ readonly parent: ParentStateValue<States, StateId>
3882
+ readonly parents: ParentStateValues<States, StateId>
3883
+ readonly snapshot: Snapshot<States>
3884
+ readonly target: TargetBuilder<States, StateId>
3885
+ readonly error: Error
3886
+ }
3887
+
3780
3888
  /**
3781
3889
  * Context passed to an eventless transition handler.
3782
3890
  *
@@ -4066,7 +4174,41 @@ export declare namespace Machine {
4066
4174
  * @category utility types
4067
4175
  * @since 0.4.0
4068
4176
  */
4069
- export type InvokeLogic<Invoke> = Invoke extends { readonly src: (...args: any) => infer Logic } ? Logic : never
4177
+ export type InvokeResolvedSource<Source> = Source extends (...args: any) => infer Resolved ? Resolved : Source
4178
+
4179
+ type ChildMachineLogic<Child> = Child extends ChildMachine<string, infer M> ? Logic<
4180
+ Snapshot<States<M>>,
4181
+ EventInput<InputEvent<M>>,
4182
+ Error<M> | ActionError<Services<M>> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
4183
+ ExcludeCompatibleRuntime<
4184
+ Exclude<ExecutionServices<InitialServices<M> | Services<M>>, internalRuntime.MachineRuntime>,
4185
+ Event<M>,
4186
+ Emit<M>
4187
+ >,
4188
+ Output<M>,
4189
+ | InitialError<M>
4190
+ | Error<M>
4191
+ | ActionError<InitialServices<M> | Services<M>>
4192
+ | InfiniteTransitionError
4193
+ | MachineSchemaDecodeError
4194
+ | StartupError
4195
+ | StoppedError
4196
+ >
4197
+ : never
4198
+
4199
+ export type InvokeLogic<Invoke> = Invoke extends { readonly effect: infer Source } ?
4200
+ InvokeResolvedSource<Source> extends infer Fx extends Effect.Effect<any, any, any> ? Logic<
4201
+ void,
4202
+ never,
4203
+ Effect.Error<Fx>,
4204
+ Effect.Services<Fx>,
4205
+ Effect.Success<Fx>
4206
+ >
4207
+ : never
4208
+ : Invoke extends { readonly after: unknown } ? Logic<void, never, never, never, void>
4209
+ : Invoke extends { readonly logic: infer Source } ? InvokeResolvedSource<Source>
4210
+ : Invoke extends { readonly child: infer Child } ? ChildMachineLogic<Child>
4211
+ : never
4070
4212
  /**
4071
4213
  * Extracts the startup error from an invoke source child process logic.
4072
4214
  *
@@ -4118,19 +4260,16 @@ export declare namespace Machine {
4118
4260
  * @since 0.4.0
4119
4261
  */
4120
4262
  export type InvokeEmits<Invoke> = Invoke extends {
4121
- readonly [InvokeTypeId]: { readonly emits: Types.Covariant<infer Emits> }
4122
- } ? Emits :
4123
- never
4124
- /**
4125
- * Extracts events returned by an invoked child snapshot mapper.
4126
- *
4127
- * @category utility types
4128
- * @since 0.4.0
4129
- */
4130
- export type InvokeSnapshotEvent<Invoke> = Invoke extends {
4131
- readonly [InvokeTypeId]: { readonly snapshotEvent: Types.Covariant<infer Event> }
4132
- } ? Event :
4133
- never
4263
+ readonly [InvokeTypeId]: { readonly emits: Types.Covariant<infer Emitted> }
4264
+ } ? Emitted
4265
+ : Invoke extends { readonly child: ChildMachine<string, infer M> } ? Emit<M>
4266
+ : never
4267
+ /** Extracts transition results returned by invocation lifecycle handlers. */
4268
+ export type InvokeOutcomeReturn<Invoke> = Invoke extends unknown ?
4269
+ | (Invoke extends { readonly onDone?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
4270
+ | (Invoke extends { readonly onFailure?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
4271
+ | (Invoke extends { readonly onSnapshot?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
4272
+ : never
4134
4273
  /**
4135
4274
  * Extracts the parent transition error contribution from invoked children.
4136
4275
  *
@@ -4138,7 +4277,10 @@ export declare namespace Machine {
4138
4277
  * @since 0.4.0
4139
4278
  */
4140
4279
  export type InvokeError<Config> = [InvokeReturn<Config>] extends [never] ? never
4141
- : ChildAlreadyExistsError | InvokeInitialError<InvokeReturn<Config>> | InvokeRuntimeError<InvokeReturn<Config>>
4280
+ :
4281
+ | ChildAlreadyExistsError
4282
+ | InvokeInitialError<InvokeReturn<Config>>
4283
+ | Effect.Error<InvokeOutcomeReturn<InvokeReturn<Config>>>
4142
4284
  /**
4143
4285
  * Extracts the parent service requirement contribution from invoked children.
4144
4286
  *
@@ -4146,7 +4288,12 @@ export declare namespace Machine {
4146
4288
  * @since 0.4.0
4147
4289
  */
4148
4290
  export type InvokeRequirements<Config> = [InvokeReturn<Config>] extends [never] ? never
4149
- : MachineRuntimeRequirement | InvokeServices<InvokeReturn<Config>>
4291
+ :
4292
+ | MachineRuntimeRequirement
4293
+ | InvokeServices<InvokeReturn<Config>>
4294
+ | Effect.Services<
4295
+ InvokeOutcomeReturn<InvokeReturn<Config>>
4296
+ >
4150
4297
  /**
4151
4298
  * Extracts the return value from an eventless transition.
4152
4299
  *
@@ -4192,204 +4339,534 @@ export declare namespace Machine {
4192
4339
  | Effect.Services<ChoiceReturn<Config>>
4193
4340
  | InvokeRequirements<Config>
4194
4341
 
4195
- /**
4196
- * Configuration for invoking a child process while a state is active.
4197
- *
4198
- * @category models
4199
- * @since 0.4.0
4200
- */
4201
- export interface InvokeConfig<
4342
+ export type InvokeTransition<
4202
4343
  States extends StateSchemas,
4203
4344
  Events extends ReadonlyArray<TaggedSchema>,
4204
4345
  Emits extends ReadonlyArray<TaggedSchema>,
4205
- StateId extends StateIdentifier<States>,
4206
- Event,
4207
- ChildState,
4208
- ChildEvent,
4209
- ChildError,
4210
- ChildRequirements,
4211
- ChildOutput,
4212
- ChildInitialError,
4213
- ChildEmits = never,
4214
- DeliveredOutput = ChildOutput
4215
- > {
4216
- readonly [InvokeTypeId]: {
4217
- readonly output: Types.Covariant<DeliveredOutput>
4218
- readonly emits: Types.Covariant<ChildEmits>
4219
- readonly snapshotEvent: Types.Covariant<Event>
4220
- readonly error: Types.Covariant<ChildError>
4221
- readonly requirements: Types.Covariant<ChildRequirements>
4222
- readonly initialError: Types.Covariant<ChildInitialError>
4346
+ Context
4347
+ > =
4348
+ | ((context: Context, enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>) => HandlerResult<States, any, any>)
4349
+ | {
4350
+ readonly reenter?: boolean
4351
+ readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4352
+ readonly transition: (
4353
+ context: Context,
4354
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4355
+ ) => HandlerResult<States, any, any>
4223
4356
  }
4224
- /** @internal Serializable descriptor metadata used by inspection. */
4225
- readonly [Activities.ActivityMetadataTypeId]?: Activities.StaticActivityMetadata
4226
- readonly id: string
4227
- /**
4228
- * Optional parent-local address for sending events to this invocation.
4229
- *
4230
- * The invocation `id` is only its state-local lifecycle key. Use an
4231
- * explicit typed address when the parent must communicate with it.
4232
- */
4233
- readonly address?: string
4234
- /** @internal */
4235
- readonly descriptor?: ChildMachine.Any
4236
- src(): Logic<
4237
- ChildState,
4238
- ChildEvent,
4239
- ChildError,
4240
- ChildRequirements,
4241
- ChildOutput,
4242
- ChildInitialError
4243
- >
4244
- snapshot?(
4245
- context: InvokeSnapshotContext<ChildState, ChildError, ChildOutput>
4246
- ): Event | undefined
4247
- onDone?(context: InvokeDoneContext<ChildOutput>): DeliveredOutput | undefined
4357
+
4358
+ export type InvokeSource<Value, Context> = Value | ((context: Context) => Value)
4359
+
4360
+ interface AnyLogicSource {
4361
+ initial(...args: ReadonlyArray<any>): Effect.Effect<any, any, any>
4362
+ run(...args: ReadonlyArray<any>): Effect.Effect<any, any, any>
4248
4363
  }
4249
4364
 
4250
- /** @internal */
4251
- export interface AnyInvokeConfig<
4252
- Output = unknown,
4253
- Error = unknown,
4254
- Requirements = unknown,
4255
- InitialError = unknown,
4256
- Emits = never,
4257
- SnapshotEvent = never
4365
+ /** Inline state-owned work that runs for the lifetime of its active state. */
4366
+ export interface InvokeOwned<
4367
+ States extends StateSchemas,
4368
+ Events extends ReadonlyArray<TaggedSchema>,
4369
+ Emits extends ReadonlyArray<TaggedSchema>,
4370
+ StateId extends StateIdentifier<States>
4258
4371
  > {
4372
+ readonly "~effect/Machine/InvokeOwner"?: Types.Covariant<readonly [States, Events, Emits, StateId]>
4373
+ }
4374
+
4375
+ /** Type evidence retained by {@link invoke} without affecting runtime data. */
4376
+ export interface InvokeTyped<Output, Error, Requirements, InitialError, Emits = never> {
4259
4377
  readonly [InvokeTypeId]: {
4260
4378
  readonly output: Types.Covariant<Output>
4261
- readonly emits: Types.Covariant<Emits>
4262
- readonly snapshotEvent: Types.Covariant<SnapshotEvent>
4263
4379
  readonly error: Types.Covariant<Error>
4264
4380
  readonly requirements: Types.Covariant<Requirements>
4265
4381
  readonly initialError: Types.Covariant<InitialError>
4382
+ readonly emits: Types.Covariant<Emits>
4266
4383
  }
4267
4384
  }
4268
4385
 
4269
- interface InvokeDefinitionValue {
4270
- readonly [InvokeTypeId]: unknown
4271
- }
4386
+ export type InvokeConfig<
4387
+ States extends StateSchemas,
4388
+ Events extends ReadonlyArray<TaggedSchema>,
4389
+ Emits extends ReadonlyArray<TaggedSchema>,
4390
+ StateId extends StateIdentifier<States>
4391
+ > =
4392
+ & InvokeOwned<States, Events, Emits, StateId>
4393
+ & (
4394
+ | {
4395
+ readonly id: string
4396
+ readonly effect: InvokeSource<Effect.Effect<any, any, any>, InvokeContext<States, Events, Emits, StateId>>
4397
+ readonly after?: never
4398
+ readonly logic?: never
4399
+ readonly child?: never
4400
+ readonly address?: never
4401
+ readonly onDone?: unknown
4402
+ readonly onFailure?: unknown
4403
+ readonly onSnapshot?: never
4404
+ }
4405
+ | {
4406
+ readonly id: string
4407
+ readonly after: InvokeSource<Duration.Input, InvokeContext<States, Events, Emits, StateId>>
4408
+ readonly effect?: never
4409
+ readonly logic?: never
4410
+ readonly child?: never
4411
+ readonly address?: never
4412
+ readonly onDone: unknown
4413
+ readonly onFailure?: never
4414
+ readonly onSnapshot?: never
4415
+ }
4416
+ | {
4417
+ readonly id: string
4418
+ readonly address: string
4419
+ readonly logic: InvokeSource<AnyLogicSource, InvokeContext<States, Events, Emits, StateId>>
4420
+ readonly effect?: never
4421
+ readonly after?: never
4422
+ readonly child?: never
4423
+ readonly onDone?: unknown
4424
+ readonly onFailure?: unknown
4425
+ readonly onSnapshot?: unknown
4426
+ }
4427
+ | {
4428
+ readonly child: ChildMachine.Any
4429
+ readonly input?: {} | null | ((context: InvokeContext<States, Events, Emits, StateId>) => unknown)
4430
+ readonly id?: never
4431
+ readonly address?: never
4432
+ readonly effect?: never
4433
+ readonly after?: never
4434
+ readonly logic?: never
4435
+ readonly onDone?: unknown
4436
+ readonly onFailure?: unknown
4437
+ readonly onSnapshot?: unknown
4438
+ }
4439
+ )
4272
4440
 
4273
- /**
4274
- * State-bound configuration for invoked child processes.
4275
- *
4276
- * **Details**
4277
- *
4278
- * A function form receives the owning state's typed value and lifecycle
4279
- * event before constructing one or more invoke configurations.
4280
- *
4281
- * @category models
4282
- * @since 0.4.0
4283
- */
4441
+ /** State-bound inline invocation configuration. */
4284
4442
  export type InvokeDefinition<
4285
4443
  States extends StateSchemas,
4286
4444
  Events extends ReadonlyArray<TaggedSchema>,
4287
4445
  Emits extends ReadonlyArray<TaggedSchema>,
4288
4446
  StateId extends StateIdentifier<States>
4289
4447
  > =
4290
- | InvokeDefinitionValue
4291
- | ReadonlyArray<InvokeDefinitionValue>
4292
- | ((context: InvokeContext<States, Events, Emits, StateId>) =>
4293
- | InvokeDefinitionValue
4294
- | ReadonlyArray<InvokeDefinitionValue>)
4448
+ | InvokeConfig<States, Events, Emits, StateId>
4449
+ | ReadonlyArray<InvokeConfig<States, Events, Emits, StateId>>
4295
4450
 
4296
- type OutputHandlerConfig<
4451
+ type InvokeHandlerRequirement<Value, Handler> = IsAny<Value> extends true ? { readonly handler: Handler }
4452
+ : [Value] extends [never] ? { readonly handler?: never }
4453
+ : { readonly handler: Handler }
4454
+
4455
+ export type InvokeDoneRequirement<Value, Handler> = InvokeHandlerRequirement<Value, Handler> extends
4456
+ infer Requirement ? Requirement extends { readonly handler: infer Required } ? { readonly onDone: Required }
4457
+ : { readonly onDone?: never }
4458
+ : never
4459
+
4460
+ export type InvokeFailureRequirement<Value, Handler> = InvokeHandlerRequirement<Value, Handler> extends
4461
+ infer Requirement ? Requirement extends { readonly handler: infer Required } ? { readonly onFailure: Required }
4462
+ : { readonly onFailure?: never }
4463
+ : never
4464
+
4465
+ export type EffectInvokeArgs<
4297
4466
  States extends StateSchemas,
4298
4467
  Events extends ReadonlyArray<TaggedSchema>,
4468
+ Emits extends ReadonlyArray<TaggedSchema>,
4299
4469
  StateId extends StateIdentifier<States>,
4300
- Context
4301
- > = NodeByIdentifier<States, StateId> extends { readonly output: Schema.Top } ? {
4302
- readonly output: (context: Context) => OutputByIdentifier<States, StateId>
4303
- }
4304
- : {
4305
- readonly output?: never
4470
+ Fx extends Effect.Effect<any, any, any>,
4471
+ Source = Fx
4472
+ > =
4473
+ & {
4474
+ readonly id: InvokeLifecycleId
4475
+ readonly effect: Source
4476
+ readonly after?: never
4477
+ readonly logic?: never
4478
+ readonly child?: never
4479
+ readonly address?: never
4480
+ readonly onSnapshot?: never
4306
4481
  }
4482
+ & InvokeDoneRequirement<
4483
+ Effect.Success<NoInfer<Fx>>,
4484
+ InvokeTransition<
4485
+ States,
4486
+ Events,
4487
+ Emits,
4488
+ InvokeDoneContext<States, Events, Emits, StateId, Effect.Success<NoInfer<Fx>>>
4489
+ >
4490
+ >
4491
+ & InvokeFailureRequirement<
4492
+ Effect.Error<NoInfer<Fx>>,
4493
+ InvokeTransition<
4494
+ States,
4495
+ Events,
4496
+ Emits,
4497
+ InvokeFailureContext<States, Events, Emits, StateId, Effect.Error<NoInfer<Fx>>>
4498
+ >
4499
+ >
4307
4500
 
4308
- type ActiveOutputHandlerConfig<
4501
+ export type TimerInvokeArgs<
4309
4502
  States extends StateSchemas,
4310
4503
  Events extends ReadonlyArray<TaggedSchema>,
4504
+ Emits extends ReadonlyArray<TaggedSchema>,
4311
4505
  StateId extends StateIdentifier<States>
4312
- > = NodeByIdentifier<States, StateId> extends { readonly type: "parallel" } ? OutputHandlerConfig<
4506
+ > = {
4507
+ readonly id: InvokeLifecycleId
4508
+ readonly after: InvokeSource<Duration.Input, InvokeContext<States, Events, Emits, StateId>>
4509
+ readonly effect?: never
4510
+ readonly logic?: never
4511
+ readonly child?: never
4512
+ readonly address?: never
4513
+ readonly onFailure?: never
4514
+ readonly onSnapshot?: never
4515
+ readonly onDone: InvokeTransition<
4313
4516
  States,
4314
4517
  Events,
4315
- StateId,
4316
- ParallelOutputContext<States, Events, StateId>
4518
+ Emits,
4519
+ InvokeDoneContext<States, Events, Emits, StateId, void>
4317
4520
  >
4318
- : {
4319
- readonly output?: never
4320
- }
4521
+ }
4321
4522
 
4322
- /**
4323
- * Configuration accepted for a non-final state.
4324
- *
4325
- * @category models
4326
- * @since 0.4.0
4327
- */
4328
- export type ActiveStateConfig<
4523
+ export type LogicInvokeArgs<
4329
4524
  States extends StateSchemas,
4330
4525
  Events extends ReadonlyArray<TaggedSchema>,
4331
4526
  Emits extends ReadonlyArray<TaggedSchema>,
4332
4527
  StateId extends StateIdentifier<States>,
4333
- E,
4334
- R
4335
- > = {
4336
- readonly entry?: (
4337
- context: StateActionContext<States, Events, Emits, StateId>,
4338
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4339
- ) => StateActionResult<any, any>
4340
- readonly exit?: (
4341
- context: StateActionContext<States, Events, Emits, StateId>,
4342
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4343
- ) => StateActionResult<any, any>
4344
- readonly invoke?: InvokeDefinition<States, Events, Emits, StateId>
4345
- readonly always?:
4346
- | ((
4347
- context: AlwaysContext<States, Events, Emits, StateId>,
4348
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4349
- ) => HandlerResult<States, any, any>)
4350
- | {
4351
- /** Statically declared upper bound of possible target paths. */
4352
- readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4353
- readonly transition: (
4354
- context: AlwaysContext<States, Events, Emits, StateId>,
4355
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4356
- ) => HandlerResult<States, any, any>
4357
- }
4358
- readonly onDone?:
4359
- | ((
4360
- context: DoneContext<States, Events, Emits, StateId>,
4361
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4362
- ) => HandlerResult<States, any, any>)
4363
- | {
4364
- /** Statically declared upper bound of possible target paths. */
4365
- readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4366
- readonly transition: (
4367
- context: DoneContext<States, Events, Emits, StateId>,
4368
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4369
- ) => HandlerResult<States, any, any>
4370
- }
4371
- readonly on?: {
4372
- readonly [EventTag in TagOf<Events[number]>]?:
4373
- | ((
4374
- context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
4375
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4376
- ) => HandlerResult<States, any, any>)
4377
- | {
4378
- readonly reenter?: boolean
4379
- /**
4380
- * Upper bound of state or history paths this handler may target.
4381
- * Returning `void` is always permitted. Declaring a parent state also
4382
- * permits concrete descendant targets below it.
4383
- */
4384
- readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4385
- readonly transition: (
4386
- context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
4387
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4388
- ) => HandlerResult<States, any, any>
4389
- }
4528
+ ChildState,
4529
+ ChildEvent,
4530
+ ChildError,
4531
+ ChildRequirements,
4532
+ ChildOutput,
4533
+ ChildInitialError,
4534
+ Address extends ChildAddress<never>,
4535
+ Source = Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>
4536
+ > =
4537
+ & {
4538
+ readonly id: InvokeLifecycleId
4539
+ readonly address: Address & ChildAddress.Compatibility<Address, ChildEvent>
4540
+ readonly logic: Source
4541
+ readonly effect?: never
4542
+ readonly after?: never
4543
+ readonly child?: never
4544
+ readonly onSnapshot?: InvokeTransition<
4545
+ States,
4546
+ Events,
4547
+ Emits,
4548
+ InvokeSnapshotContext<States, Events, Emits, StateId, ChildState, ChildError, ChildOutput>
4549
+ >
4390
4550
  }
4391
- readonly initial?: StateInitialHandler<States, Events, Emits, StateId>
4392
- } & ActiveOutputHandlerConfig<States, Events, StateId>
4551
+ & InvokeDoneRequirement<
4552
+ ChildOutput,
4553
+ InvokeTransition<States, Events, Emits, InvokeDoneContext<States, Events, Emits, StateId, ChildOutput>>
4554
+ >
4555
+ & InvokeFailureRequirement<
4556
+ ChildError,
4557
+ InvokeTransition<States, Events, Emits, InvokeFailureContext<States, Events, Emits, StateId, ChildError>>
4558
+ >
4559
+
4560
+ export type ChildInvokeArgs<
4561
+ States extends StateSchemas,
4562
+ Events extends ReadonlyArray<TaggedSchema>,
4563
+ Emits extends ReadonlyArray<TaggedSchema>,
4564
+ StateId extends StateIdentifier<States>,
4565
+ ChildDefinition extends Machine.Any,
4566
+ Child extends ChildMachine<string, ChildDefinition>
4567
+ > =
4568
+ & {
4569
+ readonly child:
4570
+ & Child
4571
+ & (ChildDefinition extends EnsureExecutable<
4572
+ Machine.States<ChildDefinition>,
4573
+ Machine.UnhandledStates<ChildDefinition>,
4574
+ Machine.OutputStates<ChildDefinition>
4575
+ > ? unknown :
4576
+ never)
4577
+ readonly id?: never
4578
+ readonly address?: never
4579
+ readonly effect?: never
4580
+ readonly after?: never
4581
+ readonly logic?: never
4582
+ readonly onSnapshot?: InvokeTransition<
4583
+ States,
4584
+ Events,
4585
+ Emits,
4586
+ InvokeSnapshotContext<
4587
+ States,
4588
+ Events,
4589
+ Emits,
4590
+ StateId,
4591
+ Snapshot<Machine.States<ChildDefinition>>,
4592
+ Error<ChildDefinition>,
4593
+ Output<ChildDefinition>
4594
+ >
4595
+ >
4596
+ }
4597
+ & (Input<ChildDefinition> extends typeof Schema.Void ? { readonly input?: never } : {
4598
+ readonly input: InvokeSource<Input<ChildDefinition>["Type"], InvokeContext<States, Events, Emits, StateId>>
4599
+ })
4600
+ & InvokeDoneRequirement<
4601
+ Output<ChildDefinition>,
4602
+ InvokeTransition<
4603
+ States,
4604
+ Events,
4605
+ Emits,
4606
+ InvokeDoneContext<States, Events, Emits, StateId, Output<ChildDefinition>>
4607
+ >
4608
+ >
4609
+ & InvokeFailureRequirement<
4610
+ Error<ChildDefinition> | ActionError<Services<ChildDefinition>>,
4611
+ InvokeTransition<
4612
+ States,
4613
+ Events,
4614
+ Emits,
4615
+ InvokeFailureContext<
4616
+ States,
4617
+ Events,
4618
+ Emits,
4619
+ StateId,
4620
+ Error<ChildDefinition> | ActionError<Services<ChildDefinition>>
4621
+ >
4622
+ >
4623
+ >
4624
+
4625
+ export type LogicStateOf<Value> = Value extends Logic<infer State, any, any, any, any, any> ? State : never
4626
+ export type LogicEventOf<Value> = Value extends Logic<any, infer Event, any, any, any, any> ? Event : never
4627
+ export type LogicErrorOf<Value> = Value extends Logic<any, any, infer Error, any, any, any> ? Error : never
4628
+ export type LogicServicesOf<Value> = Value extends Logic<any, any, any, infer Services, any, any> ? Services : never
4629
+ export type LogicOutputOf<Value> = Value extends Logic<any, any, any, any, infer Output, any> ? Output : never
4630
+ export type LogicInitialErrorOf<Value> = Value extends Logic<any, any, any, any, any, infer Error> ? Error : never
4631
+
4632
+ type ContextualInvokeConfig<
4633
+ States extends StateSchemas,
4634
+ Events extends ReadonlyArray<TaggedSchema>,
4635
+ Emits extends ReadonlyArray<TaggedSchema>,
4636
+ StateId extends StateIdentifier<States>,
4637
+ Raw
4638
+ > = Raw extends InvokeTyped<any, any, any, any, any> ? unknown
4639
+ : Raw extends { readonly effect: infer Source } ?
4640
+ InvokeResolvedSource<Source> extends infer Fx extends Effect.Effect<any, any, any> ?
4641
+ & InvokeDoneRequirement<
4642
+ Effect.Success<Fx>,
4643
+ InvokeTransition<
4644
+ States,
4645
+ Events,
4646
+ Emits,
4647
+ InvokeDoneContext<States, Events, Emits, StateId, Effect.Success<Fx>>
4648
+ >
4649
+ >
4650
+ & InvokeFailureRequirement<
4651
+ Effect.Error<Fx>,
4652
+ InvokeTransition<
4653
+ States,
4654
+ Events,
4655
+ Emits,
4656
+ InvokeFailureContext<States, Events, Emits, StateId, Effect.Error<Fx>>
4657
+ >
4658
+ >
4659
+ & { readonly onSnapshot?: never }
4660
+ : never
4661
+ : Raw extends { readonly after: unknown } ? {
4662
+ readonly onDone: InvokeTransition<
4663
+ States,
4664
+ Events,
4665
+ Emits,
4666
+ InvokeDoneContext<States, Events, Emits, StateId, void>
4667
+ >
4668
+ readonly onFailure?: never
4669
+ readonly onSnapshot?: never
4670
+ }
4671
+ : Raw extends { readonly logic: infer Source; readonly address: infer Address } ?
4672
+ InvokeResolvedSource<Source> extends infer ChildLogic extends Logic<any, any, any, any, any, any> ?
4673
+ & InvokeDoneRequirement<
4674
+ InvokeOutput<Raw>,
4675
+ InvokeTransition<
4676
+ States,
4677
+ Events,
4678
+ Emits,
4679
+ InvokeDoneContext<States, Events, Emits, StateId, InvokeOutput<Raw>>
4680
+ >
4681
+ >
4682
+ & InvokeFailureRequirement<
4683
+ InvokeRuntimeError<Raw>,
4684
+ InvokeTransition<
4685
+ States,
4686
+ Events,
4687
+ Emits,
4688
+ InvokeFailureContext<States, Events, Emits, StateId, InvokeRuntimeError<Raw>>
4689
+ >
4690
+ >
4691
+ & {
4692
+ readonly address:
4693
+ & ChildAddress<never>
4694
+ & ChildAddress.Compatibility<
4695
+ Address,
4696
+ ChildLogic extends Logic<any, infer ChildEvent, any, any, any, any> ? ChildEvent : never
4697
+ >
4698
+ readonly onSnapshot?: InvokeTransition<
4699
+ States,
4700
+ Events,
4701
+ Emits,
4702
+ InvokeSnapshotContext<
4703
+ States,
4704
+ Events,
4705
+ Emits,
4706
+ StateId,
4707
+ ChildLogic extends Logic<infer ChildState, any, any, any, any, any> ? ChildState : never,
4708
+ InvokeRuntimeError<Raw>,
4709
+ InvokeOutput<Raw>
4710
+ >
4711
+ >
4712
+ }
4713
+ : never
4714
+ : Raw extends { readonly child: infer Child extends ChildMachine<string, infer ChildDefinition> } ?
4715
+ ChildMachineLogic<Child> extends infer ChildLogic extends Logic<any, any, any, any, any, any> ?
4716
+ & InvokeDoneRequirement<
4717
+ Output<ChildDefinition>,
4718
+ InvokeTransition<
4719
+ States,
4720
+ Events,
4721
+ Emits,
4722
+ InvokeDoneContext<States, Events, Emits, StateId, Output<ChildDefinition>>
4723
+ >
4724
+ >
4725
+ & InvokeFailureRequirement<
4726
+ Error<ChildDefinition> | ActionError<Services<ChildDefinition>>,
4727
+ InvokeTransition<
4728
+ States,
4729
+ Events,
4730
+ Emits,
4731
+ InvokeFailureContext<
4732
+ States,
4733
+ Events,
4734
+ Emits,
4735
+ StateId,
4736
+ Error<ChildDefinition> | ActionError<Services<ChildDefinition>>
4737
+ >
4738
+ >
4739
+ >
4740
+ & (Input<ChildDefinition> extends typeof Schema.Void ? { readonly input?: never } : {
4741
+ readonly input: InvokeSource<Input<ChildDefinition>["Type"], InvokeContext<States, Events, Emits, StateId>>
4742
+ })
4743
+ & {
4744
+ readonly onSnapshot?: InvokeTransition<
4745
+ States,
4746
+ Events,
4747
+ Emits,
4748
+ InvokeSnapshotContext<
4749
+ States,
4750
+ Events,
4751
+ Emits,
4752
+ StateId,
4753
+ Snapshot<Machine.States<ChildDefinition>>,
4754
+ Error<ChildDefinition>,
4755
+ Output<ChildDefinition>
4756
+ >
4757
+ >
4758
+ }
4759
+ : never
4760
+ : never
4761
+
4762
+ type ContextualInvokeDefinition<
4763
+ States extends StateSchemas,
4764
+ Events extends ReadonlyArray<TaggedSchema>,
4765
+ Emits extends ReadonlyArray<TaggedSchema>,
4766
+ StateId extends StateIdentifier<States>,
4767
+ Raw
4768
+ > = Raw extends ReadonlyArray<any> ? {
4769
+ readonly [Index in keyof Raw]: ContextualInvokeConfig<States, Events, Emits, StateId, Raw[Index]>
4770
+ }
4771
+ : ContextualInvokeConfig<States, Events, Emits, StateId, Raw>
4772
+
4773
+ type OutputHandlerConfig<
4774
+ States extends StateSchemas,
4775
+ Events extends ReadonlyArray<TaggedSchema>,
4776
+ StateId extends StateIdentifier<States>,
4777
+ Context
4778
+ > = NodeByIdentifier<States, StateId> extends { readonly output: Schema.Top } ? {
4779
+ readonly output: (context: Context) => OutputByIdentifier<States, StateId>
4780
+ }
4781
+ : {
4782
+ readonly output?: never
4783
+ }
4784
+
4785
+ type ActiveOutputHandlerConfig<
4786
+ States extends StateSchemas,
4787
+ Events extends ReadonlyArray<TaggedSchema>,
4788
+ StateId extends StateIdentifier<States>
4789
+ > = NodeByIdentifier<States, StateId> extends { readonly type: "parallel" } ? OutputHandlerConfig<
4790
+ States,
4791
+ Events,
4792
+ StateId,
4793
+ ParallelOutputContext<States, Events, StateId>
4794
+ >
4795
+ : {
4796
+ readonly output?: never
4797
+ }
4798
+
4799
+ /**
4800
+ * Configuration accepted for a non-final state.
4801
+ *
4802
+ * @category models
4803
+ * @since 0.4.0
4804
+ */
4805
+ export type ActiveStateConfig<
4806
+ States extends StateSchemas,
4807
+ Events extends ReadonlyArray<TaggedSchema>,
4808
+ Emits extends ReadonlyArray<TaggedSchema>,
4809
+ StateId extends StateIdentifier<States>,
4810
+ E,
4811
+ R
4812
+ > = {
4813
+ readonly entry?: (
4814
+ context: StateActionContext<States, Events, Emits, StateId>,
4815
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4816
+ ) => StateActionResult<any, any>
4817
+ readonly exit?: (
4818
+ context: StateActionContext<States, Events, Emits, StateId>,
4819
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4820
+ ) => StateActionResult<any, any>
4821
+ readonly invoke?: InvokeDefinition<States, Events, Emits, StateId>
4822
+ readonly always?:
4823
+ | ((
4824
+ context: AlwaysContext<States, Events, Emits, StateId>,
4825
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4826
+ ) => HandlerResult<States, any, any>)
4827
+ | {
4828
+ /** Statically declared upper bound of possible target paths. */
4829
+ readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4830
+ readonly transition: (
4831
+ context: AlwaysContext<States, Events, Emits, StateId>,
4832
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4833
+ ) => HandlerResult<States, any, any>
4834
+ }
4835
+ readonly onDone?:
4836
+ | ((
4837
+ context: DoneContext<States, Events, Emits, StateId>,
4838
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4839
+ ) => HandlerResult<States, any, any>)
4840
+ | {
4841
+ /** Statically declared upper bound of possible target paths. */
4842
+ readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4843
+ readonly transition: (
4844
+ context: DoneContext<States, Events, Emits, StateId>,
4845
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4846
+ ) => HandlerResult<States, any, any>
4847
+ }
4848
+ readonly on?: {
4849
+ readonly [EventTag in TagOf<Events[number]>]?:
4850
+ | ((
4851
+ context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
4852
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4853
+ ) => HandlerResult<States, any, any>)
4854
+ | {
4855
+ readonly reenter?: boolean
4856
+ /**
4857
+ * Upper bound of state or history paths this handler may target.
4858
+ * Returning `void` is always permitted. Declaring a parent state also
4859
+ * permits concrete descendant targets below it.
4860
+ */
4861
+ readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4862
+ readonly transition: (
4863
+ context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
4864
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4865
+ ) => HandlerResult<States, any, any>
4866
+ }
4867
+ }
4868
+ readonly initial?: StateInitialHandler<States, Events, Emits, StateId>
4869
+ } & ActiveOutputHandlerConfig<States, Events, StateId>
4393
4870
 
4394
4871
  /** Values supplied when a statechart implicitly enters a state's initial children. */
4395
4872
  export type StateInitialValue<
@@ -4577,6 +5054,39 @@ export declare namespace Machine {
4577
5054
  : Path extends keyof Config ? Config[Path]
4578
5055
  : never
4579
5056
 
5057
+ type HandlerInvokeContextAtPath<
5058
+ AllStates extends StateSchemas,
5059
+ Events extends ReadonlyArray<TaggedSchema>,
5060
+ Emits extends ReadonlyArray<TaggedSchema>,
5061
+ Config,
5062
+ StateId extends StateNodeIdentifier<AllStates>,
5063
+ NodeConfig = HandlerConfigAtPath<Config, StateId>
5064
+ > = StateId extends StateIdentifier<AllStates> ?
5065
+ NodeConfig extends { readonly invoke: infer Invoke } ? HandlerValidationAtPath<
5066
+ StateId,
5067
+ { readonly invoke: ContextualInvokeDefinition<AllStates, Events, Emits, StateId, Invoke> }
5068
+ >
5069
+ : unknown
5070
+ : unknown
5071
+
5072
+ type HandlerInvokeContexts<
5073
+ AllStates extends StateSchemas,
5074
+ Events extends ReadonlyArray<TaggedSchema>,
5075
+ Emits extends ReadonlyArray<TaggedSchema>,
5076
+ Config
5077
+ > = Types.UnionToIntersection<
5078
+ StateNodeIdentifier<AllStates> extends infer StateId extends StateNodeIdentifier<AllStates> ?
5079
+ StateId extends StateNodeIdentifier<AllStates> ? HandlerInvokeContextAtPath<
5080
+ AllStates,
5081
+ Events,
5082
+ Emits,
5083
+ Config,
5084
+ StateId
5085
+ >
5086
+ : never
5087
+ : never
5088
+ >
5089
+
4580
5090
  // Rebuild the public nested handler shape so branded validation errors stay
4581
5091
  // attached to the exact property that introduced them.
4582
5092
  type HandlerValidationAtPath<Path extends string, Validation> = Path extends `${infer Head}.${infer Rest}` ? {
@@ -4891,9 +5401,7 @@ export declare namespace Machine {
4891
5401
  & HandlerOnTargetValidation<StateId, Config>
4892
5402
  & HandlerDirectTargetValidation<StateId, Config, "always">
4893
5403
  & HandlerDirectTargetValidation<StateId, Config, "onDone">
4894
- & HandlerInvokeOutputValidation<Events, StateId, Config>
4895
5404
  & HandlerInvokeEmitsValidation<Events, StateId, Config>
4896
- & HandlerInvokeSnapshotValidation<Events, StateId, Config>
4897
5405
  & HandlerChildrenValidation<Node, StateId, Config>
4898
5406
  & HandlerOutputRequirementValidation<AllStates, StateId, AvailableOutputStates, Config>
4899
5407
  & HandlerRuntimeValidation<Events, Emits, StateId, Config>
@@ -4922,20 +5430,6 @@ export declare namespace Machine {
4922
5430
  }
4923
5431
  : unknown
4924
5432
 
4925
- type HandlerInvokeOutputValidation<
4926
- Events extends ReadonlyArray<TaggedSchema>,
4927
- StateId extends string,
4928
- Config
4929
- > = [InvokeReturn<Config>] extends [never] ? unknown
4930
- : [Exclude<InvokeOutput<InvokeReturn<Config>>, EventOf<Events> | void>] extends [never] ? unknown
4931
- : {
4932
- readonly invoke: HandlerValidationError<
4933
- "Invoked child output must be a machine event or void",
4934
- StateId,
4935
- Exclude<InvokeOutput<InvokeReturn<Config>>, EventOf<Events> | void>
4936
- >
4937
- }
4938
-
4939
5433
  type HandlerInvokeEmitsValidation<
4940
5434
  Events extends ReadonlyArray<TaggedSchema>,
4941
5435
  StateId extends string,
@@ -4950,21 +5444,6 @@ export declare namespace Machine {
4950
5444
  >
4951
5445
  }
4952
5446
 
4953
- type HandlerInvokeSnapshotValidation<
4954
- Events extends ReadonlyArray<TaggedSchema>,
4955
- StateId extends string,
4956
- Config
4957
- > = [InvokeReturn<Config>] extends [never] ? unknown
4958
- : IsAny<InvokeSnapshotEvent<InvokeReturn<Config>>> extends true ? unknown
4959
- : [Exclude<InvokeSnapshotEvent<InvokeReturn<Config>>, EventOf<Events> | undefined>] extends [never] ? unknown
4960
- : {
4961
- readonly invoke: HandlerValidationError<
4962
- "Invoked child snapshot mapper must return a machine event or undefined",
4963
- StateId,
4964
- Exclude<InvokeSnapshotEvent<InvokeReturn<Config>>, EventOf<Events> | undefined>
4965
- >
4966
- }
4967
-
4968
5447
  type HandlerRuntimeValidation<
4969
5448
  Events extends ReadonlyArray<TaggedSchema>,
4970
5449
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -5168,6 +5647,7 @@ export declare namespace Machine {
5168
5647
  <const Config extends HandlerTree<States, States, Events, Emits, E, R, "">>(
5169
5648
  config:
5170
5649
  & Config
5650
+ & HandlerInvokeContexts<States, Events, Emits, NoInfer<Config>>
5171
5651
  & HandlerTreeValidation<
5172
5652
  States,
5173
5653
  Events,
@@ -5489,8 +5969,8 @@ interface Make {
5489
5969
  * to implement state behavior with ordinary TypeScript control flow.
5490
5970
  *
5491
5971
  * Schemas in `events` define the public input protocol. Schemas in
5492
- * `internalEvents` are added to the complete handler protocol for invoke
5493
- * results, child emissions, and other machine-local deliveries. Their tags
5972
+ * `internalEvents` are added to the complete handler protocol for raised
5973
+ * events, child emissions, and other machine-local deliveries. Their tags
5494
5974
  * must be disjoint.
5495
5975
  *
5496
5976
  * **Example** (Typed counter machine)
@@ -5544,6 +6024,9 @@ type EventConstructorArgs<EventSchema extends Machine.TaggedSchema> = {} extends
5544
6024
  * Events supplied through ordinary `send` and `plan` calls remain untrusted
5545
6025
  * and continue through full runtime schema validation.
5546
6026
  * Treat the returned event as immutable after construction.
6027
+ * This eager low-level constructor throws `MachineSchemaDecodeError` when
6028
+ * construction fails. Prefer {@link events} and {@link internalEvents} for
6029
+ * ordinary delivery so construction remains inside the machine error channel.
5547
6030
  *
5548
6031
  * The schema must be one of the machine's configured public or internal event
5549
6032
  * schemas, or a case schema belonging to a configured `Schema.TaggedUnion`.
@@ -5578,6 +6061,43 @@ export const event: <const M extends Machine.Any, const EventSchema extends Mach
5578
6061
  ...args: EventConstructorArgs<EventSchema>
5579
6062
  ) => EventSchema["Type"] = internal.event
5580
6063
 
6064
+ /**
6065
+ * Returns deferred constructors for every public event in a machine protocol.
6066
+ *
6067
+ * Constructor inputs retain their schema-derived required fields, defaults,
6068
+ * and transformations. Construction is deferred until delivery so failures
6069
+ * enter the machine's `MachineSchemaDecodeError` channel rather than throwing
6070
+ * at the call site.
6071
+ *
6072
+ * **Example**
6073
+ *
6074
+ * ```ts
6075
+ * const Event = Machine.events(counter)
6076
+ * yield* ref.send(Event.Increment({ by: 1 }))
6077
+ * ```
6078
+ *
6079
+ * @category constructors
6080
+ * @since 0.9.0
6081
+ */
6082
+ export const events: <const M extends Machine.Any>(
6083
+ machine: M
6084
+ ) => Machine.EventConstructors<Machine.InputEvents<M>> = internal.events
6085
+
6086
+ /**
6087
+ * Returns deferred constructors for every internal event in a machine
6088
+ * protocol.
6089
+ *
6090
+ * Use these constructors for raised events and other machine-local deliveries.
6091
+ * Construction failures are reported through the
6092
+ * owning machine's `MachineSchemaDecodeError` channel.
6093
+ *
6094
+ * @category constructors
6095
+ * @since 0.9.0
6096
+ */
6097
+ export const internalEvents: <const M extends Machine.Any>(
6098
+ machine: M
6099
+ ) => Machine.EventConstructors<Machine.InternalEvents<M>> = internal.internalEvents
6100
+
5581
6101
  /**
5582
6102
  * Encodes a decoded machine snapshot into a normalized data representation.
5583
6103
  *
@@ -5743,200 +6263,305 @@ export const decodeSnapshot: <
5743
6263
  Machine.SnapshotDecodingServices<States>
5744
6264
  > = internal.decodeSnapshot
5745
6265
 
5746
- /**
5747
- * Creates an invoked child process configuration for an active state.
5748
- *
5749
- * **When to use**
5750
- *
5751
- * Use to run a child process while a machine remains in a state. Successful
5752
- * outputs are sent directly to the parent machine as events; `void` sends
5753
- * nothing. Unrecovered child failures fail the owning machine. Active
5754
- * snapshots can optionally be mapped to progress events.
5755
- *
5756
- * **Gotchas**
5757
- *
5758
- * Invoked child processes run while their owning state is active and are
5759
- * stopped before the state exits. An unrecovered child failure fails the owning
5760
- * machine; recover inside the child Effect when failure should become an event.
5761
- * The `id` is a state-local lifecycle key, not a communication address. To
5762
- * send events to the invocation, pass a typed `childAddress` as `address`.
5763
- * The `src` callback is intentionally independent from its parent state. When
5764
- * construction depends on the typed state, lifecycle event, or runtime, use
5765
- * the state config factory form `invoke: (context) => Machine.invoke(...)` and
5766
- * close over that context from `src`.
5767
- *
5768
- * **Example** (Effect output as a parent event)
5769
- *
5770
- * ```ts
5771
- * import { Effect, Schema } from "effect"
5772
- * import { Machine } from "@typeonce/effect-machine"
5773
- *
5774
- * class Loaded extends Schema.TaggedClass<Loaded>("Loaded")("Loaded", {
5775
- * value: Schema.String
5776
- * }) {}
5777
- *
5778
- * const load = Machine.invoke({
5779
- * id: "load",
5780
- * src: () => Machine.effect(Effect.succeed(new Loaded({ value: "ready" })))
5781
- * })
5782
- * ```
5783
- *
5784
- * @see {@link effect} for one-shot child effects.
5785
- * @see {@link spawn} for children whose lifetime is controlled by actions.
5786
- * @category constructors
5787
- * @since 0.4.0
5788
- */
5789
- export const invoke: <
5790
- ChildState,
5791
- ChildEvent,
5792
- ChildError = never,
5793
- ChildRequirements = never,
5794
- ChildOutput = never,
5795
- ChildInitialError = never,
5796
- Event = never,
5797
- Address extends ChildAddress<never> | undefined = undefined
5798
- >(
5799
- config:
5800
- & {
5801
- readonly id: InvokeLifecycleId
5802
- readonly src: () => Logic<
5803
- ChildState,
5804
- ChildEvent,
5805
- ChildError,
5806
- ChildRequirements,
5807
- ChildOutput,
5808
- ChildInitialError
5809
- >
5810
- readonly snapshot?: (
5811
- context: Machine.InvokeSnapshotContext<ChildState, ChildError, ChildOutput>
5812
- ) => Event | undefined
5813
- }
5814
- & ([Address] extends [undefined] ? {
5815
- readonly address?: never
5816
- }
5817
- : {
5818
- readonly address: Exclude<Address, undefined>
5819
- } & ChildAddress.Compatibility<Exclude<Address, undefined>, NoInfer<ChildEvent>>)
5820
- ) => Machine.InvokeConfig<
5821
- any,
5822
- any,
5823
- any,
5824
- any,
5825
- Event,
5826
- ChildState,
5827
- ChildEvent,
5828
- ChildError,
5829
- ChildRequirements,
5830
- ChildOutput,
5831
- ChildInitialError
5832
- > = internal.invoke
5833
-
5834
- type InvokeEffectResult<Requirements, Event> = Machine.InvokeConfig<
5835
- any,
5836
- any,
5837
- any,
5838
- any,
5839
- never,
5840
- void,
5841
- never,
5842
- never,
5843
- Requirements,
5844
- Event | void,
5845
- never
6266
+ type DynamicEffectInvokeSource<
6267
+ States extends Machine.StateSchemas,
6268
+ Events extends ReadonlyArray<Machine.TaggedSchema>,
6269
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
6270
+ StateId extends Machine.StateIdentifier<States>,
6271
+ Source extends (
6272
+ context: Machine.InvokeContext<States, Events, Emits, StateId>
6273
+ ) => Effect.Effect<unknown, unknown, unknown>
6274
+ > = {
6275
+ readonly id: InvokeLifecycleId
6276
+ readonly effect: Source
6277
+ readonly after?: never
6278
+ readonly logic?: never
6279
+ readonly child?: never
6280
+ readonly address?: never
6281
+ readonly onSnapshot?: never
6282
+ }
6283
+
6284
+ type DynamicEffectDoneHandler<
6285
+ States extends Machine.StateSchemas,
6286
+ Events extends ReadonlyArray<Machine.TaggedSchema>,
6287
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
6288
+ StateId extends Machine.StateIdentifier<States>,
6289
+ Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
6290
+ > = Machine.InvokeTransition<
6291
+ States,
6292
+ Events,
6293
+ Emits,
6294
+ Machine.InvokeDoneContext<States, Events, Emits, StateId, Effect.Success<ReturnType<NoInfer<Source>>>>
5846
6295
  >
5847
6296
 
5848
- type InvokeEffectIsInfallible<Fx extends Effect.Effect<any, any, any>> = IsAny<Effect.Error<Fx>> extends true ? false
5849
- : [Effect.Error<Fx>] extends [never] ? true
5850
- : false
6297
+ type DynamicEffectFailureHandler<
6298
+ States extends Machine.StateSchemas,
6299
+ Events extends ReadonlyArray<Machine.TaggedSchema>,
6300
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
6301
+ StateId extends Machine.StateIdentifier<States>,
6302
+ Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
6303
+ > = Machine.InvokeTransition<
6304
+ States,
6305
+ Events,
6306
+ Emits,
6307
+ Machine.InvokeFailureContext<States, Events, Emits, StateId, Effect.Error<ReturnType<NoInfer<Source>>>>
6308
+ >
5851
6309
 
5852
- type InvokeEffectConfig<
5853
- Fx extends Effect.Effect<any, any, any>,
5854
- SuccessEvent,
5855
- FailureEvent
6310
+ type DynamicEffectInvokeResult<
6311
+ States extends Machine.StateSchemas,
6312
+ Events extends ReadonlyArray<Machine.TaggedSchema>,
6313
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
6314
+ StateId extends Machine.StateIdentifier<States>,
6315
+ Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
5856
6316
  > =
5857
- & {
5858
- readonly id: InvokeLifecycleId
5859
- readonly effect: Fx
5860
- readonly onSuccess: (value: NoInfer<Effect.Success<Fx>>) => SuccessEvent | void
5861
- }
5862
- & (
5863
- InvokeEffectIsInfallible<Fx> extends true ? {
5864
- readonly onFailure?: never
5865
- }
5866
- : {
5867
- readonly onFailure: (error: NoInfer<Effect.Error<Fx>>) => FailureEvent | void
5868
- }
5869
- )
6317
+ & Machine.InvokeConfig<States, Events, Emits, StateId>
6318
+ & Machine.InvokeTyped<
6319
+ Effect.Success<ReturnType<Source>>,
6320
+ Effect.Error<ReturnType<Source>>,
6321
+ Effect.Services<ReturnType<Source>>,
6322
+ never
6323
+ >
6324
+
6325
+ type InvokeChannelIsNever<Value> = IsAny<Value> extends true ? false : [Value] extends [never] ? true : false
5870
6326
 
5871
6327
  /**
5872
- * Invokes one Effect and maps its typed outcome into machine-local events.
6328
+ * Preserves inference for a state-owned invocation configuration.
5873
6329
  *
5874
- * **Details**
6330
+ * Use `effect` for one-shot work, `after` for a cancellable timer, `logic` for
6331
+ * reusable process logic, or `child` for a complete child machine. `onDone` is
6332
+ * required whenever the source can complete with an output, while `onFailure`
6333
+ * is required only when the source has a typed failure channel.
5875
6334
  *
5876
- * This is the high-level one-shot counterpart to `invoke`. Success and typed
5877
- * failure values are mapped independently, so callers do not need to recover
5878
- * an Effect into a common event union by hand. Defects and interruption remain
5879
- * failures of the owning machine.
5880
- *
5881
- * Declare mapped outcomes in `internalEvents` unless they are also legitimate
5882
- * public commands.
5883
- *
5884
- * **Example**
6335
+ * This constructor is an identity at runtime, but preserves lifecycle callback
6336
+ * inference through published declarations. Effects and durations may be
6337
+ * supplied directly or derived from the owning state's entry context. Dynamic
6338
+ * Effect sources infer the owner context, output, error, and service channels
6339
+ * together without a return annotation. Logic invocations require both a
6340
+ * lifecycle `id` and a typed communication `address`. Child descriptors already
6341
+ * own their identity, so `id` and `address` must not be repeated.
5885
6342
  *
5886
6343
  * ```ts
5887
- * import { Effect, Schema } from "effect"
5888
- * import { Machine } from "@typeonce/effect-machine"
5889
- *
5890
- * class Loaded extends Schema.TaggedClass<Loaded>("Loaded")("Loaded", {
5891
- * value: Schema.String
5892
- * }) {}
5893
- *
5894
- * const load = Machine.invokeEffect({
6344
+ * invoke: Machine.invoke({
5895
6345
  * id: "load",
5896
- * effect: Effect.succeed("ready"),
5897
- * onSuccess: (value) => new Loaded({ value })
6346
+ * effect: Effect.tryPromise({
6347
+ * try: () => fetch("/api/data").then((response) => response.json()),
6348
+ * catch: (cause) => new LoadError({ cause })
6349
+ * }),
6350
+ * onDone: ({ output, target }) => target.full.Ready({ data: output }),
6351
+ * onFailure: ({ error, target }) => target.full.Failed({ error })
5898
6352
  * })
5899
6353
  * ```
5900
6354
  *
5901
- * @see {@link invoke} for arbitrary child process logic.
5902
- * @see {@link after} for a state-scoped delayed event.
5903
- * @category constructors
5904
- * @since 0.4.0
5905
- */
5906
- export const invokeEffect: <const Fx extends Effect.Effect<any, any, any>, SuccessEvent, FailureEvent = never>(
5907
- config: InvokeEffectConfig<Fx, SuccessEvent, FailureEvent>
5908
- ) => InvokeEffectResult<
5909
- Effect.Services<Fx>,
5910
- SuccessEvent | (InvokeEffectIsInfallible<Fx> extends true ? never : FailureEvent)
5911
- > = internal.invokeEffect
5912
-
5913
- /**
5914
- * Creates a cancellable state-scoped delayed event.
5915
- *
5916
- * The timer starts when its owning state is entered and is interrupted when
5917
- * that state exits. The delayed value should normally be declared in
5918
- * `internalEvents`.
5919
- *
5920
- * **Example**
5921
- *
5922
- * ```ts
5923
- * import { Schema } from "effect"
5924
- * import { Machine } from "@typeonce/effect-machine"
5925
- *
5926
- * class TimedOut extends Schema.TaggedClass<TimedOut>("TimedOut")("TimedOut", {}) {}
5927
- *
5928
- * const timeout = Machine.after("5 seconds", new TimedOut({}))
5929
- * ```
5930
- *
5931
6355
  * @category constructors
5932
- * @since 0.4.0
6356
+ * @since 0.9.0
5933
6357
  */
5934
- export const after: <Event extends { readonly _tag: PropertyKey }>(
5935
- duration: Duration.Input,
5936
- event: Event,
5937
- options?: { readonly id?: InvokeLifecycleId }
5938
- ) => InvokeEffectResult<never, Event> = internal.after
5939
-
6358
+ export const invoke: {
6359
+ <
6360
+ const States extends Machine.StateSchemas,
6361
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6362
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6363
+ StateId extends Machine.StateIdentifier<States>,
6364
+ const Source extends (
6365
+ context: Machine.InvokeContext<States, Events, Emits, StateId>
6366
+ ) => Effect.Effect<unknown, unknown, unknown>
6367
+ >(
6368
+ config:
6369
+ & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
6370
+ & {
6371
+ readonly onDone: DynamicEffectDoneHandler<States, Events, Emits, StateId, Source>
6372
+ readonly onFailure: DynamicEffectFailureHandler<States, Events, Emits, StateId, Source>
6373
+ },
6374
+ ..._validation: InvokeChannelIsNever<Effect.Success<ReturnType<Source>>> extends true ? [
6375
+ "onDone must be omitted when the Effect output is never"
6376
+ ]
6377
+ : InvokeChannelIsNever<Effect.Error<ReturnType<Source>>> extends true ? [
6378
+ "onFailure must be omitted when the Effect error is never"
6379
+ ]
6380
+ : []
6381
+ ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
6382
+ <
6383
+ const States extends Machine.StateSchemas,
6384
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6385
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6386
+ StateId extends Machine.StateIdentifier<States>,
6387
+ const Source extends (
6388
+ context: Machine.InvokeContext<States, Events, Emits, StateId>
6389
+ ) => Effect.Effect<unknown, never, unknown>
6390
+ >(
6391
+ config:
6392
+ & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
6393
+ & {
6394
+ readonly onDone: DynamicEffectDoneHandler<States, Events, Emits, StateId, Source>
6395
+ readonly onFailure?: never
6396
+ },
6397
+ ..._validation: InvokeChannelIsNever<Effect.Success<ReturnType<Source>>> extends true ? [
6398
+ "onDone must be omitted when the Effect output is never"
6399
+ ]
6400
+ : []
6401
+ ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
6402
+ <
6403
+ const States extends Machine.StateSchemas,
6404
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6405
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6406
+ StateId extends Machine.StateIdentifier<States>,
6407
+ const Source extends (
6408
+ context: Machine.InvokeContext<States, Events, Emits, StateId>
6409
+ ) => Effect.Effect<never, unknown, unknown>
6410
+ >(
6411
+ config:
6412
+ & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
6413
+ & {
6414
+ readonly onDone?: never
6415
+ readonly onFailure: DynamicEffectFailureHandler<States, Events, Emits, StateId, Source>
6416
+ },
6417
+ ..._validation: InvokeChannelIsNever<Effect.Error<ReturnType<Source>>> extends true ? [
6418
+ "onFailure must be omitted when the Effect error is never"
6419
+ ]
6420
+ : []
6421
+ ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
6422
+ <
6423
+ const States extends Machine.StateSchemas,
6424
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6425
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6426
+ StateId extends Machine.StateIdentifier<States>,
6427
+ const Source extends (
6428
+ context: Machine.InvokeContext<States, Events, Emits, StateId>
6429
+ ) => Effect.Effect<never, never, unknown>
6430
+ >(
6431
+ config:
6432
+ & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
6433
+ & {
6434
+ readonly onDone?: never
6435
+ readonly onFailure?: never
6436
+ }
6437
+ ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
6438
+ <
6439
+ const States extends Machine.StateSchemas,
6440
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6441
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6442
+ StateId extends Machine.StateIdentifier<States>,
6443
+ const Fx extends Effect.Effect<any, any, any>,
6444
+ const Config extends object
6445
+ >(
6446
+ config: Config & Machine.EffectInvokeArgs<States, Events, Emits, StateId, Fx>
6447
+ ):
6448
+ & Config
6449
+ & Machine.InvokeOwned<States, Events, Emits, StateId>
6450
+ & Machine.InvokeTyped<
6451
+ Effect.Success<Fx>,
6452
+ Effect.Error<Fx>,
6453
+ Effect.Services<Fx>,
6454
+ never
6455
+ >
6456
+ <
6457
+ const States extends Machine.StateSchemas,
6458
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6459
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6460
+ StateId extends Machine.StateIdentifier<States>,
6461
+ const Config extends object
6462
+ >(
6463
+ config: Config & Machine.TimerInvokeArgs<States, Events, Emits, StateId>
6464
+ ): Config & Machine.InvokeOwned<States, Events, Emits, StateId> & Machine.InvokeTyped<void, never, never, never>
6465
+ <
6466
+ const States extends Machine.StateSchemas,
6467
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6468
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6469
+ StateId extends Machine.StateIdentifier<States>,
6470
+ ChildState,
6471
+ ChildEvent,
6472
+ ChildError,
6473
+ ChildRequirements,
6474
+ ChildOutput,
6475
+ ChildInitialError,
6476
+ Address extends ChildAddress<never>
6477
+ >(
6478
+ config: Machine.LogicInvokeArgs<
6479
+ States,
6480
+ Events,
6481
+ Emits,
6482
+ StateId,
6483
+ ChildState,
6484
+ ChildEvent,
6485
+ ChildError,
6486
+ ChildRequirements,
6487
+ ChildOutput,
6488
+ ChildInitialError,
6489
+ Address,
6490
+ (context: Machine.InvokeContext<States, Events, Emits, StateId>) => Logic<
6491
+ ChildState,
6492
+ ChildEvent,
6493
+ ChildError,
6494
+ ChildRequirements,
6495
+ ChildOutput,
6496
+ ChildInitialError
6497
+ >
6498
+ >
6499
+ ):
6500
+ & Machine.InvokeConfig<States, Events, Emits, StateId>
6501
+ & Machine.InvokeTyped<
6502
+ ChildOutput,
6503
+ ChildError,
6504
+ ChildRequirements,
6505
+ ChildInitialError
6506
+ >
6507
+ <
6508
+ const States extends Machine.StateSchemas,
6509
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6510
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6511
+ StateId extends Machine.StateIdentifier<States>,
6512
+ ChildState,
6513
+ ChildEvent,
6514
+ ChildError,
6515
+ ChildRequirements,
6516
+ ChildOutput,
6517
+ ChildInitialError,
6518
+ Address extends ChildAddress<never>,
6519
+ const Config extends object
6520
+ >(
6521
+ config:
6522
+ & Config
6523
+ & Machine.LogicInvokeArgs<
6524
+ States,
6525
+ Events,
6526
+ Emits,
6527
+ StateId,
6528
+ ChildState,
6529
+ ChildEvent,
6530
+ ChildError,
6531
+ ChildRequirements,
6532
+ ChildOutput,
6533
+ ChildInitialError,
6534
+ Address
6535
+ >
6536
+ ):
6537
+ & Config
6538
+ & Machine.InvokeOwned<States, Events, Emits, StateId>
6539
+ & Machine.InvokeTyped<
6540
+ ChildOutput,
6541
+ ChildError,
6542
+ ChildRequirements,
6543
+ ChildInitialError
6544
+ >
6545
+ <
6546
+ const States extends Machine.StateSchemas,
6547
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6548
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6549
+ StateId extends Machine.StateIdentifier<States>,
6550
+ const Child extends ChildMachine.Any,
6551
+ const Config extends object
6552
+ >(
6553
+ config: Config & Machine.ChildInvokeArgs<States, Events, Emits, StateId, Child["machine"], Child>
6554
+ ):
6555
+ & Config
6556
+ & Machine.InvokeOwned<States, Events, Emits, StateId>
6557
+ & Machine.InvokeTyped<
6558
+ Machine.Output<Child["machine"]>,
6559
+ Machine.Error<Child["machine"]> | ActionError<Machine.Services<Child["machine"]>>,
6560
+ Machine.Services<Child["machine"]>,
6561
+ Machine.InitialError<Child["machine"]>,
6562
+ Machine.Emit<Child["machine"]>
6563
+ >
6564
+ } = ((config: unknown) => config) as any
5940
6565
  type RetagFields<Target extends Machine.TaggedSchema> = Omit<Target["~type.make.in"], "_tag">
5941
6566
 
5942
6567
  type RetagTargetCompatibility<Target extends Machine.TaggedSchema> = Target extends {
@@ -5988,195 +6613,6 @@ export const retag: <const Target extends Machine.TaggedSchema, const Source ext
5988
6613
  ...args: RetagArgs<Target, Source>
5989
6614
  ) => Target["Type"] = internal.retag
5990
6615
 
5991
- type InvokeMachineInput<Input extends Schema.Top> = Input extends typeof Schema.Void ? {
5992
- readonly input?: never
5993
- }
5994
- : {
5995
- readonly input: Input["Type"]
5996
- }
5997
-
5998
- /**
5999
- * Creates an invoked child process from a complete statechart machine.
6000
- *
6001
- * **When to use**
6002
- *
6003
- * Use when a state should own another statechart machine and communicate with
6004
- * it through typed child events, emissions, snapshots, or terminal output.
6005
- *
6006
- * **Details**
6007
- *
6008
- * Child emissions are delivered directly to the parent as events. Active
6009
- * snapshots and terminal output can be mapped to parent events. The owning
6010
- * state controls the child lifetime.
6011
- *
6012
- * **Gotchas**
6013
- *
6014
- * Active invoked machines must have unique child addresses. A machine starts
6015
- * after its owning state's entry actions, so those actions cannot send events
6016
- * to a newly entered child. Unrecovered child failures fail the parent.
6017
- *
6018
- * @see {@link invoke} for invoking lower-level process logic.
6019
- * @see {@link sendTo} for sending events to the invoked machine.
6020
- * @category constructors
6021
- * @since 0.4.0
6022
- */
6023
- export const invokeMachine: {
6024
- <
6025
- const States extends Machine.StateSchemas,
6026
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
6027
- const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
6028
- const Input extends Schema.Top = typeof Schema.Void,
6029
- UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
6030
- E = never,
6031
- R = never,
6032
- InitialE = never,
6033
- InitialR = never,
6034
- FinalStates extends Machine.StateIdentifier<States> = never,
6035
- Output = never,
6036
- SnapshotEvent = never,
6037
- DoneEvent = never,
6038
- Id extends string = string,
6039
- OutputStates extends Machine.StateIdentifier<States> = never,
6040
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
6041
- >(
6042
- config:
6043
- & {
6044
- readonly child: ChildMachine<
6045
- Id,
6046
- & Machine<
6047
- States,
6048
- Events,
6049
- Input,
6050
- UnhandledStates,
6051
- E,
6052
- R,
6053
- InitialE,
6054
- InitialR,
6055
- FinalStates,
6056
- Output,
6057
- Emits,
6058
- OutputStates,
6059
- InputEvents
6060
- >
6061
- & EnsureExecutable<States, UnhandledStates, OutputStates>
6062
- >
6063
- readonly snapshot?: (
6064
- context: Machine.InvokeSnapshotContext<
6065
- Machine.Snapshot<States>,
6066
- | E
6067
- | ActionError<R>
6068
- | InfiniteTransitionError
6069
- | MachineSchemaDecodeError
6070
- | StoppedError,
6071
- Output
6072
- >
6073
- ) => SnapshotEvent | undefined
6074
- readonly onDone: (context: Machine.InvokeDoneContext<Output>) => DoneEvent | undefined
6075
- }
6076
- & InvokeMachineInput<Input>
6077
- ): Machine.InvokeConfig<
6078
- any,
6079
- any,
6080
- any,
6081
- any,
6082
- SnapshotEvent,
6083
- Machine.Snapshot<States>,
6084
- Machine.EventOf<InputEvents>,
6085
- E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
6086
- ExcludeCompatibleRuntime<
6087
- Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>,
6088
- Machine.EventOf<Events>,
6089
- Machine.EmitOf<Emits>
6090
- >,
6091
- Output,
6092
- | InitialE
6093
- | E
6094
- | ActionError<InitialR | R>
6095
- | InfiniteTransitionError
6096
- | MachineSchemaDecodeError
6097
- | StartupError
6098
- | StoppedError,
6099
- Machine.EmitOf<Emits>,
6100
- DoneEvent
6101
- >
6102
- <
6103
- const States extends Machine.StateSchemas,
6104
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
6105
- const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
6106
- const Input extends Schema.Top = typeof Schema.Void,
6107
- UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
6108
- E = never,
6109
- R = never,
6110
- InitialE = never,
6111
- InitialR = never,
6112
- FinalStates extends Machine.StateIdentifier<States> = never,
6113
- Output = never,
6114
- SnapshotEvent = never,
6115
- Id extends string = string,
6116
- OutputStates extends Machine.StateIdentifier<States> = never,
6117
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
6118
- >(
6119
- config:
6120
- & {
6121
- readonly child: ChildMachine<
6122
- Id,
6123
- & Machine<
6124
- States,
6125
- Events,
6126
- Input,
6127
- UnhandledStates,
6128
- E,
6129
- R,
6130
- InitialE,
6131
- InitialR,
6132
- FinalStates,
6133
- Output,
6134
- Emits,
6135
- OutputStates,
6136
- InputEvents
6137
- >
6138
- & EnsureExecutable<States, UnhandledStates, OutputStates>
6139
- >
6140
- readonly snapshot?: (
6141
- context: Machine.InvokeSnapshotContext<
6142
- Machine.Snapshot<States>,
6143
- | E
6144
- | ActionError<R>
6145
- | InfiniteTransitionError
6146
- | MachineSchemaDecodeError
6147
- | StoppedError,
6148
- Output
6149
- >
6150
- ) => SnapshotEvent | undefined
6151
- readonly onDone?: never
6152
- }
6153
- & InvokeMachineInput<Input>
6154
- ): Machine.InvokeConfig<
6155
- any,
6156
- any,
6157
- any,
6158
- any,
6159
- SnapshotEvent,
6160
- Machine.Snapshot<States>,
6161
- Machine.EventOf<InputEvents>,
6162
- E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
6163
- ExcludeCompatibleRuntime<
6164
- Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>,
6165
- Machine.EventOf<Events>,
6166
- Machine.EmitOf<Emits>
6167
- >,
6168
- Output,
6169
- | InitialE
6170
- | E
6171
- | ActionError<InitialR | R>
6172
- | InfiniteTransitionError
6173
- | MachineSchemaDecodeError
6174
- | StartupError
6175
- | StoppedError,
6176
- Machine.EmitOf<Emits>
6177
- >
6178
- } = internal.invokeMachine
6179
-
6180
6616
  /**
6181
6617
  * Plans the initial state for a machine without executing actor commands.
6182
6618
  *
@@ -6338,10 +6774,9 @@ export const transitionDefinitions: <M extends Machine.Any>(machine: M) => Reado
6338
6774
  *
6339
6775
  * **Details**
6340
6776
  *
6341
- * Static `invoke`, `invokeEffect`, `after`, and `invokeMachine` descriptors
6342
- * expose stable ownership and lifecycle metadata without serializing runtime
6343
- * values. Function-valued invoke factories are represented as dynamic and are
6344
- * never evaluated during inspection.
6777
+ * Static inline invocation definitions expose stable ownership and lifecycle
6778
+ * metadata without serializing runtime values. Function-valued sources are
6779
+ * represented as dynamic and are never evaluated during inspection.
6345
6780
  *
6346
6781
  * @category getters
6347
6782
  * @since 0.4.0
@@ -6489,7 +6924,7 @@ export const plan: <
6489
6924
  >
6490
6925
  & EnsureExecutable<States, UnhandledStates, OutputStates>,
6491
6926
  state: Machine.Snapshot<States>,
6492
- event: Machine.EventOf<InputEvents>
6927
+ event: Machine.EventInputOf<InputEvents>
6493
6928
  ) => Effect.Effect<
6494
6929
  & {
6495
6930
  readonly next: Machine.Snapshot<States>
@@ -6527,52 +6962,6 @@ export const plan: <
6527
6962
  never
6528
6963
  > = internal.plan
6529
6964
 
6530
- /**
6531
- * Creates a one-shot child process from an Effect.
6532
- *
6533
- * **When to use**
6534
- *
6535
- * Use when you need side effects that produce one typed output or error.
6536
- *
6537
- * **Details**
6538
- *
6539
- * The Effect may run arbitrary side effects. Its success value is the process
6540
- * output, its typed error is preserved, and its services are inferred. When
6541
- * invoked, the output is sent to the owning machine as an event unless it is
6542
- * `void`.
6543
- *
6544
- * **Gotchas**
6545
- *
6546
- * This process has no incoming event protocol. Its Effect runs once. Use
6547
- * `transition` for a process that receives events over time and `logic` for
6548
- * direct machine-local communication or intermediate snapshots.
6549
- *
6550
- * **Example** (Recover a child failure as output)
6551
- *
6552
- * ```ts
6553
- * import { Effect, Schema } from "effect"
6554
- * import { Machine } from "@typeonce/effect-machine"
6555
- *
6556
- * class LoadFailed extends Schema.TaggedClass<LoadFailed>("LoadFailed")("LoadFailed", {
6557
- * reason: Schema.String
6558
- * }) {}
6559
- *
6560
- * const load = Machine.effect(
6561
- * Effect.fail("unavailable").pipe(
6562
- * Effect.catch((reason) => Effect.succeed(new LoadFailed({ reason })))
6563
- * )
6564
- * )
6565
- * ```
6566
- *
6567
- * @see {@link transition} for event-driven state.
6568
- * @see {@link logic} for direct control over intermediate snapshots.
6569
- * @category constructors
6570
- * @since 0.4.0
6571
- */
6572
- export const effect: <Output, Error = never, Requirements = never>(
6573
- effect: Effect.Effect<Output, Error, Requirements>
6574
- ) => Logic<void, never, Error, Requirements, Output> = internal.effect
6575
-
6576
6965
  /**
6577
6966
  * Creates advanced stateful process logic from explicit initialization and
6578
6967
  * execution methods.
@@ -6594,9 +6983,9 @@ export const effect: <Output, Error = never, Requirements = never>(
6594
6983
  * This is the low-level process constructor. Parent messages sent directly
6595
6984
  * through its scope are intentionally `unknown` because the logic does not know
6596
6985
  * which machine will eventually own it. Prefer typed output, typed child
6597
- * addresses, or invoke snapshot mapping when possible.
6986
+ * addresses, or invocation lifecycle transitions when possible.
6598
6987
  *
6599
- * @see {@link effect} for one-shot work.
6988
+ * Use an inline `invoke` with an `effect` source for one-shot work.
6600
6989
  * @see {@link transition} for event-driven state.
6601
6990
  * @category constructors
6602
6991
  * @since 0.4.0
@@ -6645,7 +7034,7 @@ export const logic: <
6645
7034
  * )
6646
7035
  * ```
6647
7036
  *
6648
- * @see {@link effect} for one-shot work.
7037
+ * Use an inline `invoke` with an `effect` source for one-shot work.
6649
7038
  * @see {@link logic} for direct process lifecycle control.
6650
7039
  * @category constructors
6651
7040
  * @since 0.4.0
@@ -6858,7 +7247,7 @@ export const start: <
6858
7247
  ) => Effect.Effect<
6859
7248
  MachineRef<
6860
7249
  Machine.Snapshot<States>,
6861
- Machine.EventOf<InputEvents>,
7250
+ Machine.EventInputOf<InputEvents>,
6862
7251
  | E
6863
7252
  | ActionError<R>
6864
7253
  | InfiniteTransitionError
@@ -6890,8 +7279,8 @@ export const start: <
6890
7279
  * entry or transition actions, re-deliver raised or emitted events, or
6891
7280
  * re-evaluate historical completion and eventless transitions. Active-state
6892
7281
  * invokes start once in ancestor and document order with {@link InitialEvent};
6893
- * delayed invokes restart their complete duration and invoked machines start
6894
- * from their own initial state.
7282
+ * timer invocations restart their complete duration and child-machine
7283
+ * invocations start from their own initial state.
6895
7284
  *
6896
7285
  * Only logical state, completion, and history metadata are resumed. Queues,
6897
7286
  * scopes, subscriptions, fibers, spawned children, invoke progress, and prior
@@ -6967,7 +7356,7 @@ export const resume: <
6967
7356
  ) => Effect.Effect<
6968
7357
  MachineRef<
6969
7358
  Machine.Snapshot<States>,
6970
- Machine.EventOf<InputEvents>,
7359
+ Machine.EventInputOf<InputEvents>,
6971
7360
  | E
6972
7361
  | ActionError<R>
6973
7362
  | InfiniteTransitionError