kotlin-flow-state-event-modeling
Kotlin Flow: state and event modeling
Core principle
Pick the primitive that matches replay, fan-out, and synchronous-read requirements. StateFlow, SharedFlow, Channel-backed flows, and cold Flow differ in buffering, who sees each emission, and whether .value exists. Wrong choices drop events, leak sharing coroutines, or force fake domain sentinels into state.
When to use this skill
You're writing or reviewing Kotlin code involving:
MutableStateFlow<T>(SomeSentinel)—NoUser,Empty,Loading, etc. — because the real value is async.stateIn(...)called inside a function rather than assigned to a propertySharingStarted.WhileSubscribed(...)on a flow whose.valueis read synchronously and must stay freshMutableSharedFlowfor navigation events, snackbars, or other one-shot emissions where loss would be a bug.map { }on aStateFlowwhen consumers still need synchronous.valueMutableStateFlow.value = _state.value.copy(...)or update code that builds expensive objects insideupdate { ... }
SharedFlow for single-consumer fire-once events
More from chrisbanes/skills
compose-ui-testing-patterns
Use when writing or reviewing Jetpack Compose UI tests, screenshot tests, previews, semantics assertions, fake image loading, keyboard input, focus assertions, interaction state (hover/pressed/focused), or tests for plain state-driven UI composables.
162kotlin-coroutines-structured-concurrency
Use when writing or reviewing Kotlin code that stores CoroutineScope, launches from init/non-suspending APIs, calls runBlocking, or catches broad exceptions around suspend calls.
162compose-side-effects
Use when writing or reviewing Jetpack Compose code with LaunchedEffect, DisposableEffect, SideEffect, rememberCoroutineScope, rememberUpdatedState, snapshotFlow, snackbar, navigation, focus requests, analytics, or event Flow collection.
161compose-modifier-and-layout-style
Use when writing or reviewing Jetpack Compose layout APIs, modifier parameters, modifier chain construction, hardcoded root layout decisions, or layout wrappers around a single conditional.
161compose-stability-diagnostics
Use when writing or reviewing Jetpack Compose parameter stability, compiler reports, skippability, unstable UI state classes, collection parameters, or Kotlin 2.0+ strong skipping behavior.
160compose-state-authoring
Use when writing or reviewing Jetpack Compose code with bare local var in a @Composable, remember { mutableStateOf(...) }, mutableStateListOf/mutableStateMapOf, or @ReadOnlyComposable.
159