Skip to content

Instantly share code, notes, and snippets.

@pedrovgs
Last active June 16, 2021 09:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrovgs/990d3ddd9758e0559a3bff66a67ae6e7 to your computer and use it in GitHub Desktop.
Save pedrovgs/990d3ddd9758e0559a3bff66a67ae6e7 to your computer and use it in GitHub Desktop.
Handling Kotlin Coroutines homogeneously
class AnyTestExample {
@Test
fun `test with coroutines`() = runBlockingTest {
sut = AnyClassUsingAsyncCode(Dispatchers.Unconfined, scope, view, api)
sut.foo()
advanceTimeBy(5000)
assertTrue(true)
}
}
package com.github.pedrovgs
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
interface Async {
val context: CoroutineContext
val scope: CoroutineScope
fun async(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job = scope.launch(context, start, block)
fun <T> deferred(block: () -> T): Deferred<T> = scope.async(context) { block() }
suspend fun <T> await(block: suspend () -> T) = withContext(context) { block() }
}
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.0"
testImplementation "io.mockk:mockk:1.11.0"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.0"
package com.github.pedrovgs
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import org.kodein.di.Kodein
import org.kodein.di.erased.bind
import org.kodein.di.erased.singleton
import kotlin.coroutines.CoroutineContext
fun asyncAwaitModule(): Kodein.Module {
return Kodein.Module("Async await dependencies", allowSilentOverride = true) {
bind<CoroutineContext>() with singleton {
Dispatchers.Default
}
bind<CoroutineScope>() with singleton {
MainScope()
}
}
}
package com.github.pedrovgs
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.aplazame.core.asyncawait.Async
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlin.coroutines.CoroutineContext
open class ViewModel(
override val context: CoroutineContext,
override val scope: CoroutineScope
) : Async, LifecycleObserver {
companion object {
val empty: ViewModel = ViewModel(Dispatchers.Main, MainScope())
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
open fun initialize() { }
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
open fun resume() { }
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
open fun pause() { }
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
open fun stop() {
cancelAsyncJobs()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment