commonMain.net.humans.kmm.mvi.FlowExt.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mvi-core Show documentation
Show all versions of mvi-core Show documentation
Simple and concise implementation of Redux/MVI approach by Humans.
The newest version!
package net.humans.kmm.mvi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
suspend fun StateFlow.scanCollect(
onChanged: suspend (prev: State?, curr: State) -> Unit
) {
var prev: State? = null
collect { state ->
onChanged.invoke(prev, state)
prev = state
}
}
@Suppress("MagicNumber")
fun Flow.stateInSubscribed(
scope: CoroutineScope,
initialValue: T
) = stateIn(
scope = scope,
started = WhileSubscribed(5000),
initialValue = initialValue
)