copy

open fun copy(default: CoroutineDispatcher = this.default, io: CoroutineDispatcher = this.io, main: CoroutineDispatcher = this.main, mainImmediate: CoroutineDispatcher = this.mainImmediate, unconfined: CoroutineDispatcher = this.unconfined): DispatcherProvider

Return

a copy of this DispatcherProvider, retaining the properties of the original if they're not specified as arguments.

Samples

import dispatch.core.dispatcherProvider
import dispatch.internal.test.Sample5
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Runnable
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
fun main() { 
   //sampleStart 
   val originalDispatcherProvider = currentCoroutineContext().dispatcherProvider
originalDispatcherProvider.main shouldBe Dispatchers.Main

val newMain = MyCustomBlockingQueueDispatcher()
// create a copy of the existing DispatcherProvider, except the new main dispatcher
val bleDispatchers = originalDispatcherProvider.copy(main = newMain)

withContext(bleDispatchers) {
  // any coroutine downstream of this `withContext { }` will use the redefined main dispatcher
  dispatcherProvider.main shouldBe newMain
} 
   //sampleEnd
}

Sources

Link copied to clipboard