coroutines updates

This commit is contained in:
InsanusMokrassar 2021-05-24 12:27:54 +06:00
parent 7b00a06f3e
commit 5853f7cc49
5 changed files with 68 additions and 22 deletions

View File

@ -2,6 +2,14 @@
## 0.5.3 ## 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 ## 0.5.2
* `Ktor`: * `Ktor`:

View File

@ -1,8 +1,6 @@
package dev.inmo.micro_utils.coroutines package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
suspend inline fun <T> doInUI(noinline block: suspend CoroutineScope.() -> T) = withContext( suspend inline fun <T> doInUI(noinline block: suspend CoroutineScope.() -> T) = withContext(
Dispatchers.Main, Dispatchers.Main,
@ -12,7 +10,3 @@ suspend inline fun <T> doInDefault(noinline block: suspend CoroutineScope.() ->
Dispatchers.Default, Dispatchers.Default,
block block
) )
suspend inline fun <T> doInIO(noinline block: suspend CoroutineScope.() -> T) = withContext(
Dispatchers.IO,
block
)

View File

@ -86,6 +86,9 @@ suspend fun <T> safelyWithContextExceptionHandler(
* * [CoroutineContext.get] with [SafelyExceptionHandlerKey] as key * * [CoroutineContext.get] with [SafelyExceptionHandlerKey] as key
* * [defaultSafelyExceptionHandler] * * [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 * @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this
* exception will be available for catching * exception will be available for catching
* *
@ -98,7 +101,7 @@ suspend inline fun <T> safely(
noinline block: suspend CoroutineScope.() -> T noinline block: suspend CoroutineScope.() -> T
): T { ): T {
return try { return try {
supervisorScope(block) withContext(SupervisorJob(), block)
} catch (e: Throwable) { } catch (e: Throwable) {
coroutineContext[ContextSafelyExceptionHandlerKey] ?.handler ?.invoke(e) coroutineContext[ContextSafelyExceptionHandlerKey] ?.handler ?.invoke(e)
onException(e) onException(e)
@ -106,23 +109,23 @@ suspend inline fun <T> safely(
} }
/** /**
* Shortcut for [safely] with exception handler, that as expected must return null in case of impossible creating of * Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and
* result from exception (instead of throwing it) * returning null at one time
*
* @see safelyWithoutExceptions
* @see launchSafelyWithoutExceptions
* @see asyncSafelyWithoutExceptions
*/ */
suspend inline fun <T> safelyWithoutExceptions( val defaultSafelyWithoutExceptionHandlerWithNull: ExceptionHandler<Nothing?> = {
noinline onException: ExceptionHandler<T?>, defaultSafelyWithoutExceptionHandler.invoke(it)
noinline block: suspend CoroutineScope.() -> T null
): T? = safely(onException, block) }
/** /**
* 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 <T> safelyWithoutExceptions( suspend inline fun <T> safelyWithoutExceptions(
noinline onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
noinline block: suspend CoroutineScope.() -> T noinline block: suspend CoroutineScope.() -> T
): T? = safelyWithoutExceptions( ): T? = safely(onException, block)
{
defaultSafelyWithoutExceptionHandler.invoke(it)
null
},
block
)

View File

@ -0,0 +1,31 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.*
inline fun CoroutineScope.launchSafely(
noinline onException: ExceptionHandler<Unit> = defaultSafelyExceptionHandler,
noinline block: suspend CoroutineScope.() -> Unit
) = launch {
safely(onException, block)
}
inline fun CoroutineScope.launchSafelyWithoutExceptions(
noinline onException: ExceptionHandler<Unit?> = defaultSafelyWithoutExceptionHandlerWithNull,
noinline block: suspend CoroutineScope.() -> Unit
) = launch {
safelyWithoutExceptions(onException, block)
}
inline fun <T> CoroutineScope.asyncSafely(
noinline onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,
noinline block: suspend CoroutineScope.() -> T
) = async {
safely(onException, block)
}
inline fun <T> CoroutineScope.asyncSafelyWithoutExceptions(
noinline onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
noinline block: suspend CoroutineScope.() -> T
) = async {
safelyWithoutExceptions(onException, block)
}

View File

@ -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 <T> doInIO(noinline block: suspend CoroutineScope.() -> T) = withContext(
Dispatchers.IO,
block
)