Skip to content

Commit bafbf41

Browse files
committed
Ignore rather than crash on duplicate requests in GlideModifier
1 parent 651d796 commit bafbf41

File tree

1 file changed

+14
-8
lines changed
  • integration/compose/src/main/java/com/bumptech/glide/integration/compose

1 file changed

+14
-8
lines changed

integration/compose/src/main/java/com/bumptech/glide/integration/compose/GlideModifier.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import com.bumptech.glide.integration.ktx.Status
5555
import com.bumptech.glide.integration.ktx.flowResolvable
5656
import com.bumptech.glide.internalModel
5757
import com.bumptech.glide.load.DataSource
58-
import com.bumptech.glide.util.Preconditions
5958
import kotlinx.coroutines.CoroutineScope
6059
import kotlinx.coroutines.Dispatchers
6160
import kotlinx.coroutines.Job
@@ -395,18 +394,25 @@ internal class GlideNode : DrawModifierNode, LayoutModifierNode, SemanticsModifi
395394

396395
@OptIn(ExperimentGlideFlows::class, InternalGlideApi::class, ExperimentalComposeUiApi::class)
397396
private fun launchRequest(requestBuilder: RequestBuilder<Drawable>) =
398-
// Launch via sideEffect because onAttach is called before onNewRequest and onNewRequest is not
399-
// always called. That means in onAttach if we launch the request, we might restart an old
400-
// request only to have it immediately replaced by a new request, causing jank. Or if we don't
401-
// launch the new requests in onAttach, then onNewRequest might not be called and we won't show
402-
// the old image.
403-
// sideEffect is called after all changes in the tree, so we can always queue a new request, but
397+
// Launch via sideEffect because onAttach is called before onNewRequest and onNewRequest is not
398+
// always called. That means in onAttach if we launch the request, we might restart an old
399+
// request only to have it immediately replaced by a new request, causing jank. Or if we don't
400+
// launch the new requests in onAttach, then onNewRequest might not be called and we won't show
401+
// the old image.
402+
// sideEffect is called after all changes in the tree, so we can always queue a new request, but
404403
// drop any for old requests by comparing requests builders.
405404
sideEffect {
405+
// The request changed while our sideEffect was queued, which should also have triggered
406+
// another sideEffect. Wait for that one instead.
406407
if (this.requestBuilder != requestBuilder) {
407408
return@sideEffect
408409
}
409-
Preconditions.checkArgument(currentJob == null)
410+
// We've raced with another sideEffect, our previous check means that the request hasn't
411+
// changed and this check means we're already loading that image, so we have nothing useful to
412+
// to do.
413+
if (currentJob != null) {
414+
return@sideEffect
415+
}
410416
currentJob = (coroutineScope + Dispatchers.Main.immediate).launch {
411417
placeholder = null
412418
placeholderPositionAndSize = null

0 commit comments

Comments
 (0)