
ru.hnau.jutils.coroutines.CoroutinesExtensions.kt Maven / Gradle / Ivy
The newest version!
package ru.hnau.jutils.coroutines
import kotlinx.coroutines.*
import ru.hnau.jutils.TimeValue
import ru.hnau.jutils.possible.Possible
import kotlin.coroutines.CoroutineContext
fun CoroutineContext.async(
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T
) =
GlobalScope.async(this, start, block)
fun CoroutineContext.launch(
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
) =
GlobalScope.launch(this, start, block)
suspend fun delay(timeValue: TimeValue) =
delay(timeValue.milliseconds)
@Throws(CancellationException::class)
suspend fun withTimeout(timeValue: TimeValue, block: suspend CoroutineScope.() -> T) =
withTimeout(timeValue.milliseconds, block)
suspend fun withTimeoutOrNull(timeValue: TimeValue, block: suspend CoroutineScope.() -> T) =
withTimeoutOrNull(timeValue.milliseconds, block)
suspend fun withTimeoutOrPossibleError(timeValue: TimeValue, block: suspend CoroutineScope.() -> T) =
Possible.trySuccessCatchError { withTimeout(timeValue, block) }
suspend fun withTimeoutOrError(timeValue: TimeValue, block: suspend CoroutineScope.() -> Possible) =
Possible.trySuccessCatchErrorPossible { withTimeout(timeValue, block) }
fun T.toDeferred(): Deferred =
CompletableDeferred(this)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy