All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.net.humans.kmm.mvi.FlowExt.kt Maven / Gradle / Ivy

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
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy