@typeonce/effect-machine 0.32.0 → 0.33.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.
package/src/Machine.ts CHANGED
@@ -4729,7 +4729,7 @@ export declare namespace Machine {
4729
4729
  * do not directly control state re-entry. Exit and entry paths are derived
4730
4730
  * from the previous and next active paths. Shared active ancestors remain
4731
4731
  * entered even when a `full` target supplies their values again. Use an event
4732
- * transition with `reenter: true` when the source should explicitly exit and
4732
+ * transition with `.reenter()` when the source should explicitly exit and
4733
4733
  * enter again.
4734
4734
  *
4735
4735
  * @category models
@@ -6015,25 +6015,17 @@ export declare namespace Machine {
6015
6015
  }
6016
6016
  }
6017
6017
 
6018
- type TransitionReenterOption<Reenter extends boolean> = [Reenter] extends [true] ? {
6019
- /** Forces the source state to exit and enter even when active paths remain unchanged. */
6020
- readonly reenter?: boolean
6021
- }
6022
- : { readonly reenter?: never }
6023
-
6024
- type TransitionRequiredOptions<Reenter extends boolean> =
6025
- & TransitionReenterOption<Reenter>
6026
- & {
6027
- /** Keeps the transition required. The resolver cannot return `decline()`. */
6028
- readonly declinable?: false
6029
- }
6018
+ type TransitionRequiredOptions = {
6019
+ /** Keeps the transition required. The resolver cannot return `decline()`. */
6020
+ readonly declinable?: false
6021
+ readonly reenter?: never
6022
+ }
6030
6023
 
6031
- type TransitionDeclinableOptions<Reenter extends boolean> =
6032
- & TransitionReenterOption<Reenter>
6033
- & {
6034
- /** Adds `decline()` to the resolver context and permits declining this candidate. */
6035
- readonly declinable: true
6036
- }
6024
+ type TransitionDeclinableOptions = {
6025
+ /** Adds `decline()` to the resolver context and permits declining this candidate. */
6026
+ readonly declinable: true
6027
+ readonly reenter?: never
6028
+ }
6037
6029
 
6038
6030
  type BuiltTransition<
6039
6031
  States extends StateSchemas,
@@ -6059,7 +6051,7 @@ export declare namespace Machine {
6059
6051
  > {
6060
6052
  (
6061
6053
  resolve: TransitionResolver<Events, Emits, Context, Selection>,
6062
- options?: TransitionRequiredOptions<Reenter>
6054
+ options?: TransitionRequiredOptions
6063
6055
  ): BuiltTransition<
6064
6056
  States,
6065
6057
  Events,
@@ -6083,7 +6075,7 @@ export declare namespace Machine {
6083
6075
  > {
6084
6076
  (
6085
6077
  resolve: DeclinableTransitionResolver<Events, Emits, Context, Selection>,
6086
- options: TransitionDeclinableOptions<Reenter>
6078
+ options: TransitionDeclinableOptions
6087
6079
  ): BuiltTransition<
6088
6080
  States,
6089
6081
  Events,
@@ -6145,7 +6137,7 @@ export declare namespace Machine {
6145
6137
  & {
6146
6138
  readonly resolve: (
6147
6139
  resolve: TransitionResolver<Events, Emits, Context, Selection>,
6148
- options?: TransitionRequiredOptions<Reenter>
6140
+ options?: TransitionRequiredOptions
6149
6141
  ) => BuiltTransition<
6150
6142
  States,
6151
6143
  Events,
@@ -6161,19 +6153,15 @@ export declare namespace Machine {
6161
6153
  /**
6162
6154
  * A selected transition target with target-specific resolver operations.
6163
6155
  *
6164
- * **Example** (Updating state while reentering)
6156
+ * **Example** (Constructing state while reentering)
6165
6157
  *
6166
6158
  * ```ts
6167
6159
  * Reset: (to) =>
6168
- * to.branch.Ready().resolve(
6169
- * ({ target }) => target.from(),
6170
- * { reenter: true }
6171
- * )
6160
+ * to.branch.Ready().reenter().from(({ event }) => ({ count: event.count }))
6172
6161
  * ```
6173
6162
  *
6174
6163
  * @inlineType TransitionRequiredOptions
6175
6164
  * @inlineType TransitionDeclinableOptions
6176
- * @inlineType TransitionReenterOption
6177
6165
  */
6178
6166
  export type TransitionTarget<
6179
6167
  States extends StateSchemas,
@@ -6233,21 +6221,14 @@ export declare namespace Machine {
6233
6221
  >
6234
6222
  : {})
6235
6223
  }
6236
- & ([Reenter] extends [true] ? SelectionSupportsDefaultConstruction<Selection> extends true ? {
6237
- /** Reenters the source using the selected target's default construction. */
6238
- readonly reenter: () => BuiltTransition<
6239
- States,
6240
- Events,
6241
- Emits,
6242
- StateId,
6243
- Context,
6244
- Reenter,
6245
- SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined,
6246
- "required"
6247
- >
6248
- }
6249
- : {}
6250
- : {})
6224
+ & ([Reenter] extends [true] ? {
6225
+ /** Forces source exit and entry, preserving subsequent construction and guards. */
6226
+ readonly reenter: () => Omit<
6227
+ TransitionTarget<States, Events, Emits, StateId, Context, Reenter, Acceptance, Selection>,
6228
+ typeof Topology.TargetSelectionTypeId
6229
+ >
6230
+ } :
6231
+ {})
6251
6232
  & (SelectionKind<Selection> extends "state" ?
6252
6233
  SelectionScope<Selection> extends "local" | "branch" ?
6253
6234
  RetainedUpdateOwner<States, StateId, Selection> extends infer Owner extends ValuedStateIdentifier<States> ?
@@ -6272,7 +6253,91 @@ export declare namespace Machine {
6272
6253
  : {}
6273
6254
  : {})
6274
6255
 
6275
- /** A topology selection that requires one retained owner replacement. */
6256
+ type UpdatingConstructionCallbacks<Context, Builder, OwnerBuilder, Result> = {
6257
+ readonly [
6258
+ Method in Extract<keyof Builder & keyof OwnerBuilder, "from" | "decoded"> as Builder[Method] extends
6259
+ (...args: infer Args) => unknown ? Args extends readonly [unknown?] ? Method : never : never
6260
+ ]: Builder[Method] extends (...args: infer Args) => unknown ?
6261
+ OwnerBuilder[Method] extends (value: infer Update) => unknown ?
6262
+ (construct: (context: Context) => { readonly target: Args[0]; readonly update: Update }) => Result
6263
+ : never :
6264
+ never
6265
+ }
6266
+
6267
+ type UpdatingCallbacks<
6268
+ States extends StateSchemas,
6269
+ Events extends ReadonlyArray<TaggedSchema>,
6270
+ Emits extends ReadonlyArray<TaggedSchema>,
6271
+ StateId extends StateNodeIdentifier<States>,
6272
+ Context,
6273
+ Reenter extends boolean,
6274
+ Selection extends TargetSelection<any, any, "state">,
6275
+ Owner extends ValuedStateIdentifier<States>,
6276
+ Acceptance extends TransitionAcceptance
6277
+ > = UpdatingConstructionCallbacks<
6278
+ Omit<UpdatingTransitionResolveContext<States, Context, Selection, Owner>, "owner" | "target">,
6279
+ SelectionBuilder<Selection>,
6280
+ StateUpdateBuilder<States, Owner>,
6281
+ BuiltTransition<
6282
+ States,
6283
+ Events,
6284
+ Emits,
6285
+ StateId,
6286
+ Context,
6287
+ Reenter,
6288
+ | CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>
6289
+ | (Acceptance extends "declinable" ? Declined : never),
6290
+ Acceptance
6291
+ >
6292
+ >
6293
+
6294
+ type GuardedUpdatingTransition<
6295
+ States extends StateSchemas,
6296
+ Events extends ReadonlyArray<TaggedSchema>,
6297
+ Emits extends ReadonlyArray<TaggedSchema>,
6298
+ StateId extends StateNodeIdentifier<States>,
6299
+ Context,
6300
+ Reenter extends boolean,
6301
+ Selection extends TargetSelection<any, any, "state">,
6302
+ Owner extends ValuedStateIdentifier<States>
6303
+ > = UpdatingCallbacks<States, Events, Emits, StateId, Context, Reenter, Selection, Owner, "declinable"> & {
6304
+ readonly resolve: (
6305
+ resolve: (
6306
+ context: UpdatingTransitionResolveContext<States, Context, Selection, Owner>,
6307
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
6308
+ ) => CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>,
6309
+ options?: TransitionRequiredOptions
6310
+ ) => BuiltTransition<
6311
+ States,
6312
+ Events,
6313
+ Emits,
6314
+ StateId,
6315
+ Context,
6316
+ Reenter,
6317
+ CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner> | Declined,
6318
+ "declinable"
6319
+ >
6320
+ }
6321
+
6322
+ /**
6323
+ * A topology selection that requires one retained owner replacement.
6324
+ *
6325
+ * `.from` constructs both values from schema inputs; `.decoded` accepts both
6326
+ * decoded values. Both callbacks return `{ target, update }` and read the same
6327
+ * pre-transition snapshot. Use `.resolve` for explicit child construction,
6328
+ * mixed construction methods, or queued commands.
6329
+ *
6330
+ * **Example** (Guarding an atomic destination and owner update)
6331
+ *
6332
+ * ```ts
6333
+ * Save: (to) => to.local.Saving().updating(to.root)
6334
+ * .guard(({ current }) => current.draft.length > 0)
6335
+ * .from(({ current }) => ({
6336
+ * target: { text: current.draft },
6337
+ * update: { ...current, attempts: current.attempts + 1 }
6338
+ * }))
6339
+ * ```
6340
+ */
6276
6341
  export type UpdatingTransitionTarget<
6277
6342
  States extends StateSchemas,
6278
6343
  Events extends ReadonlyArray<TaggedSchema>,
@@ -6283,32 +6348,35 @@ export declare namespace Machine {
6283
6348
  Acceptance extends TransitionAcceptance,
6284
6349
  Selection extends TargetSelection<any, any, "state">,
6285
6350
  Owner extends ValuedStateIdentifier<States>
6286
- > = Selection & {
6287
- /** @internal */
6288
- readonly "~effect/Machine/UpdatingTransitionTarget": Owner
6289
- readonly resolve:
6290
- & ((
6291
- resolve: (
6292
- context: UpdatingTransitionResolveContext<States, Context, Selection, Owner>,
6293
- enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
6294
- ) => CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>,
6295
- options?: TransitionRequiredOptions<Reenter>
6296
- ) => BuiltTransition<
6297
- States,
6298
- Events,
6299
- Emits,
6300
- StateId,
6301
- Context,
6302
- Reenter,
6303
- CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>,
6304
- "required"
6305
- >)
6306
- & ("declinable" extends Acceptance ? (
6351
+ > =
6352
+ & Selection
6353
+ & UpdatingCallbacks<States, Events, Emits, StateId, Context, Reenter, Selection, Owner, "required">
6354
+ & ([Reenter] extends [true] ? {
6355
+ readonly reenter: () => Omit<
6356
+ UpdatingTransitionTarget<States, Events, Emits, StateId, Context, Reenter, Acceptance, Selection, Owner>,
6357
+ typeof Topology.TargetSelectionTypeId
6358
+ >
6359
+ } :
6360
+ {})
6361
+ & ("declinable" extends Acceptance ? {
6362
+ /** Declines before either value is constructed or any commands are enqueued. */
6363
+ readonly guard: (
6364
+ predicate: (
6365
+ context: Omit<UpdatingTransitionResolveContext<States, Context, Selection, Owner>, "owner" | "target">
6366
+ ) => boolean
6367
+ ) => GuardedUpdatingTransition<States, Events, Emits, StateId, Context, Reenter, Selection, Owner>
6368
+ } :
6369
+ {})
6370
+ & {
6371
+ /** @internal */
6372
+ readonly "~effect/Machine/UpdatingTransitionTarget": Owner
6373
+ readonly resolve:
6374
+ & ((
6307
6375
  resolve: (
6308
- context: UpdatingTransitionResolveContext<States, Context, Selection, Owner> & DeclineCapability,
6376
+ context: UpdatingTransitionResolveContext<States, Context, Selection, Owner>,
6309
6377
  enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
6310
- ) => CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner> | Declined,
6311
- options: TransitionDeclinableOptions<Reenter>
6378
+ ) => CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>,
6379
+ options?: TransitionRequiredOptions
6312
6380
  ) => BuiltTransition<
6313
6381
  States,
6314
6382
  Events,
@@ -6316,11 +6384,27 @@ export declare namespace Machine {
6316
6384
  StateId,
6317
6385
  Context,
6318
6386
  Reenter,
6319
- CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner> | Declined,
6320
- "declinable"
6321
- >
6322
- : {})
6323
- }
6387
+ CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>,
6388
+ "required"
6389
+ >)
6390
+ & ("declinable" extends Acceptance ? (
6391
+ resolve: (
6392
+ context: UpdatingTransitionResolveContext<States, Context, Selection, Owner> & DeclineCapability,
6393
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
6394
+ ) => CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner> | Declined,
6395
+ options: TransitionDeclinableOptions
6396
+ ) => BuiltTransition<
6397
+ States,
6398
+ Events,
6399
+ Emits,
6400
+ StateId,
6401
+ Context,
6402
+ Reenter,
6403
+ CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner> | Declined,
6404
+ "declinable"
6405
+ >
6406
+ : {})
6407
+ }
6324
6408
 
6325
6409
  /** @internal */
6326
6410
  interface StateUpdateTransitionRequired<
@@ -6340,7 +6424,7 @@ export declare namespace Machine {
6340
6424
  Context,
6341
6425
  Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>
6342
6426
  >,
6343
- options?: TransitionRequiredOptions<Reenter>
6427
+ options?: TransitionRequiredOptions
6344
6428
  ): BuiltTransition<
6345
6429
  States,
6346
6430
  Events,
@@ -6371,7 +6455,7 @@ export declare namespace Machine {
6371
6455
  Context,
6372
6456
  Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>
6373
6457
  >,
6374
- options: TransitionDeclinableOptions<Reenter>
6458
+ options: TransitionDeclinableOptions
6375
6459
  ): BuiltTransition<
6376
6460
  States,
6377
6461
  Events,
@@ -6384,6 +6468,54 @@ export declare namespace Machine {
6384
6468
  >
6385
6469
  }
6386
6470
 
6471
+ type GuardedStateUpdateTransition<
6472
+ States extends StateSchemas,
6473
+ Events extends ReadonlyArray<TaggedSchema>,
6474
+ Emits extends ReadonlyArray<TaggedSchema>,
6475
+ StateId extends StateNodeIdentifier<States>,
6476
+ Context,
6477
+ Reenter extends boolean,
6478
+ Selection extends TargetSelection<any, any, "update">
6479
+ > =
6480
+ & ConstructionCallbacks<
6481
+ Omit<
6482
+ StateUpdateResolveContext<States, Context, Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>>,
6483
+ "owner"
6484
+ >,
6485
+ SelectionBuilder<Selection>,
6486
+ BuiltTransition<
6487
+ States,
6488
+ Events,
6489
+ Emits,
6490
+ StateId,
6491
+ Context,
6492
+ Reenter,
6493
+ SelectedTargetResult<Selection> | Declined,
6494
+ "declinable"
6495
+ >
6496
+ >
6497
+ & {
6498
+ readonly resolve: (
6499
+ resolve: StateUpdateResolver<
6500
+ States,
6501
+ Events,
6502
+ Emits,
6503
+ Context,
6504
+ Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>
6505
+ >,
6506
+ options?: TransitionRequiredOptions
6507
+ ) => BuiltTransition<
6508
+ States,
6509
+ Events,
6510
+ Emits,
6511
+ StateId,
6512
+ Context,
6513
+ Reenter,
6514
+ SelectedTargetResult<Selection> | Declined,
6515
+ "declinable"
6516
+ >
6517
+ }
6518
+
6387
6519
  /** @internal */
6388
6520
  type StateUpdateTransition<
6389
6521
  States extends StateSchemas,
@@ -6396,6 +6528,30 @@ export declare namespace Machine {
6396
6528
  Selection extends TargetSelection<any, any, "update">
6397
6529
  > =
6398
6530
  & Selection
6531
+ & ([Reenter] extends [true] ? {
6532
+ /** Reenters the source while updating its retained valued ancestor. */
6533
+ readonly reenter: () => Omit<
6534
+ StateUpdateTransition<States, Events, Emits, StateId, Context, Reenter, Acceptance, Selection>,
6535
+ typeof Topology.TargetSelectionTypeId
6536
+ >
6537
+ } :
6538
+ {})
6539
+ & ("declinable" extends Acceptance ? {
6540
+ /** Declines before replacing the selected owner value. */
6541
+ readonly guard: (
6542
+ predicate: (
6543
+ context: Omit<
6544
+ StateUpdateResolveContext<
6545
+ States,
6546
+ Context,
6547
+ Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>
6548
+ >,
6549
+ "owner"
6550
+ >
6551
+ ) => boolean
6552
+ ) => GuardedStateUpdateTransition<States, Events, Emits, StateId, Context, Reenter, Selection>
6553
+ } :
6554
+ {})
6399
6555
  & ConstructionCallbacks<
6400
6556
  Omit<
6401
6557
  StateUpdateResolveContext<States, Context, Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>>,
@@ -6583,7 +6739,7 @@ export declare namespace Machine {
6583
6739
  > {
6584
6740
  (
6585
6741
  resolve: TransitionBranchesResolver<Events, Emits, Context, Branches>,
6586
- options?: TransitionRequiredOptions<Reenter>
6742
+ options?: TransitionRequiredOptions
6587
6743
  ): BuiltTransition<
6588
6744
  States,
6589
6745
  Events,
@@ -6607,7 +6763,7 @@ export declare namespace Machine {
6607
6763
  > {
6608
6764
  (
6609
6765
  resolve: DeclinableTransitionBranchesResolver<Events, Emits, Context, Branches>,
6610
- options: TransitionDeclinableOptions<Reenter>
6766
+ options: TransitionDeclinableOptions
6611
6767
  ): BuiltTransition<
6612
6768
  States,
6613
6769
  Events,
@@ -6641,6 +6797,38 @@ export declare namespace Machine {
6641
6797
  >
6642
6798
  }
6643
6799
 
6800
+ type TransitionBranchesTarget<
6801
+ States extends StateSchemas,
6802
+ Events extends ReadonlyArray<TaggedSchema>,
6803
+ Emits extends ReadonlyArray<TaggedSchema>,
6804
+ StateId extends StateNodeIdentifier<States>,
6805
+ Context,
6806
+ Reenter extends boolean,
6807
+ Acceptance extends TransitionAcceptance,
6808
+ Branches extends Readonly<Record<string, TransitionBranchInput>>
6809
+ > =
6810
+ & {
6811
+ /** Resolves exactly one declared branch after this transition is selected. */
6812
+ readonly resolve:
6813
+ & TransitionBranchesResolveRequired<States, Events, Emits, StateId, Context, Reenter, Branches>
6814
+ & ("declinable" extends Acceptance
6815
+ ? TransitionBranchesResolveDeclinable<States, Events, Emits, StateId, Context, Reenter, Branches>
6816
+ : {})
6817
+ }
6818
+ & ([Reenter] extends [true] ? {
6819
+ readonly reenter: () => TransitionBranchesTarget<
6820
+ States,
6821
+ Events,
6822
+ Emits,
6823
+ StateId,
6824
+ Context,
6825
+ Reenter,
6826
+ Acceptance,
6827
+ Branches
6828
+ >
6829
+ } :
6830
+ {})
6831
+
6644
6832
  /** Selector supplied to inline transition declarations. */
6645
6833
  export interface TransitionSelector<
6646
6834
  in out States extends StateSchemas,
@@ -6654,21 +6842,7 @@ export declare namespace Machine {
6654
6842
  /** Declares a closed set of named destinations for one resolver. */
6655
6843
  readonly branches: <const Branches extends Readonly<Record<string, TransitionBranchInput>>>(
6656
6844
  branches: Branches & ValidateTransitionBranchRecord<NoInfer<Branches>>
6657
- ) => {
6658
- /** Resolves exactly one declared branch after this transition is selected. */
6659
- readonly resolve:
6660
- & TransitionBranchesResolveRequired<States, Events, Emits, StateId, Context, Reenter, Branches>
6661
- & ("declinable" extends Acceptance ? TransitionBranchesResolveDeclinable<
6662
- States,
6663
- Events,
6664
- Emits,
6665
- StateId,
6666
- Context,
6667
- Reenter,
6668
- Branches
6669
- >
6670
- : {})
6671
- }
6845
+ ) => TransitionBranchesTarget<States, Events, Emits, StateId, Context, Reenter, Acceptance, Branches>
6672
6846
  }
6673
6847
 
6674
6848
  /** Inline transition declaration accepted by state and invocation handlers. */