commonMain.com.eygraber.vice.sources.DerivedStateSource.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vice-sources-jvm Show documentation
Show all versions of vice-sources-jvm Show documentation
Implementations of ViceSource for use with vice-core
The newest version!
package com.eygraber.vice.sources
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.State
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.snapshotFlow
import com.eygraber.vice.ViceSource
import kotlinx.coroutines.flow.Flow
public abstract class DerivedStateSource : ViceSource, State {
protected abstract fun deriveState(): T
private val state by lazy {
derivedStateOf(::deriveState)
}
public val updates: Flow get() = snapshotFlow { state.value }
override val value: T get() = state.value
@Composable
@ReadOnlyComposable
final override fun currentState(): T = state.value
}