Skip to content

Commit

Permalink
Merge pull request #216 from hotwired/cache-based-on-url-feedback
Browse files Browse the repository at this point in the history
Cache based on url feedback
  • Loading branch information
olivaresf authored Jun 4, 2024
2 parents 7a59155 + 30d0903 commit d0c7e68
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions Source/Path Configuration/PathConfigurationLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class PathConfigurationLoader {
for source in sources {
switch source {
case .data(let data):
loadData(data, for: .PathDataTemporaryURL)
loadData(data)
case .file(let url):
loadFile(url)
case .server(let url):
Expand All @@ -35,7 +35,7 @@ final class PathConfigurationLoader {

// Immediately load most recent cached version if available
if let data = cachedData(for: url) {
loadData(data, for: url)
loadData(data)
}

let session = options?.urlSessionConfiguration.map { URLSession(configuration: $0) } ?? URLSession.shared
Expand Down Expand Up @@ -106,23 +106,23 @@ final class PathConfigurationLoader {

do {
let data = try Data(contentsOf: url)
loadData(data, for: url)
loadData(data)
} catch {
debugPrint("[path-configuration] *** error loading configuration from file: \(url), error: \(error)")
}
}

// MARK: - Data

private func loadData(_ data: Data, cache: Bool = false, for url: URL) {
private func loadData(_ data: Data, cache: Bool = false, for url: URL? = nil) {
do {
guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
throw JSONDecodingError.invalidJSON
}

let config = try PathConfigurationDecoder(json: json)

if cache {
if cache, let url {
// Only cache once we ensure we have valid data
cacheRemoteData(data, for: url)
}
Expand All @@ -145,7 +145,3 @@ final class PathConfigurationLoader {
}
}
}

private extension URL {
static let PathDataTemporaryURL = URL(string: "https://localhost/path-configuration.json")!
}

0 comments on commit d0c7e68

Please sign in to comment.