UNPKG

4.21 kBTypeScriptView Raw
1import { AnyActorSystem } from "../system.js";
2import { ActorLogic, ActorRefFrom, 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> = ActorLogic<ObservableSnapshot<TContext, TInput>, {
9 type: string;
10 [k: string]: unknown;
11}, TInput, AnyActorSystem, EventObject>;
12export type ObservableActorRef<TContext> = ActorRefFrom<ObservableActorLogic<TContext, any>>;
13/**
14 * Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
15 *
16 * - Emit snapshots of the observable’s emitted value
17 *
18 * The observable’s emitted value is used as its observable actor’s `context`.
19 *
20 * Sending events to observable actors will have no effect.
21 *
22 * @param observableCreator A function that creates an observable. It receives one argument, an object with the following properties:
23 * - `input` - Data that was provided to the observable actor
24 * - `self` - The parent actor
25 * - `system` - The actor system to which the observable actor belongs
26 *
27 * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
28 *
29 * @example
30 * ```ts
31 * import { fromObservable, createActor } from 'xstate'
32 * import { interval } from 'rxjs';
33 *
34 * const logic = fromObservable((obj) => interval(1000));
35 *
36 * const actor = createActor(logic);
37 *
38 * actor.subscribe((snapshot) => {
39 * console.log(snapshot.context);
40 * });
41 *
42 * actor.start();
43 * // At every second:
44 * // Logs 0
45 * // Logs 1
46 * // Logs 2
47 * // ...
48 * ```
49 *
50 * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
51 * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
52 */
53export declare function fromObservable<TContext, TInput extends NonReducibleUnknown>(observableCreator: ({ input, system }: {
54 input: TInput;
55 system: AnyActorSystem;
56 self: ObservableActorRef<TContext>;
57}) => Subscribable<TContext>): ObservableActorLogic<TContext, TInput>;
58/**
59 * Creates event observable logic that listens to an observable that delivers event objects.
60 *
61 * Event observable actor logic is described by an observable stream of {@link https://stately.ai/docs/transitions#event-objects | event objects}. Actors created from event observable logic (“event observable actors”) can:
62 *
63 * - Implicitly send events to its parent actor
64 * - Emit snapshots of its emitted event objects
65 *
66 * Sending events to event observable actors will have no effect.
67 *
68 * @param lazyObservable A function that creates an observable that delivers event objects. It receives one argument, an object with the following properties:
69 *
70 * - `input` - Data that was provided to the event observable actor
71 * - `self` - The parent actor
72 * - `system` - The actor system to which the event observable actor belongs.
73 *
74 * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
75 *
76 * @example
77 * ```ts
78 * import {
79 * fromEventObservable,
80 * Subscribable,
81 * EventObject,
82 * createMachine,
83 * createActor
84 * } from 'xstate';
85 * import { fromEvent } from 'rxjs';
86 *
87 * const mouseClickLogic = fromEventObservable(() =>
88 * fromEvent(document.body, 'click') as Subscribable<EventObject>
89 * );
90 *
91 * const canvasMachine = createMachine({
92 * invoke: {
93 * // Will send mouse `click` events to the canvas actor
94 * src: mouseClickLogic,
95 * }
96 * });
97 *
98 * const canvasActor = createActor(canvasMachine);
99 * canvasActor.start();
100 * ```
101 */
102export declare function fromEventObservable<T extends EventObject, TInput extends NonReducibleUnknown>(lazyObservable: ({ input, system }: {
103 input: TInput;
104 system: AnyActorSystem;
105 self: ObservableActorRef<T>;
106}) => Subscribable<T>): ObservableActorLogic<T, TInput>;