mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-06-03 00:15:15 +00:00
27 lines
948 B
Kotlin
27 lines
948 B
Kotlin
package dev.inmo.micro_utils.coroutines
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
fun <T> CoroutineScope.launchSynchronously(block: suspend CoroutineScope.() -> T): T {
|
|
var result: Result<T>? = null
|
|
val objectToSynchronize = Object()
|
|
synchronized(objectToSynchronize) {
|
|
launch {
|
|
result = safelyWithResult(block)
|
|
}.invokeOnCompletion {
|
|
synchronized(objectToSynchronize) {
|
|
objectToSynchronize.notifyAll()
|
|
}
|
|
}
|
|
while (result == null) {
|
|
objectToSynchronize.wait()
|
|
}
|
|
}
|
|
return result!!.getOrThrow()
|
|
}
|
|
|
|
fun <T> launchSynchronously(block: suspend CoroutineScope.() -> T): T = CoroutineScope(Dispatchers.Default).launchSynchronously(block)
|
|
|
|
fun <T> CoroutineScope.doSynchronously(block: suspend CoroutineScope.() -> T): T = launchSynchronously(block)
|
|
fun <T> doSynchronously(block: suspend CoroutineScope.() -> T): T = launchSynchronously(block)
|