interpreter.d.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, SingleOrArray, DoneEvent, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate, ActorRef, ActorRefFrom, Behavior, Subscription, StateConfig, InteropSubscribable } from './types';
  2. import { State } from './State';
  3. import { symbolObservable } from './utils';
  4. import { AreAllImplementationsAssumedToBeProvided, MissingImplementationsError, TypegenDisabled } from './typegenTypes';
  5. export declare type StateListener<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
  6. value: any;
  7. context: TContext;
  8. }, TResolvedTypesMeta = TypegenDisabled> = (state: State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>, event: TEvent) => void;
  9. export declare type ContextListener<TContext = DefaultContext> = (context: TContext, prevContext: TContext | undefined) => void;
  10. export declare type EventListener<TEvent extends EventObject = EventObject> = (event: TEvent) => void;
  11. export declare type Listener = () => void;
  12. export interface Clock {
  13. setTimeout(fn: (...args: any[]) => void, timeout: number): any;
  14. clearTimeout(id: any): void;
  15. }
  16. interface SpawnOptions {
  17. name?: string;
  18. autoForward?: boolean;
  19. sync?: boolean;
  20. }
  21. export declare enum InterpreterStatus {
  22. NotStarted = 0,
  23. Running = 1,
  24. Stopped = 2
  25. }
  26. export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
  27. value: any;
  28. context: TContext;
  29. }, TResolvedTypesMeta = TypegenDisabled> implements ActorRef<TEvent, State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>> {
  30. machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate, any, any, TResolvedTypesMeta>;
  31. /**
  32. * The default interpreter options:
  33. *
  34. * - `clock` uses the global `setTimeout` and `clearTimeout` functions
  35. * - `logger` uses the global `console.log()` method
  36. */
  37. static defaultOptions: {
  38. execute: boolean;
  39. deferEvents: boolean;
  40. clock: Clock;
  41. logger: any;
  42. devTools: boolean;
  43. };
  44. /**
  45. * The current state of the interpreted machine.
  46. */
  47. private _state?;
  48. private _initialState?;
  49. /**
  50. * The clock that is responsible for setting and clearing timeouts, such as delayed events and transitions.
  51. */
  52. clock: Clock;
  53. options: Readonly<InterpreterOptions>;
  54. private scheduler;
  55. private delayedEventsMap;
  56. private listeners;
  57. private contextListeners;
  58. private stopListeners;
  59. private doneListeners;
  60. private eventListeners;
  61. private sendListeners;
  62. private logger;
  63. /**
  64. * Whether the service is started.
  65. */
  66. initialized: boolean;
  67. status: InterpreterStatus;
  68. parent?: Interpreter<any>;
  69. id: string;
  70. /**
  71. * The globally unique process ID for this invocation.
  72. */
  73. sessionId: string;
  74. children: Map<string | number, ActorRef<any>>;
  75. private forwardTo;
  76. private _outgoingQueue;
  77. private devTools?;
  78. private _doneEvent?;
  79. /**
  80. * Creates a new Interpreter instance (i.e., service) for the given machine with the provided options, if any.
  81. *
  82. * @param machine The machine to be interpreted
  83. * @param options Interpreter options
  84. */
  85. constructor(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate, any, any, TResolvedTypesMeta>, options?: InterpreterOptions);
  86. get initialState(): State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
  87. /**
  88. * @deprecated Use `.getSnapshot()` instead.
  89. */
  90. get state(): State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
  91. static interpret: typeof interpret;
  92. /**
  93. * Executes the actions of the given state, with that state's `context` and `event`.
  94. *
  95. * @param state The state whose actions will be executed
  96. * @param actionsConfig The action implementations to use
  97. */
  98. execute(state: State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
  99. private update;
  100. onTransition(listener: StateListener<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>): this;
  101. subscribe(observer: Partial<Observer<State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>>>): Subscription;
  102. subscribe(nextListener?: (state: State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Subscription;
  103. /**
  104. * Adds an event listener that is notified whenever an event is sent to the running interpreter.
  105. * @param listener The event listener
  106. */
  107. onEvent(listener: EventListener): this;
  108. /**
  109. * Adds an event listener that is notified whenever a `send` event occurs.
  110. * @param listener The event listener
  111. */
  112. onSend(listener: EventListener): this;
  113. /**
  114. * Adds a context listener that is notified whenever the state context changes.
  115. * @param listener The context listener
  116. */
  117. onChange(listener: ContextListener<TContext>): this;
  118. /**
  119. * Adds a listener that is notified when the machine is stopped.
  120. * @param listener The listener
  121. */
  122. onStop(listener: Listener): this;
  123. /**
  124. * Adds a state listener that is notified when the statechart has reached its final state.
  125. * @param listener The state listener
  126. */
  127. onDone(listener: EventListener<DoneEvent>): this;
  128. /**
  129. * Removes a listener.
  130. * @param listener The listener to remove
  131. */
  132. off(listener: (...args: any[]) => void): this;
  133. /**
  134. * Alias for Interpreter.prototype.start
  135. */
  136. init: (initialState?: State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta> | StateConfig<TContext, TEvent> | StateValue) => this;
  137. /**
  138. * Starts the interpreter from the given state, or the initial state.
  139. * @param initialState The state to start the statechart from
  140. */
  141. start(initialState?: State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta> | StateConfig<TContext, TEvent> | StateValue): this;
  142. private _stopChildren;
  143. private _stop;
  144. /**
  145. * Stops the interpreter and unsubscribe all listeners.
  146. *
  147. * This will also notify the `onStop` listeners.
  148. */
  149. stop(): this;
  150. /**
  151. * Sends an event to the running interpreter to trigger a transition.
  152. *
  153. * An array of events (batched) can be sent as well, which will send all
  154. * batched events to the running interpreter. The listeners will be
  155. * notified only **once** when all events are processed.
  156. *
  157. * @param event The event(s) to send
  158. */
  159. send: (event: SingleOrArray<Event<TEvent>> | SCXML.Event<TEvent>, payload?: EventData) => State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
  160. private batch;
  161. /**
  162. * Returns a send function bound to this interpreter instance.
  163. *
  164. * @param event The event to be sent by the sender.
  165. */
  166. sender(event: Event<TEvent>): () => State<TContext, TEvent, TStateSchema, TTypestate>;
  167. private sendTo;
  168. private _nextState;
  169. /**
  170. * Returns the next state given the interpreter's current state and the event.
  171. *
  172. * This is a pure method that does _not_ update the interpreter's state.
  173. *
  174. * @param event The event to determine the next state
  175. */
  176. nextState(event: Event<TEvent> | SCXML.Event<TEvent>): State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
  177. private forward;
  178. private defer;
  179. private cancel;
  180. private _exec;
  181. private exec;
  182. private removeChild;
  183. private stopChild;
  184. spawn(entity: Spawnable, name: string, options?: SpawnOptions): ActorRef<any>;
  185. spawnMachine<TChildContext, TChildStateSchema extends StateSchema, TChildEvent extends EventObject>(machine: StateMachine<TChildContext, TChildStateSchema, TChildEvent>, options?: {
  186. id?: string;
  187. autoForward?: boolean;
  188. sync?: boolean;
  189. }): ActorRef<TChildEvent, State<TChildContext, TChildEvent>>;
  190. private spawnBehavior;
  191. private spawnPromise;
  192. private spawnCallback;
  193. private spawnObservable;
  194. private spawnActor;
  195. private spawnActivity;
  196. private spawnEffect;
  197. private attachDev;
  198. toJSON(): {
  199. id: string;
  200. };
  201. [Symbol.observable](): InteropSubscribable<State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>>;
  202. getSnapshot(): State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
  203. }
  204. export declare function spawn<T extends Behavior<any, any>>(entity: T, nameOrOptions?: string | SpawnOptions): ActorRefFrom<T>;
  205. export declare function spawn<TC, TE extends EventObject>(entity: StateMachine<TC, any, TE, any, any, any, any>, nameOrOptions?: string | SpawnOptions): ActorRefFrom<StateMachine<TC, any, TE, any, any, any, any>>;
  206. export declare function spawn(entity: Spawnable, nameOrOptions?: string | SpawnOptions): ActorRef<any>;
  207. /**
  208. * Creates a new Interpreter instance for the given machine with the provided options, if any.
  209. *
  210. * @param machine The machine to interpret
  211. * @param options Interpreter options
  212. */
  213. export declare function interpret<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
  214. value: any;
  215. context: TContext;
  216. }, TResolvedTypesMeta = TypegenDisabled>(machine: AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends true ? StateMachine<TContext, TStateSchema, TEvent, TTypestate, any, any, TResolvedTypesMeta> : MissingImplementationsError<TResolvedTypesMeta>, options?: InterpreterOptions): Interpreter<TContext, TStateSchema, TEvent, TTypestate, TResolvedTypesMeta>;
  217. export {};
  218. //# sourceMappingURL=interpreter.d.ts.map