State Machines with Kotlin Flow
summary
Implements a finite state machine atop Kotlin coroutines using MutableStateFlow as a hot, conflated shared flow that guarantees ordered, thread‑safe updates; transitions are emitted synchronously to collectors and missing mappings emit a NoMappingException on an error flow. Uses a coroutine scope with SupervisorJob and a CoroutineExceptionHandler, exposes transitions and errors as read‑only flows, and returns a Job for each transition so callers can cancel long operations. Provides a Kotlin DSL for state/action mappings (turnstile and water‑glass examples), has tests but is labelled opinionated and not production‑guaranteed.
AI-written summary · about
chapters (17)
- 0:00Intro
- 1:01Why Kotlin Flow
- 1:16More serious reasons
- 2:03Finite State Machines
- 3:59Kotlin
- 4:45Launching Core Things
- 6:02Scope Context
- 8:51Core Routines
- 11:55Flows Channels
- 13:41Channels vs Flows
- 17:48State Flow
- 21:26Disclaimer
- 24:22Interface
- 27:40Implementation
- 31:42Transition Function
- 36:31Creating State Machines
- 38:19State Machine Example








