UNPKG

2.33 kBTypeScriptView Raw
1import { AnyActorSystem } from "../system.js";
2import { ActorLogic, ActorRefFrom, EventObject, NonReducibleUnknown, Snapshot } from "../types.js";
3export type PromiseSnapshot<TOutput, TInput> = Snapshot<TOutput> & {
4 input: TInput | undefined;
5};
6export type PromiseActorLogic<TOutput, TInput = unknown> = ActorLogic<PromiseSnapshot<TOutput, TInput>, {
7 type: string;
8 [k: string]: unknown;
9}, TInput, // input
10AnyActorSystem, EventObject>;
11export type PromiseActorRef<TOutput> = ActorRefFrom<PromiseActorLogic<TOutput, unknown>>;
12/**
13 * An actor logic creator which returns promise logic as defined by an async process that resolves or rejects after some time.
14 *
15 * Actors created from promise actor logic (“promise actors”) can:
16 * - Emit the resolved value of the promise
17 * - Output the resolved value of the promise
18 *
19 * Sending events to promise actors will have no effect.
20 *
21 * @param promiseCreator
22 * A function which returns a Promise, and accepts an object with the following properties:
23 * - `input` - Data that was provided to the promise actor
24 * - `self` - The parent actor of the promise actor
25 * - `system` - The actor system to which the promise actor belongs
26 * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
27 *
28 * @example
29 * ```ts
30 * const promiseLogic = fromPromise(async () => {
31 * const result = await fetch('https://example.com/...')
32 * .then((data) => data.json());
33 *
34 * return result;
35 * });
36 *
37 * const promiseActor = createActor(promiseLogic);
38 * promiseActor.subscribe((snapshot) => {
39 * console.log(snapshot);
40 * });
41 * promiseActor.start();
42 * // => {
43 * // output: undefined,
44 * // status: 'active'
45 * // ...
46 * // }
47 *
48 * // After promise resolves
49 * // => {
50 * // output: { ... },
51 * // status: 'done',
52 * // ...
53 * // }
54 * ```
55 */
56export declare function fromPromise<TOutput, TInput = NonReducibleUnknown>(promiseCreator: ({ input, system }: {
57 /**
58 * Data that was provided to the promise actor
59 */
60 input: TInput;
61 /**
62 * The actor system to which the promise actor belongs
63 */
64 system: AnyActorSystem;
65 /**
66 * The parent actor of the promise actor
67 */
68 self: PromiseActorRef<TOutput>;
69}) => PromiseLike<TOutput>): PromiseActorLogic<TOutput, TInput>;