Skip to content

Function: composeGuardsOr()

API / @xmachines/play-xstate / composeGuardsOr

function composeGuardsOr<TContext, TEvent>(guards): ComposedGuard;

Defined in: packages/play-xstate/src/guards/compose.ts:159

Composes the guards with the OR logic, through the or() helper of XState

The function joins more than one guard predicate with the OR semantics: one guard must pass at least, and the composition then succeeds. It uses the built-in or() helper of XState, and the type inference is therefore correct.

Type Parameters

Type ParameterDefault typeDescription
TContextanyThe context type of the state machine
TEventanyThe event type

Parameters

ParameterTypeDescription
guardsGuardArray<TContext, TEvent>The array of the guard predicates, or of the guard names

Returns

ComposedGuard

The or() guard composition of XState

Throws

When the array of the guards is empty

Example

An OR composition with named guards

import { setup } from "xstate";
import { composeGuardsOr } from "@xmachines/play-xstate";
const machine = setup({
guards: {
isOwner: ({ context }) => context.role === "owner",
isAdmin: ({ context }) => context.role === "admin",
},
}).createMachine({
on: {
deleteResource: {
// One guard is sufficient
guard: composeGuardsOr(["isOwner", "isAdmin"]),
actions: "delete",
},
},
});

See

Deprecated

Use the and(), or(), and not() combinators of XState directly. This helper does not compose with a guard slot that setup() types, and the next major version removes it.