mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 05:43:50 +00:00
improvements in AwaitFirst
This commit is contained in:
parent
a10d2184ff
commit
1642f7abd9
@ -2,6 +2,9 @@
|
||||
|
||||
## 0.12.14
|
||||
|
||||
* `Coroutines`:
|
||||
* Rewrite `awaitFirstWithDeferred` onto `CompletableDeferred` instead of coroutines suspending
|
||||
|
||||
## 0.12.13
|
||||
|
||||
* `Coroutines`:
|
||||
|
@ -6,23 +6,19 @@ 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()
|
||||
}
|
||||
): Pair<Deferred<T>, T> {
|
||||
val resultDeferred = CompletableDeferred<Pair<Deferred<T>, T>>()
|
||||
val scope = scope.LinkedSupervisorScope()
|
||||
forEach {
|
||||
scope.launch {
|
||||
resultDeferred.complete(it to it.await())
|
||||
scope.cancel()
|
||||
}
|
||||
}
|
||||
}.also {
|
||||
if (cancelOnResult) {
|
||||
forEach {
|
||||
try {
|
||||
it.cancel()
|
||||
} catch (e: IllegalStateException) {
|
||||
e.printStackTrace()
|
||||
return resultDeferred.await().also {
|
||||
if (cancelOnResult) {
|
||||
forEach {
|
||||
runCatchingSafely { it.cancel() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user