commonMain.com.eygraber.vice.sources.MoleculeStateSource.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
package com.eygraber.vice.sources
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshotFlow
import com.eygraber.vice.ViceSource
import kotlinx.coroutines.flow.Flow
public abstract class MoleculeStateSource : ViceSource, State {
private val state by lazy {
mutableStateOf(initial)
}
public val updates: Flow get() = snapshotFlow { state.value }
override val value: T get() = state.value
protected abstract val initial: T
@Composable
@ReadOnlyComposable
protected abstract fun calculate(): T
@Composable
@ReadOnlyComposable
final override fun currentState(): T {
state.value = calculate()
return state.value
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy