Skip activity reporting when reading image from disk.

This commit is contained in:
Brent Simmons committed 2026-05-28 10:35:48 -07:00
1 parent abdee458e3
commit 055b2f7076
3 files changed
+42 -38

No files matched your search

+5 -14
View File
@@ -46,19 +46,13 @@ extension Notification.Name {
return cachedImage
}
if let imageData = imageDownloader.image(for: avatarURL) {
// `ImageDownloader.image(for:)` handles the activity log itself — it
// fires the activity only if the fetch goes to network. Disk-cached
// fetches stay silent.
let kind = ActivityKind.downloadAvatar(avatarURL: avatarURL)
if let imageData = imageDownloader.image(for: avatarURL, activityOwner: .avatarDownloader, activityKind: kind) {
scaleAndCacheImageData(imageData, avatarURL)
} else {
// Multiple authors can share the same avatar URL. Only produce the
// activity once per URL — the imageDidBecomeAvailable notification
// will complete the single in-flight activity.
if !waitingForAvatarURLs.contains(avatarURL) {
let activityLog = ActivityLog.shared
let kind = ActivityKind.downloadAvatar(avatarURL: avatarURL)
activityLog.createActivity(owner: .avatarDownloader, kind: kind)
activityLog.didStart(.avatarDownloader, kind: kind)
}
waitingForAvatarURLs.insert(avatarURL)
}
@@ -85,9 +79,6 @@ extension Notification.Name {
return
}
scaleAndCacheImageData(imageData, avatarURL)
let kind = ActivityKind.downloadAvatar(avatarURL: avatarURL)
ActivityLog.shared.didComplete(.avatarDownloader, kind: kind)
}
}
+6 -18
View File
@@ -140,10 +140,6 @@ extension Notification.Name {
return
}
waitingForFeedURLs[url] = nil
let kind = ActivityKind.downloadFeedImage(feedURL: url)
ActivityLog.shared.didComplete(.feedImageDownloader, kind: kind)
_ = icon(for: feed)
}
@@ -222,24 +218,16 @@ private extension FeedIconDownloader {
let url = Self.sanitizedIconURL(url)
// If the ImageDownloader already has it in memory, process inline and
// skip activity logging — no fetch is happening.
if let imageData = imageDownloader.image(for: url) {
// `ImageDownloader.image(for:)` handles the activity log itself — it
// fires the activity only if the fetch goes to network. Disk-cached
// fetches stay silent.
let kind = ActivityKind.downloadFeedImage(feedURL: url)
if let imageData = imageDownloader.image(for: url, activityOwner: .feedImageDownloader, activityKind: kind, activityDetail: feed.nameForDisplay) {
RSImage.image(with: imageData, imageResultBlock: imageResultBlock)
return
}
// Data not in memory — `imageDownloader.image(for:)` has dispatched an
// async findImage (disk or network). Completion arrives via
// imageDidBecomeAvailable. Multiple feeds can share the same icon URL;
// only produce one activity per URL.
if waitingForFeedURLs[url] == nil {
let kind = ActivityKind.downloadFeedImage(feedURL: url)
let activityLog = ActivityLog.shared
activityLog.createActivity(owner: .feedImageDownloader, kind: kind, detail: feed.nameForDisplay)
activityLog.didStart(.feedImageDownloader, kind: kind)
}
// Async fetch dispatched — completion arrives via imageDidBecomeAvailable.
waitingForFeedURLs[url] = feed
imageResultBlock(nil)
}
+31 -6
View File
@@ -10,6 +10,7 @@ import Foundation
import os
import RSCore
import RSWeb
import ActivityLog
extension Notification.Name {
static let imageDidBecomeAvailable = Notification.Name("ImageDidBecomeAvailableNotification") // UserInfoKey.url
@@ -43,15 +44,22 @@ extension Notification.Name {
imageCache.removeAll()
}
/// Returns the image data if it's already in the in-memory cache. Otherwise
/// dispatches an async fetch — disk first, then network — and returns nil;
/// completion arrives via `.imageDidBecomeAvailable`.
///
/// Pass `activityOwner` / `activityKind` (and optionally `activityDetail`)
/// to have an entry written to the activity log if and only if the fetch
/// goes to network. Disk-only fetches stay silent.
@discardableResult
func image(for url: String) -> Data? {
func image(for url: String, activityOwner: ActivityOwner? = nil, activityKind: ActivityKind? = nil, activityDetail: String? = nil) -> Data? {
assert(Thread.isMainThread)
if let data = imageCache[url] {
return data
}
Task { @MainActor in
await findImage(url)
await findImage(url, activityOwner: activityOwner, activityKind: activityKind, activityDetail: activityDetail)
}
return nil
@@ -66,7 +74,7 @@ private extension ImageDownloader {
postImageDidBecomeAvailableNotification(url)
}
func findImage(_ url: String) async {
func findImage(_ url: String, activityOwner: ActivityOwner?, activityKind: ActivityKind?, activityDetail: String?) async {
guard !urlsInProgress.contains(url) && !badURLs.contains(url) else {
return
}
@@ -78,10 +86,27 @@ private extension ImageDownloader {
return
}
if let image = await downloadImage(url) {
cacheImage(url, image)
urlsInProgress.remove(url)
// Disk missed — going to network. Produce activity if the caller asked.
let activityLog = ActivityLog.shared
if let activityOwner, let activityKind {
activityLog.createActivity(owner: activityOwner, kind: activityKind, detail: activityDetail)
activityLog.didStart(activityOwner, kind: activityKind)
}
let image = await downloadImage(url)
if let activityOwner, let activityKind {
if image != nil {
activityLog.didComplete(activityOwner, kind: activityKind)
} else {
let error = NSError(domain: "ImageDownloader", code: 0, userInfo: [NSLocalizedDescriptionKey: "Download failed"])
activityLog.didFail(activityOwner, kind: activityKind, error: error)
}
}
if let image {
cacheImage(url, image)
}
urlsInProgress.remove(url)
}
func readFromDisk(url: String) async -> Data? {