Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache based on url feedback #216

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")!
}