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

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

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.mutableStateOf
import androidx.compose.runtime.snapshotFlow
import com.eygraber.vice.ViceSource
import kotlinx.coroutines.flow.Flow

public abstract class MutableStateSource : 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

  protected fun update(value: T) {
    state.value = value
  }

  @Composable
  @ReadOnlyComposable
  final override fun currentState(): T = state.value
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy