Class Guard<TContext, TEventData>

Represents a condition (guard) within a state machine context using XState. Guards in XState are boolean-returning functions that determine the feasibility of a transition or action based on current state conditions. This Guard class allows for defining such conditions with a name and a unique identifier, enhancing traceability and management within complex state machines.

Guards are especially useful in scenarios where transitions need to be contingent upon dynamic conditions within the state context or external factors.

Example usage:

const guard = new Guard<MyContext>((snapshot) => {
return snapshot.context.data.isValid; // Condition based on the context data
}, 'validateUser');
console.log(guard.ref); // Outputs the unique ID of the guard, incorporating the name 'validateUser'

Typeparam

TContext The type of the state context within which the action operates.

Typeparam

TEventData The type of the event within which the action operates.

Type Parameters

  • TContext extends Record<string, any> = Record<string, any>

  • TEventData extends Record<string, any> = Record<string, any>

Constructors

Properties

Accessors

Constructors

  • Initializes a new instance of the Guard class with a specified name and a boolean-returning handler function. The handler function evaluates the state conditions and determines whether a specific transition or action can be performed based on the current state captured in a MachineSnapshot.

    Type Parameters

    • TContext extends Record<string, any> = Record<string, any>

    • TEventData extends Record<string, any> = Record<string, any>

    Parameters

    • params: GuardParams<TContext, TEventData> | ((params) => boolean)

      An object containing:

      • name: A string to name the guard. This name is incorporated into the guard's unique identifier.
      • handler: A function that defines the logic for the guard's condition. It takes a MachineSnapshot of the current state and returns a boolean indicating whether the conditions are met for the action or transition to proceed.

    Returns Guard<TContext, TEventData>

Properties

id: string
params: GuardParams<TContext, TEventData>

Accessors

Generated using TypeDoc