commonMain.arrow.optics.Flow.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of arrow-optics-compose Show documentation
Show all versions of arrow-optics-compose Show documentation
Functional companion to Kotlin's Standard Library
The newest version!
@file:JvmName("ComposeFlowKt")
@file:OptIn(ExperimentalCoroutinesApi::class, ExperimentalForInheritanceCoroutinesApi::class)
package arrow.optics
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ExperimentalForInheritanceCoroutinesApi
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlin.jvm.JvmName
/**
* Exposes the values of [this] through the optic.
*/
public fun SharedFlow.optic(g: Lens): SharedFlow = object : SharedFlow {
override suspend fun collect(collector: FlowCollector): Nothing =
[email protected] { collector.emit(g.get(it)) }
override val replayCache: List
get() = [email protected] { g.get(it) }
}
/**
* Exposes the values of [this] through the optic.
*/
public fun StateFlow.optic(g: Lens): StateFlow = object : StateFlow {
override val value: A
get() = g.get([email protected])
override suspend fun collect(collector: FlowCollector): Nothing =
[email protected] { collector.emit(g.get(it)) }
override val replayCache: List
get() = [email protected] { g.get(it) }
}
/**
* Exposes the values of [this] through the optic.
* Any change made to [value] is reflected in the original [MutableStateFlow].
*/
public fun MutableStateFlow.optic(lens: Lens): MutableStateFlow = object : MutableStateFlow {
override var value: A
get() = lens.get([email protected])
set(newValue) {
[email protected] = lens.set([email protected], newValue)
}
override suspend fun collect(collector: FlowCollector): Nothing =
[email protected] { collector.emit(lens.get(it)) }
override fun compareAndSet(expect: A, update: A): Boolean {
val expectT = lens.set([email protected], expect)
val updateT = lens.set([email protected], update)
return compareAndSet(expectT, updateT)
}
override fun tryEmit(value: A): Boolean =
[email protected](lens.set([email protected], value))
override suspend fun emit(value: A): Unit =
[email protected](lens.set([email protected], value))
override val subscriptionCount: StateFlow
get() = [email protected]
override val replayCache: List
get() = [email protected] { lens.get(it) }
override fun resetReplayCache() {
[email protected]()
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy