@@ -9,43 +9,60 @@ import CryptoKit
99import Foundation
1010import DevLogData
1111
12- actor WebPageImageStoreImpl : WebPageImageStore {
12+ final class WebPageImageStoreImpl : WebPageImageStore {
13+ private let queue = DispatchQueue (
14+ label: " devlog.web-page-image-store " ,
15+ qos: . utility
16+ )
17+
1318 func cachedImageURL( for url: URL ) async throws -> URL {
14- return try await Task . detached ( priority : . utility ) {
15- return try Self . cachedImageURL ( for: url)
16- } . value
19+ return try await perform {
20+ try Self . cachedImageURL ( for: url)
21+ }
1722 }
1823
1924 func saveImage( _ data: Data , for url: URL ) async throws -> URL {
20- return try await Task . detached ( priority : . utility ) {
21- return try Self . saveImage ( data, for: url)
22- } . value
25+ return try await perform {
26+ try Self . saveImage ( data, for: url)
27+ }
2328 }
2429
2530 func dirSizeInBytes( ) async -> Int64 {
2631 do {
27- return try await Task . detached ( priority : . utility ) {
28- return try Self . dirSizeInBytes ( )
29- } . value
32+ return try await perform {
33+ try Self . dirSizeInBytes ( )
34+ }
3035 } catch {
3136 return 0
3237 }
3338 }
3439
3540 func clearDirectory( ) async throws {
36- try await Task . detached ( priority : . utility ) {
41+ try await perform {
3742 try Self . clearDirectory ( )
38- } . value
43+ }
3944 }
4045
4146 func removeImage( for url: URL ) async throws -> Bool {
42- return try await Task . detached ( priority : . utility ) {
43- return try Self . removeImage ( for: url)
44- } . value
47+ return try await perform {
48+ try Self . removeImage ( for: url)
49+ }
4550 }
4651}
4752
4853private extension WebPageImageStoreImpl {
54+ func perform< T: Sendable > ( _ operation: @escaping @Sendable ( ) throws -> T ) async throws -> T {
55+ try await withCheckedThrowingContinuation { continuation in
56+ queue. async {
57+ do {
58+ continuation. resume ( returning: try operation ( ) )
59+ } catch {
60+ continuation. resume ( throwing: error)
61+ }
62+ }
63+ }
64+ }
65+
4966 static func hashedFileName( for url: URL ) -> String {
5067 let hashValue = SHA256 . hash ( data: Data ( url. absoluteString. utf8) )
5168 return hashValue. map { String ( format: " %02x " , $0) } . joined ( )
0 commit comments