1 | import { AnyActorSystem } from "../system.js";
|
2 | import { ActorLogic, ActorRefFromLogic, ActorScope, EventObject, NonReducibleUnknown, Snapshot } from "../types.js";
|
3 | export type TransitionSnapshot<TContext> = Snapshot<undefined> & {
|
4 | context: TContext;
|
5 | };
|
6 | export type TransitionActorLogic<TContext, TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ActorLogic<TransitionSnapshot<TContext>, TEvent, TInput, AnyActorSystem, TEmitted>;
|
7 | /**
|
8 | * Represents an actor created by `fromTransition`.
|
9 | *
|
10 | * The type of `self` within the actor's logic.
|
11 | *
|
12 | * @example
|
13 | *
|
14 | * ```ts
|
15 | * import {
|
16 | * fromTransition,
|
17 | * createActor,
|
18 | * type AnyActorSystem
|
19 | * } from 'xstate';
|
20 | *
|
21 | * //* The actor's stored context.
|
22 | * type Context = {
|
23 | * // The current count.
|
24 | * count: number;
|
25 | * // The amount to increase `count` by.
|
26 | * step: number;
|
27 | * };
|
28 | * // The events the actor receives.
|
29 | * type Event = { type: 'increment' };
|
30 | * // The actor's input.
|
31 | * type Input = { step?: number };
|
32 | *
|
33 | * // Actor logic that increments `count` by `step` when it receives an event of
|
34 | * // type `increment`.
|
35 | * const logic = fromTransition<Context, Event, AnyActorSystem, Input>(
|
36 | * (state, event, actorScope) => {
|
37 | * actorScope.self;
|
38 | * // ^? TransitionActorRef<Context, Event>
|
39 | *
|
40 | * if (event.type === 'increment') {
|
41 | * return {
|
42 | * ...state,
|
43 | * count: state.count + state.step
|
44 | * };
|
45 | * }
|
46 | * return state;
|
47 | * },
|
48 | * ({ input, self }) => {
|
49 | * self;
|
50 | * // ^? TransitionActorRef<Context, Event>
|
51 | *
|
52 | * return {
|
53 | * count: 0,
|
54 | * step: input.step ?? 1
|
55 | * };
|
56 | * }
|
57 | * );
|
58 | *
|
59 | * const actor = createActor(logic, { input: { step: 10 } });
|
60 | * // ^? TransitionActorRef<Context, Event>
|
61 | * ```
|
62 | *
|
63 | * @see {@link fromTransition}
|
64 | */
|
65 | export type TransitionActorRef<TContext, TEvent extends EventObject> = ActorRefFromLogic<TransitionActorLogic<TransitionSnapshot<TContext>, TEvent, unknown>>;
|
66 | /**
|
67 | * Returns actor logic given a transition function and its initial state.
|
68 | *
|
69 | * A “transition function” is a function that takes the current `state` and
|
70 | * received `event` object as arguments, and returns the next state, similar to
|
71 | * a reducer.
|
72 | *
|
73 | * Actors created from transition logic (“transition actors”) can:
|
74 | *
|
75 | * - Receive events
|
76 | * - Emit snapshots of its state
|
77 | *
|
78 | * The transition function’s `state` is used as its transition actor’s
|
79 | * `context`.
|
80 | *
|
81 | * Note that the "state" for a transition function is provided by the initial
|
82 | * state argument, and is not the same as the State object of an actor or a
|
83 | * state within a machine configuration.
|
84 | *
|
85 | * @example
|
86 | *
|
87 | * ```ts
|
88 | * const transitionLogic = fromTransition(
|
89 | * (state, event) => {
|
90 | * if (event.type === 'increment') {
|
91 | * return {
|
92 | * ...state,
|
93 | * count: state.count + 1
|
94 | * };
|
95 | * }
|
96 | * return state;
|
97 | * },
|
98 | * { count: 0 }
|
99 | * );
|
100 | *
|
101 | * const transitionActor = createActor(transitionLogic);
|
102 | * transitionActor.subscribe((snapshot) => {
|
103 | * console.log(snapshot);
|
104 | * });
|
105 | * transitionActor.start();
|
106 | * // => {
|
107 | * // status: 'active',
|
108 | * // context: { count: 0 },
|
109 | * // ...
|
110 | * // }
|
111 | *
|
112 | * transitionActor.send({ type: 'increment' });
|
113 | * // => {
|
114 | * // status: 'active',
|
115 | * // context: { count: 1 },
|
116 | * // ...
|
117 | * // }
|
118 | * ```
|
119 | *
|
120 | * @param transition The transition function used to describe the transition
|
121 | * logic. It should return the next state given the current state and event.
|
122 | * It receives the following arguments:
|
123 | *
|
124 | * - `state` - the current state.
|
125 | * - `event` - the received event.
|
126 | * - `actorScope` - the actor scope object, with properties like `self` and
|
127 | * `system`.
|
128 | *
|
129 | * @param initialContext The initial state of the transition function, either an
|
130 | * object representing the state, or a function which returns a state object.
|
131 | * If a function, it will receive as its only argument an object with the
|
132 | * following properties:
|
133 | *
|
134 | * - `input` - the `input` provided to its parent transition actor.
|
135 | * - `self` - a reference to its parent transition actor.
|
136 | *
|
137 | * @returns Actor logic
|
138 | * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
|
139 | */
|
140 | export declare function fromTransition<TContext, TEvent extends EventObject, TSystem extends AnyActorSystem, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(transition: (snapshot: TContext, event: TEvent, actorScope: ActorScope<TransitionSnapshot<TContext>, TEvent, TSystem, TEmitted>) => TContext, initialContext: TContext | (({ input, self }: {
|
141 | input: TInput;
|
142 | self: TransitionActorRef<TContext, TEvent>;
|
143 | }) => TContext)): TransitionActorLogic<TContext, TEvent, TInput, TEmitted>;
|