diff --git a/coil-compose-core/src/commonMain/kotlin/coil3/compose/AsyncImagePainter.kt b/coil-compose-core/src/commonMain/kotlin/coil3/compose/AsyncImagePainter.kt index 16f974d0cf..072777142c 100644 --- a/coil-compose-core/src/commonMain/kotlin/coil3/compose/AsyncImagePainter.kt +++ b/coil-compose-core/src/commonMain/kotlin/coil3/compose/AsyncImagePainter.kt @@ -31,13 +31,13 @@ import coil3.compose.AsyncImagePainter.Companion.DefaultTransform import coil3.compose.AsyncImagePainter.Input import coil3.compose.AsyncImagePainter.State import coil3.compose.internal.AsyncImageState +import coil3.compose.internal.ForwardingUnconfinedCoroutineDispatcher import coil3.compose.internal.ForwardingUnconfinedCoroutineScope import coil3.compose.internal.dispatcher import coil3.compose.internal.onStateOf import coil3.compose.internal.requestOf import coil3.compose.internal.toScale import coil3.compose.internal.transformOf -import coil3.compose.internal.withForwardingUnconfinedCoroutineDispatcher import coil3.request.ErrorResult import coil3.request.ImageRequest import coil3.request.ImageResult @@ -56,6 +56,7 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.transformLatest +import kotlinx.coroutines.withContext /** * Return an [AsyncImagePainter] that executes an [ImageRequest] asynchronously and renders the result. @@ -215,13 +216,12 @@ class AsyncImagePainter internal constructor( override fun onRemembered() = trace("AsyncImagePainter.onRemembered") { (painter as? RememberObserver)?.onRemembered() - val originalDispatcher = scope.coroutineContext.dispatcher ?: Dispatchers.Unconfined - // Observe the latest request and execute any emissions. + val originalDispatcher = scope.coroutineContext.dispatcher ?: Dispatchers.Unconfined rememberJob = restartSignal .transformLatest { _input.collect { input -> - withForwardingUnconfinedCoroutineDispatcher(originalDispatcher) { + withContext(ForwardingUnconfinedCoroutineDispatcher(originalDispatcher)) { val previewHandler = previewHandler val state = if (previewHandler != null) { // If we're in inspection mode use the preview renderer. diff --git a/coil-compose-core/src/commonMain/kotlin/coil3/compose/internal/ForwardingUnconfinedCoroutineScope.kt b/coil-compose-core/src/commonMain/kotlin/coil3/compose/internal/ForwardingUnconfinedCoroutineScope.kt index bcde3c9bd7..ad53806d40 100644 --- a/coil-compose-core/src/commonMain/kotlin/coil3/compose/internal/ForwardingUnconfinedCoroutineScope.kt +++ b/coil-compose-core/src/commonMain/kotlin/coil3/compose/internal/ForwardingUnconfinedCoroutineScope.kt @@ -7,7 +7,6 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.InternalCoroutinesApi import kotlinx.coroutines.Runnable -import kotlinx.coroutines.withContext /** * A [CoroutineScope] with a special [CoroutineContext] that enables [ForwardingUnconfinedCoroutineDispatcher] @@ -26,24 +25,6 @@ internal fun ForwardingUnconfinedCoroutineScope( } ) -/** - * Calls [block] with a [ForwardingUnconfinedCoroutineDispatcher] replacing the current dispatcher. - */ -internal suspend inline fun withForwardingUnconfinedCoroutineDispatcher( - originalDispatcher: CoroutineDispatcher, - crossinline block: suspend CoroutineScope.() -> T -): T { - val unconfinedDispatcher = ForwardingUnconfinedCoroutineDispatcher(originalDispatcher) - return withContext(unconfinedDispatcher) { - try { - block() - } finally { - // Optimization to avoid dispatching when there's nothing left to do. - unconfinedDispatcher.unconfined = true - } - } -} - /** * A [CoroutineDispatcher] that delegates to [Dispatchers.Unconfined] while [unconfined] is true * and [delegate] when [unconfined] is false.