How to make a CoroutineContext.Element not to propagate via launch/async in Kotlin

Viewed 32

Is there a way to change how a custom CoroutineContext.Element is propagated through launch and async to satisfy the code below?

suspend fun CoroutingScope.foo(): Foo? = TODO()

fun test() = runBlocking {
  withContext(FooElement(foo1)) {
    assertSame(foo1, foo())
    coroutineScope {
      assertSame(foo1, foo())
    }
    launch {
      assertNull(foo())   
    }
    async {
      assertNull(foo())   
    }
  }
}

In my IntelliJ plugin, I'd like to marry ProgressIndicator with coroutines via a custom progressScope that injects a ProgressElement(val pi: ProgressIndicator) in coroutineContext, so if it's not a separate job created via launch or async, a nested progressScope uses pushState/popState against the parent ProgressIndicator instead of creating a new one.

So far, the only option I see is to replace launch and async with custom builders, but I'm not sure if I'll be able to enforce it in the codebase.

0 Answers
Related