Giter VIP home page Giter VIP logo

05nelsonm / component-coroutines Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 2.0 130 KB

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

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

License: Apache License 2.0

Kotlin 100.00%
kotlin kotlin-multiplatform kotlin-coroutines kotlin-multiplatform-library kotlin-multiplatform-mobile android android-library android-lib android-libraries android-libs

component-coroutines's Introduction

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

component-coroutines's People

Contributors

05nelsonm avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

woren

component-coroutines's Issues

Use `explicitApi`

Use explicitApi() for library modules

// build.gradle.kts

plugins {
    id(pluginId.kmp.configuration)
    id(pluginId.kmp.publish)
}

kmpConfiguration {
    setupMultiplatform(
        // ...
        kotlin = {
            explicitApi()
        }
    )
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.