UNPKG

5.44 kBTypeScriptView Raw
1import { AnyActorSystem } from "../system.js";
2import { ActorLogic, ActorRefFromLogic, EventObject, NonReducibleUnknown, Snapshot, Subscribable, Subscription } from "../types.js";
3export type ObservableSnapshot<TContext, TInput extends NonReducibleUnknown> = Snapshot<undefined> & {
4 context: TContext | undefined;
5 input: TInput | undefined;
6 _subscription: Subscription | undefined;
7};
8export type ObservableActorLogic<TContext, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ActorLogic<ObservableSnapshot<TContext, TInput>, {
9 type: string;
10 [k: string]: unknown;
11}, TInput, AnyActorSystem, TEmitted>;
12/**
13 * Represents an actor created by `fromObservable` or `fromEventObservable`.
14 *
15 * The type of `self` within the actor's logic.
16 *
17 * @example
18 *
19 * ```ts
20 * import { fromObservable, createActor } from 'xstate';
21 * import { interval } from 'rxjs';
22 *
23 * // The type of the value observed by the actor's logic.
24 * type Context = number;
25 * // The actor's input.
26 * type Input = { period?: number };
27 *
28 * // Actor logic that observes a number incremented every `input.period`
29 * // milliseconds (default: 1_000).
30 * const logic = fromObservable<Context, Input>(({ input, self }) => {
31 * self;
32 * // ^? ObservableActorRef<Event, Input>
33 *
34 * return interval(input.period ?? 1_000);
35 * });
36 *
37 * const actor = createActor(logic, { input: { period: 2_000 } });
38 * // ^? ObservableActorRef<Event, Input>
39 * ```
40 *
41 * @see {@link fromObservable}
42 * @see {@link fromEventObservable}
43 */
44export type ObservableActorRef<TContext> = ActorRefFromLogic<ObservableActorLogic<TContext, any>>;
45/**
46 * Observable actor logic is described by an observable stream of values. Actors
47 * created from observable logic (“observable actors”) can:
48 *
49 * - Emit snapshots of the observable’s emitted value
50 *
51 * The observable’s emitted value is used as its observable actor’s `context`.
52 *
53 * Sending events to observable actors will have no effect.
54 *
55 * @example
56 *
57 * ```ts
58 * import { fromObservable, createActor } from 'xstate';
59 * import { interval } from 'rxjs';
60 *
61 * const logic = fromObservable((obj) => interval(1000));
62 *
63 * const actor = createActor(logic);
64 *
65 * actor.subscribe((snapshot) => {
66 * console.log(snapshot.context);
67 * });
68 *
69 * actor.start();
70 * // At every second:
71 * // Logs 0
72 * // Logs 1
73 * // Logs 2
74 * // ...
75 * ```
76 *
77 * @param observableCreator A function that creates an observable. It receives
78 * one argument, an object with the following properties:
79 *
80 * - `input` - Data that was provided to the observable actor
81 * - `self` - The parent actor
82 * - `system` - The actor system to which the observable actor belongs
83 *
84 * It should return a {@link Subscribable}, which is compatible with an RxJS
85 * Observable, although RxJS is not required to create them.
86 * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
87 * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
88 */
89export declare function fromObservable<TContext, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(observableCreator: ({ input, system, self }: {
90 input: TInput;
91 system: AnyActorSystem;
92 self: ObservableActorRef<TContext>;
93 emit: (emitted: TEmitted) => void;
94}) => Subscribable<TContext>): ObservableActorLogic<TContext, TInput, TEmitted>;
95/**
96 * Creates event observable logic that listens to an observable that delivers
97 * event objects.
98 *
99 * Event observable actor logic is described by an observable stream of
100 * {@link https://stately.ai/docs/transitions#event-objects | event objects}.
101 * Actors created from event observable logic (“event observable actors”) can:
102 *
103 * - Implicitly send events to its parent actor
104 * - Emit snapshots of its emitted event objects
105 *
106 * Sending events to event observable actors will have no effect.
107 *
108 * @example
109 *
110 * ```ts
111 * import {
112 * fromEventObservable,
113 * Subscribable,
114 * EventObject,
115 * createMachine,
116 * createActor
117 * } from 'xstate';
118 * import { fromEvent } from 'rxjs';
119 *
120 * const mouseClickLogic = fromEventObservable(
121 * () => fromEvent(document.body, 'click') as Subscribable<EventObject>
122 * );
123 *
124 * const canvasMachine = createMachine({
125 * invoke: {
126 * // Will send mouse `click` events to the canvas actor
127 * src: mouseClickLogic
128 * }
129 * });
130 *
131 * const canvasActor = createActor(canvasMachine);
132 * canvasActor.start();
133 * ```
134 *
135 * @param lazyObservable A function that creates an observable that delivers
136 * event objects. It receives one argument, an object with the following
137 * properties:
138 *
139 * - `input` - Data that was provided to the event observable actor
140 * - `self` - The parent actor
141 * - `system` - The actor system to which the event observable actor belongs.
142 *
143 * It should return a {@link Subscribable}, which is compatible with an RxJS
144 * Observable, although RxJS is not required to create them.
145 */
146export declare function fromEventObservable<TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(lazyObservable: ({ input, system, self, emit }: {
147 input: TInput;
148 system: AnyActorSystem;
149 self: ObservableActorRef<TEvent>;
150 emit: (emitted: TEmitted) => void;
151}) => Subscribable<TEvent>): ObservableActorLogic<TEvent, TInput, TEmitted>;