actions.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { Action, Event, EventObject, SingleOrArray, SendAction, SendActionOptions, CancelAction, ActionObject, ActionType, Assigner, PropertyAssigner, AssignAction, ActionFunction, ActionFunctionMap, ActivityActionObject, ActionTypes, ActivityDefinition, RaiseAction, RaiseActionObject, DoneEvent, ErrorPlatformEvent, DoneEventObject, SendExpr, SendActionObject, PureAction, LogExpr, LogAction, LogActionObject, DelayFunctionMap, SCXML, ExprWithMeta, ChooseCondition, ChooseAction, AnyEventObject, Expr, StopAction, StopActionObject, Cast, EventFrom, AnyActorRef, PredictableActionArgumentsExec, RaiseActionOptions, NoInfer, BaseActionObject, LowInfer } from './types';
  2. import * as actionTypes from './actionTypes';
  3. import { State } from './State';
  4. import { StateNode } from './StateNode';
  5. export { actionTypes };
  6. export declare const initEvent: SCXML.Event<{
  7. type: ActionTypes;
  8. }>;
  9. export declare function getActionFunction<TContext, TEvent extends EventObject>(actionType: ActionType, actionFunctionMap?: ActionFunctionMap<TContext, TEvent>): ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | undefined;
  10. export declare function toActionObject<TContext, TEvent extends EventObject>(action: Action<TContext, TEvent>, actionFunctionMap?: ActionFunctionMap<TContext, TEvent>): ActionObject<TContext, TEvent>;
  11. export declare const toActionObjects: <TContext, TEvent extends EventObject>(action?: SingleOrArray<Action<TContext, TEvent, TEvent>> | undefined, actionFunctionMap?: ActionFunctionMap<TContext, TEvent, BaseActionObject> | undefined) => ActionObject<TContext, TEvent, TEvent, BaseActionObject>[];
  12. export declare function toActivityDefinition<TContext, TEvent extends EventObject>(action: string | ActivityDefinition<TContext, TEvent>): ActivityDefinition<TContext, TEvent>;
  13. /**
  14. * Raises an event. This places the event in the internal event queue, so that
  15. * the event is immediately consumed by the machine in the current step.
  16. *
  17. * @param eventType The event to raise.
  18. */
  19. export declare function raise<TContext, TExpressionEvent extends EventObject, TEvent extends EventObject = TExpressionEvent>(event: NoInfer<Event<TEvent>> | SendExpr<TContext, TExpressionEvent, TEvent>, options?: RaiseActionOptions<TContext, TExpressionEvent>): RaiseAction<TContext, TExpressionEvent, TEvent>;
  20. export declare function resolveRaise<TContext, TEvent extends EventObject, TExpressionEvent extends EventObject>(action: RaiseAction<TContext, TExpressionEvent, TEvent>, ctx: TContext, _event: SCXML.Event<TExpressionEvent>, delaysMap?: DelayFunctionMap<TContext, TEvent>): RaiseActionObject<TContext, TExpressionEvent, TEvent>;
  21. /**
  22. * Sends an event. This returns an action that will be read by an interpreter to
  23. * send the event in the next step, after the current step is finished executing.
  24. *
  25. * @deprecated Use the `sendTo(...)` action creator instead.
  26. *
  27. * @param event The event to send.
  28. * @param options Options to pass into the send event:
  29. * - `id` - The unique send event identifier (used with `cancel()`).
  30. * - `delay` - The number of milliseconds to delay the sending of the event.
  31. * - `to` - The target of this event (by default, the machine the event was sent from).
  32. */
  33. export declare function send<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject>(event: Event<AnyEventObject> | SendExpr<TContext, TEvent, AnyEventObject>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, TSentEvent>;
  34. export declare function resolveSend<TContext, TEvent extends EventObject, TSentEvent extends EventObject>(action: SendAction<TContext, TEvent, TSentEvent>, ctx: TContext, _event: SCXML.Event<TEvent>, delaysMap?: DelayFunctionMap<TContext, TEvent>): SendActionObject<TContext, TEvent, TSentEvent>;
  35. /**
  36. * Sends an event to this machine's parent.
  37. *
  38. * @param event The event to send to the parent machine.
  39. * @param options Options to pass into the send event.
  40. */
  41. export declare function sendParent<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject>(event: Event<AnyEventObject> | SendExpr<TContext, TEvent, AnyEventObject>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, TSentEvent>;
  42. declare type InferEvent<E extends EventObject> = {
  43. [T in E['type']]: {
  44. type: T;
  45. } & Extract<E, {
  46. type: T;
  47. }>;
  48. }[E['type']];
  49. /**
  50. * Sends an event to an actor.
  51. *
  52. * @param actor The `ActorRef` to send the event to.
  53. * @param event The event to send, or an expression that evaluates to the event to send
  54. * @param options Send action options
  55. * @returns An XState send action object
  56. */
  57. export declare function sendTo<TContext, TEvent extends EventObject, TActor extends AnyActorRef>(actor: string | TActor | ((ctx: TContext, event: TEvent) => TActor), event: EventFrom<TActor> | SendExpr<TContext, TEvent, InferEvent<Cast<EventFrom<TActor>, EventObject>>>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, any>;
  58. /**
  59. * Sends an update event to this machine's parent.
  60. */
  61. export declare function sendUpdate<TContext, TEvent extends EventObject>(): SendAction<TContext, TEvent, any>;
  62. /**
  63. * Sends an event back to the sender of the original event.
  64. *
  65. * @param event The event to send back to the sender
  66. * @param options Options to pass into the send event
  67. */
  68. export declare function respond<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject>(event: Event<TEvent> | SendExpr<TContext, TEvent, TSentEvent>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, AnyEventObject>;
  69. /**
  70. *
  71. * @param expr The expression function to evaluate which will be logged.
  72. * Takes in 2 arguments:
  73. * - `ctx` - the current state context
  74. * - `event` - the event that caused this action to be executed.
  75. * @param label The label to give to the logged expression.
  76. */
  77. export declare function log<TContext, TExpressionEvent extends EventObject, TEvent extends EventObject = TExpressionEvent>(expr?: string | LogExpr<TContext, TExpressionEvent>, label?: string): LogAction<TContext, TExpressionEvent, TEvent>;
  78. export declare const resolveLog: <TContext, TEvent extends EventObject>(action: LogAction<TContext, TEvent, TEvent>, ctx: TContext, _event: SCXML.Event<TEvent>) => LogActionObject<TContext, TEvent>;
  79. /**
  80. * Cancels an in-flight `send(...)` action. A canceled sent action will not
  81. * be executed, nor will its event be sent, unless it has already been sent
  82. * (e.g., if `cancel(...)` is called after the `send(...)` action's `delay`).
  83. *
  84. * @param sendId The `id` of the `send(...)` action to cancel.
  85. */
  86. export declare const cancel: <TContext, TExpressionEvent extends EventObject, TEvent extends EventObject>(sendId: string | number) => CancelAction<TContext, TExpressionEvent, TEvent>;
  87. /**
  88. * Starts an activity.
  89. *
  90. * @param activity The activity to start.
  91. */
  92. export declare function start<TContext, TEvent extends EventObject>(activity: string | ActivityDefinition<TContext, TEvent>): ActivityActionObject<TContext, TEvent>;
  93. /**
  94. * Stops an activity.
  95. *
  96. * @param actorRef The activity to stop.
  97. */
  98. export declare function stop<TContext, TExpressionEvent extends EventObject, TEvent extends EventObject = TExpressionEvent>(actorRef: string | Expr<TContext, TExpressionEvent, string | {
  99. id: string;
  100. }>): StopAction<TContext, TExpressionEvent, TEvent>;
  101. export declare function resolveStop<TContext, TEvent extends EventObject>(action: StopAction<TContext, TEvent>, context: TContext, _event: SCXML.Event<TEvent>): StopActionObject;
  102. /**
  103. * Updates the current context of the machine.
  104. *
  105. * @param assignment An object that represents the partial context to update.
  106. */
  107. export declare const assign: <TContext, TExpressionEvent extends EventObject = EventObject, TEvent extends EventObject = TExpressionEvent>(assignment: Assigner<LowInfer<TContext>, TExpressionEvent> | PropertyAssigner<LowInfer<TContext>, TExpressionEvent>) => AssignAction<TContext, TExpressionEvent, TEvent>;
  108. export declare function isActionObject<TContext, TEvent extends EventObject>(action: Action<TContext, TEvent>): action is ActionObject<TContext, TEvent>;
  109. /**
  110. * Returns an event type that represents an implicit event that
  111. * is sent after the specified `delay`.
  112. *
  113. * @param delayRef The delay in milliseconds
  114. * @param id The state node ID where this event is handled
  115. */
  116. export declare function after(delayRef: number | string, id?: string): string;
  117. /**
  118. * Returns an event that represents that a final state node
  119. * has been reached in the parent state node.
  120. *
  121. * @param id The final state node's parent state node `id`
  122. * @param data The data to pass into the event
  123. */
  124. export declare function done(id: string, data?: any): DoneEventObject;
  125. /**
  126. * Returns an event that represents that an invoked service has terminated.
  127. *
  128. * An invoked service is terminated when it has reached a top-level final state node,
  129. * but not when it is canceled.
  130. *
  131. * @param id The final state node ID
  132. * @param data The data to pass into the event
  133. */
  134. export declare function doneInvoke(id: string, data?: any): DoneEvent;
  135. export declare function error(id: string, data?: any): ErrorPlatformEvent & string;
  136. export declare function pure<TContext, TExpressionEvent extends EventObject, TEvent extends EventObject = TExpressionEvent>(getActions: (context: TContext, event: TExpressionEvent) => SingleOrArray<BaseActionObject | BaseActionObject['type'] | ActionObject<TContext, TExpressionEvent, TEvent> | ActionFunction<TContext, TExpressionEvent>> | undefined): PureAction<TContext, TExpressionEvent, TEvent>;
  137. /**
  138. * Forwards (sends) an event to a specified service.
  139. *
  140. * @param target The target service to forward the event to.
  141. * @param options Options to pass into the send action creator.
  142. */
  143. export declare function forwardTo<TContext, TEvent extends EventObject>(target: Required<SendActionOptions<TContext, TEvent>>['to'], options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, any>;
  144. /**
  145. * Escalates an error by sending it as an event to this machine's parent.
  146. *
  147. * @param errorData The error data to send, or the expression function that
  148. * takes in the `context`, `event`, and `meta`, and returns the error data to send.
  149. * @param options Options to pass into the send action creator.
  150. */
  151. export declare function escalate<TContext, TEvent extends EventObject, TErrorData = any>(errorData: TErrorData | ExprWithMeta<TContext, TEvent, TErrorData>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, any>;
  152. export declare function choose<TContext, TExpressionEvent extends EventObject, TEvent extends EventObject = TExpressionEvent>(conds: Array<ChooseCondition<TContext, TExpressionEvent, TEvent>>): ChooseAction<TContext, TExpressionEvent, TEvent>;
  153. export declare function resolveActions<TContext, TEvent extends EventObject>(machine: StateNode<TContext, any, TEvent, any, any, any>, currentState: State<TContext, TEvent, any, any, any> | undefined, currentContext: TContext, _event: SCXML.Event<TEvent>, actionBlocks: Array<{
  154. type: string;
  155. actions: Array<ActionObject<TContext, TEvent>>;
  156. }>, predictableExec?: PredictableActionArgumentsExec, preserveActionOrder?: boolean): [Array<ActionObject<TContext, TEvent>>, TContext];
  157. //# sourceMappingURL=actions.d.ts.map