Last active
March 31, 2025 21:33
-
-
Save bklimt/240d76466ba33d20a9a5c95e687d96d3 to your computer and use it in GitHub Desktop.
Examples of CoroutineScope getting propagated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bklimt.kotlinapplication | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.activity.enableEdgeToEdge | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.layout.Arrangement | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.material3.Button | |
import androidx.compose.material3.Scaffold | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.rememberCoroutineScope | |
import androidx.compose.runtime.setValue | |
import com.bklimt.kotlinapplication.ui.theme.KotlinApplicationTheme | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.ThreadContextElement | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.runBlocking | |
import kotlinx.coroutines.suspendCancellableCoroutine | |
import kotlinx.coroutines.withContext | |
import kotlin.coroutines.AbstractCoroutineContextElement | |
import kotlin.coroutines.CoroutineContext | |
import kotlin.coroutines.resume | |
val THREAD_LOCAL_TEXT: ThreadLocal<String?> = ThreadLocal() | |
class BasicCoroutineContextElement(val text: String) : AbstractCoroutineContextElement(Key) { | |
companion object Key : CoroutineContext.Key<BasicCoroutineContextElement> | |
} | |
class ThreadyCoroutineContextElement(private val text: String) : ThreadContextElement<String> { | |
companion object Key : CoroutineContext.Key<ThreadyCoroutineContextElement> | |
override val key: CoroutineContext.Key<ThreadyCoroutineContextElement> | |
get() = Key | |
override fun updateThreadContext(context: CoroutineContext): String { | |
THREAD_LOCAL_TEXT.set(text) | |
return text | |
} | |
override fun restoreThreadContext(context: CoroutineContext, oldState: String) { | |
THREAD_LOCAL_TEXT.set(null) | |
} | |
} | |
fun getThreadyText(): String? { | |
return THREAD_LOCAL_TEXT.get() | |
} | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
enableEdgeToEdge() | |
setContent { | |
KotlinApplicationTheme { | |
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> | |
Playground( | |
modifier = Modifier | |
.fillMaxSize() | |
.padding(innerPadding) | |
) | |
} | |
} | |
} | |
} | |
} | |
suspend fun doBasicStuff(): String { | |
return suspendCancellableCoroutine { continuation -> | |
val text = continuation.context[BasicCoroutineContextElement]?.text | |
continuation.resume(text ?: "(null)") | |
} | |
} | |
fun doThreadyStuff(): String { | |
return getThreadyText() ?: "(null)" | |
} | |
suspend fun doThreadyCoroutineStuff(): String { | |
return suspendCancellableCoroutine { continuation -> | |
val text = getThreadyText() | |
continuation.resume(text ?: "(null)") | |
} | |
} | |
@Composable | |
fun Playground(modifier: Modifier = Modifier) { | |
val scope = rememberCoroutineScope() | |
Column( | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally, | |
modifier = modifier | |
.background(Color.LightGray) | |
) { | |
var blockingText by remember { mutableStateOf("") } | |
var blockingThreadyText by remember { mutableStateOf("") } | |
var basicText by remember { mutableStateOf("") } | |
var threadyText by remember { mutableStateOf("") } | |
var threadyCoroutineText by remember { mutableStateOf("") } | |
Text( | |
text = "basic: $blockingText", | |
modifier = Modifier | |
) | |
Text( | |
text = "basic: $blockingThreadyText", | |
modifier = Modifier | |
) | |
Text( | |
text = "basic: $basicText", | |
modifier = Modifier | |
) | |
Text( | |
text = "thready: $threadyText", | |
modifier = Modifier | |
) | |
Text( | |
text = "thready coroutine: $threadyCoroutineText", | |
modifier = Modifier | |
) | |
Button(onClick = { | |
runBlocking( | |
BasicCoroutineContextElement("ganbare!") + | |
ThreadyCoroutineContextElement("頑張れ!") | |
) { | |
blockingText = doBasicStuff() | |
blockingThreadyText = doThreadyStuff() | |
} | |
scope.launch { | |
withContext( | |
Dispatchers.IO + | |
BasicCoroutineContextElement("hello!") + | |
ThreadyCoroutineContextElement("world!") | |
) { | |
launch(Dispatchers.Main) { | |
basicText = doBasicStuff() | |
threadyText = doThreadyStuff() | |
threadyCoroutineText = doThreadyCoroutineStuff() | |
} | |
} | |
} | |
}) { | |
Text( | |
text = "Do stuff", | |
modifier = Modifier | |
) | |
} | |
} | |
} | |
@Preview(showBackground = true) | |
@Composable | |
fun GreetingPreview() { | |
KotlinApplicationTheme { | |
Playground() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment