UNPKG

5.16 kBTypeScriptView Raw
1import { AnyActorSystem } from "../system.js";
2import { ActorLogic, ActorRefFromLogic, 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, TEmitted extends EventObject = EventObject> = ActorLogic<CallbackSnapshot<TInput>, TEvent, TInput, AnyActorSystem, TEmitted>;
7/**
8 * Represents an actor created by `fromCallback`.
9 *
10 * The type of `self` within the actor's logic.
11 *
12 * @example
13 *
14 * ```ts
15 * import { fromCallback, createActor } from 'xstate';
16 *
17 * // The events the actor receives.
18 * type Event = { type: 'someEvent' };
19 * // The actor's input.
20 * type Input = { name: string };
21 *
22 * // Actor logic that logs whenever it receives an event of type `someEvent`.
23 * const logic = fromCallback<Event, Input>(({ self, input, receive }) => {
24 * self;
25 * // ^? CallbackActorRef<Event, Input>
26 *
27 * receive((event) => {
28 * if (event.type === 'someEvent') {
29 * console.log(`${input.name}: received "someEvent" event`);
30 * // logs 'myActor: received "someEvent" event'
31 * }
32 * });
33 * });
34 *
35 * const actor = createActor(logic, { input: { name: 'myActor' } });
36 * // ^? CallbackActorRef<Event, Input>
37 * ```
38 *
39 * @see {@link fromCallback}
40 */
41export type CallbackActorRef<TEvent extends EventObject, TInput = NonReducibleUnknown> = ActorRefFromLogic<CallbackActorLogic<TEvent, TInput>>;
42type Receiver<TEvent extends EventObject> = (listener: {
43 bivarianceHack(event: TEvent): void;
44}['bivarianceHack']) => void;
45export type CallbackLogicFunction<TEvent extends EventObject = AnyEventObject, TSentEvent extends EventObject = AnyEventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ({ input, system, self, sendBack, receive, emit }: {
46 /**
47 * Data that was provided to the callback actor
48 *
49 * @see {@link https://stately.ai/docs/input | Input docs}
50 */
51 input: TInput;
52 /** The actor system to which the callback actor belongs */
53 system: AnyActorSystem;
54 /** The parent actor of the callback actor */
55 self: CallbackActorRef<TEvent>;
56 /** A function that can send events back to the parent actor */
57 sendBack: (event: TSentEvent) => void;
58 /**
59 * A function that can be called with a listener function argument; the
60 * listener is then called whenever events are received by the callback actor
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 */
131export declare function fromCallback<TEvent extends EventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(callback: CallbackLogicFunction<TEvent, AnyEventObject, TInput, TEmitted>): CallbackActorLogic<TEvent, TInput, TEmitted>;
132export {};
133
\No newline at end of file