get back old function to break less API

This commit is contained in:
InsanusMokrassar 2024-06-19 20:03:14 +06:00
parent ab58478686
commit ec98029467
2 changed files with 36 additions and 8 deletions

View File

@ -29,6 +29,13 @@ suspend inline fun <T> runCatchingSafely(
} }
} }
suspend inline fun <T, R> R.runCatchingSafely(
onException: ExceptionHandler<T>,
block: suspend R.() -> T
): Result<T> = runCatchingSafely<T>(onException) {
block()
}
/** /**
* Launching [runCatchingSafely] with [defaultSafelyExceptionHandler] as `onException` parameter * Launching [runCatchingSafely] with [defaultSafelyExceptionHandler] as `onException` parameter
*/ */
@ -36,6 +43,12 @@ suspend inline fun <T> runCatchingSafely(
block: suspend () -> T block: suspend () -> T
): Result<T> = runCatchingSafely(defaultSafelyExceptionHandler, block) ): Result<T> = runCatchingSafely(defaultSafelyExceptionHandler, block)
suspend inline fun <T, R> R.runCatchingSafely(
block: suspend R.() -> T
): Result<T> = runCatchingSafely<T> {
block()
}
//suspend inline fun <T, R> T.runCatchingSafely( //suspend inline fun <T, R> T.runCatchingSafely(
// onException: ExceptionHandler<R>, // onException: ExceptionHandler<R>,
// block: suspend T.() -> R // block: suspend T.() -> R
@ -94,11 +107,18 @@ suspend inline fun <T> safely(
suspend inline fun <T> safely( suspend inline fun <T> safely(
block: suspend () -> T block: suspend () -> T
): T = safely(defaultSafelyExceptionHandler, block) ): T = safely(defaultSafelyExceptionHandler, block)
suspend inline fun <T, R> R.safely(
block: suspend R.() -> T
): T = safely<T> { block() }
@Deprecated("Renamed", ReplaceWith("runCatchingSafely(block)", "dev.inmo.micro_utils.coroutines.runCatchingSafely")) @Deprecated("Renamed", ReplaceWith("runCatchingSafely(block)", "dev.inmo.micro_utils.coroutines.runCatchingSafely"))
suspend fun <T> safelyWithResult( suspend fun <T> safelyWithResult(
block: suspend () -> T block: suspend () -> T
): Result<T> = runCatchingSafely(defaultSafelyExceptionHandler, block) ): Result<T> = runCatchingSafely(defaultSafelyExceptionHandler, block)
@Deprecated("Renamed", ReplaceWith("this.runCatchingSafely(block)", "dev.inmo.micro_utils.coroutines.runCatchingSafely"))
suspend fun <T, R> R.safelyWithResult(
block: suspend R.() -> T
): Result<T> = safelyWithResult<T> { block() }
/** /**
* Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and * Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and

View File

@ -8,34 +8,42 @@ fun CoroutineScope.launchSafely(
context: CoroutineContext = EmptyCoroutineContext, context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT, start: CoroutineStart = CoroutineStart.DEFAULT,
onException: ExceptionHandler<Unit> = defaultSafelyExceptionHandler, onException: ExceptionHandler<Unit> = defaultSafelyExceptionHandler,
block: suspend () -> Unit block: suspend CoroutineScope.() -> Unit
) = launch(context, start) { ) = launch(context, start) {
runCatchingSafely(onException, block = block) runCatchingSafely(onException) {
block()
}
} }
fun CoroutineScope.launchSafelyWithoutExceptions( fun CoroutineScope.launchSafelyWithoutExceptions(
context: CoroutineContext = EmptyCoroutineContext, context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT, start: CoroutineStart = CoroutineStart.DEFAULT,
onException: ExceptionHandler<Unit?> = defaultSafelyWithoutExceptionHandlerWithNull, onException: ExceptionHandler<Unit?> = defaultSafelyWithoutExceptionHandlerWithNull,
block: suspend () -> Unit block: suspend CoroutineScope.() -> Unit
) = launch(context, start) { ) = launch(context, start) {
runCatchingSafelyWithoutExceptions(onException, block = block) runCatchingSafelyWithoutExceptions(onException) {
block()
}
} }
fun <T> CoroutineScope.asyncSafely( fun <T> CoroutineScope.asyncSafely(
context: CoroutineContext = EmptyCoroutineContext, context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT, start: CoroutineStart = CoroutineStart.DEFAULT,
onException: ExceptionHandler<T> = defaultSafelyExceptionHandler, onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,
block: suspend () -> T block: suspend CoroutineScope.() -> T
) = async(context, start) { ) = async(context, start) {
runCatchingSafely(onException, block = block) runCatchingSafely(onException) {
block()
}
} }
fun <T> CoroutineScope.asyncSafelyWithoutExceptions( fun <T> CoroutineScope.asyncSafelyWithoutExceptions(
context: CoroutineContext = EmptyCoroutineContext, context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT, start: CoroutineStart = CoroutineStart.DEFAULT,
onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull, onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
block: suspend () -> T block: suspend CoroutineScope.() -> T
) = async(context, start) { ) = async(context, start) {
runCatchingSafelyWithoutExceptions(onException, block = block) runCatchingSafelyWithoutExceptions(onException) {
block()
}
} }