Skip to content

Instantly share code, notes, and snippets.

View RBusarow's full-sized avatar
🕶️

Rick Busarow RBusarow

🕶️
View GitHub Profile
class CoroutineTestRule : TestRule,
TestPolymorphicCoroutineScope by TestPolymorphicCoroutineScope() {
val dispatcher = coroutineContext[ContinuationInterceptor] as TestCoroutineDispatcher
override fun apply(
base: Statement, description: Description?
) = object : Statement() {
override fun evaluate() {
@Throws(Throwable::class)
@ExperimentalCoroutinesApi
interface TestPolymorphicCoroutineScope : TestCoroutineScope,
DefaultCoroutineScope,
IOCoroutineScope,
MainCoroutineScope,
MainImmediateCoroutineScope,
UnconfinedCoroutineScope
@ExperimentalCoroutinesApi
private class TestPolymorphicCoroutineScopeImpl(
val scope = MainCoroutineScope()
val progressIndicator = ProgressIndicator(scope)
@Module
object CoroutineScopeModule {
@Provides
fun provideMainCoroutineScope(): MainCoroutineScope = MainCoroutineScope()
}
public fun MainCoroutineScope(
job: Job = SupervisorJob()
): MainCoroutineScope = object : MainCoroutineScope {
override val coroutineContext = job + Dispatchers.Main
}
public fun MainCoroutineScope(
context: CoroutineContext
): MainCoroutineScope = object : MainCoroutineScope {
override val coroutineContext = context + Dispatchers.Main
class ProgressIndicator(
private val coroutineScope: MainCoroutineScope
) {
...
}
interface DefaultCoroutineScope : CoroutineScope
interface IOCoroutineScope : CoroutineScope
interface MainCoroutineScope : CoroutineScope
interface MainImmediateCoroutineScope : CoroutineScope
interface UnconfinedCoroutineScope : CoroutineScope
@Qualifier
annotation class MainCoroutineScope
@Module
object CoroutineScopeModule {
@Provides
@MainCoroutineScope
fun provideMainCoroutineScope(): CoroutineScope = CoroutineScope(Job() + Dispatchers.Main)
class ProgressIndicator(
private val coroutineScope: CoroutineScope
) {
fun start(
timeout: Milliseconds,
message: String? = null
) = coroutineScope.launch {
...
}
}
@RBusarow
RBusarow / MyCoroutineScope.kt
Created January 11, 2020 23:58
CoroutineScope implementations
class MyCoroutineScope : CoroutineScope {
override val coroutineContext = Job() + Dispatchers.Main
}
val someScope = CoroutineScope(Job() + Dispatchers.IO)