kotlin-flow-state-event-modeling

Reviews Kotlin Flow state and event APIs for correct StateFlow, SharedFlow, and Channel usage.

1|Updated Jul 6, 2026
One-click install
npx skills add https://github.com/citytexi/team-yg-pesonal-agent --skill kotlin-flow-state-event-modeling-citytexi
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: kotlin-flow-state-event-modeling
Source: https://github.com/citytexi/team-yg-pesonal-agent/tree/main/.claude/skills/kotlin-flow-state-event-modeling
Command: npx skills add https://github.com/citytexi/team-yg-pesonal-agent --skill kotlin-flow-state-event-modeling-citytexi

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Kotlin developers often misuse Flow primitives: sentinel initial values leak fake domain data into StateFlow, SharedFlow drops one-shot events, stateIn inside functions spawns leaking coroutines, and read-modify-write on .value loses concurrent updates. This Skill provides review rules and fixes for these recurring mistakes. ## Core Features & Use Cases - Primitive selection guidance: Decision table mapping requirements (replay, fan-out, synchronous .value) to StateFlow, SharedFlow, Channel.receiveAsFlow, or cold Flow. - Anti-pattern detection with fixes: Covers sentinel defaults, per-call stateIn, WhileSubscribed staleness, .map losing .value, and non-atomic state mutation, each with bad/good Kotlin code pairs. - Use Case: While reviewing a ViewModel that exposes MutableSharedFlow navigation events and calls _state.value = _state.value.copy(...), apply the rules to switch to Channel(BUFFERED).receiveAsFlow() and atomic update { } blocks. ## Quick Start Review this Kotlin ViewModel's StateFlow and SharedFlow usage and flag any event-loss or stale-state problems.

Frequently Asked Questions about kotlin-flow-state-event-modeling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I emit one-shot events like navigation from a Kotlin ViewModel?▼

For a single consumer handling exactly-once events, use Channel(Channel.BUFFERED) exposed via receiveAsFlow() instead of MutableSharedFlow. Default SharedFlow has no replay buffer, so events emitted with no active collector are lost.

StateFlow vs SharedFlow: which should I use in Kotlin?▼

Use StateFlow when state always has a value and consumers need synchronous .value reads. Use SharedFlow for hot streams with multiple subscribers and no .value requirement. For single-consumer exactly-once events, consider Channel.receiveAsFlow().

Why does MutableStateFlow.value assignment lose updates?▼

Reading .value and writing back a copy is a non-atomic read-modify-write, so concurrent coroutines can overwrite each other. Use MutableStateFlow.update { current -> ... } which applies the transform atomically against the latest state.

Does SharingStarted.WhileSubscribed keep StateFlow .value fresh?▼

No. WhileSubscribed disconnects upstream when there are no collectors, so .value returns the last cached or initial value. If synchronous .value must be fresh or initialized, use SharingStarted.Eagerly or explicit initialization.

Why is calling stateIn inside a function a problem?▼

Each call to stateIn launches a new sharing coroutine on the given scope that never completes, so repeated calls leak coroutines. Assign stateIn to a val property once so a single shared instance is reused.

How do I avoid sentinel initial values in StateFlow?▼

Model absence explicitly with a nullable type, sealed interface UI state, or Result, or use phase initialization where the StateFlow is only exposed after the real value loads. Fake domain sentinels force consumers to treat placeholders as real data.