From 0a626b1904ede0096331bef486051bb081306321 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 10 Apr 2024 19:03:23 -0700 Subject: [PATCH] Add async await version of downloadUsingCache. --- Web/Sources/Web/OneShotDownload.swift | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Web/Sources/Web/OneShotDownload.swift b/Web/Sources/Web/OneShotDownload.swift index 6bbe0d329..6afb1625c 100755 --- a/Web/Sources/Web/OneShotDownload.swift +++ b/Web/Sources/Web/OneShotDownload.swift @@ -183,6 +183,28 @@ private struct CallbackRecord { } } +public struct DownloadData: Sendable { + + public let data: Data? + public let response: URLResponse? +} + +@MainActor public func downloadUsingCache(_ url: URL) async throws -> DownloadData { + + precondition(Thread.isMainThread) + + return try await withCheckedThrowingContinuation { continuation in + downloadUsingCache(url) { data, response, error in + if let error { + continuation.resume(throwing: error) + } else { + let downloadData = DownloadData(data: data, response: response) + continuation.resume(returning: downloadData) + } + } + } +} + @MainActor public func downloadUsingCache(_ url: URL, _ completion: @escaping OneShotDownloadCallback) { precondition(Thread.isMainThread) DownloadWithCacheManager.shared.download(url, completion)