xstate
Version:
Finite State Machines and Statecharts for the Modern Web.
107 lines (106 loc) • 4.21 kB
TypeScript
import { AnyActorSystem } from "../system.js";
import { ActorLogic, ActorRefFrom, EventObject, NonReducibleUnknown, Snapshot, Subscribable, Subscription } from "../types.js";
export type ObservableSnapshot<TContext, TInput extends NonReducibleUnknown> = Snapshot<undefined> & {
context: TContext | undefined;
input: TInput | undefined;
_subscription: Subscription | undefined;
};
export type ObservableActorLogic<TContext, TInput extends NonReducibleUnknown> = ActorLogic<ObservableSnapshot<TContext, TInput>, {
type: string;
[k: string]: unknown;
}, TInput, AnyActorSystem, EventObject>;
export type ObservableActorRef<TContext> = ActorRefFrom<ObservableActorLogic<TContext, any>>;
/**
* 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.
*
* @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.
*
* @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
* // ...
* ```
*
* @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<TContext, TInput extends NonReducibleUnknown>(observableCreator: ({ input, system }: {
input: TInput;
system: AnyActorSystem;
self: ObservableActorRef<TContext>;
}) => Subscribable<TContext>): ObservableActorLogic<TContext, TInput>;
/**
* 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.
*
* @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.
*
* @example
* ```ts
* import {
* fromEventObservable,
* Subscribable,
* EventObject,
* createMachine,
* createActor
* } from 'xstate';
* import { fromEvent } from 'rxjs';
*
* const mouseClickLogic = fromEventObservable(() =>
* fromEvent(document.body, 'click') as Subscribable<EventObject>
* );
*
* const canvasMachine = createMachine({
* invoke: {
* // Will send mouse `click` events to the canvas actor
* src: mouseClickLogic,
* }
* });
*
* const canvasActor = createActor(canvasMachine);
* canvasActor.start();
* ```
*/
export declare function fromEventObservable<T extends EventObject, TInput extends NonReducibleUnknown>(lazyObservable: ({ input, system }: {
input: TInput;
system: AnyActorSystem;
self: ObservableActorRef<T>;
}) => Subscribable<T>): ObservableActorLogic<T, TInput>;