mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-18 06:40:07 +00:00
commit
3252b61abe
@ -1,5 +1,14 @@
|
||||
# Changelog
|
||||
|
||||
## 0.5.6
|
||||
|
||||
* `Versions`
|
||||
* `Exposed`: `0.31.1` -> `0.32.1`
|
||||
* `Coroutines`
|
||||
* `JVM`
|
||||
* `launchSynchronously` and subsequent functions got improved mechanism
|
||||
* New method `safelyWithResult`
|
||||
|
||||
## 0.5.5
|
||||
|
||||
* `Versions`
|
||||
|
@ -115,6 +115,10 @@ suspend inline fun <T> runCatchingSafely(
|
||||
safely(onException, block)
|
||||
}
|
||||
|
||||
suspend inline fun <T> safelyWithResult(
|
||||
noinline block: suspend CoroutineScope.() -> T
|
||||
): Result<T> = runCatchingSafely(defaultSafelyExceptionHandler, block)
|
||||
|
||||
/**
|
||||
* Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and
|
||||
* returning null at one time
|
||||
|
@ -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)
|
||||
|
@ -10,7 +10,7 @@ org.gradle.jvmargs=-Xmx2g
|
||||
kotlin_version=1.5.10
|
||||
kotlin_coroutines_version=1.5.0
|
||||
kotlin_serialisation_core_version=1.2.1
|
||||
kotlin_exposed_version=0.31.1
|
||||
kotlin_exposed_version=0.32.1
|
||||
|
||||
ktor_version=1.6.0
|
||||
|
||||
@ -45,5 +45,5 @@ dokka_version=1.4.32
|
||||
# Project data
|
||||
|
||||
group=dev.inmo
|
||||
version=0.5.5
|
||||
android_code_version=46
|
||||
version=0.5.6
|
||||
android_code_version=47
|
||||
|
Loading…
Reference in New Issue
Block a user