xstate 5.0.0-beta.44 → 5.0.0-beta.46

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 (51) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -2
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -2
  4. package/actions/dist/xstate-actions.esm.js +2 -2
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +134 -9
  8. package/actors/dist/xstate-actors.development.cjs.js +134 -9
  9. package/actors/dist/xstate-actors.development.esm.js +134 -9
  10. package/actors/dist/xstate-actors.esm.js +134 -9
  11. package/actors/dist/xstate-actors.umd.min.js +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  13. package/dist/declarations/src/State.d.ts +18 -28
  14. package/dist/declarations/src/StateMachine.d.ts +11 -29
  15. package/dist/declarations/src/StateNode.d.ts +8 -6
  16. package/dist/declarations/src/actions/choose.d.ts +3 -3
  17. package/dist/declarations/src/actions/pure.d.ts +4 -4
  18. package/dist/declarations/src/actors/observable.d.ts +80 -4
  19. package/dist/declarations/src/actors/transition.d.ts +53 -4
  20. package/dist/declarations/src/createMachine.d.ts +5 -0
  21. package/dist/declarations/src/guards.d.ts +26 -4
  22. package/dist/declarations/src/index.d.ts +3 -2
  23. package/dist/declarations/src/interpreter.d.ts +1 -0
  24. package/dist/declarations/src/setup.d.ts +35 -0
  25. package/dist/declarations/src/stateUtils.d.ts +7 -7
  26. package/dist/declarations/src/types.d.ts +54 -25
  27. package/dist/declarations/src/utils.d.ts +2 -1
  28. package/dist/{raise-5854eaca.esm.js → raise-1682abb7.esm.js} +99 -135
  29. package/dist/{raise-fb6f017b.cjs.js → raise-a1d3d7e9.cjs.js} +100 -136
  30. package/dist/{raise-ed700d14.development.cjs.js → raise-a9e7e31c.development.cjs.js} +100 -136
  31. package/dist/{raise-348cc74e.development.esm.js → raise-fa23c2b9.development.esm.js} +99 -135
  32. package/dist/{send-53e5693c.cjs.js → send-2fa3a204.cjs.js} +24 -28
  33. package/dist/{send-00466e37.development.cjs.js → send-5b256a89.development.cjs.js} +24 -28
  34. package/dist/{send-a0193bdb.development.esm.js → send-9acdf858.development.esm.js} +24 -28
  35. package/dist/{send-b7b4befa.esm.js → send-a237e4e8.esm.js} +24 -28
  36. package/dist/xstate.cjs.js +102 -92
  37. package/dist/xstate.cjs.mjs +2 -0
  38. package/dist/xstate.development.cjs.js +102 -92
  39. package/dist/xstate.development.cjs.mjs +2 -0
  40. package/dist/xstate.development.esm.js +97 -89
  41. package/dist/xstate.esm.js +97 -89
  42. package/dist/xstate.umd.min.js +1 -1
  43. package/dist/xstate.umd.min.js.map +1 -1
  44. package/guards/dist/xstate-guards.cjs.js +1 -1
  45. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  46. package/guards/dist/xstate-guards.development.esm.js +1 -1
  47. package/guards/dist/xstate-guards.esm.js +1 -1
  48. package/guards/dist/xstate-guards.umd.min.js +1 -1
  49. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  50. package/package.json +1 -1
  51. package/dist/declarations/src/Machine.d.ts +0 -5
@@ -1,10 +1,10 @@
1
1
  import { MachineSnapshot } from "./State.js";
2
2
  import { StateNode } from "./StateNode.js";
3
3
  import type { AreAllImplementationsAssumedToBeProvided, MarkAllImplementationsAsProvided, ResolveTypegenMeta, TypegenDisabled } from "./typegenTypes.js";
4
- import type { ActorScope, ActorLogic, EventObject, InternalMachineImplementations, MachineConfig, MachineContext, MachineImplementationsSimplified, MachineTypes, NoInfer, StateMachineDefinition, StateValue, TransitionDefinition, ParameterizedObject, AnyActorScope, ProvidedActor, Equals, TODO, Snapshot, HistoryValue } from "./types.js";
4
+ import type { ActorScope, ActorLogic, EventObject, InternalMachineImplementations, MachineConfig, MachineContext, MachineImplementationsSimplified, MachineTypes, NoInfer, StateMachineDefinition, StateValue, TransitionDefinition, ParameterizedObject, AnyActorScope, ProvidedActor, AnyActorRef, Equals, TODO, Snapshot, HistoryValue } from "./types.js";
5
5
  export declare const STATE_IDENTIFIER = "#";
6
6
  export declare const WILDCARD = "*";
7
- export declare class StateMachine<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, TInput, TOutput, TResolvedTypesMeta = ResolveTypegenMeta<TypegenDisabled, NoInfer<TEvent>, TActor, TAction, TGuard, TDelay, TTag>> implements ActorLogic<MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>, TEvent, TInput, TODO> {
7
+ export declare class StateMachine<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, TInput, TOutput, TResolvedTypesMeta = ResolveTypegenMeta<TypegenDisabled, NoInfer<TEvent>, TActor, TAction, TGuard, TDelay, TTag>> implements ActorLogic<MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>, TEvent, TInput, TODO> {
8
8
  /**
9
9
  * The raw config used to create the machine.
10
10
  */
@@ -35,7 +35,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
35
35
  *
36
36
  * @returns A new `StateMachine` instance with the provided implementations.
37
37
  */
38
- provide(implementations: InternalMachineImplementations<TContext, TEvent, TActor, TAction, TDelay, TResolvedTypesMeta, true>): StateMachine<TContext, TEvent, TActor, TAction, TGuard, TDelay, TTag, TInput, TOutput, AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? MarkAllImplementationsAsProvided<TResolvedTypesMeta> : TResolvedTypesMeta>;
38
+ provide(implementations: InternalMachineImplementations<TContext, TResolvedTypesMeta, true>): StateMachine<TContext, TEvent, TChildren, TActor, TAction, TGuard, TDelay, TTag, TInput, TOutput, AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? MarkAllImplementationsAsProvided<TResolvedTypesMeta> : TResolvedTypesMeta>;
39
39
  resolveState(config: {
40
40
  value: StateValue;
41
41
  context?: TContext;
@@ -45,7 +45,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
45
45
  error?: unknown;
46
46
  } & (Equals<TContext, MachineContext> extends false ? {
47
47
  context: unknown;
48
- } : {})): MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>;
48
+ } : {})): MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>;
49
49
  /**
50
50
  * Determines the next state given the current `state` and received `event`.
51
51
  * Calculates a full macrostep from all microsteps.
@@ -53,7 +53,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
53
53
  * @param state The current State instance or state value
54
54
  * @param event The received event
55
55
  */
56
- transition(state: MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>, event: TEvent, actorScope: ActorScope<typeof state, TEvent>): MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>;
56
+ transition(state: MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>, event: TEvent, actorScope: ActorScope<typeof state, TEvent>): MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>;
57
57
  /**
58
58
  * Determines the next state given the current `state` and `event`.
59
59
  * Calculates a microstep.
@@ -61,8 +61,8 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
61
61
  * @param state The current state
62
62
  * @param event The received event
63
63
  */
64
- microstep(state: MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>, event: TEvent, actorScope: AnyActorScope): Array<MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>>;
65
- getTransitionData(state: MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>, event: TEvent): Array<TransitionDefinition<TContext, TEvent>>;
64
+ microstep(state: MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>, event: TEvent, actorScope: AnyActorScope): Array<MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>>;
65
+ getTransitionData(state: MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>, event: TEvent): Array<TransitionDefinition<TContext, TEvent>>;
66
66
  /**
67
67
  * The initial state _before_ evaluating any microsteps.
68
68
  * This "pre-initial" state is provided to initial actions executed in the initial state.
@@ -71,31 +71,13 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
71
71
  /**
72
72
  * Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
73
73
  */
74
- getInitialState(actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>, TEvent>, input?: TInput): MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>;
75
- start(state: MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>): void;
74
+ getInitialState(actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>, TEvent>, input?: TInput): MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>;
75
+ start(state: MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>): void;
76
76
  getStateNodeById(stateId: string): StateNode<TContext, TEvent>;
77
77
  get definition(): StateMachineDefinition<TContext, TEvent>;
78
78
  toJSON(): StateMachineDefinition<TContext, TEvent>;
79
- getPersistedState(state: MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>, options?: unknown): Snapshot<unknown>;
80
- restoreState(snapshot: Snapshot<unknown>, _actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>, TEvent>): MachineSnapshot<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta>;
81
- /**@deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
82
- __TContext: TContext;
83
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
84
- __TEvent: TEvent;
85
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
86
- __TActor: TActor;
87
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
88
- __TAction: TAction;
89
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
90
- __TGuard: TGuard;
91
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
92
- __TDelay: TDelay;
93
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
94
- __TTag: TTag;
95
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
96
- __TInput: TInput;
97
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
98
- __TOutput: TOutput;
79
+ getPersistedState(state: MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>, options?: unknown): Snapshot<unknown>;
80
+ restoreState(snapshot: Snapshot<unknown>, _actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>, TEvent>): MachineSnapshot<TContext, TEvent, TChildren, TTag, TOutput, TResolvedTypesMeta>;
99
81
  /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
100
82
  __TResolvedTypesMeta: TResolvedTypesMeta;
101
83
  }
@@ -64,13 +64,15 @@ export declare class StateNode<TContext extends MachineContext = MachineContext,
64
64
  /**
65
65
  * The root machine node.
66
66
  */
67
- machine: StateMachine<TContext, TEvent, any, // actors
67
+ machine: StateMachine<TContext, TEvent, any, // children
68
+ any, // actor
69
+ any, // action
70
+ any, // guard
71
+ any, // delay
72
+ any, // tag
68
73
  any, // input
69
- TODO, // output
70
- TODO, // guards
71
- TODO, // delays
72
- TODO, // tags
73
- TODO>;
74
+ any, // output
75
+ any>;
74
76
  /**
75
77
  * The meta data associated with this state node, which will be returned in State instances.
76
78
  */
@@ -1,9 +1,9 @@
1
1
  import { EventObject, ChooseBranch, MachineContext, ActionArgs, ParameterizedObject, NoInfer, ProvidedActor } from "../types.js";
2
- export interface ChooseAction<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string> {
3
- (args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: TParams): void;
2
+ export interface ChooseAction<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string> {
3
+ (args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: unknown): void;
4
4
  _out_TActor?: TActor;
5
5
  _out_TAction?: TAction;
6
6
  _out_TGuard?: TGuard;
7
7
  _out_TDelay?: TDelay;
8
8
  }
9
- export declare function choose<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string>(branches: ReadonlyArray<ChooseBranch<TContext, TExpressionEvent, TEvent, TActor, NoInfer<TAction>, NoInfer<TGuard>, TDelay>>): ChooseAction<TContext, TExpressionEvent, TParams, TEvent, TActor, TAction, TGuard, TDelay>;
9
+ export declare function choose<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string>(branches: ReadonlyArray<ChooseBranch<TContext, TExpressionEvent, TEvent, TActor, NoInfer<TAction>, NoInfer<TGuard>, TDelay>>): ChooseAction<TContext, TExpressionEvent, TEvent, TActor, TAction, TGuard, TDelay>;
@@ -1,13 +1,13 @@
1
1
  import { Actions, ActionArgs, EventObject, MachineContext, ParameterizedObject, NoInfer, ProvidedActor } from "../types.js";
2
- export interface PureAction<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string> {
3
- (args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: TParams): void;
2
+ export interface PureAction<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string> {
3
+ (args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: unknown): void;
4
4
  _out_TEvent?: TEvent;
5
5
  _out_TActor?: TActor;
6
6
  _out_TAction?: TAction;
7
7
  _out_TGuard?: TGuard;
8
8
  _out_TDelay?: TDelay;
9
9
  }
10
- export declare function pure<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined = ParameterizedObject['params'] | undefined, TEvent extends EventObject = TExpressionEvent, TActor extends ProvidedActor = ProvidedActor, TAction extends ParameterizedObject = ParameterizedObject, TGuard extends ParameterizedObject = ParameterizedObject, TDelay extends string = string>(getActions: ({ context, event }: {
10
+ export declare function pure<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject = TExpressionEvent, TActor extends ProvidedActor = ProvidedActor, TAction extends ParameterizedObject = ParameterizedObject, TGuard extends ParameterizedObject = ParameterizedObject, TDelay extends string = string>(getActions: ({ context, event }: {
11
11
  context: TContext;
12
12
  event: TExpressionEvent;
13
- }) => Actions<TContext, TExpressionEvent, NoInfer<TEvent>, undefined, TActor, NoInfer<TAction>, NoInfer<TGuard>, TDelay> | undefined): PureAction<TContext, TExpressionEvent, TParams, TEvent, TActor, TAction, TGuard, TDelay>;
13
+ }) => Actions<TContext, TExpressionEvent, NoInfer<TEvent>, undefined, TActor, NoInfer<TAction>, NoInfer<TGuard>, TDelay> | undefined): PureAction<TContext, TExpressionEvent, TEvent, TActor, TAction, TGuard, TDelay>;
@@ -9,18 +9,94 @@ export type ObservableActorLogic<TContext, TInput> = ActorLogic<ObservableSnapsh
9
9
  [k: string]: unknown;
10
10
  }, TInput, AnyActorSystem>;
11
11
  export type ObservableActorRef<TContext> = ActorRefFrom<ObservableActorLogic<TContext, any>>;
12
+ /**
13
+ * Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
14
+ *
15
+ * - Emit snapshots of the observable’s emitted value
16
+ *
17
+ * The observable’s emitted value is used as its observable actor’s `context`.
18
+ *
19
+ * Sending events to observable actors will have no effect.
20
+ *
21
+ * @param observableCreator A function that creates an observable. It receives one argument, an object with the following properties:
22
+ * - `input` - Data that was provided to the observable actor
23
+ * - `self` - The parent actor
24
+ * - `system` - The actor system to which the observable actor belongs
25
+ *
26
+ * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import { fromObservable, createActor } from 'xstate'
31
+ * import { interval } from 'rxjs';
32
+ *
33
+ * const logic = fromObservable((obj) => interval(1000));
34
+ *
35
+ * const actor = createActor(logic);
36
+ *
37
+ * actor.subscribe((snapshot) => {
38
+ * console.log(snapshot.context);
39
+ * });
40
+ *
41
+ * actor.start();
42
+ * // At every second:
43
+ * // Logs 0
44
+ * // Logs 1
45
+ * // Logs 2
46
+ * // ...
47
+ * ```
48
+ *
49
+ * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
50
+ * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
51
+ */
12
52
  export declare function fromObservable<TContext, TInput>(observableCreator: ({ input, system }: {
13
53
  input: TInput;
14
54
  system: AnyActorSystem;
15
55
  self: ObservableActorRef<TContext>;
16
56
  }) => Subscribable<TContext>): ObservableActorLogic<TContext, TInput>;
17
57
  /**
18
- * Creates event observable logic that listens to an observable
19
- * that delivers event objects.
58
+ * Creates event observable logic that listens to an observable that delivers event objects.
59
+ *
60
+ * Event observable actor logic is described by an observable stream of {@link https://stately.ai/docs/transitions#event-objects | event objects}. Actors created from event observable logic (“event observable actors”) can:
61
+ *
62
+ * - Implicitly send events to its parent actor
63
+ * - Emit snapshots of its emitted event objects
64
+ *
65
+ * Sending events to event observable actors will have no effect.
66
+ *
67
+ * @param lazyObservable A function that creates an observable that delivers event objects. It receives one argument, an object with the following properties:
68
+ *
69
+ * - `input` - Data that was provided to the event observable actor
70
+ * - `self` - The parent actor
71
+ * - `system` - The actor system to which the event observable actor belongs.
72
+ *
73
+ * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * import {
78
+ * fromEventObservable,
79
+ * Subscribable,
80
+ * EventObject,
81
+ * createMachine,
82
+ * createActor
83
+ * } from 'xstate';
84
+ * import { fromEvent } from 'rxjs';
85
+ *
86
+ * const mouseClickLogic = fromEventObservable(() =>
87
+ * fromEvent(document.body, 'click') as Subscribable<EventObject>
88
+ * );
20
89
  *
90
+ * const canvasMachine = createMachine({
91
+ * invoke: {
92
+ * // Will send mouse `click` events to the canvas actor
93
+ * src: mouseClickLogic,
94
+ * }
95
+ * });
21
96
  *
22
- * @param lazyObservable A function that creates an observable
23
- * @returns Event observable logic
97
+ * const canvasActor = createActor(canvasMachine);
98
+ * canvasActor.start();
99
+ * ```
24
100
  */
25
101
  export declare function fromEventObservable<T extends EventObject, TInput>(lazyObservable: ({ input, system }: {
26
102
  input: TInput;
@@ -5,13 +5,62 @@ export type TransitionSnapshot<TContext> = Snapshot<undefined> & {
5
5
  export type TransitionActorLogic<TContext, TEvent extends EventObject, TInput> = ActorLogic<TransitionSnapshot<TContext>, TEvent, TInput, AnyActorSystem>;
6
6
  export type TransitionActorRef<TContext, TEvent extends EventObject> = ActorRefFrom<TransitionActorLogic<TransitionSnapshot<TContext>, TEvent, unknown>>;
7
7
  /**
8
- * Returns actor logic from a transition function and its initial state.
8
+ * Returns actor logic given a transition function and its initial state.
9
9
  *
10
- * A transition function is a function that takes the current state and an event and returns the next state.
10
+ * A transition function is a function that takes the current `state` and received `event` object as arguments, and returns the next state, similar to a reducer.
11
11
  *
12
- * @param transition The transition function that returns the next state given the current state and event.
13
- * @param initialContext The initial state of the transition function.
12
+ * Actors created from transition logic (“transition actors”) can:
13
+ *
14
+ * - Receive events
15
+ * - Emit snapshots of its state
16
+ *
17
+ * The transition function’s `state` is used as its transition actor’s `context`.
18
+ *
19
+ * Note that the "state" for a transition function is provided by the initial state argument, and is not the same as the State object of an actor or a state within a machine configuration.
20
+ *
21
+ * @param transition The transition function used to describe the transition logic. It should return the next state given the current state and event. It receives the following arguments:
22
+ * - `state` - the current state.
23
+ * - `event` - the received event.
24
+ * - `actorScope` - the actor scope object, with properties like `self` and `system`.
25
+ * @param initialContext The initial state of the transition function, either an object representing the state, or a function which returns a state object. If a function, it will receive as its only argument an object with the following properties:
26
+ * - `input` - the `input` provided to its parent transition actor.
27
+ * - `self` - a reference to its parent transition actor.
28
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
14
29
  * @returns Actor logic
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const transitionLogic = fromTransition(
34
+ * (state, event) => {
35
+ * if (event.type === 'increment') {
36
+ * return {
37
+ * ...state,
38
+ * count: state.count + 1,
39
+ * };
40
+ * }
41
+ * return state;
42
+ * },
43
+ * { count: 0 },
44
+ * );
45
+ *
46
+ * const transitionActor = createActor(transitionLogic);
47
+ * transitionActor.subscribe((snapshot) => {
48
+ * console.log(snapshot);
49
+ * });
50
+ * transitionActor.start();
51
+ * // => {
52
+ * // status: 'active',
53
+ * // context: { count: 0 },
54
+ * // ...
55
+ * // }
56
+ *
57
+ * transitionActor.send({ type: 'increment' });
58
+ * // => {
59
+ * // status: 'active',
60
+ * // context: { count: 1 },
61
+ * // ...
62
+ * // }
63
+ * ```
15
64
  */
16
65
  export declare function fromTransition<TContext, TEvent extends EventObject, TSystem extends ActorSystem<any>, TInput>(transition: (state: TContext, event: TEvent, actorScope: ActorScope<TransitionSnapshot<TContext>, TEvent, TSystem>) => TContext, initialContext: TContext | (({ input, self }: {
17
66
  input: TInput;
@@ -0,0 +1,5 @@
1
+ import { StateMachine } from "./StateMachine.js";
2
+ import { ResolveTypegenMeta, TypegenConstraint, TypegenDisabled } from "./typegenTypes.js";
3
+ import { AnyActorRef, AnyEventObject, Cast, InternalMachineImplementations, MachineConfig, MachineContext, NonReducibleUnknown, ParameterizedObject, Prop, ProvidedActor, ToChildren } from "./types.js";
4
+ export declare function createMachine<TContext extends MachineContext, TEvent extends AnyEventObject, // TODO: consider using a stricter `EventObject` here
5
+ TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, TInput, TOutput extends NonReducibleUnknown, TTypesMeta extends TypegenConstraint = TypegenDisabled>(config: MachineConfig<TContext, TEvent, TActor, TAction, TGuard, TDelay, TTag, TInput, TOutput, TTypesMeta>, implementations?: InternalMachineImplementations<TContext, ResolveTypegenMeta<TTypesMeta, TEvent, TActor, TAction, TGuard, TDelay, TTag>>): StateMachine<TContext, TEvent, Cast<ToChildren<TActor>, Record<string, AnyActorRef | undefined>>, TActor, TAction, TGuard, TDelay, Prop<ResolveTypegenMeta<TTypesMeta, TEvent, TActor, TAction, TGuard, TDelay, TTag>['resolved'], 'tags'> & string, TInput, TOutput, ResolveTypegenMeta<TTypesMeta, TEvent, TActor, TAction, TGuard, TDelay, TTag>>;
@@ -1,4 +1,18 @@
1
- import type { EventObject, StateValue, MachineContext, ParameterizedObject, AnyMachineSnapshot, NoRequiredParams, NoInfer, WithDynamicParams } from "./types.js";
1
+ import type { EventObject, StateValue, MachineContext, ParameterizedObject, AnyMachineSnapshot, NoRequiredParams, NoInfer, WithDynamicParams, Identity, Elements } from "./types.js";
2
+ type SingleGuardArg<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TGuardArg> = [TGuardArg] extends [{
3
+ type: string;
4
+ }] ? Identity<TGuardArg> : [TGuardArg] extends [string] ? TGuardArg : GuardPredicate<TContext, TExpressionEvent, TParams, ParameterizedObject>;
5
+ type NormalizeGuardArg<TGuardArg> = TGuardArg extends {
6
+ type: string;
7
+ } ? Identity<TGuardArg> & {
8
+ params: unknown;
9
+ } : TGuardArg extends string ? {
10
+ type: TGuardArg;
11
+ params: undefined;
12
+ } : '_out_TGuard' extends keyof TGuardArg ? TGuardArg['_out_TGuard'] & ParameterizedObject : never;
13
+ type NormalizeGuardArgArray<TArg extends unknown[]> = Elements<{
14
+ [K in keyof TArg]: NormalizeGuardArg<TArg[K]>;
15
+ }>;
2
16
  export type GuardPredicate<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TGuard extends ParameterizedObject> = {
3
17
  (args: GuardArgs<TContext, TExpressionEvent>, params: TParams): boolean;
4
18
  _out_TGuard?: TGuard;
@@ -12,8 +26,16 @@ export type UnknownGuard = UnknownReferencedGuard | UnknownInlineGuard;
12
26
  type UnknownReferencedGuard = Guard<MachineContext, EventObject, ParameterizedObject['params'], ParameterizedObject>;
13
27
  type UnknownInlineGuard = Guard<MachineContext, EventObject, undefined, ParameterizedObject>;
14
28
  export declare function stateIn<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined>(stateValue: StateValue): GuardPredicate<TContext, TExpressionEvent, TParams, any>;
15
- export declare function not<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TGuard extends ParameterizedObject>(guard: Guard<TContext, TExpressionEvent, TParams, NoInfer<TGuard>>): GuardPredicate<TContext, TExpressionEvent, TParams, TGuard>;
16
- export declare function and<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TGuard extends ParameterizedObject>(guards: ReadonlyArray<Guard<TContext, TExpressionEvent, TParams, NoInfer<TGuard>>>): GuardPredicate<TContext, TExpressionEvent, TParams, TGuard>;
17
- export declare function or<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TGuard extends ParameterizedObject>(guards: ReadonlyArray<Guard<TContext, TExpressionEvent, TParams, NoInfer<TGuard>>>): GuardPredicate<TContext, TExpressionEvent, TParams, TGuard>;
29
+ export declare function not<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg>(guard: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg>): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArg<NoInfer<TArg>>>;
30
+ export declare function and<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg extends unknown[]>(guards: readonly [
31
+ ...{
32
+ [K in keyof TArg]: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg[K]>;
33
+ }
34
+ ]): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArgArray<NoInfer<TArg>>>;
35
+ export declare function or<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg extends unknown[]>(guards: readonly [
36
+ ...{
37
+ [K in keyof TArg]: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg[K]>;
38
+ }
39
+ ]): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArgArray<NoInfer<TArg>>>;
18
40
  export declare function evaluateGuard<TContext extends MachineContext, TExpressionEvent extends EventObject>(guard: UnknownGuard | UnknownInlineGuard, context: TContext, event: TExpressionEvent, state: AnyMachineSnapshot): boolean;
19
41
  export {};
@@ -8,13 +8,14 @@ export * from "./typegenTypes.js";
8
8
  export * from "./types.js";
9
9
  export { waitFor } from "./waitFor.js";
10
10
  import { Actor, createActor, interpret, Interpreter } from "./interpreter.js";
11
- import { createMachine } from "./Machine.js";
11
+ import { createMachine } from "./createMachine.js";
12
12
  export { type MachineSnapshot, isMachineSnapshot } from "./State.js";
13
13
  import { StateNode } from "./StateNode.js";
14
- export { matchesState, pathToStateValue, toObserver } from "./utils.js";
14
+ export { matchesState, pathToStateValue, toObserver, getAllOwnEventDescriptors as __unsafe_getAllOwnEventDescriptors } from "./utils.js";
15
15
  export { Actor, createActor, createMachine, interpret, StateNode, type Interpreter };
16
16
  export type { InspectedActorEvent, InspectedEventEvent, InspectedSnapshotEvent, InspectionEvent } from "./system.js";
17
17
  export { and, not, or, stateIn } from "./guards.js";
18
+ export { setup } from "./setup.js";
18
19
  declare global {
19
20
  interface SymbolConstructor {
20
21
  readonly observable: symbol;
@@ -39,6 +39,7 @@ export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<Eve
39
39
  private observers;
40
40
  private logger;
41
41
  _parent?: ActorRef<any, any>;
42
+ _syncSnapshot?: boolean;
42
43
  ref: ActorRef<EventFromLogic<TLogic>, SnapshotFrom<TLogic>>;
43
44
  private _actorScope;
44
45
  private _systemId;
@@ -0,0 +1,35 @@
1
+ import { StateMachine } from "./StateMachine.js";
2
+ import { GuardPredicate } from "./guards.js";
3
+ import { ResolveTypegenMeta, TypegenDisabled } from "./typegenTypes.js";
4
+ import { ActionFunction, AnyActorLogic, AnyActorRef, AnyEventObject, Cast, DelayConfig, Invert, IsNever, MachineConfig, MachineContext, NonReducibleUnknown, ParameterizedObject, SetupTypes, ToChildren, Values } from "./types.js";
5
+ type ToParameterizedObject<TParameterizedMap extends Record<string, ParameterizedObject['params'] | undefined>> = Values<{
6
+ [K in keyof TParameterizedMap & string]: {
7
+ type: K;
8
+ params: TParameterizedMap[K];
9
+ };
10
+ }>;
11
+ type DefaultToAnyActors<TActors extends Record<string, AnyActorLogic>> = IsNever<keyof TActors> extends true ? Record<string, AnyActorLogic> : TActors;
12
+ type ToProvidedActor<TChildrenMap extends Record<string, string>, TActors extends Record<Values<TChildrenMap>, AnyActorLogic>> = Values<{
13
+ [K in keyof DefaultToAnyActors<TActors> & string]: {
14
+ src: K;
15
+ logic: TActors[K];
16
+ id: IsNever<TChildrenMap> extends true ? string | undefined : K extends keyof Invert<TChildrenMap> ? Invert<TChildrenMap>[K] : string | undefined;
17
+ };
18
+ }>;
19
+ export declare function setup<TContext extends MachineContext, TEvent extends AnyEventObject, // TODO: consider using a stricter `EventObject` here
20
+ TActors extends Record<Values<TChildrenMap>, AnyActorLogic>, TActions extends Record<string, ParameterizedObject['params'] | undefined>, TGuards extends Record<string, ParameterizedObject['params'] | undefined>, TDelay extends string, TTag extends string, TInput, TOutput extends NonReducibleUnknown, TChildrenMap extends Record<string, string> = never>({ actors, actions, guards, delays }: {
21
+ types?: SetupTypes<TContext, TEvent, TChildrenMap, TTag, TInput, TOutput>;
22
+ actors?: TActors;
23
+ actions?: {
24
+ [K in keyof TActions]: ActionFunction<TContext, TEvent, TEvent, TActions[K], ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay>;
25
+ };
26
+ guards?: {
27
+ [K in keyof TGuards]: GuardPredicate<TContext, TEvent, TGuards[K], ToParameterizedObject<TGuards>>;
28
+ };
29
+ delays?: {
30
+ [K in TDelay]: DelayConfig<TContext, TEvent, ToParameterizedObject<TActions>['params'], TEvent>;
31
+ };
32
+ }): {
33
+ createMachine: <const TConfig extends MachineConfig<TContext, TEvent, ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay, TTag, TInput, TOutput, ResolveTypegenMeta<TypegenDisabled, TEvent, ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay, TTag>>>(config: TConfig) => StateMachine<TContext, TEvent, Cast<ToChildren<ToProvidedActor<TChildrenMap, TActors>>, Record<string, AnyActorRef | undefined>>, ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay, TTag, TInput, TOutput, ResolveTypegenMeta<TypegenDisabled, TEvent, ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay, TTag>>;
34
+ };
35
+ export {};
@@ -1,14 +1,14 @@
1
1
  import { MachineSnapshot } from "./State.js";
2
2
  import type { StateNode } from "./StateNode.js";
3
3
  import { AnyEventObject, AnyHistoryValue, AnyMachineSnapshot, AnyStateNode, AnyTransitionDefinition, DelayedTransitionDefinition, EventObject, InitialTransitionConfig, InitialTransitionDefinition, MachineContext, StateValue, StateValueMap, TransitionDefinition, TODO, UnknownAction, AnyTransitionConfig, AnyActorScope } from "./types.js";
4
- type Configuration<TContext extends MachineContext, TE extends EventObject> = Iterable<StateNode<TContext, TE>>;
5
- type AnyConfiguration = Configuration<any, any>;
4
+ type StateNodeIterable<TContext extends MachineContext, TE extends EventObject> = Iterable<StateNode<TContext, TE>>;
5
+ type AnyStateNodeIterable = StateNodeIterable<any, any>;
6
6
  type AdjList = Map<AnyStateNode, Array<AnyStateNode>>;
7
7
  export declare const isAtomicStateNode: (stateNode: StateNode<any, any>) => boolean;
8
- export declare function getConfiguration(stateNodes: Iterable<AnyStateNode>): Set<AnyStateNode>;
9
- export declare function getAdjList<TContext extends MachineContext, TE extends EventObject>(configuration: Configuration<TContext, TE>): AdjList;
10
- export declare function getStateValue(rootNode: AnyStateNode, configuration: AnyConfiguration): StateValue;
11
- export declare function isInFinalState(configuration: Set<AnyStateNode>, stateNode: AnyStateNode): boolean;
8
+ export declare function getAllStateNodes(stateNodes: Iterable<AnyStateNode>): Set<AnyStateNode>;
9
+ export declare function getAdjList<TContext extends MachineContext, TE extends EventObject>(stateNodes: StateNodeIterable<TContext, TE>): AdjList;
10
+ export declare function getStateValue(rootNode: AnyStateNode, stateNodes: AnyStateNodeIterable): StateValue;
11
+ export declare function isInFinalState(stateNodeSet: Set<AnyStateNode>, stateNode: AnyStateNode): boolean;
12
12
  export declare const isStateId: (str: string) => boolean;
13
13
  export declare function getCandidates<TEvent extends EventObject>(stateNode: StateNode<any, TEvent>, receivedEventType: TEvent['type']): Array<TransitionDefinition<any, TEvent>>;
14
14
  /**
@@ -41,7 +41,7 @@ export declare function transitionAtomicNode<TContext extends MachineContext, TE
41
41
  export declare function transitionCompoundNode<TContext extends MachineContext, TEvent extends EventObject>(stateNode: AnyStateNode, stateValue: StateValueMap, state: MachineSnapshot<TContext, TEvent, any, any, any, any>, event: TEvent): Array<TransitionDefinition<TContext, TEvent>> | undefined;
42
42
  export declare function transitionParallelNode<TContext extends MachineContext, TEvent extends EventObject>(stateNode: AnyStateNode, stateValue: StateValueMap, state: MachineSnapshot<TContext, TEvent, any, any, any, any>, event: TEvent): Array<TransitionDefinition<TContext, TEvent>> | undefined;
43
43
  export declare function transitionNode<TContext extends MachineContext, TEvent extends EventObject>(stateNode: AnyStateNode, stateValue: StateValue, state: MachineSnapshot<TContext, TEvent, any, any, any, any>, event: TEvent): Array<TransitionDefinition<TContext, TEvent>> | undefined;
44
- export declare function removeConflictingTransitions(enabledTransitions: Array<AnyTransitionDefinition>, configuration: Set<AnyStateNode>, historyValue: AnyHistoryValue): Array<AnyTransitionDefinition>;
44
+ export declare function removeConflictingTransitions(enabledTransitions: Array<AnyTransitionDefinition>, stateNodeSet: Set<AnyStateNode>, historyValue: AnyHistoryValue): Array<AnyTransitionDefinition>;
45
45
  /**
46
46
  * https://www.w3.org/TR/scxml/#microstepProcedure
47
47
  */