Skip to content

Commit

Permalink
Remove unnecessary optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite committed Dec 21, 2024
1 parent 39ddd2a commit e2cda61
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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<Unit, Nothing> {
_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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -26,24 +25,6 @@ internal fun ForwardingUnconfinedCoroutineScope(
}
)

/**
* Calls [block] with a [ForwardingUnconfinedCoroutineDispatcher] replacing the current dispatcher.
*/
internal suspend inline fun <T> 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.
Expand Down

0 comments on commit e2cda61

Please sign in to comment.