Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ein.core.coroutine
- import kotlinx.coroutines.*
- import kotlin.coroutines.CoroutineContext
- import kotlin.coroutines.EmptyCoroutineContext
- expect fun runBlock(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> Unit): Unit
- //multiplatform coroutine runner util
- object eCo{
- private val jobs = mutableSetOf<Job>()
- fun run(
- context:CoroutineContext = Dispatchers.Default,
- block: suspend CoroutineScope.() -> Unit
- ) = runBlock(context, block)
- fun await(
- context:CoroutineContext = Dispatchers.Default,
- start: CoroutineStart = CoroutineStart.DEFAULT,
- block: suspend CoroutineScope.() -> Unit
- ):Job{
- val job = GlobalScope.launch(context, start){
- async{block()}.await()
- }
- jobs += job
- return job
- }
- fun cancel() = jobs.forEach {it.cancel()}
- suspend fun wait() = jobs.forEach {it.join()}
- suspend fun waitUntil(waitMillis:Long){
- val job = GlobalScope.launch{
- delay(waitMillis)
- jobs.filter{it.isActive}.forEach{it.cancel()}
- }
- jobs.filter{it.isActive}.forEach{it.join()}
- job.cancel()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement