11package com.lasthopesoftware.bluewater.shared.observables
22
33import androidx.compose.runtime.Composable
4+ import androidx.compose.runtime.DisposableEffect
45import androidx.compose.runtime.LaunchedEffect
56import androidx.compose.runtime.MutableState
67import androidx.compose.runtime.State
78import androidx.compose.runtime.mutableStateOf
89import androidx.compose.runtime.remember
910import androidx.compose.runtime.snapshotFlow
1011import kotlinx.coroutines.flow.drop
11- import kotlinx.coroutines.rx3.asFlow
1212import kotlinx.coroutines.withContext
1313import kotlin.coroutines.CoroutineContext
1414import kotlin.coroutines.EmptyCoroutineContext
1515
1616@Composable
17- fun <T , S : InteractionState <T >> S.subscribeAsState (
18- context : CoroutineContext = EmptyCoroutineContext
19- ): State <T > = updatingState(context)
17+ fun <T , S : InteractionState <T >> S.subscribeAsState (): State <T > = updatingState()
2018
2119@Composable
2220fun <T , S : MutableInteractionState <T >> S.subscribeAsMutableState (
2321 context : CoroutineContext = EmptyCoroutineContext
2422): MutableState <T > {
25- val state = updatingState(context )
23+ val state = updatingState()
2624
2725 LaunchedEffect (key1 = this , context) {
2826 if (context == EmptyCoroutineContext ) {
@@ -39,17 +37,15 @@ fun <T, S : MutableInteractionState<T>> S.subscribeAsMutableState(
3937}
4038
4139@Composable
42- private fun <T , S : InteractionState <T >> S.updatingState (context : CoroutineContext ): MutableState <T > {
40+ private fun <T , S : InteractionState <T >> S.updatingState (): MutableState <T > {
4341 val state = remember { mutableStateOf(value) }
44- LaunchedEffect (this , context) {
45- if (context == EmptyCoroutineContext ) {
46- asFlow().collect {
47- state.value = it.value
48- }
49- } else withContext(context) {
50- asFlow().collect {
51- state.value = it.value
52- }
42+ DisposableEffect (this ) {
43+ val subscription = subscribe {
44+ state.value = it.value
45+ }
46+
47+ onDispose {
48+ subscription.dispose()
5349 }
5450 }
5551 return state
0 commit comments