import { AnyActorSystem } from "../system.js"; import { ActorLogic, ActorRefFromLogic, EventObject, NonReducibleUnknown, Snapshot, Subscribable, Subscription } from "../types.js"; export type ObservableSnapshot = Snapshot & { context: TContext | undefined; input: TInput | undefined; _subscription: Subscription | undefined; }; export type ObservableActorLogic = ActorLogic, { type: string; [k: string]: unknown; }, TInput, AnyActorSystem, TEmitted>; /** * Represents an actor created by `fromObservable` or `fromEventObservable`. * * The type of `self` within the actor's logic. * * @example * * ```ts * import { fromObservable, createActor } from 'xstate'; * import { interval } from 'rxjs'; * * // The type of the value observed by the actor's logic. * type Context = number; * // The actor's input. * type Input = { period?: number }; * * // Actor logic that observes a number incremented every `input.period` * // milliseconds (default: 1_000). * const logic = fromObservable(({ input, self }) => { * self; * // ^? ObservableActorRef * * return interval(input.period ?? 1_000); * }); * * const actor = createActor(logic, { input: { period: 2_000 } }); * // ^? ObservableActorRef * ``` * * @see {@link fromObservable} * @see {@link fromEventObservable} */ export type ObservableActorRef = ActorRefFromLogic>; /** * Observable actor logic is described by an observable stream of values. Actors * created from observable logic (“observable actors”) can: * * - Emit snapshots of the observable’s emitted value * * The observable’s emitted value is used as its observable actor’s `context`. * * Sending events to observable actors will have no effect. * * @example * * ```ts * import { fromObservable, createActor } from 'xstate'; * import { interval } from 'rxjs'; * * const logic = fromObservable((obj) => interval(1000)); * * const actor = createActor(logic); * * actor.subscribe((snapshot) => { * console.log(snapshot.context); * }); * * actor.start(); * // At every second: * // Logs 0 * // Logs 1 * // Logs 2 * // ... * ``` * * @param observableCreator A function that creates an observable. It receives * one argument, an object with the following properties: * * - `input` - Data that was provided to the observable actor * - `self` - The parent actor * - `system` - The actor system to which the observable actor belongs * * It should return a {@link Subscribable}, which is compatible with an RxJS * Observable, although RxJS is not required to create them. * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators. * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable. */ export declare function fromObservable(observableCreator: ({ input, system, self }: { input: TInput; system: AnyActorSystem; self: ObservableActorRef; emit: (emitted: TEmitted) => void; }) => Subscribable): ObservableActorLogic; /** * Creates event observable logic that listens to an observable that delivers * event objects. * * 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: * * - Implicitly send events to its parent actor * - Emit snapshots of its emitted event objects * * Sending events to event observable actors will have no effect. * * @example * * ```ts * import { * fromEventObservable, * Subscribable, * EventObject, * createMachine, * createActor * } from 'xstate'; * import { fromEvent } from 'rxjs'; * * const mouseClickLogic = fromEventObservable( * () => fromEvent(document.body, 'click') as Subscribable * ); * * const canvasMachine = createMachine({ * invoke: { * // Will send mouse `click` events to the canvas actor * src: mouseClickLogic * } * }); * * const canvasActor = createActor(canvasMachine); * canvasActor.start(); * ``` * * @param lazyObservable A function that creates an observable that delivers * event objects. It receives one argument, an object with the following * properties: * * - `input` - Data that was provided to the event observable actor * - `self` - The parent actor * - `system` - The actor system to which the event observable actor belongs. * * It should return a {@link Subscribable}, which is compatible with an RxJS * Observable, although RxJS is not required to create them. */ export declare function fromEventObservable(lazyObservable: ({ input, system, self, emit }: { input: TInput; system: AnyActorSystem; self: ObservableActorRef; emit: (emitted: TEmitted) => void; }) => Subscribable): ObservableActorLogic;