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

  1. Model state as an enum with associated values. Each case carries only data valid in that state.
  2. Keep the state machine as a value type (struct) with a private state.
  3. Transition methods are pure: mutate state and return Action values.
  4. Execute side effects outside the transition (and outside any lock).
  5. Use strong types for events (enum or method parameters), actions, and identifiers. Avoid strings or boolean flags for state.
  6. Prefer per-state structs for larger associated data to prevent accidental access to irrelevant fields.
  7. Make invalid states unrepresentable by construction, not by runtime checks.
Installs
5
GitHub Stars
13
First Seen
Feb 28, 2026
swift-state-machine-patterns — nonameplum/agent-skills