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

commonMain.com.eygraber.vice.sources.MoleculeStateSource.kt Maven / Gradle / Ivy

There is a newer version: 0.9.7
Show 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.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