UNPKG

5.24 kBTypeScriptView Raw
1import type { EventObject, StateValue, MachineContext, ParameterizedObject, AnyMachineSnapshot, NoRequiredParams, WithDynamicParams, Identity, Elements, DoNotInfer } from "./types.js";
2type SingleGuardArg<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TGuardArg> = [TGuardArg] extends [{
3 type: string;
4}] ? Identity<TGuardArg> : [TGuardArg] extends [string] ? TGuardArg : GuardPredicate<TContext, TExpressionEvent, TParams, ParameterizedObject>;
5type NormalizeGuardArg<TGuardArg> = TGuardArg extends {
6 type: string;
7} ? Identity<TGuardArg> & {
8 params: unknown;
9} : TGuardArg extends string ? {
10 type: TGuardArg;
11 params: undefined;
12} : '_out_TGuard' extends keyof TGuardArg ? TGuardArg['_out_TGuard'] & ParameterizedObject : never;
13type NormalizeGuardArgArray<TArg extends unknown[]> = Elements<{
14 [K in keyof TArg]: NormalizeGuardArg<TArg[K]>;
15}>;
16export type GuardPredicate<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TGuard extends ParameterizedObject> = {
17 (args: GuardArgs<TContext, TExpressionEvent>, params: TParams): boolean;
18 _out_TGuard?: TGuard;
19};
20export interface GuardArgs<TContext extends MachineContext, TExpressionEvent extends EventObject> {
21 context: TContext;
22 event: TExpressionEvent;
23}
24export type Guard<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TGuard extends ParameterizedObject> = NoRequiredParams<TGuard> | WithDynamicParams<TContext, TExpressionEvent, TGuard> | GuardPredicate<TContext, TExpressionEvent, TParams, TGuard>;
25export type UnknownGuard = UnknownReferencedGuard | UnknownInlineGuard;
26type UnknownReferencedGuard = Guard<MachineContext, EventObject, ParameterizedObject['params'], ParameterizedObject>;
27type UnknownInlineGuard = Guard<MachineContext, EventObject, undefined, ParameterizedObject>;
28export declare function stateIn<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined>(stateValue: StateValue): GuardPredicate<TContext, TExpressionEvent, TParams, any>;
29/**
30 * Higher-order guard that evaluates to `true` if the `guard` passed to it
31 * evaluates to `false`.
32 *
33 * @category Guards
34 * @example
35 *
36 * ```ts
37 * import { setup, not } from 'xstate';
38 *
39 * const machine = setup({
40 * guards: {
41 * someNamedGuard: () => false
42 * }
43 * }).createMachine({
44 * on: {
45 * someEvent: {
46 * guard: not('someNamedGuard'),
47 * actions: () => {
48 * // will be executed if guard in `not(...)`
49 * // evaluates to `false`
50 * }
51 * }
52 * }
53 * });
54 * ```
55 *
56 * @returns A guard
57 */
58export declare function not<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg>(guard: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg>): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArg<DoNotInfer<TArg>>>;
59/**
60 * Higher-order guard that evaluates to `true` if all `guards` passed to it
61 * evaluate to `true`.
62 *
63 * @category Guards
64 * @example
65 *
66 * ```ts
67 * import { setup, and } from 'xstate';
68 *
69 * const machine = setup({
70 * guards: {
71 * someNamedGuard: () => true
72 * }
73 * }).createMachine({
74 * on: {
75 * someEvent: {
76 * guard: and([({ context }) => context.value > 0, 'someNamedGuard']),
77 * actions: () => {
78 * // will be executed if all guards in `and(...)`
79 * // evaluate to true
80 * }
81 * }
82 * }
83 * });
84 * ```
85 *
86 * @returns A guard action object
87 */
88export declare function and<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg extends unknown[]>(guards: readonly [
89 ...{
90 [K in keyof TArg]: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg[K]>;
91 }
92]): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArgArray<DoNotInfer<TArg>>>;
93/**
94 * Higher-order guard that evaluates to `true` if any of the `guards` passed to
95 * it evaluate to `true`.
96 *
97 * @category Guards
98 * @example
99 *
100 * ```ts
101 * import { setup, or } from 'xstate';
102 *
103 * const machine = setup({
104 * guards: {
105 * someNamedGuard: () => true
106 * }
107 * }).createMachine({
108 * on: {
109 * someEvent: {
110 * guard: or([({ context }) => context.value > 0, 'someNamedGuard']),
111 * actions: () => {
112 * // will be executed if any of the guards in `or(...)`
113 * // evaluate to true
114 * }
115 * }
116 * }
117 * });
118 * ```
119 *
120 * @returns A guard action object
121 */
122export declare function or<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg extends unknown[]>(guards: readonly [
123 ...{
124 [K in keyof TArg]: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg[K]>;
125 }
126]): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArgArray<DoNotInfer<TArg>>>;
127export declare function evaluateGuard<TContext extends MachineContext, TExpressionEvent extends EventObject>(guard: UnknownGuard | UnknownInlineGuard, context: TContext, event: TExpressionEvent, snapshot: AnyMachineSnapshot): boolean;
128export {};