From ec9802946779794bedcdec5bab2649e19f2886ea Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 19 Jun 2024 20:03:14 +0600 Subject: [PATCH] get back old function to break less API --- .../micro_utils/coroutines/HandleSafely.kt | 20 ++++++++++++++++ .../micro_utils/coroutines/LaunchSafely.kt | 24 ++++++++++++------- 2 files changed, 36 insertions(+), 8 deletions(-) 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 bf104ceaf6c..ce84e2c71fe 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 @@ -29,6 +29,13 @@ suspend inline fun runCatchingSafely( } } +suspend inline fun R.runCatchingSafely( + onException: ExceptionHandler, + block: suspend R.() -> T +): Result = runCatchingSafely(onException) { + block() +} + /** * Launching [runCatchingSafely] with [defaultSafelyExceptionHandler] as `onException` parameter */ @@ -36,6 +43,12 @@ suspend inline fun runCatchingSafely( block: suspend () -> T ): Result = runCatchingSafely(defaultSafelyExceptionHandler, block) +suspend inline fun R.runCatchingSafely( + block: suspend R.() -> T +): Result = runCatchingSafely { + block() +} + //suspend inline fun T.runCatchingSafely( // onException: ExceptionHandler, // block: suspend T.() -> R @@ -94,11 +107,18 @@ suspend inline fun safely( suspend inline fun safely( block: suspend () -> T ): T = safely(defaultSafelyExceptionHandler, block) +suspend inline fun R.safely( + block: suspend R.() -> T +): T = safely { block() } @Deprecated("Renamed", ReplaceWith("runCatchingSafely(block)", "dev.inmo.micro_utils.coroutines.runCatchingSafely")) suspend fun safelyWithResult( block: suspend () -> T ): Result = runCatchingSafely(defaultSafelyExceptionHandler, block) +@Deprecated("Renamed", ReplaceWith("this.runCatchingSafely(block)", "dev.inmo.micro_utils.coroutines.runCatchingSafely")) +suspend fun R.safelyWithResult( + block: suspend R.() -> T +): Result = safelyWithResult { block() } /** * Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and 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 5e7c6380027..13583d2c622 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 @@ -8,34 +8,42 @@ fun CoroutineScope.launchSafely( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, onException: ExceptionHandler = defaultSafelyExceptionHandler, - block: suspend () -> Unit + block: suspend CoroutineScope.() -> Unit ) = launch(context, start) { - runCatchingSafely(onException, block = block) + runCatchingSafely(onException) { + block() + } } fun CoroutineScope.launchSafelyWithoutExceptions( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, - block: suspend () -> Unit + block: suspend CoroutineScope.() -> Unit ) = launch(context, start) { - runCatchingSafelyWithoutExceptions(onException, block = block) + runCatchingSafelyWithoutExceptions(onException) { + block() + } } fun CoroutineScope.asyncSafely( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, onException: ExceptionHandler = defaultSafelyExceptionHandler, - block: suspend () -> T + block: suspend CoroutineScope.() -> T ) = async(context, start) { - runCatchingSafely(onException, block = block) + runCatchingSafely(onException) { + block() + } } fun CoroutineScope.asyncSafelyWithoutExceptions( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, - block: suspend () -> T + block: suspend CoroutineScope.() -> T ) = async(context, start) { - runCatchingSafelyWithoutExceptions(onException, block = block) + runCatchingSafelyWithoutExceptions(onException) { + block() + } }