diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fe543a821c..2cb04dda479 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.16.1 + +* `Coroutines`: + * New `runCatchingSafely`/`safelyWithResult` with receivers +* `SafeWrapper`: + * Module inited + ## 0.16.0 * `Versions`: 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 bc662e79086..c8ab319ea48 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 @@ -115,10 +115,21 @@ suspend inline fun runCatchingSafely( safely(onException, block) } +suspend inline fun T.runCatchingSafely( + noinline onException: ExceptionHandler = defaultSafelyExceptionHandler, + noinline block: suspend T.() -> R +): Result = runCatching { + safely(onException) { block() } +} + suspend inline fun safelyWithResult( noinline block: suspend CoroutineScope.() -> T ): Result = runCatchingSafely(defaultSafelyExceptionHandler, block) +suspend inline fun T.safelyWithResult( + noinline block: suspend T.() -> R +): Result = runCatchingSafely(defaultSafelyExceptionHandler, block) + /** * Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and * returning null at one time diff --git a/gradle.properties b/gradle.properties index 1eee1b76667..c382506e62f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,5 +14,5 @@ crypto_js_version=4.1.1 # Project data group=dev.inmo -version=0.16.0 -android_code_version=168 +version=0.16.1 +android_code_version=169 diff --git a/safe_wrapper/build.gradle b/safe_wrapper/build.gradle new file mode 100644 index 00000000000..854f51c5dfb --- /dev/null +++ b/safe_wrapper/build.gradle @@ -0,0 +1,17 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" + id "com.android.library" +} + +apply from: "$mppProjectWithSerializationPresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + api project(":micro_utils.coroutines") + } + } + } +} diff --git a/safe_wrapper/src/commonMain/kotlin/SafeWrapper.kt b/safe_wrapper/src/commonMain/kotlin/SafeWrapper.kt new file mode 100644 index 00000000000..6426ba6703e --- /dev/null +++ b/safe_wrapper/src/commonMain/kotlin/SafeWrapper.kt @@ -0,0 +1,17 @@ +package dev.inmo.micro_utils.safe_wrapper + +import dev.inmo.micro_utils.coroutines.runCatchingSafely + +interface SafeWrapper { + fun safe(block: T.() -> R): Result = unsafeTarget().runCatching(block) + fun unsafe(block: T.() -> R): R = unsafeTarget().block() + suspend fun safeS(block: suspend T.() -> R): Result = unsafeTarget().runCatchingSafely(block = block) + suspend fun unsafeS(block: suspend T.() -> R): R = unsafeTarget().block() + fun unsafeTarget(): T + + class Default(private val t: T) : SafeWrapper { override fun unsafeTarget(): T = t } + + companion object { + operator fun invoke(t: T) = Default(t) + } +} diff --git a/safe_wrapper/src/main/AndroidManifest.xml b/safe_wrapper/src/main/AndroidManifest.xml new file mode 100644 index 00000000000..86e29b98d1c --- /dev/null +++ b/safe_wrapper/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + diff --git a/settings.gradle b/settings.gradle index 069bd208c00..76ab4756747 100644 --- a/settings.gradle +++ b/settings.gradle @@ -4,6 +4,7 @@ String[] includes = [ ":common", ":common:compose", ":matrix", + ":safe_wrapper", ":crypto", ":koin", ":selector:common",