SHARE
TWEET

Untitled

a guest Sep 21st, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ein.core.coroutine
  2.  
  3. import kotlinx.coroutines.*
  4. import kotlin.coroutines.CoroutineContext
  5. import kotlin.coroutines.EmptyCoroutineContext
  6.  
  7. expect fun runBlock(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> Unit): Unit
  8.  
  9. //multiplatform coroutine runner util
  10. object eCo{
  11.     private val jobs = mutableSetOf<Job>()
  12.  
  13.     fun run(
  14.             context:CoroutineContext = Dispatchers.Default,
  15.             block: suspend CoroutineScope.() -> Unit
  16.     ) = runBlock(context, block)
  17.     fun await(
  18.             context:CoroutineContext = Dispatchers.Default,
  19.             start: CoroutineStart = CoroutineStart.DEFAULT,
  20.             block: suspend CoroutineScope.() -> Unit
  21.     ):Job{
  22.         val job = GlobalScope.launch(context, start){
  23.             async{block()}.await()
  24.         }
  25.         jobs += job
  26.         return job
  27.     }
  28.     fun cancel() = jobs.forEach {it.cancel()}
  29.     suspend fun wait() = jobs.forEach {it.join()}
  30.     suspend fun waitUntil(waitMillis:Long){
  31.         val job = GlobalScope.launch{
  32.             delay(waitMillis)
  33.             jobs.filter{it.isActive}.forEach{it.cancel()}
  34.         }
  35.         jobs.filter{it.isActive}.forEach{it.join()}
  36.         job.cancel()
  37.     }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top