Commit 766b02be authored by Geoffrey Métais's avatar Geoffrey Métais

Create custom application coroutine scope

parent fdc6fce2
package org.videolan.vlc.util
import android.os.Looper
import android.os.Process
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.Main
import kotlinx.coroutines.experimental.android.UI
import java.util.concurrent.ThreadFactory
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.Runnable
import kotlinx.coroutines.experimental.launch
import kotlin.coroutines.experimental.CoroutineContext
fun runBackground(runnable: Runnable) {
if (Looper.myLooper() != Looper.getMainLooper()) runnable.run()
else GlobalScope.launch(Dispatchers.Default) { runnable.run() }
else AppScope.launch(Dispatchers.Default) { runnable.run() }
}
fun runOnMainThread(runnable: Runnable) {
GlobalScope.launch(Dispatchers.Main.immediate) { runnable.run() }
AppScope.launch { runnable.run() }
}
fun runIO(runnable: Runnable) {
GlobalScope.launch(Dispatchers.IO) { runnable.run() }
AppScope.launch(Dispatchers.IO) { runnable.run() }
}
object AppScope : CoroutineScope {
/**
* @suppress **Deprecated**: Deprecated in favor of top-level extension property
*/
@Deprecated(level = DeprecationLevel.HIDDEN, message = "Deprecated in favor of top-level extension property")
override val isActive: Boolean
get() = true
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main.immediate
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment