Skip to content

Commit

Permalink
use currentCoroutineContext to resolve context inside a Flow (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
RBusarow authored Oct 18, 2020
1 parent 4e7b136 commit 974e58e
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions dispatch-core/src/main/java/dispatch/core/Flow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package dispatch.core

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.coroutines.*

/**
* Extracts the [DispatcherProvider] from the `coroutineContext` of the *collector* coroutine,
Expand All @@ -29,7 +28,7 @@ import kotlin.coroutines.*
*/
@ExperimentalCoroutinesApi
public fun <T> Flow<T>.flowOnDefault(): Flow<T> = flow {
flowOn(coroutineContext.dispatcherProvider.default)
flowOn(currentCoroutineContext().dispatcherProvider.default)
.collect { emit(it) }
}

Expand All @@ -43,7 +42,7 @@ public fun <T> Flow<T>.flowOnDefault(): Flow<T> = flow {
*/
@ExperimentalCoroutinesApi
public fun <T> Flow<T>.flowOnIO(): Flow<T> = flow {
flowOn(coroutineContext.dispatcherProvider.io)
flowOn(currentCoroutineContext().dispatcherProvider.io)
.collect { emit(it) }
}

Expand All @@ -57,7 +56,7 @@ public fun <T> Flow<T>.flowOnIO(): Flow<T> = flow {
*/
@ExperimentalCoroutinesApi
public fun <T> Flow<T>.flowOnMain(): Flow<T> = flow {
flowOn(coroutineContext.dispatcherProvider.main)
flowOn(currentCoroutineContext().dispatcherProvider.main)
.collect { emit(it) }
}

Expand All @@ -71,7 +70,7 @@ public fun <T> Flow<T>.flowOnMain(): Flow<T> = flow {
*/
@ExperimentalCoroutinesApi
public fun <T> Flow<T>.flowOnMainImmediate(): Flow<T> = flow {
flowOn(coroutineContext.dispatcherProvider.mainImmediate)
flowOn(currentCoroutineContext().dispatcherProvider.mainImmediate)
.collect { emit(it) }
}

Expand All @@ -85,6 +84,6 @@ public fun <T> Flow<T>.flowOnMainImmediate(): Flow<T> = flow {
*/
@ExperimentalCoroutinesApi
public fun <T> Flow<T>.flowOnUnconfined(): Flow<T> = flow {
flowOn(coroutineContext.dispatcherProvider.unconfined)
flowOn(currentCoroutineContext().dispatcherProvider.unconfined)
.collect { emit(it) }
}

0 comments on commit 974e58e

Please sign in to comment.