commonMain.dev.stateholder.StateOwner.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
A simple library for managing state in Kotlin Multiplatform projects, using Kotlin Coroutines and `StateFlow`.
package dev.stateholder
import kotlinx.coroutines.flow.StateFlow
/**
* Exposes a [state] property that can be used to access the state of a [StateHolder].
*/
public interface StateOwner {
public val state: StateFlow
public companion object {
/**
* Create a state owner from the given [stateHolder].
*/
internal fun from(
stateHolder: StateHolder,
): StateOwner = object : StateOwner {
override val state: StateFlow get() = stateHolder.state
}
}
}
/**
* Create a [StateOwner] from the given [StateHolder].
*
* This is primarily used when delegating to the [StateHolder.state] property.
*
* Example:
*
* ```
* class MyModel(
* private val container: StateContainer
* ) : StateOwner by container.asStateOwner() {
* // ...
* }
* ```
*/
public fun StateHolder.asStateOwner(): StateOwner {
return StateOwner.from(this)
}