flowOnDefault

fun <T> Flow<T>.flowOnDefault(): Flow<T>

Extracts the DispatcherProvider from the coroutineContext of the collector coroutine, then uses its DispatcherProvider.default property to call flowOn(theDispatcher), and returns the result.

Samples

import dispatch.core.flowOnDefault
import dispatch.core.flowOnIO
import dispatch.core.flowOnMain
import dispatch.core.flowOnMainImmediate
import dispatch.core.flowOnUnconfined
import dispatch.internal.test.Sample5
import dispatch.internal.test.dispatcherName
import dispatch.internal.test.someDispatcherProvider
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
fun main() { 
   //sampleStart 
   runBlocking(someDispatcherProvider) {

    dispatcherName() shouldBe "runBlocking thread"

    flow {

      dispatcherName() shouldBe "default"

      emit(Unit)
    }.flowOnDefault() // switch to the "default" dispatcher for everything upstream
      .collect() // collect the flow from the "main" dispatcher
  } 
   //sampleEnd
}

See also

flowOn

Sources

Link copied to clipboard