This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val scope : CoroutineScope = MainScope() | |
| val parentJob = scope.launch { | |
| val jobA = scope.launch { | |
| val jobC = scope.launch { delayForever() } | |
| val jobD = scope.launch { delayForever() } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Dispatchers { | |
| val Default: CoroutineDispatcher = createDefaultDispatcher() | |
| val Main: MainCoroutineDispatcher get() = MainDispatcherLoader.dispatcher | |
| val Unconfined: CoroutineDispatcher = kotlinx.coroutines.Unconfined | |
| val IO: CoroutineDispatcher = DefaultScheduler.IO | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SomeClass : CoroutineScope { | |
| val job = Job() | |
| override val coroutineContext = job + Dispatchers.Main | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public fun CoroutineScope.launch( | |
| context: CoroutineContext = EmptyCoroutineContext, | |
| start: CoroutineStart = CoroutineStart.DEFAULT, | |
| block: suspend CoroutineScope.() -> Unit | |
| ): Job {...} | |
| public fun <T> CoroutineScope.async( | |
| context: CoroutineContext = EmptyCoroutineContext, | |
| start: CoroutineStart = CoroutineStart.DEFAULT, | |
| block: suspend CoroutineScope.() -> T |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SomeClass : CoroutineScope { | |
| val job = Job() | |
| // set the default dispatcher to Dispatchers.Main | |
| override val coroutineContext = job + Dispatchers.Main | |
| fun updateSomething() = launch(Dispatchers.Default) { | |
| // this coroutine overrides the CoroutineScope's ContinuationInterceptor | |
| // and uses Dispatchers.Default | |
| val result = getSomethingExpensive() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SomeRepository(private val api: SomeApi) { | |
| // note that the class doesn't have a CoroutineScope or CoroutineContext | |
| suspend fun getSomethingExpensive() = withContext(Dispatchers.IO) { | |
| // regardless of the caller, this coroutine will execute | |
| // with the IO dispatcher. | |
| api.getTheWindsOfWinter() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public suspend fun <T> withContext( | |
| context: CoroutineContext, | |
| block: suspend CoroutineScope.() -> T | |
| ): T = suspendCoroutineUninterceptedOrReturn sc@ { ... } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class MockkedDispatcherTest { | |
| val dispatcher = TestCoroutineDispatcher() | |
| val scope = TestCoroutineScope(dispatcher) | |
| @Before | |
| fun setUp() { | |
| Dispatchers.setMain(dispatcher) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import io.kotlintest.* | |
| @Test | |
| fun `dispatcher mutation`() = runBlocking { | |
| val testDispatcher = TestCoroutineDispatcher() | |
| launch(testDispatcher) { | |
| currentDispatcher() shouldBe testDispatcher |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CoroutineContext { | |
| internal val map = mutableMapOf<Key<*>, Element>() | |
| operator fun <E : Element> get(key: Key<E>): E? = map[key] as? E | |
| operator fun plus(context: CoroutineContext): CoroutineContext { | |
| val new = CoroutineContext() | |
| new.map.putAll(map) | |
| new.map.putAll(context.map) | |
| return new |