swift-state-machine-patterns
Installation
SKILL.md
Swift State Machine Patterns
When to use
- Reentrancy-sensitive flows
- Protocol or lifecycle logic with strict transition rules
- Async streams or continuations that need cancellation safety
- Boundaries where invalid state must be unrepresentable
Core rules
- Model state as an enum with associated values. Each case carries only data valid in that state.
- Keep the state machine as a value type (
struct) with a privatestate. - Transition methods are pure: mutate state and return
Actionvalues. - Execute side effects outside the transition (and outside any lock).
- Use strong types for events (enum or method parameters), actions, and identifiers. Avoid strings or boolean flags for state.
- Prefer per-state structs for larger associated data to prevent accidental access to irrelevant fields.
- Make invalid states unrepresentable by construction, not by runtime checks.