commonMain.pro.respawn.flowmvi.dsl.StateDsl.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 pro.respawn.flowmvi.MVIStore
import pro.respawn.flowmvi.api.FlowMVIDSL
import pro.respawn.flowmvi.api.MVIState
import pro.respawn.flowmvi.api.StateReceiver
import pro.respawn.flowmvi.updateState
import pro.respawn.flowmvi.util.withType
import pro.respawn.flowmvi.withState
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
/**
* Run [block] if current [MVIStore.state] is of type [T], otherwise do nothing.
*
* **This function will suspend until all previous [MVIStore.withState] invocations are finished.**
* @see MVIStore.withState
*/
@OverloadResolutionByLambdaReturnType
@FlowMVIDSL
public suspend inline fun StateReceiver.withState(
@BuilderInference crossinline block: suspend T.() -> Unit
) {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
withState { (this as? T)?.let { it.block() } }
}
/**
* Obtain the current [MVIStore.state] and update it with
* the result of [transform] if it is of type [T], otherwise do nothing.
*
* **This function will suspend until all previous [MVIStore.withState] invocations are finished.**
* @see MVIStore.updateState
* @see [withState]
*/
@FlowMVIDSL
public suspend inline fun StateReceiver.updateState(
@BuilderInference crossinline transform: suspend T.() -> S
) {
contract {
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}
return updateState { withType { transform() } }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy