From 019c499b80ce09c3e65d98da22d3f923d4e257a9 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 10 Apr 2024 20:49:55 -0700 Subject: [PATCH] Convert findFaviconURLs to async await. --- Shared/Favicons/FaviconURLFinder.swift | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/Shared/Favicons/FaviconURLFinder.swift b/Shared/Favicons/FaviconURLFinder.swift index 213db70fe..c05769708 100644 --- a/Shared/Favicons/FaviconURLFinder.swift +++ b/Shared/Favicons/FaviconURLFinder.swift @@ -21,26 +21,21 @@ struct FaviconURLFinder { /// Finds favicon URLs in a web page. /// - Parameters: /// - homePageURL: The page to search. - /// - completion: A closure called when the links have been found. /// - urls: An array of favicon URLs as strings. - @MainActor static func findFaviconURLs(with homePageURL: String, _ completion: @escaping (_ urls: [String]?) -> Void) { + @MainActor static func findFaviconURLs(with homePageURL: String) async -> [String]? { guard let _ = URL(unicodeString: homePageURL) else { - completion(nil) - return + return nil } - Task { @MainActor in + // If the favicon has an explicit type, check that for an ignored type; otherwise, check the file extension. + let htmlMetadata = try? await HTMLMetadataDownloader.downloadMetadata(for: homePageURL) - // If the favicon has an explicit type, check that for an ignored type; otherwise, check the file extension. - let htmlMetadata = try? await HTMLMetadataDownloader.downloadMetadata(for: homePageURL) - - let faviconURLs = htmlMetadata?.favicons.compactMap { favicon -> String? in - shouldAllowFavicon(favicon) ? favicon.urlString : nil - } - - completion(faviconURLs) + let faviconURLs = htmlMetadata?.favicons.compactMap { favicon -> String? in + shouldAllowFavicon(favicon) ? favicon.urlString : nil } + + return faviconURLs } static func shouldAllowFavicon(_ favicon: RSHTMLMetadataFavicon) -> Bool {