flowOnUnconfined

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

Extracts the DispatcherProvider from the coroutineContext of the collector coroutine, then uses its DispatcherProvider.unconfined 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 "unconfined"

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

See also

flowOn

Sources

Link copied to clipboard