IJPL-196231 Extract GlobalScope.launch in uiScope.kt

To get rid of the context duplication before we modify it.

Reviewed in IJ-CR-168711 for 252 and release.

(cherry picked from commit 02107333ea28d825af9550c9a70634e087414342)

GitOrigin-RevId: 5ab9e46d71271e4dd854deaa5ac4e5fd01092acd
This commit is contained in:
Sergei Tachenov
2025-07-09 15:08:04 +03:00
committed by intellij-monorepo-bot
parent 270af06e6c
commit c95451c5d7

View File

@@ -58,8 +58,7 @@ fun <C : Component> C.launchOnceOnShow(
ThreadingAssertions.assertEventDispatchThread()
val component = this
@OptIn(DelicateCoroutinesApi::class)
return GlobalScope.launch(Dispatchers.Unconfined + CoroutineName(debugName)) {
return launchUnconfined(debugName) {
var started = false
showingAsChannel(component) { channel ->
while (!started) {
@@ -128,8 +127,7 @@ fun <C : Component> C.launchOnShow(
ThreadingAssertions.assertEventDispatchThread()
val component = this
@OptIn(DelicateCoroutinesApi::class)
return GlobalScope.launch(Dispatchers.Unconfined + CoroutineName(debugName)) {
return launchUnconfined(debugName) {
showingAsChannel(component) { channel ->
supervisorScope {
// Poor man's [distinctUntilChanged] + [collectLatest]
@@ -153,6 +151,17 @@ fun <C : Component> C.launchOnShow(
}
}
/**
* Launches the given task in the global scope without dispatching.
*/
private fun launchUnconfined(debugName: String, block: suspend CoroutineScope.() -> Unit): Job {
@OptIn(DelicateCoroutinesApi::class)
return GlobalScope.launch(
context = Dispatchers.Unconfined + CoroutineName(debugName),
block = block,
)
}
private suspend fun showingAsChannel(component: Component, block: suspend (ReceiveChannel<Boolean>) -> Unit) {
val channel = Channel<Boolean>(capacity = Int.MAX_VALUE) // don't skip events
try {