several small improvements

This commit is contained in:
InsanusMokrassar 2021-05-24 20:26:48 +06:00
parent 9dd1848337
commit 00c23c73a8
3 changed files with 34 additions and 10 deletions

View File

@ -1,12 +1,23 @@
package dev.inmo.micro_utils.coroutines package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
suspend inline fun <T> doInUI(noinline block: suspend CoroutineScope.() -> T) = withContext( inline val UI
Dispatchers.Main, get() = Dispatchers.Main
inline val Default
get() = Dispatchers.Default
suspend inline fun <T> doIn(context: CoroutineContext, noinline block: suspend CoroutineScope.() -> T) = withContext(
context,
block block
) )
suspend inline fun <T> doInDefault(noinline block: suspend CoroutineScope.() -> T) = withContext(
Dispatchers.Default, suspend inline fun <T> doInUI(noinline block: suspend CoroutineScope.() -> T) = doIn(
UI,
block
)
suspend inline fun <T> doInDefault(noinline block: suspend CoroutineScope.() -> T) = doIn(
Default,
block block
) )

View File

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

View File

@ -2,7 +2,10 @@ package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.* import kotlinx.coroutines.*
suspend inline fun <T> doInIO(noinline block: suspend CoroutineScope.() -> T) = withContext( val IO
Dispatchers.IO, get() = Dispatchers.IO
suspend inline fun <T> doInIO(noinline block: suspend CoroutineScope.() -> T) = doIn(
IO,
block block
) )