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