Skip to content

Commit 45a01ed

Browse files
committed
Finish moving package collections to async/await
1 parent 4a233d7 commit 45a01ed

File tree

3 files changed

+411
-600
lines changed

3 files changed

+411
-600
lines changed

Sources/PackageCollections/Storage/FilePackageCollectionsSourcesStorage.swift

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ import class Foundation.JSONDecoder
1818
import class Foundation.JSONEncoder
1919
import struct Foundation.URL
2020

21-
extension DispatchQueue {
22-
func awaitingAsync<T>(_ closure: @escaping () throws -> T) async throws -> T {
23-
try await withCheckedThrowingContinuation { continuation in
24-
self.async {
25-
do {
26-
try continuation.resume(returning: closure())
27-
} catch {
28-
continuation.resume(throwing: error)
29-
}
30-
}
31-
}
32-
}
33-
}
34-
3521
struct FilePackageCollectionsSourcesStorage: PackageCollectionsSourcesStorage {
3622
let fileSystem: FileSystem
3723
let path: AbsolutePath
@@ -47,64 +33,64 @@ struct FilePackageCollectionsSourcesStorage: PackageCollectionsSourcesStorage {
4733
self.decoder = JSONDecoder.makeWithDefaults()
4834
}
4935

50-
func list() async throws -> [PackageCollectionsModel.CollectionSource] {
51-
try await DispatchQueue.sharedConcurrent.awaitingAsync {
52-
try self.withLock {
53-
try self.loadFromDisk()
36+
func asyncWithLock<T>(_ operation: @escaping () throws -> T) async throws -> T {
37+
try await withCheckedThrowingContinuation { continuation in
38+
DispatchQueue.sharedConcurrent.async {
39+
do {
40+
try continuation.resume(returning: self.withLock(operation))
41+
} catch {
42+
continuation.resume(throwing: error)
43+
}
5444
}
5545
}
5646
}
5747

48+
func list() async throws -> [PackageCollectionsModel.CollectionSource] {
49+
try await self.asyncWithLock {
50+
try self.loadFromDisk()
51+
}
52+
}
53+
5854
func add(source: PackageCollectionsModel.CollectionSource, order: Int? = nil) async throws {
59-
try await DispatchQueue.sharedConcurrent.awaitingAsync {
60-
try self.withLock {
61-
var sources = try self.loadFromDisk()
62-
sources = sources.filter { $0 != source }
63-
let order = order.flatMap { $0 >= 0 && $0 < sources.endIndex ? order : sources.endIndex } ?? sources.endIndex
64-
sources.insert(source, at: order)
65-
try self.saveToDisk(sources)
66-
}
55+
try await self.asyncWithLock {
56+
var sources = try self.loadFromDisk()
57+
sources = sources.filter { $0 != source }
58+
let order = order.flatMap { $0 >= 0 && $0 < sources.endIndex ? order : sources.endIndex } ?? sources.endIndex
59+
sources.insert(source, at: order)
60+
try self.saveToDisk(sources)
6761
}
6862
}
6963

7064
func remove(source: PackageCollectionsModel.CollectionSource) async throws {
71-
try await DispatchQueue.sharedConcurrent.awaitingAsync {
72-
try self.withLock {
73-
var sources = try self.loadFromDisk()
74-
sources = sources.filter { $0 != source }
75-
try self.saveToDisk(sources)
76-
}
65+
try await self.asyncWithLock {
66+
var sources = try self.loadFromDisk()
67+
sources = sources.filter { $0 != source }
68+
try self.saveToDisk(sources)
7769
}
7870
}
7971

8072
func move(source: PackageCollectionsModel.CollectionSource, to order: Int) async throws {
81-
try await DispatchQueue.sharedConcurrent.awaitingAsync {
82-
try self.withLock {
83-
var sources = try self.loadFromDisk()
84-
sources = sources.filter { $0 != source }
85-
let order = order >= 0 && order < sources.endIndex ? order : sources.endIndex
86-
sources.insert(source, at: order)
87-
try self.saveToDisk(sources)
88-
}
73+
try await self.asyncWithLock {
74+
var sources = try self.loadFromDisk()
75+
sources = sources.filter { $0 != source }
76+
let order = order >= 0 && order < sources.endIndex ? order : sources.endIndex
77+
sources.insert(source, at: order)
78+
try self.saveToDisk(sources)
8979
}
9080
}
9181

9282
func exists(source: PackageCollectionsModel.CollectionSource) async throws -> Bool {
93-
try await DispatchQueue.sharedConcurrent.awaitingAsync {
94-
try self.withLock {
95-
try self.loadFromDisk()
96-
}.contains(source)
97-
}
83+
try await self.asyncWithLock {
84+
try self.loadFromDisk()
85+
}.contains(source)
9886
}
9987

10088
func update(source: PackageCollectionsModel.CollectionSource) async throws {
101-
try await DispatchQueue.sharedConcurrent.awaitingAsync {
102-
try self.withLock {
103-
var sources = try self.loadFromDisk()
104-
if let index = sources.firstIndex(where: { $0 == source }) {
105-
sources[index] = source
106-
try self.saveToDisk(sources)
107-
}
89+
try await self.asyncWithLock {
90+
var sources = try self.loadFromDisk()
91+
if let index = sources.firstIndex(where: { $0 == source }) {
92+
sources[index] = source
93+
try self.saveToDisk(sources)
10894
}
10995
}
11096
}

Sources/PackageCollections/Storage/PackageCollectionsStorage.swift

Lines changed: 14 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,35 @@ public protocol PackageCollectionsStorage {
1818
///
1919
/// - Parameters:
2020
/// - collection: The `PackageCollection`
21-
/// - callback: The closure to invoke when result becomes available
22-
@available(*, noasync, message: "Use the async alternative")
23-
func put(collection: PackageCollectionsModel.Collection,
24-
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void)
21+
func put(collection: PackageCollectionsModel.Collection) async throws -> PackageCollectionsModel.Collection
2522

2623
/// Removes `PackageCollection` from storage.
2724
///
2825
/// - Parameters:
2926
/// - identifier: The identifier of the `PackageCollection`
30-
/// - callback: The closure to invoke when result becomes available
31-
@available(*, noasync, message: "Use the async alternative")
32-
func remove(identifier: PackageCollectionsModel.CollectionIdentifier,
33-
callback: @escaping (Result<Void, Error>) -> Void)
27+
func remove(identifier: PackageCollectionsModel.CollectionIdentifier) async throws
3428

3529
/// Returns `PackageCollection` for the given identifier.
3630
///
3731
/// - Parameters:
3832
/// - identifier: The identifier of the `PackageCollection`
39-
/// - callback: The closure to invoke when result becomes available
40-
@available(*, noasync, message: "Use the async alternative")
41-
func get(identifier: PackageCollectionsModel.CollectionIdentifier,
42-
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void)
33+
func get(identifier: PackageCollectionsModel.CollectionIdentifier) async throws -> PackageCollectionsModel.Collection
4334

4435
/// Returns `PackageCollection`s for the given identifiers, or all if none specified.
4536
///
4637
/// - Parameters:
4738
/// - identifiers: Optional. The identifiers of the `PackageCollection`
48-
/// - callback: The closure to invoke when result becomes available
49-
@available(*, noasync, message: "Use the async alternative")
50-
func list(identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
51-
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void)
39+
func list(identifiers: [PackageCollectionsModel.CollectionIdentifier]?) async throws -> [PackageCollectionsModel.Collection]
5240

5341
/// Returns `PackageSearchResult` for the given search criteria.
5442
///
5543
/// - Parameters:
5644
/// - identifiers: Optional. The identifiers of the `PackageCollection`s
5745
/// - query: The search query expression
58-
/// - callback: The closure to invoke when result becomes available
59-
@available(*, noasync, message: "Use the async alternative")
60-
func searchPackages(identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
61-
query: String,
62-
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void)
46+
func searchPackages(
47+
identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
48+
query: String
49+
) async throws -> PackageCollectionsModel.PackageSearchResult
6350

6451
/// Returns packages for the given package identity.
6552
///
@@ -68,72 +55,20 @@ public protocol PackageCollectionsStorage {
6855
/// - Parameters:
6956
/// - identifier: The package identifier
7057
/// - collectionIdentifiers: Optional. The identifiers of the `PackageCollection`s
71-
/// - callback: The closure to invoke when result becomes available
72-
@available(*, noasync, message: "Use the async alternative")
73-
func findPackage(identifier: PackageIdentity,
74-
collectionIdentifiers: [PackageCollectionsModel.CollectionIdentifier]?,
75-
callback: @escaping (Result<(packages: [PackageCollectionsModel.Package], collections: [PackageCollectionsModel.CollectionIdentifier]), Error>) -> Void)
58+
func findPackage(
59+
identifier: PackageIdentity,
60+
collectionIdentifiers: [PackageCollectionsModel.CollectionIdentifier]?
61+
) async throws -> (packages: [PackageCollectionsModel.Package], collections: [PackageCollectionsModel.CollectionIdentifier])
7662

7763
/// Returns `TargetSearchResult` for the given search criteria.
7864
///
7965
/// - Parameters:
8066
/// - identifiers: Optional. The identifiers of the `PackageCollection`
8167
/// - query: The search query expression
8268
/// - type: The search type
83-
/// - callback: The closure to invoke when result becomes available
84-
@available(*, noasync, message: "Use the async alternative")
85-
func searchTargets(identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
86-
query: String,
87-
type: PackageCollectionsModel.TargetSearchType,
88-
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void)
89-
}
90-
91-
public extension PackageCollectionsStorage {
92-
func put(collection: PackageCollectionsModel.Collection) async throws -> PackageCollectionsModel.Collection {
93-
try await safe_async {
94-
self.put(collection: collection, callback: $0)
95-
}
96-
}
97-
func remove(identifier: PackageCollectionsModel.CollectionIdentifier) async throws {
98-
try await safe_async {
99-
self.remove(identifier: identifier, callback: $0)
100-
}
101-
}
102-
func get(identifier: PackageCollectionsModel.CollectionIdentifier) async throws -> PackageCollectionsModel.Collection {
103-
try await safe_async {
104-
self.get(identifier: identifier, callback: $0)
105-
}
106-
}
107-
func list(identifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil) async throws -> [PackageCollectionsModel.Collection] {
108-
try await safe_async {
109-
self.list(identifiers: identifiers, callback: $0)
110-
}
111-
}
112-
113-
func searchPackages(
114-
identifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil,
115-
query: String
116-
) async throws -> PackageCollectionsModel.PackageSearchResult {
117-
try await safe_async {
118-
self.searchPackages(identifiers: identifiers, query: query, callback: $0)
119-
}
120-
}
121-
func findPackage(
122-
identifier: PackageIdentity,
123-
collectionIdentifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil
124-
) async throws -> (packages: [PackageCollectionsModel.Package], collections: [PackageCollectionsModel.CollectionIdentifier]) {
125-
try await safe_async {
126-
self.findPackage(identifier: identifier, collectionIdentifiers: collectionIdentifiers, callback: $0)
127-
}
128-
}
129-
13069
func searchTargets(
131-
identifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil,
70+
identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
13271
query: String,
13372
type: PackageCollectionsModel.TargetSearchType
134-
) async throws -> PackageCollectionsModel.TargetSearchResult {
135-
try await safe_async {
136-
self.searchTargets(identifiers: identifiers, query: query, type: type, callback: $0)
137-
}
138-
}
73+
) async throws -> PackageCollectionsModel.TargetSearchResult
13974
}

0 commit comments

Comments
 (0)