@@ -11,81 +11,7 @@ import kotlin.coroutines.EmptyCoroutineContext
11
11
public actual fun <T > MutableStateFlow (
12
12
viewModelScope : ViewModelScope ,
13
13
value : T
14
- ): MutableStateFlow <T > = MutableStateFlowImpl (viewModelScope.asNative(), MutableStateFlow (value))
15
-
16
- /* *
17
- * A [MutableStateFlow] that triggers [ViewModelScopeImpl.sendObjectWillChange]
18
- * and accounts for the [ViewModelScopeImpl.subscriptionCount].
19
- */
20
- @OptIn(ExperimentalForInheritanceCoroutinesApi ::class )
21
- private class MutableStateFlowImpl <T >(
22
- private val viewModelScope : NativeViewModelScope ,
23
- private val stateFlow : MutableStateFlow <T >
24
- ): MutableStateFlow<T> {
25
-
26
- override var value: T
27
- get() = stateFlow.value
28
- set(value) {
29
- if (stateFlow.value != value) {
30
- viewModelScope.sendObjectWillChange()
31
- }
32
- stateFlow.value = value
33
- }
34
-
35
- override val replayCache: List <T >
36
- get() = stateFlow.replayCache
37
-
38
- override val subscriptionCount: StateFlow <Int > =
39
- SubscriptionCountFlow (viewModelScope.subscriptionCount, stateFlow.subscriptionCount)
40
-
41
- override suspend fun collect (collector : FlowCollector <T >): Nothing =
42
- stateFlow.collect(collector)
43
-
44
- override fun compareAndSet (expect : T , update : T ): Boolean {
45
- if (stateFlow.value == expect && expect != update) {
46
- viewModelScope.sendObjectWillChange()
47
- }
48
- return stateFlow.compareAndSet(expect, update)
49
- }
50
-
51
- @ExperimentalCoroutinesApi
52
- override fun resetReplayCache () = stateFlow.resetReplayCache()
53
-
54
- // Same implementation as in StateFlowImpl, but we need to go through our own value property.
55
- // https://github.com/Kotlin/kotlinx.coroutines/blob/6dfabf763fe9fc91fbb73eb0f2d5b488f53043f1/kotlinx-coroutines-core/common/src/flow/StateFlow.kt#L369
56
- override fun tryEmit (value : T ): Boolean {
57
- this .value = value
58
- return true
59
- }
60
-
61
- // Same implementation as in StateFlowImpl, but we need to go through our own value property.
62
- // https://github.com/Kotlin/kotlinx.coroutines/blob/6dfabf763fe9fc91fbb73eb0f2d5b488f53043f1/kotlinx-coroutines-core/common/src/flow/StateFlow.kt#L374
63
- override suspend fun emit (value : T ) {
64
- this .value = value
65
- }
66
- }
67
-
68
- /* *
69
- * A [StateFlow] that combines the subscription counts of a [ViewModelScopeImpl] and [StateFlow].
70
- */
71
- @OptIn(ExperimentalForInheritanceCoroutinesApi ::class )
72
- private class SubscriptionCountFlow (
73
- private val viewModelScopeSubscriptionCount : StateFlow <Int >,
74
- private val stateFlowSubscriptionCount : StateFlow <Int >
75
- ): StateFlow<Int> {
76
- override val value: Int
77
- get() = viewModelScopeSubscriptionCount.value + stateFlowSubscriptionCount.value
78
-
79
- override val replayCache: List <Int >
80
- get() = listOf (value)
81
-
82
- override suspend fun collect (collector : FlowCollector <Int >): Nothing {
83
- viewModelScopeSubscriptionCount.combine(stateFlowSubscriptionCount) { count1, count2 ->
84
- count1 + count2
85
- }.collect(collector)
86
- throw IllegalStateException (" SubscriptionCountFlow collect completed" )
87
- }
88
- }
14
+ ): MutableStateFlow <T > = ObservableMutableStateFlow (viewModelScope.asNative(), MutableStateFlow (value))
89
15
90
16
/* *
91
17
* @see kotlinx.coroutines.flow.stateIn
@@ -98,9 +24,9 @@ public actual fun <T> Flow<T>.stateIn(
98
24
// Similar to kotlinx.coroutines, but using our custom MutableStateFlowImpl and CoroutineContext logic.
99
25
// https://github.com/Kotlin/kotlinx.coroutines/blob/6dfabf763fe9fc91fbb73eb0f2d5b488f53043f1/kotlinx-coroutines-core/common/src/flow/operators/Share.kt#L135
100
26
val scope = viewModelScope.asNative()
101
- val state = MutableStateFlowImpl (scope, MutableStateFlow (initialValue))
27
+ val state = ObservableMutableStateFlow (scope, MutableStateFlow (initialValue))
102
28
val job = scope.coroutineScope.launchSharing(EmptyCoroutineContext , this , state, started, initialValue)
103
- return ReadonlyStateFlow (state, job)
29
+ return ObservableStateFlow (state, job)
104
30
}
105
31
106
32
/* *
@@ -136,14 +62,3 @@ private fun <T> CoroutineScope.launchSharing(
136
62
}
137
63
}
138
64
}
139
-
140
- /* *
141
- * Similar to the kotlinx.coroutines implementation, used to return a read-only StateFlow with an optional Job.
142
- * https://github.com/Kotlin/kotlinx.coroutines/blob/6dfabf763fe9fc91fbb73eb0f2d5b488f53043f1/kotlinx-coroutines-core/common/src/flow/operators/Share.kt#L379
143
- */
144
- @OptIn(ExperimentalForInheritanceCoroutinesApi ::class )
145
- private class ReadonlyStateFlow <T >(
146
- flow : StateFlow <T >,
147
- @Suppress(" unused" )
148
- private val job : Job ?
149
- ): StateFlow<T> by flow
0 commit comments