Skip to content

Commit

Permalink
Add CoroutineScope parameter to stateIn function (GH-6)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickclephas committed Dec 23, 2022
1 parent 091dd2a commit 2cb7c91
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rickclephas.kmm.viewmodel

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*

/**
Expand All @@ -17,6 +18,7 @@ public actual inline fun <T> MutableStateFlow(
@Suppress("NOTHING_TO_INLINE")
public actual inline fun <T> Flow<T>.stateIn(
viewModelScope: ViewModelScope,
coroutineScope: CoroutineScope,
started: SharingStarted,
initialValue: T
): StateFlow<T> = stateIn(viewModelScope.coroutineScope, started, initialValue)
): StateFlow<T> = stateIn(coroutineScope, started, initialValue)
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ private class SubscriptionCountFlow(
*/
public actual fun <T> Flow<T>.stateIn(
viewModelScope: ViewModelScope,
coroutineScope: CoroutineScope,
started: SharingStarted,
initialValue: T
): StateFlow<T> {
// Similar to the kotlinx.coroutines implementation, but using our custom MutableStateFlowImpl.
// https://github.com/Kotlin/kotlinx.coroutines/blob/6dfabf763fe9fc91fbb73eb0f2d5b488f53043f1/kotlinx-coroutines-core/common/src/flow/operators/Share.kt#L135
val scope = viewModelScope.asImpl()
val state = MutableStateFlowImpl(scope, MutableStateFlow(initialValue))
val job = scope.coroutineScope.launchSharing(EmptyCoroutineContext, this, state, started, initialValue)
val job = coroutineScope.launchSharing(EmptyCoroutineContext, this, state, started, initialValue)
return ReadonlyStateFlow(state, job)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rickclephas.kmm.viewmodel

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -18,6 +19,7 @@ public expect fun <T> MutableStateFlow(
*/
public expect fun <T> Flow<T>.stateIn(
viewModelScope: ViewModelScope,
coroutineScope: CoroutineScope,
started: SharingStarted,
initialValue: T
): StateFlow<T>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.rickclephas.kmm.viewmodel

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.plus
import kotlin.coroutines.CoroutineContext

/**
* @see kotlinx.coroutines.flow.stateIn
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun <T> Flow<T>.stateIn(
viewModelScope: ViewModelScope,
started: SharingStarted,
initialValue: T
): StateFlow<T> = stateIn(viewModelScope, viewModelScope.coroutineScope, started, initialValue)

/**
* @see kotlinx.coroutines.flow.stateIn
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun <T> Flow<T>.stateIn(
viewModelScope: ViewModelScope,
context: CoroutineContext,
started: SharingStarted,
initialValue: T
): StateFlow<T> = stateIn(viewModelScope, viewModelScope.coroutineScope + context, started, initialValue)

0 comments on commit 2cb7c91

Please sign in to comment.