commonMain.com.arkivanov.decompose.extensions.compose.SubscribeAsState.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of extensions-compose-jvm Show documentation
Show all versions of extensions-compose-jvm Show documentation
Kotlin Multiplatform lifecycle-aware business logic components
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