Start a waiting image download immediately when article view needs the image.

This commit is contained in:
Brent Simmons committed 2026-10-28 11:13:02 -07:00
1 parent 48cc714ce1
commit 14f570bbf1
3 files changed
+29 -2

No files matched your search

@@ -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)
}
}
@@ -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()
}
@@ -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)
}
}