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

commonMain.com.arkivanov.decompose.extensions.compose.SubscribeAsState.kt Maven / Gradle / Ivy

The newest version!
package com.arkivanov.decompose.extensions.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.SnapshotMutationPolicy
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.structuralEqualityPolicy
import com.arkivanov.decompose.value.Value

/**
 * Subscribes to the [Value] and returns Compose [State].
 *
 * @param policy a [SnapshotMutationPolicy] that will be used by Compose when comparing values.
 * Default is [structuralEqualityPolicy].
 */
@Composable
fun  Value.subscribeAsState(policy: SnapshotMutationPolicy = structuralEqualityPolicy()): State {
    val state = remember(this, policy) { mutableStateOf(value, policy) }

    DisposableEffect(this) {
        val disposable = subscribe { state.value = it }
        onDispose { disposable.cancel() }
    }

    return state
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy