Compare commits

...

16 Commits
0.5.3 ... 0.5.6

6 changed files with 39 additions and 22 deletions

View File

@@ -1,5 +1,24 @@
# Changelog # 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`
* `Ktor`: `1.5.4` -> `1.6.0`
## 0.5.4
* `Versions`:
* `Klock`: `2.1.0` -> `2.1.2`
## 0.5.3 ## 0.5.3
* `Versions`: * `Versions`:

View File

@@ -38,7 +38,7 @@ inline fun CoroutineScope.createSafeActionsActor(
suspend fun <T> Channel<suspend () -> Unit>.doWithSuspending( suspend fun <T> Channel<suspend () -> Unit>.doWithSuspending(
action: ActorAction<T> action: ActorAction<T>
) = suspendCoroutine<T> { ) = suspendCoroutine<T> {
offer { trySend {
safely({ e -> it.resumeWithException(e) }) { safely({ e -> it.resumeWithException(e) }) {
it.resume(action()) it.resume(action())
} }

View File

@@ -115,6 +115,10 @@ suspend inline fun <T> runCatchingSafely(
safely(onException, block) 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 * Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and
* returning null at one time * returning null at one time

View File

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

View File

@@ -10,11 +10,11 @@ org.gradle.jvmargs=-Xmx2g
kotlin_version=1.5.10 kotlin_version=1.5.10
kotlin_coroutines_version=1.5.0 kotlin_coroutines_version=1.5.0
kotlin_serialisation_core_version=1.2.1 kotlin_serialisation_core_version=1.2.1
kotlin_exposed_version=0.31.1 kotlin_exposed_version=0.32.1
ktor_version=1.5.4 ktor_version=1.6.0
klockVersion=2.1.0 klockVersion=2.1.2
github_release_plugin_version=2.2.12 github_release_plugin_version=2.2.12
@@ -45,5 +45,5 @@ dokka_version=1.4.32
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.5.3 version=0.5.6
android_code_version=44 android_code_version=47

View File

@@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.collect
import kotlinx.serialization.SerializationStrategy import kotlinx.serialization.SerializationStrategy
private suspend fun DefaultWebSocketSession.checkReceivedAndCloseIfExists() { private suspend fun DefaultWebSocketSession.checkReceivedAndCloseIfExists() {
if (incoming.poll() != null) { if (incoming.tryReceive() != null) {
close() close()
throw CorrectCloseException throw CorrectCloseException
} }