Coroutines-Extensions

Adds concurrent mapAsync, pairwise emissions, flow racing, exponential-backoff retries, timeout-or-null, and default/fallback helpers for building composable, resilient asynchronous streams with concise APIs.

JVM
GitHub stars1
Authorsrain991
Dependents0
OSS Health
LicenseApache License 2.0
Creation date5 months ago

Last activity5 months ago
Latest release1.0.0 (5 months ago)

Kotlin Coroutines Extensions

Library that extends standard library of Kotlin's coroutines with missing functions.

Detailed documentation for all functions

Add to your project

Ensure you have MavenCentral in your settings.gradle.kts:

repositories {
        ...
        mavenCentral()
}

Add dependency in build.gradle.kts in project/subproject where helper functions are needed:

implementation("io.github.rain991:coroutines-extensions:1.0.0")

Functions overview

Name Summary
mapAsync suspend fun <T, R> Iterable<T>.mapAsync(transform: suspend (T) -> R): List<R>

Transforms all elements of the iterable concurrently.
pairwise fun <T> Flow<T>.pairwise(): Flow<Pair<T, T>>

For each emission after the first, emits a Pair(previous, current).
raceWith suspend fun <T> Flow<T>.raceWith(other: Flow<T>): T

Returns the value from the flow that emits first.
retryWithExponentialBackoff fun <T> Flow<T>.retryWithExponentialBackoff(maxAttempts: Int, initialDelay: Duration, multiplier: Double = 2.0, retryOn: (Throwable) -> Boolean = { true }): Flow<T>

Retries the flow when it fails, increasing the delay between retries exponentially.
timeoutOrNull fun <T> Flow<T>.timeoutOrNull(timeout: Duration): Flow<T?>

Emits the first value from the upstream flow or null if no value is emitted within the given timeout.
withDefault fun <T> Flow<T>.withDefault(default: T): Flow<T>

Emits the provided default value if the upstream flow is empty.

fun <T> Flow<T>.withDefault(default: T, shouldDefault: (Throwable) -> Boolean): Flow<T>

Emits the provided default value if the upstream flow is empty or if shouldDefault returns true for an exception.
withFallback fun <T> Flow<T>.withFallback(fallbackWhen: (Throwable) -> Boolean = { true }, fallback: () -> Flow<T>): Flow<T>

Emits from the upstream flow and switches to the fallback flow in case of an exception.
JVM
GitHub stars1
Authorsrain991
Dependents0
OSS Health
LicenseApache License 2.0
Creation date5 months ago

Last activity5 months ago
Latest release1.0.0 (5 months ago)

Kotlin Coroutines Extensions

Library that extends standard library of Kotlin's coroutines with missing functions.

Detailed documentation for all functions

Add to your project

Ensure you have MavenCentral in your settings.gradle.kts:

repositories {
        ...
        mavenCentral()
}

Add dependency in build.gradle.kts in project/subproject where helper functions are needed:

implementation("io.github.rain991:coroutines-extensions:1.0.0")

Functions overview

Name Summary
mapAsync suspend fun <T, R> Iterable<T>.mapAsync(transform: suspend (T) -> R): List<R>

Transforms all elements of the iterable concurrently.
pairwise fun <T> Flow<T>.pairwise(): Flow<Pair<T, T>>

For each emission after the first, emits a Pair(previous, current).
raceWith suspend fun <T> Flow<T>.raceWith(other: Flow<T>): T

Returns the value from the flow that emits first.
retryWithExponentialBackoff fun <T> Flow<T>.retryWithExponentialBackoff(maxAttempts: Int, initialDelay: Duration, multiplier: Double = 2.0, retryOn: (Throwable) -> Boolean = { true }): Flow<T>

Retries the flow when it fails, increasing the delay between retries exponentially.
timeoutOrNull fun <T> Flow<T>.timeoutOrNull(timeout: Duration): Flow<T?>

Emits the first value from the upstream flow or null if no value is emitted within the given timeout.
withDefault fun <T> Flow<T>.withDefault(default: T): Flow<T>

Emits the provided default value if the upstream flow is empty.

fun <T> Flow<T>.withDefault(default: T, shouldDefault: (Throwable) -> Boolean): Flow<T>

Emits the provided default value if the upstream flow is empty or if shouldDefault returns true for an exception.
withFallback fun <T> Flow<T>.withFallback(fallbackWhen: (Throwable) -> Boolean = { true }, fallback: () -> Flow<T>): Flow<T>

Emits from the upstream flow and switches to the fallback flow in case of an exception.