```class ProduceStateScopeImpl<T>( state...
# compose
c
Copy code
class ProduceStateScopeImpl<T>(
    state: MutableState<T>,
    override val coroutineContext: CoroutineContext
) : ProduceStateScope<T>, MutableState<T> by state {

    override suspend fun awaitDispose(onDispose: () -> Unit): Nothing {
        try {
            suspendCancellableCoroutine<Nothing> { }
        } finally {
            onDispose()
        }
    }
}
Why
suspendCancellableCoroutine<Nothing> { }
is being executed?
z
Presumably because something is calling
awaitDispose
?
Also this question has nothing to do with compose, did you mean to post in #coroutines ?
a
It's a class from compose-runtime ๐Ÿ™‚
It's the same basic pattern from `channelFlow`'s
awaitClose
c
Thanks both for the answers
I will try to find out more about this, because it isn't clear to me
z
Can you elaborate on your question a bit? Why would you expect
suspendCancellableCoroutine
not to be executed?
c
when i saw the empty block i thought that nothing occurs
but of course the cancellation occurs anyway
Copy code
public suspend inline fun <T> suspendCancellableCoroutine(
    crossinline block: (CancellableContinuation<T>) -> Unit
): T =
    suspendCoroutineUninterceptedOrReturn { uCont ->
        val cancellable = CancellableContinuationImpl(uCont.intercepted(), resumeMode = MODE_CANCELLABLE)
        /*
         * For non-atomic cancellation we setup parent-child relationship immediately
         * in case when `block` blocks the current thread (e.g. Rx2 with trampoline scheduler), but
         * properly supports cancellation.
         */
        cancellable.initCancellability()
        block(cancellable)
        cancellable.getResult()
    }
I was expecting a
coroutineContext.cancel ()
or something like that ๐Ÿ˜…
a
Ah, yeah that happens when the underlying LaunchedEffect restarts for a new key or leaves the composition ๐Ÿ™‚
๐Ÿ‘ 1
c
there are a new function in the kotlinx.coroutines a little more idiomatic,
awaitCancellation()
Copy code
/**
 * Suspends until cancellation, in which case it will throw a [CancellationException].
 *
 * This function returns [Nothing], so it can be used in any coroutine,
 * regardless of the required return type.
 */
public suspend fun awaitCancellation(): Nothing = suspendCancellableCoroutine {}
is the same implementation, no surprises ๐Ÿ˜†
a
Yep, we added the
awaitDispose
scoped method before
awaitCancellation
came in ๐Ÿ™‚
๐Ÿ‘ 1