UNPKG

4.62 kBTypeScriptView Raw
1import { Guard } from "../guards.js";
2import { Action, ActionArgs, ActionFunction, AnyActorRef, AnyEventObject, EventObject, MachineContext, ParameterizedObject, ProvidedActor, UnifiedArg } from "../types.js";
3import { assign } from "./assign.js";
4import { cancel } from "./cancel.js";
5import { emit } from "./emit.js";
6import { raise } from "./raise.js";
7import { sendParent, sendTo } from "./send.js";
8import { spawnChild } from "./spawnChild.js";
9import { stopChild } from "./stopChild.js";
10interface ActionEnqueuer<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject> {
11 (action: Action<TContext, TExpressionEvent, TEvent, undefined, TActor, TAction, TGuard, TDelay, TEmitted>): void;
12 assign: (...args: Parameters<typeof assign<TContext, TExpressionEvent, undefined, TEvent, TActor>>) => void;
13 cancel: (...args: Parameters<typeof cancel<TContext, TExpressionEvent, undefined, TEvent>>) => void;
14 raise: (...args: Parameters<typeof raise<TContext, TExpressionEvent, TEvent, undefined, TDelay, TDelay>>) => void;
15 sendTo: <TTargetActor extends AnyActorRef>(...args: Parameters<typeof sendTo<TContext, TExpressionEvent, undefined, TTargetActor, TEvent, TDelay, TDelay>>) => void;
16 sendParent: (...args: Parameters<typeof sendParent<TContext, TExpressionEvent, undefined, AnyEventObject, TEvent, TDelay, TDelay>>) => void;
17 spawnChild: (...args: Parameters<typeof spawnChild<TContext, TExpressionEvent, undefined, TEvent, TActor>>) => void;
18 stopChild: (...args: Parameters<typeof stopChild<TContext, TExpressionEvent, undefined, TEvent>>) => void;
19 emit: (...args: Parameters<typeof emit<TContext, TExpressionEvent, undefined, TEvent, TEmitted>>) => void;
20}
21export interface EnqueueActionsAction<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string> {
22 (args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: TParams): void;
23 _out_TEvent?: TEvent;
24 _out_TActor?: TActor;
25 _out_TAction?: TAction;
26 _out_TGuard?: TGuard;
27 _out_TDelay?: TDelay;
28}
29interface CollectActionsArg<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject> extends UnifiedArg<TContext, TExpressionEvent, TEvent> {
30 check: (guard: Guard<TContext, TExpressionEvent, undefined, TGuard>) => boolean;
31 enqueue: ActionEnqueuer<TContext, TExpressionEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted>;
32}
33type CollectActions<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject> = ({ context, event, check, enqueue, self }: CollectActionsArg<TContext, TExpressionEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted>, params: TParams) => void;
34/**
35 * Creates an action object that will execute actions that are queued by the
36 * `enqueue(action)` function.
37 *
38 * @example
39 *
40 * ```ts
41 * import { createMachine, enqueueActions } from 'xstate';
42 *
43 * const machine = createMachine({
44 * entry: enqueueActions(({ enqueue, check }) => {
45 * enqueue.assign({ count: 0 });
46 *
47 * if (check('someGuard')) {
48 * enqueue.assign({ count: 1 });
49 * }
50 *
51 * enqueue('someAction');
52 * })
53 * });
54 * ```
55 */
56export declare function enqueueActions<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject = TExpressionEvent, TActor extends ProvidedActor = ProvidedActor, TAction extends ParameterizedObject = ParameterizedObject, TGuard extends ParameterizedObject = ParameterizedObject, TDelay extends string = never, TEmitted extends EventObject = EventObject>(collect: CollectActions<TContext, TExpressionEvent, TParams, TEvent, TActor, TAction, TGuard, TDelay, TEmitted>): ActionFunction<TContext, TExpressionEvent, TEvent, TParams, TActor, TAction, TGuard, TDelay, TEmitted>;
57export {};