From 7b00a06f3e345368679baf2dac05c6e996e28c89 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 23 May 2021 00:03:43 +0600 Subject: [PATCH 1/9] start 0.5.3 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a05cb100c8e..31de6800a1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.5.3 + ## 0.5.2 * `Ktor`: diff --git a/gradle.properties b/gradle.properties index 423a73fbded..dd3b55293cf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -45,5 +45,5 @@ dokka_version=1.4.32 # Project data group=dev.inmo -version=0.5.2 -android_code_version=43 +version=0.5.3 +android_code_version=44 From 5853f7cc493f78e6e1d5561745400cee381d9303 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 24 May 2021 12:27:54 +0600 Subject: [PATCH 2/9] 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 +) From 9b30efd9a2f0759fe7639e623d9ffa3ed6c07bca Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 24 May 2021 12:29:31 +0600 Subject: [PATCH 3/9] optimize imports --- .../jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt | 4 +--- .../dev/inmo/micro_utils/coroutines/DoWithFirstTests.kt | 5 ++--- 2 files changed, 3 insertions(+), 6 deletions(-) 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 index c4d6fc4d2eb..f76bca82e2f 100644 --- a/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt +++ b/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.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 doInIO(noinline block: suspend CoroutineScope.() -> T) = withContext( Dispatchers.IO, diff --git a/coroutines/src/jvmTest/kotlin/dev/inmo/micro_utils/coroutines/DoWithFirstTests.kt b/coroutines/src/jvmTest/kotlin/dev/inmo/micro_utils/coroutines/DoWithFirstTests.kt index 22e611770ac..4d9e318824c 100644 --- a/coroutines/src/jvmTest/kotlin/dev/inmo/micro_utils/coroutines/DoWithFirstTests.kt +++ b/coroutines/src/jvmTest/kotlin/dev/inmo/micro_utils/coroutines/DoWithFirstTests.kt @@ -1,9 +1,8 @@ package dev.inmo.micro_utils.coroutines -import dev.inmo.micro_utils.coroutines.asDeferred -import dev.inmo.micro_utils.coroutines.launchSynchronously import kotlinx.coroutines.* -import kotlin.test.* +import kotlin.test.Test +import kotlin.test.assertEquals class DoWithFirstTests { @Test From 9dd18483372391c3a0e6e6ab18368ea893404594 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 24 May 2021 12:31:32 +0600 Subject: [PATCH 4/9] note about safely logic --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 678a2d02e7f..38529067b90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 0.5.3 * `Coroutines`: + * **Mechanism of `safely` has been changed: instead of creating new scope now is used `withContext`** * 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 From 00c23c73a8f0fdcc4430b7e33eb23412fb248338 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 24 May 2021 20:26:48 +0600 Subject: [PATCH 5/9] several small improvements --- .../micro_utils/coroutines/DoInContext.kt | 19 +++++++++++++++---- .../micro_utils/coroutines/LaunchSafely.kt | 18 ++++++++++++++---- .../dev/inmo/micro_utils/coroutines/DoInIO.kt | 7 +++++-- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt index 61da575b59a..d1587c784a1 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt @@ -1,12 +1,23 @@ package dev.inmo.micro_utils.coroutines import kotlinx.coroutines.* +import kotlin.coroutines.CoroutineContext -suspend inline fun doInUI(noinline block: suspend CoroutineScope.() -> T) = withContext( - Dispatchers.Main, +inline val UI + get() = Dispatchers.Main +inline val Default + get() = Dispatchers.Default + +suspend inline fun doIn(context: CoroutineContext, noinline block: suspend CoroutineScope.() -> T) = withContext( + context, block ) -suspend inline fun doInDefault(noinline block: suspend CoroutineScope.() -> T) = withContext( - Dispatchers.Default, + +suspend inline fun doInUI(noinline block: suspend CoroutineScope.() -> T) = doIn( + UI, + block +) +suspend inline fun doInDefault(noinline block: suspend CoroutineScope.() -> T) = doIn( + Default, 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 index e5d6b48a7e9..ff4850d5463 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSafely.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSafely.kt @@ -1,31 +1,41 @@ package dev.inmo.micro_utils.coroutines import kotlinx.coroutines.* +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext inline fun CoroutineScope.launchSafely( + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, noinline onException: ExceptionHandler = defaultSafelyExceptionHandler, noinline block: suspend CoroutineScope.() -> Unit -) = launch { +) = launch(context, start) { safely(onException, block) } inline fun CoroutineScope.launchSafelyWithoutExceptions( + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, noinline onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, noinline block: suspend CoroutineScope.() -> Unit -) = launch { +) = launch(context, start) { safelyWithoutExceptions(onException, block) } inline fun CoroutineScope.asyncSafely( + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, noinline onException: ExceptionHandler = defaultSafelyExceptionHandler, noinline block: suspend CoroutineScope.() -> T -) = async { +) = async(context, start) { safely(onException, block) } inline fun CoroutineScope.asyncSafelyWithoutExceptions( + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, noinline onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, noinline block: suspend CoroutineScope.() -> T -) = async { +) = async(context, start) { 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 index f76bca82e2f..fc5f79a7b06 100644 --- a/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt +++ b/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt @@ -2,7 +2,10 @@ package dev.inmo.micro_utils.coroutines import kotlinx.coroutines.* -suspend inline fun doInIO(noinline block: suspend CoroutineScope.() -> T) = withContext( - Dispatchers.IO, +val IO + get() = Dispatchers.IO + +suspend inline fun doInIO(noinline block: suspend CoroutineScope.() -> T) = doIn( + IO, block ) From 976ce056c1733882eb80989920f5a30cfd27e90d Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 25 May 2021 00:18:46 +0600 Subject: [PATCH 6/9] runCatching for safely and safelyWithoutExceptions --- CHANGELOG.md | 1 + .../inmo/micro_utils/coroutines/HandleSafely.kt | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38529067b90..31a461020f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ default value * New value `defaultSafelyWithoutExceptionHandlerWithNull` which is used in all `*WithoutExceptions` by default * Analogs of `launch` and `async` for `safely` and `safelyWithoutExceptions` were added + * Analogs of `runCatching` for `safely` and `safelyWithoutExceptions` were added ## 0.5.2 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 84dedadb58f..2472837ef50 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 @@ -108,6 +108,13 @@ suspend inline fun safely( } } +suspend inline fun runCatchingSafely( + noinline onException: ExceptionHandler = defaultSafelyExceptionHandler, + noinline block: suspend CoroutineScope.() -> T +): Result = runCatching { + safely(onException, block) +} + /** * Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and * returning null at one time @@ -129,3 +136,10 @@ suspend inline fun safelyWithoutExceptions( noinline onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, noinline block: suspend CoroutineScope.() -> T ): T? = safely(onException, block) + +suspend inline fun runCatchingSafelyWithoutExceptions( + noinline onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, + noinline block: suspend CoroutineScope.() -> T +): Result = runCatching { + safelyWithoutExceptions(onException, block) +} From 041be5a1d1ccff697ca3bd626fe522051fa36f73 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 25 May 2021 16:08:07 +0600 Subject: [PATCH 7/9] Update gradle.properties --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index dd3b55293cf..2a97f11ba4e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,7 +7,7 @@ android.useAndroidX=true android.enableJetifier=true org.gradle.jvmargs=-Xmx2g -kotlin_version=1.5.0 +kotlin_version=1.5.10 kotlin_coroutines_version=1.5.0 kotlin_serialisation_core_version=1.2.1 kotlin_exposed_version=0.31.1 From 242f4b02d047514a312e26a9db6789653cc27be4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 26 May 2021 17:59:40 +0600 Subject: [PATCH 8/9] return previous logic of work with safely --- CHANGELOG.md | 1 - coroutines/build.gradle | 2 +- .../kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31a461020f8..9208bfebe22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,6 @@ ## 0.5.3 * `Coroutines`: - * **Mechanism of `safely` has been changed: instead of creating new scope now is used `withContext`** * 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 diff --git a/coroutines/build.gradle b/coroutines/build.gradle index 1db1b17ee79..840c8f52822 100644 --- a/coroutines/build.gradle +++ b/coroutines/build.gradle @@ -19,4 +19,4 @@ kotlin { } } } -} \ No newline at end of file +} 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 2472837ef50..f84e6cdb8e1 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 @@ -101,7 +101,7 @@ suspend inline fun safely( noinline block: suspend CoroutineScope.() -> T ): T { return try { - withContext(SupervisorJob(), block) + supervisorScope(block) } catch (e: Throwable) { coroutineContext[ContextSafelyExceptionHandlerKey] ?.handler ?.invoke(e) onException(e) From e78e984943b10f1be4705f86b5771d0605e9fcd5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 26 May 2021 18:00:24 +0600 Subject: [PATCH 9/9] fillup changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9208bfebe22..f5e3fa4d824 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.5.3 +* `Versions`: + * `Kotlin`: `1.5.0` -> `1.5.10` * `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