Skip to content

Instantly share code, notes, and snippets.

View RBusarow's full-sized avatar
🕶️

Rick Busarow RBusarow

🕶️
View GitHub Profile
val scope : CoroutineScope = MainScope()
val parentJob = scope.launch {
val jobA = scope.launch {
val jobC = scope.launch { delayForever() }
val jobD = scope.launch { delayForever() }
object Dispatchers {
val Default: CoroutineDispatcher = createDefaultDispatcher()
val Main: MainCoroutineDispatcher get() = MainDispatcherLoader.dispatcher
val Unconfined: CoroutineDispatcher = kotlinx.coroutines.Unconfined
val IO: CoroutineDispatcher = DefaultScheduler.IO
}
@RBusarow
RBusarow / SomeClass.kt
Last active July 7, 2019 18:00
Basic CoroutineScope implementation which defaults to Main
class SomeClass : CoroutineScope {
val job = Job()
override val coroutineContext = job + Dispatchers.Main
}
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
@RBusarow
RBusarow / SomeClass.kt
Last active July 8, 2019 00:38
Example of overriding ContinuationInterceptor in coroutine builders
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()
@RBusarow
RBusarow / SomeRepository.kt
Last active July 8, 2019 01:02
Example of creating a new coroutine, using withContext to modify its CoroutineContext
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()
}
}
@RBusarow
RBusarow / Builders.common.kt
Created July 8, 2019 00:55
Snippet of the withContext() coroutine builder
public suspend fun <T> withContext(
context: CoroutineContext,
block: suspend CoroutineScope.() -> T
): T = suspendCoroutineUninterceptedOrReturn sc@ { ... }
internal class MockkedDispatcherTest {
val dispatcher = TestCoroutineDispatcher()
val scope = TestCoroutineScope(dispatcher)
@Before
fun setUp() {
Dispatchers.setMain(dispatcher)
@RBusarow
RBusarow / DispatcherMutation.kt
Last active July 13, 2019 23:39
Example of dispatcher mutation
import io.kotlintest.*
@Test
fun `dispatcher mutation`() = runBlocking {
val testDispatcher = TestCoroutineDispatcher()
launch(testDispatcher) {
currentDispatcher() shouldBe testDispatcher
@RBusarow
RBusarow / FakeCoroutineContext.kt
Last active July 22, 2019 13:34
Re-imagined CoroutineContext as a decorator around a standard map.
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