mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-26 03:58:45 +00:00
fix of #374
This commit is contained in:
parent
40f7cf7678
commit
5314833041
@ -4,6 +4,10 @@
|
|||||||
|
|
||||||
* `Colors`:
|
* `Colors`:
|
||||||
* Added as a module. It should be used by default in case you wish all the API currently realized for `HEXAColor`
|
* Added as a module. It should be used by default in case you wish all the API currently realized for `HEXAColor`
|
||||||
|
* `Coroutines`:
|
||||||
|
* Fix of [#374](https://github.com/InsanusMokrassar/MicroUtils/issues/374):
|
||||||
|
* Add vararg variants of `awaitFirst`
|
||||||
|
* Add `joinFirst`
|
||||||
|
|
||||||
## 0.21.2
|
## 0.21.2
|
||||||
|
|
||||||
|
@ -3,6 +3,12 @@ package dev.inmo.micro_utils.coroutines
|
|||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlin.coroutines.*
|
import kotlin.coroutines.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trying to [Deferred.await] on all [this] [Deferred]s. The first [Deferred] completed its work will interrupt all
|
||||||
|
* others awaits and, if [cancelOnResult] passed as true (**by default**), will also cancel all the others [Deferred]s
|
||||||
|
*
|
||||||
|
* @param scope Will be used to create [CoroutineScope.LinkedSupervisorScope] and launch joining of all [Job]s in it
|
||||||
|
*/
|
||||||
suspend fun <T> Iterable<Deferred<T>>.awaitFirstWithDeferred(
|
suspend fun <T> Iterable<Deferred<T>>.awaitFirstWithDeferred(
|
||||||
scope: CoroutineScope,
|
scope: CoroutineScope,
|
||||||
cancelOnResult: Boolean = true
|
cancelOnResult: Boolean = true
|
||||||
@ -24,10 +30,45 @@ suspend fun <T> Iterable<Deferred<T>>.awaitFirstWithDeferred(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trying to [Deferred.await] on all [this] [Deferred]s. The first [Deferred] completed its work will interrupt all
|
||||||
|
* others awaits and, if [cancelOnResult] passed as true (**by default**), will also cancel all the others [Deferred]s
|
||||||
|
*
|
||||||
|
* @param scope Will be used to create [CoroutineScope.LinkedSupervisorScope] and launch joining of all [Job]s in it
|
||||||
|
*/
|
||||||
suspend fun <T> Iterable<Deferred<T>>.awaitFirst(
|
suspend fun <T> Iterable<Deferred<T>>.awaitFirst(
|
||||||
scope: CoroutineScope,
|
scope: CoroutineScope,
|
||||||
cancelOnResult: Boolean = true
|
cancelOnResult: Boolean = true
|
||||||
): T = awaitFirstWithDeferred(scope, cancelOnResult).second
|
): T = awaitFirstWithDeferred(scope, cancelOnResult).second
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trying to [Deferred.await] on all [this] [Deferred]s. The first [Deferred] completed its work will interrupt all
|
||||||
|
* others awaits and, if [cancelOthers] passed as true (**by default**), will also cancel all the others [Deferred]s
|
||||||
|
*
|
||||||
|
* Creates [CoroutineScope] using [coroutineContext] for internal purposes
|
||||||
|
*/
|
||||||
suspend fun <T> Iterable<Deferred<T>>.awaitFirst(
|
suspend fun <T> Iterable<Deferred<T>>.awaitFirst(
|
||||||
cancelOthers: Boolean = true
|
cancelOthers: Boolean = true
|
||||||
): T = awaitFirst(CoroutineScope(coroutineContext), cancelOthers)
|
): T = awaitFirst(CoroutineScope(coroutineContext), cancelOthers)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trying to [Deferred.await] on all [deferreds]. The first [Deferred] completed its work will interrupt all
|
||||||
|
* others awaits and, if [cancelOnResult] passed as true (**by default**), will also cancel all the others [deferreds]
|
||||||
|
*
|
||||||
|
* @param scope Will be used to create [CoroutineScope.LinkedSupervisorScope] and launch joining of all [Job]s in it
|
||||||
|
*/
|
||||||
|
suspend fun <T> awaitFirst(
|
||||||
|
vararg deferreds: Deferred<T>,
|
||||||
|
scope: CoroutineScope,
|
||||||
|
cancelOnResult: Boolean = true
|
||||||
|
): T = deferreds.toList().awaitFirstWithDeferred(scope, cancelOnResult).second
|
||||||
|
/**
|
||||||
|
* Trying to [Deferred.await] on all [deferreds]. The first [Deferred] completed its work will interrupt all
|
||||||
|
* others awaits and, if [cancelOthers] passed as true (**by default**), will also cancel all the others [deferreds]
|
||||||
|
*
|
||||||
|
* Creates [CoroutineScope] using [coroutineContext] for internal purposes
|
||||||
|
*/
|
||||||
|
suspend fun <T> awaitFirst(
|
||||||
|
vararg deferreds: Deferred<T>,
|
||||||
|
cancelOthers: Boolean = true
|
||||||
|
): T = awaitFirst(*deferreds, scope = CoroutineScope(coroutineContext), cancelOnResult = cancelOthers)
|
||||||
|
@ -0,0 +1,64 @@
|
|||||||
|
package dev.inmo.micro_utils.coroutines
|
||||||
|
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import kotlin.coroutines.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trying to [Job.join] on all [this] [Job]s. The first [Job] completed its work will interrupt all others joins
|
||||||
|
* and, if [cancelOthers] passed as true (**by default**), will also cancel all the others [Job]s
|
||||||
|
*
|
||||||
|
* @param scope Will be used to create [CoroutineScope.LinkedSupervisorScope] and launch joining of all [Job]s in it
|
||||||
|
*/
|
||||||
|
suspend fun Iterable<Job>.joinFirst(
|
||||||
|
scope: CoroutineScope,
|
||||||
|
cancelOthers: Boolean = true
|
||||||
|
): Job {
|
||||||
|
val resultDeferred = CompletableDeferred<Job>()
|
||||||
|
val scope = scope.LinkedSupervisorScope()
|
||||||
|
forEach {
|
||||||
|
scope.launch {
|
||||||
|
it.join()
|
||||||
|
resultDeferred.complete(it)
|
||||||
|
scope.cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultDeferred.await().also {
|
||||||
|
if (cancelOthers) {
|
||||||
|
forEach {
|
||||||
|
runCatchingSafely { it.cancel() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Trying to [Job.join] on all [this] [Job]s. The first [Job] completed its work will interrupt all others joins
|
||||||
|
* and, if [cancelOthers] passed as true (**by default**), will also cancel all the others [Job]s
|
||||||
|
*
|
||||||
|
* Creates [CoroutineScope] using [coroutineContext] for internal purposes
|
||||||
|
*/
|
||||||
|
suspend fun Iterable<Job>.joinFirst(
|
||||||
|
cancelOthers: Boolean = true
|
||||||
|
): Job = joinFirst(CoroutineScope(coroutineContext), cancelOthers)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trying to [Job.join] on all [jobs]. The first [Job] completed its work will interrupt all others joins
|
||||||
|
* and, if [cancelOthers] passed as true (**by default**), will also cancel all the others [Job]s
|
||||||
|
*
|
||||||
|
* @param scope Will be used to create [CoroutineScope.LinkedSupervisorScope] and launch joining of all [Job]s in it
|
||||||
|
*/
|
||||||
|
suspend fun joinFirst(
|
||||||
|
vararg jobs: Job,
|
||||||
|
scope: CoroutineScope,
|
||||||
|
cancelOthers: Boolean = true
|
||||||
|
): Job = jobs.toList().joinFirst(scope, cancelOthers)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trying to [Job.join] on all [jobs]. The first [Job] completed its work will interrupt all others joins
|
||||||
|
* and, if [cancelOthers] passed as true (**by default**), will also cancel all the others [Job]s
|
||||||
|
*
|
||||||
|
* Creates [CoroutineScope] using [coroutineContext] for internal purposes
|
||||||
|
*/
|
||||||
|
suspend fun joinFirst(
|
||||||
|
vararg jobs: Job,
|
||||||
|
cancelOthers: Boolean = true
|
||||||
|
): Job = joinFirst(*jobs, scope = CoroutineScope(coroutineContext), cancelOthers = cancelOthers)
|
Loading…
Reference in New Issue
Block a user