diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt index 61da575b59a..d1587c784a1 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DoInContext.kt @@ -1,12 +1,23 @@ package dev.inmo.micro_utils.coroutines import kotlinx.coroutines.* +import kotlin.coroutines.CoroutineContext -suspend inline fun doInUI(noinline block: suspend CoroutineScope.() -> T) = withContext( - Dispatchers.Main, +inline val UI + get() = Dispatchers.Main +inline val Default + get() = Dispatchers.Default + +suspend inline fun doIn(context: CoroutineContext, noinline block: suspend CoroutineScope.() -> T) = withContext( + context, block ) -suspend inline fun doInDefault(noinline block: suspend CoroutineScope.() -> T) = withContext( - Dispatchers.Default, + +suspend inline fun doInUI(noinline block: suspend CoroutineScope.() -> T) = doIn( + UI, + block +) +suspend inline fun doInDefault(noinline block: suspend CoroutineScope.() -> T) = doIn( + Default, block ) 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 e5d6b48a7e9..ff4850d5463 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 @@ -1,31 +1,41 @@ package dev.inmo.micro_utils.coroutines import kotlinx.coroutines.* +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext inline fun CoroutineScope.launchSafely( + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, noinline onException: ExceptionHandler = defaultSafelyExceptionHandler, noinline block: suspend CoroutineScope.() -> Unit -) = launch { +) = launch(context, start) { safely(onException, block) } inline fun CoroutineScope.launchSafelyWithoutExceptions( + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, noinline onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, noinline block: suspend CoroutineScope.() -> Unit -) = launch { +) = launch(context, start) { safelyWithoutExceptions(onException, block) } inline fun CoroutineScope.asyncSafely( + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, noinline onException: ExceptionHandler = defaultSafelyExceptionHandler, noinline block: suspend CoroutineScope.() -> T -) = async { +) = async(context, start) { safely(onException, block) } inline fun CoroutineScope.asyncSafelyWithoutExceptions( + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, noinline onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, noinline block: suspend CoroutineScope.() -> T -) = async { +) = async(context, start) { safelyWithoutExceptions(onException, block) } diff --git a/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt b/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt index f76bca82e2f..fc5f79a7b06 100644 --- a/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt +++ b/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/DoInIO.kt @@ -2,7 +2,10 @@ package dev.inmo.micro_utils.coroutines import kotlinx.coroutines.* -suspend inline fun doInIO(noinline block: suspend CoroutineScope.() -> T) = withContext( - Dispatchers.IO, +val IO + get() = Dispatchers.IO + +suspend inline fun doInIO(noinline block: suspend CoroutineScope.() -> T) = doIn( + IO, block )