UNPKG

4 kBTypeScriptView Raw
1import { AnyActorSystem } from "../system.js";
2import { ActorLogic, ActorRefFrom, AnyEventObject, EventObject, NonReducibleUnknown, Snapshot } from "../types.js";
3export type CallbackSnapshot<TInput> = Snapshot<undefined> & {
4 input: TInput;
5};
6export type CallbackActorLogic<TEvent extends EventObject, TInput = NonReducibleUnknown> = ActorLogic<CallbackSnapshot<TInput>, TEvent, TInput, AnyActorSystem, EventObject>;
7export type CallbackActorRef<TEvent extends EventObject, TInput = NonReducibleUnknown> = ActorRefFrom<CallbackActorLogic<TEvent, TInput>>;
8export type Receiver<TEvent extends EventObject> = (listener: {
9 bivarianceHack(event: TEvent): void;
10}['bivarianceHack']) => void;
11export type InvokeCallback<TEvent extends EventObject = AnyEventObject, TSentEvent extends EventObject = AnyEventObject, TInput = NonReducibleUnknown> = ({ input, system, self, sendBack, receive }: {
12 /**
13 * Data that was provided to the callback actor
14 * @see {@link https://stately.ai/docs/input | Input docs}
15 */
16 input: TInput;
17 /**
18 * The actor system to which the callback actor belongs
19 */
20 system: AnyActorSystem;
21 /**
22 * The parent actor of the callback actor
23 */
24 self: CallbackActorRef<TEvent>;
25 /**
26 * A function that can send events back to the parent actor
27 */
28 sendBack: (event: TSentEvent) => void;
29 /**
30 * A function that can be called with a listener function argument;
31 * the listener is then called whenever events are received by the callback actor
32 */
33 receive: Receiver<TEvent>;
34}) => (() => void) | void;
35/**
36 * An actor logic creator which returns callback logic as defined by a callback function.
37 *
38 * @remarks
39 * Useful for subscription-based or other free-form logic that can send events back to the parent actor.
40 *
41 * Actors created from callback logic (“callback actors”) can:
42 * - Receive events via the `receive` function
43 * - Send events to the parent actor via the `sendBack` function
44 *
45 * Callback actors are a bit different from other actors in that they:
46 * - Do not work with `onDone`
47 * - Do not produce a snapshot using `.getSnapshot()`
48 * - Do not emit values when used with `.subscribe()`
49 * - Can not be stopped with `.stop()`
50 *
51 * @param invokeCallback - The callback function used to describe the callback logic
52 * The callback function is passed an object with the following properties:
53 * - `receive` - A function that can send events back to the parent actor; the listener is then called whenever events are received by the callback actor
54 * - `sendBack` - A function that can send events back to the parent actor
55 * - `input` - Data that was provided to the callback actor
56 * - `self` - The parent actor of the callback actor
57 * - `system` - The actor system to which the callback actor belongs
58 * The callback function can (optionally) return a cleanup function, which is called when the actor is stopped.
59 * @see {@link InvokeCallback} for more information about the callback function and its object argument
60 * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
61
62 * @returns Callback logic
63 *
64 * @example
65 * ```typescript
66 * const callbackLogic = fromCallback(({ sendBack, receive }) => {
67 * let lockStatus = 'unlocked';
68 *
69 * const handler = (event) => {
70 * if (lockStatus === 'locked') {
71 * return;
72 * }
73 * sendBack(event);
74 * };
75 *
76 * receive((event) => {
77 * if (event.type === 'lock') {
78 * lockStatus = 'locked';
79 * } else if (event.type === 'unlock') {
80 * lockStatus = 'unlocked';
81 * }
82 * });
83 *
84 * document.body.addEventListener('click', handler);
85 *
86 * return () => {
87 * document.body.removeEventListener('click', handler);
88 * };
89 * });
90 * ```
91 */
92export declare function fromCallback<TEvent extends EventObject, TInput = NonReducibleUnknown>(invokeCallback: InvokeCallback<TEvent, AnyEventObject, TInput>): CallbackActorLogic<TEvent, TInput>;
93
\No newline at end of file