05nelsonm / component-coroutines

A wedge for retrieving coroutine Dispatchers to make testing and working in Kotlin Multiplatform common code easier

Home Page:https://kotlin-components.matthewnelson.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

component-coroutines

badge-license badge-latest-release

badge-kotlin badge-coroutines

badge-platform-android badge-platform-jvm badge-platform-js badge-platform-js-node badge-platform-linux badge-platform-macos badge-platform-ios badge-platform-tvos badge-platform-watchos badge-platform-windows badge-support-apple-silicon badge-support-js-ir

Referencing Coroutine Dispatchers within a class makes testing difficult which is why passing the CoroutineDispatcher via constructor arguments is a wise decision. component-coroutines' CoroutineDispatchers interface serves as a wedge that points to whatever dispatcher you set it up with.

A full list of kotlin-components projects can be found HERE

Get Started

// build.gradle.kts

dependencies {
    implementation("io.matthewnelson.kotlin-components:coroutines:1.1.5")
}
// build.gradle

dependencies {
    implementation "io.matthewnelson.kotlin-components:coroutines:1.1.5"
}

Kotlin Version Compatibility

Note: as of 1.1.0, the experimental memory model for KotlinNative is enabled.

coroutines kotlin kotlinx-coroutines
1.1.5 1.8.0 1.6.4
1.1.4 1.7.20 1.6.4
1.1.3 1.6.21 1.6.3
1.1.2 1.6.21 1.6.1
1.1.1 1.6.21 1.6.1
1.1.0 1.6.10 1.6.0
1.0.0 1.5.31 1.5.2

CoroutineDispatchers Example (Using Android ViewModel + Hilt)

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    
    @Provides
    @Singleton
    fun provideCoroutineDispatchers(): CoroutineDispatchers =
        object : CoroutineDispatchers {
            override val default: CoroutineDispatcher
                get() = Dispatchers.Default
            override val io: CoroutineDispatcher
                get() = Dispatchers.IO
            override val main: CoroutineDispatcher
                get() = Dispatchers.Main
            override val mainImmediate: CoroutineDispatcher
                get() = Dispatchers.Main.immediate
            override val unconfined: CoroutineDispatcher
                get() = Dispatchers.Unconfined
        }

}

@HiltViewModel
class MyViewModel @Inject constructor(
    dispatchers: CoroutineDispatchers
): ViewModel(), CoroutineDispatchers by dispatchers {

    init {
        viewModelScope.launch(io) {
            // do work
        }
    }

}

Git

This project utilizes git submodules. You will need to initialize them when cloning the repository via:

$ git clone --recursive https://github.com/05nelsonm/component-coroutines.git

If you've already cloned the repository, run:

$ git checkout master
$ git pull
$ git submodule update --init

In order to keep submodules updated when pulling the latest code, run:

$ git pull --recurse-submodules

About

A wedge for retrieving coroutine Dispatchers to make testing and working in Kotlin Multiplatform common code easier

https://kotlin-components.matthewnelson.io

License:Apache License 2.0


Languages

Language:Kotlin 100.0%