1 | import { symbolObservable } from "./symbolObservable.js";
|
2 | import { AnyActorSystem, Clock } from "./system.js";
|
3 | import type { AnyActorLogic, AnyActorRef, ConditionalRequired, EmittedFrom, EventFromLogic, InputFrom, IsNotNever, Snapshot, SnapshotFrom } from "./types.js";
|
4 | import { ActorOptions, ActorRef, EventObject, InteropSubscribable, Observer, Subscription } from "./types.js";
|
5 | export declare const $$ACTOR_TYPE = 1;
|
6 | export type SnapshotListener<TLogic extends AnyActorLogic> = (snapshot: SnapshotFrom<TLogic>) => void;
|
7 | export type EventListener<TEvent extends EventObject = EventObject> = (event: TEvent) => void;
|
8 | export type Listener = () => void;
|
9 | export type ErrorListener = (error: any) => void;
|
10 | export declare enum ProcessingStatus {
|
11 | NotStarted = 0,
|
12 | Running = 1,
|
13 | Stopped = 2
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 | export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<SnapshotFrom<TLogic>, EventFromLogic<TLogic>, EmittedFrom<TLogic>> {
|
19 | logic: TLogic;
|
20 | |
21 |
|
22 |
|
23 | private _snapshot;
|
24 | |
25 |
|
26 |
|
27 | clock: Clock;
|
28 | options: Readonly<ActorOptions<TLogic>>;
|
29 | |
30 |
|
31 |
|
32 | id: string;
|
33 | private mailbox;
|
34 | private observers;
|
35 | private eventListeners;
|
36 | private logger;
|
37 | _parent?: AnyActorRef;
|
38 | ref: ActorRef<SnapshotFrom<TLogic>, EventFromLogic<TLogic>, EmittedFrom<TLogic>>;
|
39 | private _actorScope;
|
40 | private _systemId;
|
41 | |
42 |
|
43 |
|
44 | sessionId: string;
|
45 | |
46 |
|
47 |
|
48 | system: AnyActorSystem;
|
49 | private _doneEvent?;
|
50 | src: string | AnyActorLogic;
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | constructor(logic: TLogic, options?: ActorOptions<TLogic>);
|
58 | private _initState;
|
59 | private _deferred;
|
60 | private update;
|
61 | /**
|
62 | * Subscribe an observer to an actor’s snapshot values.
|
63 | *
|
64 | * @remarks
|
65 | * The observer will receive the actor’s snapshot value when it is emitted. The observer can be:
|
66 | * - A plain function that receives the latest snapshot, or
|
67 | * - An observer object whose `.next(snapshot)` method receives the latest snapshot
|
68 | *
|
69 | * @example
|
70 | * ```ts
|
71 | * // Observer as a plain function
|
72 | * const subscription = actor.subscribe((snapshot) => {
|
73 | * console.log(snapshot);
|
74 | * });
|
75 | * ```
|
76 | *
|
77 | * @example
|
78 | * ```ts
|
79 | * // Observer as an object
|
80 | * const subscription = actor.subscribe({
|
81 | * next(snapshot) {
|
82 | * console.log(snapshot);
|
83 | * },
|
84 | * error(err) {
|
85 | *
|
86 | * },
|
87 | * complete() {
|
88 | *
|
89 | * },
|
90 | * });
|
91 | * ```
|
92 | *
|
93 | * The return value of `actor.subscribe(observer)` is a subscription object that has an `.unsubscribe()` method. You can call `subscription.unsubscribe()` to unsubscribe the observer:
|
94 | *
|
95 | * @example
|
96 | * ```ts
|
97 | * const subscription = actor.subscribe((snapshot) => {
|
98 | *
|
99 | * });
|
100 | *
|
101 | * // Unsubscribe the observer
|
102 | * subscription.unsubscribe();
|
103 | * ```
|
104 | *
|
105 | * When the actor is stopped, all of its observers will automatically be unsubscribed.
|
106 | *
|
107 | * @param observer - Either a plain function that receives the latest snapshot, or an observer object whose `.next(snapshot)` method receives the latest snapshot
|
108 | */
|
109 | subscribe(observer: Observer<SnapshotFrom<TLogic>>): Subscription;
|
110 | subscribe(nextListener?: (snapshot: SnapshotFrom<TLogic>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Subscription;
|
111 | on<TType extends EmittedFrom<TLogic>['type']>(type: TType, handler: (emitted: EmittedFrom<TLogic> & {
|
112 | type: TType;
|
113 | }) => void): Subscription;
|
114 | /**
|
115 | * Starts the Actor from the initial state
|
116 | */
|
117 | start(): this;
|
118 | private _process;
|
119 | private _stop;
|
120 | /**
|
121 | * Stops the Actor and unsubscribe all listeners.
|
122 | */
|
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 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | getPersistedSnapshot(): Snapshot<unknown>;
|
152 | [symbolObservable](): InteropSubscribable<SnapshotFrom<TLogic>>;
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | getSnapshot(): SnapshotFrom<TLogic>;
|
168 | }
|
169 | type RequiredOptions<TLogic extends AnyActorLogic> = undefined extends InputFrom<TLogic> ? never : 'input';
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | export declare function createActor<TLogic extends AnyActorLogic>(logic: TLogic, ...[options]: ConditionalRequired<[
|
203 | options?: ActorOptions<TLogic> & {
|
204 | [K in RequiredOptions<TLogic>]: unknown;
|
205 | }
|
206 | ], IsNotNever<RequiredOptions<TLogic>>>): Actor<TLogic>;
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | export declare const interpret: typeof createActor;
|
213 |
|
214 |
|
215 |
|
216 | export type Interpreter = typeof Actor;
|
217 | export {};
|