commonMain.pro.respawn.flowmvi.plugins.SavedStatePlugin.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.plugins
import kotlinx.coroutines.launch
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.StorePlugin
import pro.respawn.flowmvi.dsl.StoreBuilder
import pro.respawn.flowmvi.dsl.plugin
/**
* Default name for the SavedStatePlugin
*/
public const val DefaultSavedStatePluginName: String = "SavedState"
/**
* A plugin that restores the [pro.respawn.flowmvi.api.StateProvider.state] using [get] in [StorePlugin.onStart]
* and saves using [set] asynchronously in [StorePlugin.onState].
* There are platform overloads for this function.
*/
@FlowMVIDSL
public inline fun savedStatePlugin(
name: String = DefaultSavedStatePluginName,
crossinline get: S.() -> S?,
crossinline set: (S) -> Unit,
): StorePlugin = plugin {
this.name = name
onState { _, new ->
launch { set(new) }
new
}
onStart {
updateState {
get() ?: this
}
}
}
/**
* Creates and installs a new [savedStatePlugin].
*/
@FlowMVIDSL
public inline fun StoreBuilder.saveState(
name: String = DefaultSavedStatePluginName,
crossinline get: S.() -> S?,
crossinline set: S.() -> Unit,
): Unit = install(savedStatePlugin(name, get, set))
© 2015 - 2025 Weber Informatics LLC | Privacy Policy