Improve RequestBuilder.submitAsync() to ensure the coroutine continuation is resumed only once (#4436)

`RequestListener.onResourceReady()` may also be called to provide the
placeholder image when the request is starting or when it is cleared.

Check if the current request is complete (its status set to COMPLETE) to
determine if the Glide resource is for a thumbnail/placeholder or the
final image, and resume the coroutine only for the final image (or in
case of error).
This logic is [borrowed from the Glide Flow
API](a7351b0ecf/integration/ktx/src/main/java/com/bumptech/glide/integration/ktx/Flows.kt (L378)).
This commit is contained in:
Christophe Beyls 2024-05-12 11:25:49 +02:00 committed by GitHub
parent 11d18e1e70
commit f9221b3d75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 3 deletions

View File

@ -13,8 +13,8 @@ import kotlinx.coroutines.suspendCancellableCoroutine
* Allows waiting for a Glide request to complete without blocking a background thread.
*/
suspend fun <R> RequestBuilder<R>.submitAsync(
width: Int = Int.MIN_VALUE,
height: Int = Int.MIN_VALUE
width: Int = Target.SIZE_ORIGINAL,
height: Int = Target.SIZE_ORIGINAL
): R {
return suspendCancellableCoroutine { continuation ->
val target = addListener(
@ -36,7 +36,9 @@ suspend fun <R> RequestBuilder<R>.submitAsync(
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
continuation.resume(resource)
if (target?.request?.isComplete == true) {
continuation.resume(resource)
}
return false
}
}