Lifecycle
Transition Flow: Lifecycle Hooks
Section titled “Transition Flow: Lifecycle Hooks”All machine types implement the same generic EventLifecycle and hook methods for consistency. This allows you to use the same patterns across different machine types, whether you’re using a FactoryMachine, a StateMachine, or a StoreMachine.
When you call machine.send(type, ...params)
, the following flow occurs:
Step-by-step
Section titled “Step-by-step”1. send
Section titled “1. send”Called with an event type and parameters.
2. resolveExit
Section titled “2. resolveExit”Determines the target state for the event.
3. guard
Section titled “3. guard”Checks if the transition is allowed.
- If
false
, exit early.
4. handle
Section titled “4. handle”First opportunity to process the event.
- If returns
undefined
, exit early.
5. before
Section titled “5. before”Called before the state change.
- If returns
undefined
, exit early.
6. update
Section titled “6. update”Updates the internal state.
7. effect
Section titled “7. effect”Runs side effects.
8. leave
Section titled “8. leave”Called when leaving the previous state.
9. enter
Section titled “9. enter”Called when entering the new state.
10. notify
Section titled “10. notify”Notifies subscribers.
11. after
Section titled “11. after”Final hook after transition completes.
Early exits
Section titled “Early exits”- If
guard
returnsfalse
, orhandle
/before
returnundefined
, the transition stops and no further steps run.