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
}
}