From 5853f7cc493f78e6e1d5561745400cee381d9303 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 24 May 2021 12:27:54 +0600 Subject: [PATCH] coroutines updates --- CHANGELOG.md | 8 +++++ .../micro_utils/coroutines/DoInContext.kt} | 8 +---- .../micro_utils/coroutines/HandleSafely.kt | 33 ++++++++++--------- .../micro_utils/coroutines/LaunchSafely.kt | 31 +++++++++++++++++ .../dev/inmo/micro_utils/coroutines/DoInIO.kt | 10 ++++++ 5 files changed, 68 insertions(+), 22 deletions(-) rename coroutines/src/{main/kotlin/dev/inmo/micro_utils/coroutines/DoInUI.kt => commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt} (55%) create mode 100644 coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSafely.kt create mode 100644 coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 31de6800a1a..678a2d02e7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## 0.5.3 +* `Coroutines`: + * Extensions `doInUI` and `doInDefault` were replaced in common and available on any supported platform + * Extension `doInIO` replaced into `jvm` and available on any `JVM` platform + * Old extension `safelyWithouException` without `onException` has been replaced by its copy with `onException` and + default value + * New value `defaultSafelyWithoutExceptionHandlerWithNull` which is used in all `*WithoutExceptions` by default + * Analogs of `launch` and `async` for `safely` and `safelyWithoutExceptions` were added + ## 0.5.2 * `Ktor`: diff --git a/coroutines/src/main/kotlin/dev/inmo/micro_utils/coroutines/DoInUI.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt similarity index 55% rename from coroutines/src/main/kotlin/dev/inmo/micro_utils/coroutines/DoInUI.kt rename to coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt index ab23e3f87d7..61da575b59a 100644 --- a/coroutines/src/main/kotlin/dev/inmo/micro_utils/coroutines/DoInUI.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt @@ -1,8 +1,6 @@ package dev.inmo.micro_utils.coroutines -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext +import kotlinx.coroutines.* suspend inline fun doInUI(noinline block: suspend CoroutineScope.() -> T) = withContext( Dispatchers.Main, @@ -12,7 +10,3 @@ suspend inline fun doInDefault(noinline block: suspend CoroutineScope.() -> Dispatchers.Default, block ) -suspend inline fun doInIO(noinline block: suspend CoroutineScope.() -> T) = withContext( - Dispatchers.IO, - block -) 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 d0bad07fc90..84dedadb58f 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 @@ -86,6 +86,9 @@ suspend fun safelyWithContextExceptionHandler( * * [CoroutineContext.get] with [SafelyExceptionHandlerKey] as key * * [defaultSafelyExceptionHandler] * + * Remember, that [ExceptionHandler] from [CoroutineContext.get] will be used anyway if it is available. After it will + * be called [onException] + * * @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this * exception will be available for catching * @@ -98,7 +101,7 @@ suspend inline fun safely( noinline block: suspend CoroutineScope.() -> T ): T { return try { - supervisorScope(block) + withContext(SupervisorJob(), block) } catch (e: Throwable) { coroutineContext[ContextSafelyExceptionHandlerKey] ?.handler ?.invoke(e) onException(e) @@ -106,23 +109,23 @@ suspend inline fun safely( } /** - * 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) + * Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and + * returning null at one time + * + * @see safelyWithoutExceptions + * @see launchSafelyWithoutExceptions + * @see asyncSafelyWithoutExceptions */ -suspend inline fun safelyWithoutExceptions( - noinline onException: ExceptionHandler, - noinline block: suspend CoroutineScope.() -> T -): T? = safely(onException, block) +val defaultSafelyWithoutExceptionHandlerWithNull: ExceptionHandler = { + defaultSafelyWithoutExceptionHandler.invoke(it) + null +} /** - * Shortcut for [safely] without exception handler (instead of this you will always 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, by default always returns null) */ suspend inline fun safelyWithoutExceptions( + noinline onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, noinline block: suspend CoroutineScope.() -> T -): T? = safelyWithoutExceptions( - { - defaultSafelyWithoutExceptionHandler.invoke(it) - null - }, - block -) +): T? = safely(onException, block) diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSafely.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSafely.kt new file mode 100644 index 00000000000..e5d6b48a7e9 --- /dev/null +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSafely.kt @@ -0,0 +1,31 @@ +package dev.inmo.micro_utils.coroutines + +import kotlinx.coroutines.* + +inline fun CoroutineScope.launchSafely( + noinline onException: ExceptionHandler = defaultSafelyExceptionHandler, + noinline block: suspend CoroutineScope.() -> Unit +) = launch { + safely(onException, block) +} + +inline fun CoroutineScope.launchSafelyWithoutExceptions( + noinline onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, + noinline block: suspend CoroutineScope.() -> Unit +) = launch { + safelyWithoutExceptions(onException, block) +} + +inline fun CoroutineScope.asyncSafely( + noinline onException: ExceptionHandler = defaultSafelyExceptionHandler, + noinline block: suspend CoroutineScope.() -> T +) = async { + safely(onException, block) +} + +inline fun CoroutineScope.asyncSafelyWithoutExceptions( + noinline onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, + noinline block: suspend CoroutineScope.() -> T +) = async { + safelyWithoutExceptions(onException, block) +} diff --git a/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt b/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt new file mode 100644 index 00000000000..c4d6fc4d2eb --- /dev/null +++ b/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt @@ -0,0 +1,10 @@ +package dev.inmo.micro_utils.coroutines + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +suspend inline fun doInIO(noinline block: suspend CoroutineScope.() -> T) = withContext( + Dispatchers.IO, + block +)