Skip to content

Commit cead02d

Browse files
committed
Always collect stateIn on Main thread (GH-7)
1 parent 62b2994 commit cead02d

4 files changed

Lines changed: 11 additions & 22 deletions

File tree

kmm-viewmodel-core/src/androidMain/kotlin/com/rickclephas/kmm/viewmodel/StateFlow.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.rickclephas.kmm.viewmodel
22

3-
import kotlinx.coroutines.CoroutineScope
43
import kotlinx.coroutines.flow.*
4+
import kotlin.coroutines.CoroutineContext
55

66
/**
77
* @see kotlinx.coroutines.flow.MutableStateFlow
@@ -18,7 +18,7 @@ public actual inline fun <T> MutableStateFlow(
1818
@Suppress("NOTHING_TO_INLINE")
1919
public actual inline fun <T> Flow<T>.stateIn(
2020
viewModelScope: ViewModelScope,
21-
coroutineScope: CoroutineScope,
21+
coroutineContext: CoroutineContext,
2222
started: SharingStarted,
2323
initialValue: T
24-
): StateFlow<T> = stateIn(coroutineScope, started, initialValue)
24+
): StateFlow<T> = flowOn(coroutineContext).stateIn(viewModelScope.coroutineScope, started, initialValue)

kmm-viewmodel-core/src/appleMain/kotlin/com/rickclephas/kmm/viewmodel/StateFlow.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,16 @@ private class SubscriptionCountFlow(
9090
*/
9191
public actual fun <T> Flow<T>.stateIn(
9292
viewModelScope: ViewModelScope,
93-
coroutineScope: CoroutineScope,
93+
coroutineContext: CoroutineContext,
9494
started: SharingStarted,
9595
initialValue: T
9696
): StateFlow<T> {
97-
// Similar to the kotlinx.coroutines implementation, but using our custom MutableStateFlowImpl.
97+
// Similar to kotlinx.coroutines, but using our custom MutableStateFlowImpl and CoroutineContext logic.
9898
// https://github.com/Kotlin/kotlinx.coroutines/blob/6dfabf763fe9fc91fbb73eb0f2d5b488f53043f1/kotlinx-coroutines-core/common/src/flow/operators/Share.kt#L135
9999
val scope = viewModelScope.asImpl()
100+
val upstream = flowOn(coroutineContext)
100101
val state = MutableStateFlowImpl(scope, MutableStateFlow(initialValue))
101-
val job = coroutineScope.launchSharing(EmptyCoroutineContext, this, state, started, initialValue)
102+
val job = scope.coroutineScope.launchSharing(EmptyCoroutineContext, upstream, state, started, initialValue)
102103
return ReadonlyStateFlow(state, job)
103104
}
104105

kmm-viewmodel-core/src/commonMain/kotlin/com/rickclephas/kmm/viewmodel/StateFlow.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.rickclephas.kmm.viewmodel
22

3-
import kotlinx.coroutines.CoroutineScope
43
import kotlinx.coroutines.flow.Flow
54
import kotlinx.coroutines.flow.MutableStateFlow
65
import kotlinx.coroutines.flow.SharingStarted
76
import kotlinx.coroutines.flow.StateFlow
7+
import kotlin.coroutines.CoroutineContext
88

99
/**
1010
* @see kotlinx.coroutines.flow.MutableStateFlow
@@ -19,7 +19,7 @@ public expect fun <T> MutableStateFlow(
1919
*/
2020
public expect fun <T> Flow<T>.stateIn(
2121
viewModelScope: ViewModelScope,
22-
coroutineScope: CoroutineScope,
22+
coroutineContext: CoroutineContext,
2323
started: SharingStarted,
2424
initialValue: T
2525
): StateFlow<T>

kmm-viewmodel-core/src/commonMain/kotlin/com/rickclephas/kmm/viewmodel/StateFlowUtils.kt

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package com.rickclephas.kmm.viewmodel
33
import kotlinx.coroutines.flow.Flow
44
import kotlinx.coroutines.flow.SharingStarted
55
import kotlinx.coroutines.flow.StateFlow
6-
import kotlinx.coroutines.plus
7-
import kotlin.coroutines.CoroutineContext
6+
import kotlin.coroutines.EmptyCoroutineContext
87

98
/**
109
* @see kotlinx.coroutines.flow.stateIn
@@ -14,15 +13,4 @@ public inline fun <T> Flow<T>.stateIn(
1413
viewModelScope: ViewModelScope,
1514
started: SharingStarted,
1615
initialValue: T
17-
): StateFlow<T> = stateIn(viewModelScope, viewModelScope.coroutineScope, started, initialValue)
18-
19-
/**
20-
* @see kotlinx.coroutines.flow.stateIn
21-
*/
22-
@Suppress("NOTHING_TO_INLINE")
23-
public inline fun <T> Flow<T>.stateIn(
24-
viewModelScope: ViewModelScope,
25-
context: CoroutineContext,
26-
started: SharingStarted,
27-
initialValue: T
28-
): StateFlow<T> = stateIn(viewModelScope, viewModelScope.coroutineScope + context, started, initialValue)
16+
): StateFlow<T> = stateIn(viewModelScope, EmptyCoroutineContext, started, initialValue)

0 commit comments

Comments
 (0)