Last active
June 16, 2021 09:01
-
-
Save pedrovgs/990d3ddd9758e0559a3bff66a67ae6e7 to your computer and use it in GitHub Desktop.
Handling Kotlin Coroutines homogeneously
This file contains 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 AnyTestExample { | |
@Test | |
fun `test with coroutines`() = runBlockingTest { | |
sut = AnyClassUsingAsyncCode(Dispatchers.Unconfined, scope, view, api) | |
sut.foo() | |
advanceTimeBy(5000) | |
assertTrue(true) | |
} | |
} |
This file contains 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
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() } | |
} |
This file contains 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
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" |
This file contains 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
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() | |
} | |
} | |
} |
This file contains 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
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