@ue-too/being / index / StateDefinition
介面: StateDefinition<Context, EventPayloadMapping, StateNames, EventOutputMapping>
Definition of a single state in the state machine.
範例
{
name: "PAYING",
guards: {
hasEnoughBalance: (context) => context.balance >= context.itemPrice,
hasInsufficientBalance: (context) => context.balance < context.itemPrice,
},
transitions: [
{
event: "pay",
targetState: "SELECTING",
guards: [
{ guard: "hasEnoughBalance", targetState: "CONFIRMED" },
{ guard: "hasInsufficientBalance", targetState: "PAYING" },
],
},
],
}型別參數
Context
Context extends BaseContext = BaseContext
The context type
EventPayloadMapping
EventPayloadMapping = any
Mapping of event names to their payload types
StateNames
StateNames extends string = string
Union type of all valid state names
EventOutputMapping
EventOutputMapping extends Partial<Record<keyof EventPayloadMapping, unknown>> = DefaultOutputMapping<EventPayloadMapping>
Optional mapping of events to their output types
屬性
guards?
optionalguards:Record<string,GuardFunction<Context>>
Optional mapping of guard names to guard functions. Guards defined here can be reused across multiple transitions within this state by referencing them by name in the transition's guards array.
The guard functions receive the context parameter typed with the Context type parameter, not BaseContext, so you get full type safety for your context properties.
name
name:
StateNames
Name of this state
onEnter()?
optionalonEnter: (context,fromState) =>void
Optional callback when entering this state
參數
context
Context
fromState
StateNames
回傳
void
onExit()?
optionalonExit: (context,toState) =>void
Optional callback when exiting this state
參數
context
Context
toState
StateNames
回傳
void
transitions
transitions:
TransitionDefinitionUnion<Context,EventPayloadMapping,StateNames,EventOutputMapping>[]
Transitions available from this state