From 51719b58689b14ba989a75b939a3a5eb123a0468 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 7 Jan 2021 10:08:39 +0600 Subject: [PATCH 1/2] start 0.4.16 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dda2413ccba..be686b28a3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.4.16 + ## 0.4.15 * `Coroutines`: diff --git a/gradle.properties b/gradle.properties index 59f4dad4376..b7fa50d32b8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -40,5 +40,5 @@ dokka_version=1.4.20 # Project data group=dev.inmo -version=0.4.15 -android_code_version=19 +version=0.4.16 +android_code_version=20 From 502a49644c74e0430e89b537efb4e9f40b14a61c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 7 Jan 2021 10:22:28 +0600 Subject: [PATCH 2/2] safelyWithoutException updates --- CHANGELOG.md | 6 ++++ .../micro_utils/coroutines/HandleSafely.kt | 30 +++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be686b28a3d..a295b3cd2c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.4.16 +* `Coroutines`: + * `safely`: + * New `safelyWithoutExceptions` function may accept `onException` parameter with nullable result + * Old `safelyWithoutExceptions` now using `defaultSafelyWithoutExceptionHandler` to handle exceptions "like in + `safely`", but it is expected that `defaultSafelyWithoutExceptionHandler` will not throw any exception + ## 0.4.15 * `Coroutines`: diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt index 423d4ad76e5..7b55ec37843 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt @@ -11,6 +11,17 @@ typealias ExceptionHandler = suspend (Throwable) -> T */ var defaultSafelyExceptionHandler: ExceptionHandler = { throw it } +/** + * This instance will be used in all calls of [safelyWithoutExceptions] as an exception handler for [safely] call + */ +var defaultSafelyWithoutExceptionHandler: ExceptionHandler = { + try { + defaultSafelyExceptionHandler(it) + } catch (e: Throwable) { + // do nothing + } +} + /** * Key for [SafelyExceptionHandler] which can be used in [CoroutineContext.get] to get current default * [SafelyExceptionHandler] @@ -123,8 +134,23 @@ suspend inline fun safely( } /** - * Shortcut for [safely] without exception handler (instead of this you will receive null as a result) + * Shortcut for [safely] with exception handler, that as expected must return null in case of impossible creating of + * result from exception (instead of throwing it) + */ +suspend inline fun safelyWithoutExceptions( + noinline onException: ExceptionHandler, + noinline block: suspend CoroutineScope.() -> T +): T? = safely(onException, block) + +/** + * Shortcut for [safely] without exception handler (instead of this you will always receive null as a result) */ suspend inline fun safelyWithoutExceptions( noinline block: suspend CoroutineScope.() -> T -): T? = safely({ null }, block) +): T? = safelyWithoutExceptions( + { + defaultSafelyWithoutExceptionHandler.invoke(it) + null + }, + block +)