From 14f570bbf1fd233564d09ae035b8ebc4c72ee47a Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 28 Oct 2026 11:13:02 -0700 Subject: [PATCH] Start a waiting image download immediately when article view needs the image. --- Modules/Images/Sources/Images/DownloadLimiter.swift | 10 ++++++++++ .../Images/Sources/Images/DownloadedImageCache.swift | 9 +++++++-- .../Tests/ImagesTests/DownloadLimiterTests.swift | 12 ++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Modules/Images/Sources/Images/DownloadLimiter.swift b/Modules/Images/Sources/Images/DownloadLimiter.swift index 70095fb7b..4a1cc7fa5 100644 --- a/Modules/Images/Sources/Images/DownloadLimiter.swift +++ b/Modules/Images/Sources/Images/DownloadLimiter.swift @@ -83,4 +83,14 @@ public enum DownloadPriority: Sendable { let waitingDownload = waiting.remove(at: index) waiting.append(WaitingDownload(key: key, priority: .visible, continuation: waitingDownload.continuation)) } + + /// Starts a waiting download now, even when that goes over the limit. + func startNow(key: String) { + guard let index = waiting.lastIndex(where: { $0.key == key }) else { + return + } + let waitingDownload = waiting.remove(at: index) + runningCount += 1 + waitingDownload.continuation.resume(returning: true) + } } diff --git a/Modules/Images/Sources/Images/DownloadedImageCache.swift b/Modules/Images/Sources/Images/DownloadedImageCache.swift index 32e0d8261..9342f95e1 100644 --- a/Modules/Images/Sources/Images/DownloadedImageCache.swift +++ b/Modules/Images/Sources/Images/DownloadedImageCache.swift @@ -74,12 +74,17 @@ import ActivityLog /// The stored copy, read from disk or downloaded. With a `downloadPriority`, a download /// waits its turn in the download limiter, and doesn’t start when offline or in Low Data Mode. - /// Without one, it starts right away. + /// Without one, it starts right away, as does a waiting download for the same URL. /// Activity log fires only when the request reaches the network. func imageData(for url: String, downloadPriority: DownloadPriority?, checksForUpdates: Bool, activityOwner: ActivityOwner?, activityKind: ActivityKind?, activityDetail: String?) async throws(ImageDownloadError) -> Data { if let task = tasks[url] { - if downloadPriority == .visible { + switch downloadPriority { + case .visible: prioritizeDownload(for: url) + case nil: + downloadLimiter.startNow(key: url) + case .prefetch: + break } return try await task.value.get() } diff --git a/Modules/Images/Tests/ImagesTests/DownloadLimiterTests.swift b/Modules/Images/Tests/ImagesTests/DownloadLimiterTests.swift index a9c0102de..9df6052ec 100644 --- a/Modules/Images/Tests/ImagesTests/DownloadLimiterTests.swift +++ b/Modules/Images/Tests/ImagesTests/DownloadLimiterTests.swift @@ -52,4 +52,16 @@ import Testing limiter.downloadDidFinish() #expect(await secondPrefetch.value) } + + @Test func articleViewImageStartsWithoutWaitingForASlot() async { + let limiter = DownloadLimiter(maximumConcurrentDownloads: 1, maximumWaitingDownloads: 10) + #expect(await limiter.waitForTurn(key: "running", priority: .visible)) + + let articleImage = Task { await limiter.waitForTurn(key: "articleImage", priority: .prefetch) } + await Task.yield() + + // Starts while the running download still holds the only slot. + limiter.startNow(key: "articleImage") + #expect(await articleImage.value) + } }