CoroutineScope with safely handler parameter

This commit is contained in:
InsanusMokrassar 2021-09-04 14:46:12 +06:00
parent bd9b0d16ab
commit eed7cfdc42
2 changed files with 12 additions and 1 deletions

View File

@ -5,6 +5,8 @@
* `Versions`: * `Versions`:
* `Coroutines`: `1.5.1` -> `1.5.2` * `Coroutines`: `1.5.1` -> `1.5.2`
* `Klock`: `2.3.4` -> `2.4.1` * `Klock`: `2.3.4` -> `2.4.1`
* `Coroutines`:
* New function `CoroutineScope` with safely exceptions handler as second parameter
## 0.5.23 ## 0.5.23

View File

@ -39,9 +39,11 @@ object ContextSafelyExceptionHandlerKey : CoroutineContext.Key<ContextSafelyExce
*/ */
class ContextSafelyExceptionHandler( class ContextSafelyExceptionHandler(
val handler: ExceptionHandler<Unit> val handler: ExceptionHandler<Unit>
) : CoroutineContext.Element { ) : CoroutineContext.Element, CoroutineExceptionHandler {
override val key: CoroutineContext.Key<*> override val key: CoroutineContext.Key<*>
get() = ContextSafelyExceptionHandlerKey get() = ContextSafelyExceptionHandlerKey
override fun handleException(context: CoroutineContext, exception: Throwable) = handler(exception)
} }
/** /**
@ -147,3 +149,10 @@ suspend inline fun <T> runCatchingSafelyWithoutExceptions(
): Result<T?> = runCatching { ): Result<T?> = runCatching {
safelyWithoutExceptions(onException, block) safelyWithoutExceptions(onException, block)
} }
suspend inline fun CoroutineScope(
context: CoroutineContext,
defaultExceptionsHandler: ExceptionHandler<Unit>
) = CoroutineScope(
context + ContextSafelyExceptionHandler(defaultExceptionsHandler)
)