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

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

package com.eygraber.vice.sources

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.eygraber.vice.ViceSource
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.StateFlow

public abstract class StateFlowSource : ViceSource {
  protected abstract val flow: StateFlow

  protected abstract suspend fun onAttached(scope: CoroutineScope)

  public val value: T get() = flow.value

  @Composable
  final override fun currentState(): T {
    LaunchedEffect(Unit) {
      onAttached(this)
    }

    return flow.collectAsStateWithLifecycle().value
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy