UNPKG

3.35 kBTypeScriptView Raw
1import { AnyActorSystem } from "../system.js";
2import { ActorLogic, ActorRefFromLogic, EventObject, NonReducibleUnknown, Snapshot } from "../types.js";
3export type PromiseSnapshot<TOutput, TInput> = Snapshot<TOutput> & {
4 input: TInput | undefined;
5};
6export type PromiseActorLogic<TOutput, TInput = unknown, TEmitted extends EventObject = EventObject> = ActorLogic<PromiseSnapshot<TOutput, TInput>, {
7 type: string;
8 [k: string]: unknown;
9}, TInput, // input
10AnyActorSystem, TEmitted>;
11/**
12 * Represents an actor created by `fromPromise`.
13 *
14 * The type of `self` within the actor's logic.
15 *
16 * @example
17 *
18 * ```ts
19 * import { fromPromise, createActor } from 'xstate';
20 *
21 * // The actor's resolved output
22 * type Output = string;
23 * // The actor's input.
24 * type Input = { message: string };
25 *
26 * // Actor logic that fetches the url of an image of a cat saying `input.message`.
27 * const logic = fromPromise<Output, Input>(async ({ input, self }) => {
28 * self;
29 * // ^? PromiseActorRef<Output, Input>
30 *
31 * const data = await fetch(
32 * `https://cataas.com/cat/says/${input.message}`
33 * );
34 * const url = await data.json();
35 * return url;
36 * });
37 *
38 * const actor = createActor(logic, { input: { message: 'hello world' } });
39 * // ^? PromiseActorRef<Output, Input>
40 * ```
41 *
42 * @see {@link fromPromise}
43 */
44export type PromiseActorRef<TOutput> = ActorRefFromLogic<PromiseActorLogic<TOutput, unknown>>;
45/**
46 * An actor logic creator which returns promise logic as defined by an async
47 * process that resolves or rejects after some time.
48 *
49 * Actors created from promise actor logic (“promise actors”) can:
50 *
51 * - Emit the resolved value of the promise
52 * - Output the resolved value of the promise
53 *
54 * Sending events to promise actors will have no effect.
55 *
56 * @example
57 *
58 * ```ts
59 * const promiseLogic = fromPromise(async () => {
60 * const result = await fetch('https://example.com/...').then((data) =>
61 * data.json()
62 * );
63 *
64 * return result;
65 * });
66 *
67 * const promiseActor = createActor(promiseLogic);
68 * promiseActor.subscribe((snapshot) => {
69 * console.log(snapshot);
70 * });
71 * promiseActor.start();
72 * // => {
73 * // output: undefined,
74 * // status: 'active'
75 * // ...
76 * // }
77 *
78 * // After promise resolves
79 * // => {
80 * // output: { ... },
81 * // status: 'done',
82 * // ...
83 * // }
84 * ```
85 *
86 * @param promiseCreator A function which returns a Promise, and accepts an
87 * object with the following properties:
88 *
89 * - `input` - Data that was provided to the promise actor
90 * - `self` - The parent actor of the promise actor
91 * - `system` - The actor system to which the promise actor belongs
92 *
93 * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
94 */
95export declare function fromPromise<TOutput, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(promiseCreator: ({ input, system, self, signal, emit }: {
96 /** Data that was provided to the promise actor */
97 input: TInput;
98 /** The actor system to which the promise actor belongs */
99 system: AnyActorSystem;
100 /** The parent actor of the promise actor */
101 self: PromiseActorRef<TOutput>;
102 signal: AbortSignal;
103 emit: (emitted: TEmitted) => void;
104}) => PromiseLike<TOutput>): PromiseActorLogic<TOutput, TInput, TEmitted>;