2023-01-28 04:31:12 +01:00
|
|
|
package tachiyomi.core.util.lang
|
2017-10-13 00:12:29 +02:00
|
|
|
|
2020-02-27 00:12:44 +01:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.CoroutineStart
|
2022-08-23 16:16:52 +02:00
|
|
|
import kotlinx.coroutines.DelicateCoroutinesApi
|
2020-02-27 00:12:44 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
import kotlinx.coroutines.Job
|
2022-09-02 17:50:44 +02:00
|
|
|
import kotlinx.coroutines.NonCancellable
|
2020-02-27 00:12:44 +01:00
|
|
|
import kotlinx.coroutines.launch
|
2021-01-09 00:05:51 +01:00
|
|
|
import kotlinx.coroutines.withContext
|
2017-10-13 00:12:29 +02:00
|
|
|
|
2022-08-23 16:16:52 +02:00
|
|
|
/**
|
|
|
|
* Think twice before using this. This is a delicate API. It is easy to accidentally create resource or memory leaks when GlobalScope is used.
|
|
|
|
*
|
|
|
|
* **Possible replacements**
|
|
|
|
* - suspend function
|
|
|
|
* - custom scope like view or presenter scope
|
|
|
|
*/
|
|
|
|
@DelicateCoroutinesApi
|
2017-10-13 00:12:29 +02:00
|
|
|
fun launchUI(block: suspend CoroutineScope.() -> Unit): Job =
|
2020-04-25 20:24:45 +02:00
|
|
|
GlobalScope.launch(Dispatchers.Main, CoroutineStart.DEFAULT, block)
|
2017-10-13 00:12:29 +02:00
|
|
|
|
2022-08-23 16:16:52 +02:00
|
|
|
/**
|
|
|
|
* Think twice before using this. This is a delicate API. It is easy to accidentally create resource or memory leaks when GlobalScope is used.
|
|
|
|
*
|
|
|
|
* **Possible replacements**
|
|
|
|
* - suspend function
|
|
|
|
* - custom scope like view or presenter scope
|
|
|
|
*/
|
|
|
|
@DelicateCoroutinesApi
|
2020-02-04 01:11:18 +01:00
|
|
|
fun launchIO(block: suspend CoroutineScope.() -> Unit): Job =
|
2020-04-25 20:24:45 +02:00
|
|
|
GlobalScope.launch(Dispatchers.IO, CoroutineStart.DEFAULT, block)
|
2020-02-04 01:11:18 +01:00
|
|
|
|
2022-08-23 16:16:52 +02:00
|
|
|
/**
|
|
|
|
* Think twice before using this. This is a delicate API. It is easy to accidentally create resource or memory leaks when GlobalScope is used.
|
|
|
|
*
|
|
|
|
* **Possible replacements**
|
|
|
|
* - suspend function
|
|
|
|
* - custom scope like view or presenter scope
|
|
|
|
*/
|
|
|
|
@DelicateCoroutinesApi
|
2017-10-13 00:12:29 +02:00
|
|
|
fun launchNow(block: suspend CoroutineScope.() -> Unit): Job =
|
2020-04-25 20:24:45 +02:00
|
|
|
GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED, block)
|
2021-01-09 00:05:51 +01:00
|
|
|
|
|
|
|
fun CoroutineScope.launchUI(block: suspend CoroutineScope.() -> Unit): Job =
|
|
|
|
launch(Dispatchers.Main, block = block)
|
|
|
|
|
|
|
|
fun CoroutineScope.launchIO(block: suspend CoroutineScope.() -> Unit): Job =
|
|
|
|
launch(Dispatchers.IO, block = block)
|
|
|
|
|
2022-09-24 16:14:16 +02:00
|
|
|
fun CoroutineScope.launchNonCancellable(block: suspend CoroutineScope.() -> Unit): Job =
|
2022-09-02 17:50:44 +02:00
|
|
|
launchIO { withContext(NonCancellable, block) }
|
|
|
|
|
2023-09-02 05:02:18 +02:00
|
|
|
suspend fun <T> withUIContext(block: suspend CoroutineScope.() -> T) = withContext(
|
|
|
|
Dispatchers.Main,
|
|
|
|
block,
|
|
|
|
)
|
2021-01-09 00:05:51 +01:00
|
|
|
|
2023-09-02 05:02:18 +02:00
|
|
|
suspend fun <T> withIOContext(block: suspend CoroutineScope.() -> T) = withContext(
|
|
|
|
Dispatchers.IO,
|
|
|
|
block,
|
|
|
|
)
|
2022-09-24 16:14:16 +02:00
|
|
|
|
|
|
|
suspend fun <T> withNonCancellableContext(block: suspend CoroutineScope.() -> T) =
|
|
|
|
withContext(NonCancellable, block)
|