Skip to content

Commit ddfe7c9

Browse files
committed
Pass cache policy into EditorAssetLibrary
1 parent 48e3640 commit ddfe7c9

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

ios/Sources/GutenbergKit/Sources/Services/EditorService.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public actor EditorService {
8383
self.assetLibrary = EditorAssetLibrary(
8484
configuration: configuration,
8585
httpClient: httpClient,
86+
cachePolicy: cachePolicy,
8687
storageRoot: storageRoot ?? Paths.storageRoot(for: configuration)
8788
)
8889
}

ios/Sources/GutenbergKit/Sources/Stores/EditorAssetLibrary.swift

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ import Foundation
33
/// The Editor Asset Library is a site-specific repository of remote assets that can be downloaded to the local device to support plugins and theme styles.
44
///
55
public actor EditorAssetLibrary {
6-
7-
public enum CachePolicy: Sendable {
8-
case useExisting
9-
case forceRefresh
10-
}
11-
6+
127
private let configuration: EditorConfiguration
138
private let httpClient: EditorHTTPClientProtocol
149
private let storageRoot: URL
15-
10+
private let cachePolicy: EditorCachePolicy
11+
1612
/// Creates a new `EditorAssetLibrary` instance.
1713
///
1814
/// - Parameters:
@@ -22,33 +18,36 @@ public actor EditorAssetLibrary {
2218
public init(
2319
configuration: EditorConfiguration,
2420
httpClient: EditorHTTPClientProtocol,
21+
cachePolicy: EditorCachePolicy = .always,
2522
storageRoot: URL
2623
) {
2724
self.configuration = configuration
2825
self.httpClient = httpClient
2926
self.storageRoot = storageRoot
27+
self.cachePolicy = cachePolicy
3028
}
31-
29+
3230
// MARK: - Manifest Handling
33-
31+
3432
/// Retrieve the manifest for a given site configuration.
3533
///
3634
/// Applications should periodically check for a new editor manifest. This can be very expensive, so this method defaults to returning an existing one on-disk.
3735
///
38-
package func fetchManifest(cachePolicy: CachePolicy = .useExisting) async throws
39-
-> LocalEditorAssetManifest {
36+
package func fetchManifest() async throws -> LocalEditorAssetManifest {
4037
guard configuration.shouldUsePlugins else { return .empty }
4138
let data = try await httpClient.perform(
4239
URLRequest(method: .GET, url: self.editorAssetsUrl(for: self.configuration))
4340
).0
4441
let remoteManifest = try RemoteEditorAssetManifest(data: data)
45-
46-
if case .useExisting = cachePolicy,
47-
let existingManifest = try self.existingBundle(forManifestChecksum: remoteManifest.checksum) {
48-
return existingManifest.manifest
42+
43+
guard
44+
let existingManifest = try self.existingBundle(forManifestChecksum: remoteManifest.checksum),
45+
self.cachePolicy.allowsResponseWith(date: existingManifest.downloadDate)
46+
else {
47+
return try LocalEditorAssetManifest(remoteManifest: remoteManifest)
4948
}
50-
51-
return try LocalEditorAssetManifest(remoteManifest: remoteManifest)
49+
50+
return existingManifest.manifest
5251
}
5352

5453
// MARK: - Bundle Handling
@@ -72,10 +71,10 @@ public actor EditorAssetLibrary {
7271
/// - Returns: The downloaded `EditorAssetBundle` containing all cached assets.
7372
/// - Throws: An error if the manifest cannot be fetched or assets fail to download.
7473
public func downloadAssetBundle(
75-
cachePolicy: CachePolicy = .useExisting,
74+
cachePolicy: EditorCachePolicy = .always,
7675
progress: EditorProgressCallback? = nil
7776
) async throws -> EditorAssetBundle {
78-
let manifest = try await self.fetchManifest(cachePolicy: cachePolicy)
77+
let manifest = try await self.fetchManifest()
7978
return try await self.buildBundle(for: manifest, progress: progress)
8079
}
8180

0 commit comments

Comments
 (0)