first async refactor of fetch/index

This commit is contained in:
2024-11-17 02:31:44 +11:00
parent badc6dd39e
commit 8c7f0f23d5
13 changed files with 112 additions and 32 deletions

View File

@ -30,6 +30,39 @@ public extension ApkIndex {
}
}
public extension ApkIndex {
static func resolve<S: Sequence>(_ repositories: S, fetch: ApkIndexFetchMode) async throws -> Self where S.Element == ApkIndexRepository {
try await withThrowingTaskGroup(of: Self.self) { group in
for repository in repositories {
group.addTask(priority: .userInitiated) {
let local: URL
switch fetch {
case .local:
local = URL(filePath: repository.localName)
case .lazy:
if !FileManager.default.fileExists(atPath: repository.localName) {
fallthrough
}
local = URL(filePath: repository.localName)
case .update:
print("Fetching \"\(repository.name)\"")
local = try await ApkIndexDownloader.fetch(repository: repository)
}
let index = try ApkIndex(readFrom: local)
return index
}
}
return try await ApkIndex.merge(group.reduce(into: []) { $0.append($1) })
}
}
}
public enum ApkIndexFetchMode: Sendable {
case update
case lazy
case local
}
public enum ApkIndexReadingError: Error, LocalizedError {
case missingSignature
case missingIndex