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 Parameter | Default type | Description |
|---|---|---|
TContext | any | The context type of the state machine |
TEvent | any | The event type |
Parameters
| Parameter | Type | Description |
|---|---|---|
guards | GuardArray<TContext, TEvent> | The array of the guard predicates, or of the guard names |
Returns
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
- composeGuards for the AND composition
- negateGuard for the NOT logic
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.