Loading...
Loading...
Kotlin Flow - StateFlow, SharedFlow, operators, testing
npx skill4agent add pluginagentmarketplace/custom-plugin-kotlin kotlin-flow// Cold Flow - starts fresh for each collector
fun loadData(): Flow<Data> = flow {
emit(fetchData())
}
// Hot Flow - shared state
private val _state = MutableStateFlow(State())
val state: StateFlow<State> = _state.asStateFlow()fun searchUsers(query: Flow<String>): Flow<List<User>> =
query
.debounce(300)
.filter { it.length >= 2 }
.distinctUntilChanged()
.flatMapLatest { term -> userRepository.search(term) }
.catch { emit(emptyList()) }val dashboard: Flow<Dashboard> = combine(
userFlow,
ordersFlow,
notificationsFlow
) { user, orders, notifications ->
Dashboard(user, orders.size, notifications.count())
}@Test
fun `flow emits values`() = runTest {
viewModel.state.test {
assertThat(awaitItem().isLoading).isFalse()
viewModel.load()
assertThat(awaitItem().isLoading).isTrue()
advanceUntilIdle()
assertThat(awaitItem().data).isNotNull()
}
}| Issue | Resolution |
|---|---|
| Flow never emits | Add terminal operator (collect, first) |
| Stale data in UI | Use stateIn or shareIn properly |
Skill("kotlin-flow")