UNPKG

3.35 kBTypeScriptView Raw
1import { AnyActorSystem } from "../system.js";
2import { ActorLogic, ActorRefFrom, ActorScope, EventObject, NonReducibleUnknown, Snapshot } from "../types.js";
3export type TransitionSnapshot<TContext> = Snapshot<undefined> & {
4 context: TContext;
5};
6export type TransitionActorLogic<TContext, TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ActorLogic<TransitionSnapshot<TContext>, TEvent, TInput, AnyActorSystem, TEmitted>;
7export type TransitionActorRef<TContext, TEvent extends EventObject> = ActorRefFrom<TransitionActorLogic<TransitionSnapshot<TContext>, TEvent, unknown>>;
8/**
9 * Returns actor logic given a transition function and its initial state.
10 *
11 * 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.
12 *
13 * Actors created from transition logic (“transition actors”) can:
14 *
15 * - Receive events
16 * - Emit snapshots of its state
17 *
18 * The transition function’s `state` is used as its transition actor’s `context`.
19 *
20 * 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.
21 *
22 * @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:
23 * - `state` - the current state.
24 * - `event` - the received event.
25 * - `actorScope` - the actor scope object, with properties like `self` and `system`.
26 * @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:
27 * - `input` - the `input` provided to its parent transition actor.
28 * - `self` - a reference to its parent transition actor.
29 * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
30 * @returns Actor logic
31 *
32 * @example
33 * ```ts
34 * const transitionLogic = fromTransition(
35 * (state, event) => {
36 * if (event.type === 'increment') {
37 * return {
38 * ...state,
39 * count: state.count + 1,
40 * };
41 * }
42 * return state;
43 * },
44 * { count: 0 },
45 * );
46 *
47 * const transitionActor = createActor(transitionLogic);
48 * transitionActor.subscribe((snapshot) => {
49 * console.log(snapshot);
50 * });
51 * transitionActor.start();
52 * // => {
53 * // status: 'active',
54 * // context: { count: 0 },
55 * // ...
56 * // }
57 *
58 * transitionActor.send({ type: 'increment' });
59 * // => {
60 * // status: 'active',
61 * // context: { count: 1 },
62 * // ...
63 * // }
64 * ```
65 */
66export 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>) => TContext, initialContext: TContext | (({ input, self }: {
67 input: TInput;
68 self: TransitionActorRef<TContext, TEvent>;
69}) => TContext)): TransitionActorLogic<TContext, TEvent, TInput, TEmitted>;