mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-06-01 23:45:28 +00:00
38 lines
1.0 KiB
Kotlin
38 lines
1.0 KiB
Kotlin
package dev.inmo.micro_utils.coroutines
|
|
|
|
import kotlinx.coroutines.*
|
|
import kotlin.coroutines.*
|
|
|
|
suspend fun <T> Iterable<Deferred<T>>.awaitFirstWithDeferred(
|
|
scope: CoroutineScope,
|
|
cancelOnResult: Boolean = true
|
|
): Pair<Deferred<T>, T> = suspendCoroutine<Pair<Deferred<T>, T>> { continuation ->
|
|
scope.launch(SupervisorJob()) {
|
|
val scope = this
|
|
forEach {
|
|
scope.launch {
|
|
continuation.resume(it to it.await())
|
|
scope.cancel()
|
|
}
|
|
}
|
|
}
|
|
}.also {
|
|
if (cancelOnResult) {
|
|
forEach {
|
|
try {
|
|
it.cancel()
|
|
} catch (e: IllegalStateException) {
|
|
e.printStackTrace()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
suspend fun <T> Iterable<Deferred<T>>.awaitFirst(
|
|
scope: CoroutineScope,
|
|
cancelOnResult: Boolean = true
|
|
): T = awaitFirstWithDeferred(scope, cancelOnResult).second
|
|
suspend fun <T> Iterable<Deferred<T>>.awaitFirst(
|
|
cancelOthers: Boolean = true
|
|
): T = awaitFirst(CoroutineScope(coroutineContext), cancelOthers)
|