Skip to content

Instantly share code, notes, and snippets.

Avatar
🎧
Writing code

Rick Busarow RBusarow

🎧
Writing code
View GitHub Profile
View CoroutineTestRule.kt
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)
View TestPolymorphicCoroutineScope.kt
@ExperimentalCoroutinesApi
interface TestPolymorphicCoroutineScope : TestCoroutineScope,
DefaultCoroutineScope,
IOCoroutineScope,
MainCoroutineScope,
MainImmediateCoroutineScope,
UnconfinedCoroutineScope
@ExperimentalCoroutinesApi
private class TestPolymorphicCoroutineScopeImpl(
View MainScopeCreation.kt
val scope = MainCoroutineScope()
val progressIndicator = ProgressIndicator(scope)
View CoroutineScopeModule.kt
@Module
object CoroutineScopeModule {
@Provides
fun provideMainCoroutineScope(): MainCoroutineScope = MainCoroutineScope()
}
View CoroutineScopeFactories.kt
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
View ProgressIndicator.kt
class ProgressIndicator(
private val coroutineScope: MainCoroutineScope
) {
...
}
View CoroutineScopes.kt
interface DefaultCoroutineScope : CoroutineScope
interface IOCoroutineScope : CoroutineScope
interface MainCoroutineScope : CoroutineScope
interface MainImmediateCoroutineScope : CoroutineScope
interface UnconfinedCoroutineScope : CoroutineScope
View Dagger2QualifiedCoroutineScope.kt
@Qualifier
annotation class MainCoroutineScope
@Module
object CoroutineScopeModule {
@Provides
@MainCoroutineScope
fun provideMainCoroutineScope(): CoroutineScope = CoroutineScope(Job() + Dispatchers.Main)
View ProgressIndicator.kt
class ProgressIndicator(
private val coroutineScope: CoroutineScope
) {
fun start(
timeout: Milliseconds,
message: String? = null
) = coroutineScope.launch {
...
}
}
@RBusarow
RBusarow / MyCoroutineScope.kt
Created Jan 11, 2020
CoroutineScope implementations
View MyCoroutineScope.kt
class MyCoroutineScope : CoroutineScope {
override val coroutineContext = Job() + Dispatchers.Main
}
val someScope = CoroutineScope(Job() + Dispatchers.IO)