UNPKG

5.22 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 evaluates to `false`.
31 *
32 * @category Guards
33 * @example
34 ```ts
35 import { setup, not } from 'xstate';
36
37 const machine = setup({
38 guards: {
39 someNamedGuard: () => false
40 }
41 }).createMachine({
42 on: {
43 someEvent: {
44 guard: not('someNamedGuard'),
45 actions: () => {
46 // will be executed if guard in `not(...)`
47 // evaluates to `false`
48 }
49 }
50 }
51 });
52 ```
53 * @returns A guard
54 */
55export declare function not<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg>(guard: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg>): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArg<DoNotInfer<TArg>>>;
56/**
57 * Higher-order guard that evaluates to `true` if all `guards` passed to it
58 * evaluate to `true`.
59 *
60 * @category Guards
61 * @example
62 ```ts
63 import { setup, and } from 'xstate';
64
65 const machine = setup({
66 guards: {
67 someNamedGuard: () => true
68 }
69 }).createMachine({
70 on: {
71 someEvent: {
72 guard: and([
73 ({ context }) => context.value > 0,
74 'someNamedGuard'
75 ]),
76 actions: () => {
77 // will be executed if all guards in `and(...)`
78 // evaluate to true
79 }
80 }
81 }
82 });
83 ```
84 * @returns A guard action object
85 */
86export declare function and<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg extends unknown[]>(guards: readonly [
87 ...{
88 [K in keyof TArg]: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg[K]>;
89 }
90]): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArgArray<DoNotInfer<TArg>>>;
91/**
92 * Higher-order guard that evaluates to `true` if any of the `guards` passed to it
93 * evaluate to `true`.
94 *
95 * @category Guards
96 * @example
97 ```ts
98 import { setup, or } from 'xstate';
99
100 const machine = setup({
101 guards: {
102 someNamedGuard: () => true
103 }
104 }).createMachine({
105 on: {
106 someEvent: {
107 guard: or([
108 ({ context }) => context.value > 0,
109 'someNamedGuard'
110 ]),
111 actions: () => {
112 // will be executed if any of the guards in `or(...)`
113 // evaluate to true
114 }
115 }
116 }
117 });
118 ```
119 * @returns A guard action object
120 */
121export declare function or<TContext extends MachineContext, TExpressionEvent extends EventObject, TArg extends unknown[]>(guards: readonly [
122 ...{
123 [K in keyof TArg]: SingleGuardArg<TContext, TExpressionEvent, unknown, TArg[K]>;
124 }
125]): GuardPredicate<TContext, TExpressionEvent, unknown, NormalizeGuardArgArray<DoNotInfer<TArg>>>;
126export declare function evaluateGuard<TContext extends MachineContext, TExpressionEvent extends EventObject>(guard: UnknownGuard | UnknownInlineGuard, context: TContext, event: TExpressionEvent, snapshot: AnyMachineSnapshot): boolean;
127export {};