All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.dev.stateholder.StateOwner.kt Maven / Gradle / Ivy

Go to download

A simple library for managing state in Kotlin Multiplatform projects, using Kotlin Coroutines and `StateFlow`.

There is a newer version: 1.2.0
Show newest version
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)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy