UNPKG

8.17 kBTypeScriptView Raw
1import { symbolObservable } from "./symbolObservable.js";
2import { AnyActorSystem, Clock } from "./system.js";
3export declare let executingCustomAction: boolean;
4import type { AnyActorLogic, AnyActorRef, ConditionalRequired, EmittedFrom, EventFromLogic, InputFrom, IsNotNever, Snapshot, SnapshotFrom } from "./types.js";
5import { ActorOptions, ActorRef, InteropSubscribable, Observer, Subscription } from "./types.js";
6export declare const $$ACTOR_TYPE = 1;
7export declare enum ProcessingStatus {
8 NotStarted = 0,
9 Running = 1,
10 Stopped = 2
11}
12/**
13 * An Actor is a running process that can receive events, send events and change
14 * its behavior based on the events it receives, which can cause effects outside
15 * of the actor. When you run a state machine, it becomes an actor.
16 */
17export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<SnapshotFrom<TLogic>, EventFromLogic<TLogic>, EmittedFrom<TLogic>> {
18 logic: TLogic;
19 /** The current internal state of the actor. */
20 private _snapshot;
21 /**
22 * The clock that is responsible for setting and clearing timeouts, such as
23 * delayed events and transitions.
24 */
25 clock: Clock;
26 options: Readonly<ActorOptions<TLogic>>;
27 /** The unique identifier for this actor relative to its parent. */
28 id: string;
29 private mailbox;
30 private observers;
31 private eventListeners;
32 private logger;
33 _parent?: AnyActorRef;
34 ref: ActorRef<SnapshotFrom<TLogic>, EventFromLogic<TLogic>, EmittedFrom<TLogic>>;
35 private _actorScope;
36 private _systemId;
37 /** The globally unique process ID for this invocation. */
38 sessionId: string;
39 /** The system to which this actor belongs. */
40 system: AnyActorSystem;
41 private _doneEvent?;
42 src: string | AnyActorLogic;
43 /**
44 * Creates a new actor instance for the given logic with the provided options,
45 * if any.
46 *
47 * @param logic The logic to create an actor from
48 * @param options Actor options
49 */
50 constructor(logic: TLogic, options?: ActorOptions<TLogic>);
51 private _initState;
52 private _deferred;
53 private update;
54 /**
55 * Subscribe an observer to an actor’s snapshot values.
56 *
57 * @remarks
58 * The observer will receive the actor’s snapshot value when it is emitted.
59 * The observer can be:
60 *
61 * - A plain function that receives the latest snapshot, or
62 * - An observer object whose `.next(snapshot)` method receives the latest
63 * snapshot
64 *
65 * @example
66 *
67 * ```ts
68 * // Observer as a plain function
69 * const subscription = actor.subscribe((snapshot) => {
70 * console.log(snapshot);
71 * });
72 * ```
73 *
74 * @example
75 *
76 * ```ts
77 * // Observer as an object
78 * const subscription = actor.subscribe({
79 * next(snapshot) {
80 * console.log(snapshot);
81 * },
82 * error(err) {
83 * // ...
84 * },
85 * complete() {
86 * // ...
87 * }
88 * });
89 * ```
90 *
91 * The return value of `actor.subscribe(observer)` is a subscription object
92 * that has an `.unsubscribe()` method. You can call
93 * `subscription.unsubscribe()` to unsubscribe the observer:
94 *
95 * @example
96 *
97 * ```ts
98 * const subscription = actor.subscribe((snapshot) => {
99 * // ...
100 * });
101 *
102 * // Unsubscribe the observer
103 * subscription.unsubscribe();
104 * ```
105 *
106 * When the actor is stopped, all of its observers will automatically be
107 * unsubscribed.
108 *
109 * @param observer - Either a plain function that receives the latest
110 * snapshot, or an observer object whose `.next(snapshot)` method receives
111 * the latest snapshot
112 */
113 subscribe(observer: Observer<SnapshotFrom<TLogic>>): Subscription;
114 subscribe(nextListener?: (snapshot: SnapshotFrom<TLogic>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Subscription;
115 on<TType extends EmittedFrom<TLogic>['type'] | '*'>(type: TType, handler: (emitted: EmittedFrom<TLogic> & (TType extends '*' ? unknown : {
116 type: TType;
117 })) => void): Subscription;
118 /** Starts the Actor from the initial state */
119 start(): this;
120 private _process;
121 private _stop;
122 /** Stops the Actor and unsubscribe all listeners. */
123 stop(): this;
124 private _complete;
125 private _reportError;
126 private _error;
127 private _stopProcedure;
128 /**
129 * Sends an event to the running Actor to trigger a transition.
130 *
131 * @param event The event to send
132 */
133 send(event: EventFromLogic<TLogic>): void;
134 private attachDevTools;
135 toJSON(): {
136 xstate$$type: number;
137 id: string;
138 };
139 /**
140 * Obtain the internal state of the actor, which can be persisted.
141 *
142 * @remarks
143 * The internal state can be persisted from any actor, not only machines.
144 *
145 * Note that the persisted state is not the same as the snapshot from
146 * {@link Actor.getSnapshot}. Persisted state represents the internal state of
147 * the actor, while snapshots represent the actor's last emitted value.
148 *
149 * Can be restored with {@link ActorOptions.state}
150 * @see https://stately.ai/docs/persistence
151 */
152 getPersistedSnapshot(): Snapshot<unknown>;
153 [symbolObservable](): InteropSubscribable<SnapshotFrom<TLogic>>;
154 /**
155 * Read an actor’s snapshot synchronously.
156 *
157 * @remarks
158 * The snapshot represent an actor's last emitted value.
159 *
160 * When an actor receives an event, its internal state may change. An actor
161 * may emit a snapshot when a state transition occurs.
162 *
163 * Note that some actors, such as callback actors generated with
164 * `fromCallback`, will not emit snapshots.
165 * @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
166 * @see {@link Actor.getPersistedSnapshot} to persist the internal state of an actor (which is more than just a snapshot).
167 */
168 getSnapshot(): SnapshotFrom<TLogic>;
169}
170export type RequiredActorOptionsKeys<TLogic extends AnyActorLogic> = undefined extends InputFrom<TLogic> ? never : 'input';
171/**
172 * Creates a new actor instance for the given actor logic with the provided
173 * options, if any.
174 *
175 * @remarks
176 * When you create an actor from actor logic via `createActor(logic)`, you
177 * implicitly create an actor system where the created actor is the root actor.
178 * Any actors spawned from this root actor and its descendants are part of that
179 * actor system.
180 * @example
181 *
182 * ```ts
183 * import { createActor } from 'xstate';
184 * import { someActorLogic } from './someActorLogic.ts';
185 *
186 * // Creating the actor, which implicitly creates an actor system with itself as the root actor
187 * const actor = createActor(someActorLogic);
188 *
189 * actor.subscribe((snapshot) => {
190 * console.log(snapshot);
191 * });
192 *
193 * // Actors must be started by calling `actor.start()`, which will also start the actor system.
194 * actor.start();
195 *
196 * // Actors can receive events
197 * actor.send({ type: 'someEvent' });
198 *
199 * // You can stop root actors by calling `actor.stop()`, which will also stop the actor system and all actors in that system.
200 * actor.stop();
201 * ```
202 *
203 * @param logic - The actor logic to create an actor from. For a state machine
204 * actor logic creator, see {@link createMachine}. Other actor logic creators
205 * include {@link fromCallback}, {@link fromEventObservable},
206 * {@link fromObservable}, {@link fromPromise}, and {@link fromTransition}.
207 * @param options - Actor options
208 */
209export declare function createActor<TLogic extends AnyActorLogic>(logic: TLogic, ...[options]: ConditionalRequired<[
210 options?: ActorOptions<TLogic> & {
211 [K in RequiredActorOptionsKeys<TLogic>]: unknown;
212 }
213], IsNotNever<RequiredActorOptionsKeys<TLogic>>>): Actor<TLogic>;
214/**
215 * Creates a new Interpreter instance for the given machine with the provided
216 * options, if any.
217 *
218 * @deprecated Use `createActor` instead
219 * @alias
220 */
221export declare const interpret: typeof createActor;
222/**
223 * @deprecated Use `Actor` instead.
224 * @alias
225 */
226export type Interpreter = typeof Actor;