improve launchSynchronously

This commit is contained in:
InsanusMokrassar 2021-06-05 15:16:07 +06:00
parent 1f466747f0
commit 623e0cd369
3 changed files with 18 additions and 15 deletions

View File

@ -4,6 +4,9 @@
* `Versions`
* `Exposed`: `0.31.1` -> `0.32.1`
* `Coroutines`
* `JVM`
* `launchSynchronously` and subsequent functions got improved mechanism
## 0.5.5

View File

@ -115,6 +115,12 @@ suspend inline fun <T> runCatchingSafely(
safely(onException, block)
}
suspend inline fun <T> safelyWithResult(
noinline block: suspend CoroutineScope.() -> T
): Result<T> = runCatching {
safely({ throw it }, block)
}
/**
* Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and
* returning null at one time

View File

@ -3,27 +3,21 @@ package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.*
fun <T> CoroutineScope.launchSynchronously(block: suspend CoroutineScope.() -> T): T {
val deferred = CompletableDeferred<T>()
val objectToSynchronize = java.lang.Object()
val launchCallback = {
var result: Result<T>? = null
val objectToSynchronize = Object()
synchronized(objectToSynchronize) {
launch {
safely(
{
deferred.completeExceptionally(it)
}
) {
deferred.complete(block())
}
result = safelyWithResult(block)
}.invokeOnCompletion {
synchronized(objectToSynchronize) {
objectToSynchronize.notifyAll()
}
}
while (result == null) {
objectToSynchronize.wait()
}
}
synchronized(objectToSynchronize) {
launchCallback()
objectToSynchronize.wait()
}
return deferred.getCompleted()
return result!!.getOrThrow()
}
fun <T> launchSynchronously(block: suspend CoroutineScope.() -> T): T = CoroutineScope(Dispatchers.Default).launchSynchronously(block)