mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-23 02:28:47 +00:00
several small improvements
This commit is contained in:
parent
9dd1848337
commit
00c23c73a8
@ -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
|
||||||
)
|
)
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user