From 5e53a1bee9d98f1e5bbd4f2485880f2e4b7d61ea Mon Sep 17 00:00:00 2001 From: Colin White Date: Mon, 23 Dec 2024 16:38:43 -0500 Subject: [PATCH] Don't pay for dispatch. --- .../kotlin/coil3/compose/AsyncImagePainter.kt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) 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 072777142c..48367e5006 100644 --- a/coil-compose-core/src/commonMain/kotlin/coil3/compose/AsyncImagePainter.kt +++ b/coil-compose-core/src/commonMain/kotlin/coil3/compose/AsyncImagePainter.kt @@ -46,6 +46,7 @@ import coil3.size.Precision import coil3.size.SizeResolver import kotlin.coroutines.EmptyCoroutineContext import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Job @@ -54,8 +55,9 @@ import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow -import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.transformLatest +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext /** @@ -218,8 +220,9 @@ class AsyncImagePainter internal constructor( // Observe the latest request and execute any emissions. val originalDispatcher = scope.coroutineContext.dispatcher ?: Dispatchers.Unconfined - rememberJob = restartSignal - .transformLatest { + val scope = ForwardingUnconfinedCoroutineScope(scope.coroutineContext) + rememberJob = scope.launch(Dispatchers.Unconfined, CoroutineStart.UNDISPATCHED) { + restartSignal.transformLatest { _input.collect { input -> withContext(ForwardingUnconfinedCoroutineDispatcher(originalDispatcher)) { val previewHandler = previewHandler @@ -235,8 +238,8 @@ class AsyncImagePainter internal constructor( updateState(state) } } - } - .launchIn(ForwardingUnconfinedCoroutineScope(scope.coroutineContext + Dispatchers.Unconfined)) + }.collect() + } } override fun onForgotten() {