Giter Site home page Giter Site logo

coroutineskit's Introduction

coroutineskit

implementation "com.github.skgmn:coroutineskit:0.4.0"

shareRefCount, stateRefCount

val flow: Flow<T> = ...
val sharedFlow: SharedFlow<T> = flow.shareRefCount()
val stateFlow: StateFlow<T> = flow.stateRefCount { initialValue }

Turns Flow<T> into SharedFlow<T> or StateFlow<T>. Unlike shareIn() or stateIn() it does not require any CoroutineScope. It subscribes the upstream when the downstream is firstly collected, and it cancels the upstream when the downstream is lastly completed so that it can be used like RxJava's publish().refCount() or replay().refCount().

listenerFlow, listenerSharedFlow, listenerStateFlow

A series corresponding to callbackFlow. They can be used like below.

val flow = listenerFlow(Dispatchers.Main.immediate) {
    val listener = { emit(it) }
    addListener(listener)
    invokeOnClose { removeListener(listener) }
}

These have been created because

  1. callbackFlow is still experimental.
  2. callbackFlow does not have proper default values for backpressure I think.
  3. callbackFlow cannot create SharedFlow and StateFlow.

stateMap, stateCombine

Operators corresponding to map and combine keeping StateFlow<T> type.

val stateFlow: StateFlow<Int> = ...
val map1 = stateFlow.map { it * 2 }      // This is Flow<Int>
val map2 = stateFlow.stateMap { it * 2 } // This is StateFlow<Int>

It only accepts non-suspend lambda.

runWhile

Run block only while the given Flow<Boolean> or StateFlow<Boolean> emits true. It can be used similarly to RxJava's takeUntil() operator.

val conditionFlow: Flow<Boolean> = ...
runWhile(conditionFlow) {
   // Code here runs while conditionFlow emits true, and cancelled when conditionFlow emits false.
}

capture

Same as RxJava's withLatestFrom().

val flow: Flow<T>
val flow1: Flow<T1>
val flow2: Flow<T2>
flow.capture(flow1, flow2) { source, one, two ->
    someTransform(source, one, two)
}

chunked

Same as RxJava's buffer().

fun <T> Flow<T>.chunked(count: Int): Flow<List<T>>
fun <T> Flow<T>.chunked(timeMillis: Long): Flow<List<T>>
fun <T> Flow<T>.chunked(count: Int, timeMillis: Long): Flow<List<T>>

defer

Same as RxJava's defer().

val flow = defer {
    // Code here runs when the flow is being collected.
    val parameter = getParameter()
    createFlow(parameter)
}

coroutineskit-lifecycle

implementation "com.github.skgmn:coroutineskit-lifecycle:0.4.0"

isAtLeast

fun Lifecycle.isAtLeast(state: Lifecycle.State): StateFlow<Boolean>
fun LifecycleOwner.isAtLeast(state: Lifecycle.State): StateFlow<Boolean>

Flow version of Lifecycle.State.isAtLeast. It's useful when used with runWhile.

whenStarted {
    runWhile(isAtLeast(State.STARTED)) {
        // Code here starts on onStart() and stops on onStop().
    }
}

LiveData.toStateFlow

fun <T: Any> LiveData<T>.toStateFlow(): StateFlow<T?>

Turns LiveData<T> into StateFlow<T?>.

coroutineskit's People

Contributors

skgmn avatar

Stargazers

Dex avatar

Watchers

 avatar Dex avatar

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.