commonMain.pro.respawn.flowmvi.dsl.SubscribeDsl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-jvm Show documentation
Show all versions of core-jvm Show documentation
A Kotlin Multiplatform MVI library based on plugins that is simple, fast, powerful & flexible
package pro.respawn.flowmvi.dsl
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import pro.respawn.flowmvi.api.ActionConsumer
import pro.respawn.flowmvi.api.FlowMVIDSL
import pro.respawn.flowmvi.api.MVIAction
import pro.respawn.flowmvi.api.MVIIntent
import pro.respawn.flowmvi.api.MVIState
import pro.respawn.flowmvi.api.StateConsumer
import pro.respawn.flowmvi.api.Store
/**
* Subscribe to the [store] and invoke [consume] and [render] in parallel in the provided scope.
* This function does **not** handle the lifecycle of the UI layer. For that, see platform implementations.
* @see [Store.subscribe]
*/
@FlowMVIDSL
public inline fun CoroutineScope.subscribe(
store: Store,
noinline consume: (suspend (action: A) -> Unit)?,
crossinline render: suspend (state: S) -> Unit,
): Job = with(store) {
subscribe outer@{
coroutineScope inner@{
consume?.let { consume ->
launch {
actions.collect { consume(it) }
}
}
launch {
states.collect { render(it) }
}
}
}
}
/**
* Subscribe to the [store] and invoke [ActionConsumer.consume] and [StateConsumer.render] in parallel in the provided scope.
* This function does **not** handle the lifecycle of the UI layer. For that, see platform implementations.
* @see [Store.subscribe]
*/
@FlowMVIDSL
public fun T.subscribe(
store: Store,
scope: CoroutineScope
): Job where T : ActionConsumer, T : StateConsumer = with(scope) {
subscribe(store, ::consume, ::render)
}
/**
* Subscribe to the [store] and invoke [StateConsumer.render] in the provided scope.
* This function does **not** handle the lifecycle of the UI layer. For that, see platform implementations.
* @see [Store.subscribe]
*/
@FlowMVIDSL
public fun StateConsumer.subscribe(
store: Store,
scope: CoroutineScope
): Job where T : StateConsumer = with(scope) {
subscribe(store, null, ::render)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy