commonMain.com.plusmobileapps.firebase.auth.util.DerivedStateFlow.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of auth-jvm Show documentation
Show all versions of auth-jvm Show documentation
A kotlin multiplatform mobile library for authenticating with Firebase for Android, iOS, and JVM
The newest version!
package com.plusmobileapps.firebase.auth.util
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
/**
* Does not produce the same value in a raw, so respect "distinct until changed emissions"
* */
class DerivedStateFlow(
private val getValue: () -> T,
private val flow: Flow
) : StateFlow {
override val replayCache: List
get () = listOf(value)
override val value: T
get () = getValue()
override suspend fun collect(collector: FlowCollector): Nothing {
coroutineScope { flow.distinctUntilChanged().stateIn(this).collect(collector) }
}
}
fun StateFlow.mapState(transform: (a: T1) -> R): StateFlow {
return DerivedStateFlow(
getValue = { transform(this.value) },
flow = this.map { a -> transform(a) }
)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy