@@ -43,17 +43,17 @@ internal struct PackageCollections: PackageCollectionsProtocol {
43
43
}
44
44
let collectionOrder = identifiers. enumerated ( ) . reduce ( [ PackageCollectionsModel . CollectionIdentifier: Int] ( ) ) { partial, element in
45
45
var dictionary = partial
46
- map [ element. element] = element. offset
47
- return map
46
+ dictionary [ element. element] = element. offset
47
+ return dictionary
48
48
}
49
49
self . storage. collections. list ( identifiers: identifiers) { result in
50
50
switch result {
51
51
case . failure( let error) :
52
52
callback ( . failure( error) )
53
- case . success( var groups ) :
53
+ case . success( var collections ) :
54
54
// re-order by profile order which reflects the user's election
55
- groups . sort ( by: { lhs, rhs in groupOrder [ lhs. identifier] ?? 0 < groupOrder [ rhs. identifier] ?? 0 } )
56
- callback ( . success( groups ) )
55
+ collections . sort ( by: { lhs, rhs in collectionOrder [ lhs. identifier] ?? 0 < collectionOrder [ rhs. identifier] ?? 0 } )
56
+ callback ( . success( collections ) )
57
57
}
58
58
}
59
59
}
@@ -97,7 +97,7 @@ internal struct PackageCollections: PackageCollectionsProtocol {
97
97
let lock = Lock ( )
98
98
var refreshResults = [ Result < PackageCollectionsModel . Collection , Error > ] ( )
99
99
sources. forEach { source in
100
- self . refreshPackageGroupFromSource ( source: source, profile: profile) { refreshResult in
100
+ self . refreshCollectionFromSource ( source: source, profile: profile) { refreshResult in
101
101
lock. withLock { refreshResults. append ( refreshResult) }
102
102
if refreshResults. count == ( lock. withLock { sources. count } ) {
103
103
let errors = refreshResults. compactMap { $0. failure }
@@ -119,12 +119,14 @@ internal struct PackageCollections: PackageCollectionsProtocol {
119
119
return callback ( . failure( MultipleErrors ( errors) ) )
120
120
}
121
121
122
+ // first record the registration
122
123
self . storage. collectionsProfiles. add ( source: source, order: order, to: profile) { result in
123
124
switch result {
124
125
case . failure( let error) :
125
126
callback ( . failure( error) )
126
127
case . success:
127
- self . refreshPackageGroupFromSource ( source: source, order: order, profile: profile, callback: callback)
128
+ // next try to fetch the collection from the network and store it locally so future operations dont need to access the network
129
+ self . refreshCollectionFromSource ( source: source, order: order, profile: profile, callback: callback)
128
130
}
129
131
}
130
132
}
@@ -139,6 +141,8 @@ internal struct PackageCollections: PackageCollectionsProtocol {
139
141
case . failure( let error) :
140
142
callback ( . failure( error) )
141
143
case . success:
144
+ // check to see if the collection is used in some other profile,
145
+ // if not delete it from storage to reduce disk space
142
146
self . storage. collectionsProfiles. exists ( source: source, in: nil ) { result in
143
147
switch result {
144
148
case . failure( let error) :
@@ -163,6 +167,9 @@ internal struct PackageCollections: PackageCollectionsProtocol {
163
167
self . storage. collectionsProfiles. move ( source: source, to: order, in: profile, callback: callback)
164
168
}
165
169
170
+ // Returns information about a package collection.
171
+ // The collection is not required to be in the configured list.
172
+ // If not found locally (storage), the collection will be fetched from the source.
166
173
func getCollection( _ source: PackageCollectionsModel . CollectionSource ,
167
174
callback: @escaping ( Result < PackageCollectionsModel . Collection , Error > ) -> Void ) {
168
175
if let errors = source. validate ( ) {
@@ -177,7 +184,7 @@ internal struct PackageCollections: PackageCollectionsProtocol {
177
184
}
178
185
provider. get ( source, callback: callback)
179
186
case . success( let collection) :
180
- callback ( . success( group ) )
187
+ callback ( . success( collection ) )
181
188
}
182
189
}
183
190
}
@@ -229,10 +236,12 @@ internal struct PackageCollections: PackageCollectionsProtocol {
229
236
fatalError ( " not implemented " )
230
237
}
231
238
232
- private func refreshPackageGroupFromSource( source: PackageCollectionsModel . CollectionSource ,
233
- order _: Int ? = nil ,
234
- profile _: PackageCollectionsModel . Profile ? = nil ,
235
- callback: @escaping ( Result < PackageCollectionsModel . Collection , Error > ) -> Void ) {
239
+ // Fetch the collection from the network and store it in local storage
240
+ // This helps avoid network access in normal operations
241
+ private func refreshCollectionFromSource( source: PackageCollectionsModel . CollectionSource ,
242
+ order _: Int ? = nil ,
243
+ profile _: PackageCollectionsModel . Profile ? = nil ,
244
+ callback: @escaping ( Result < PackageCollectionsModel . Collection , Error > ) -> Void ) {
236
245
if let errors = source. validate ( ) {
237
246
return callback ( . failure( MultipleErrors ( errors) ) )
238
247
}
@@ -250,15 +259,15 @@ internal struct PackageCollections: PackageCollectionsProtocol {
250
259
}
251
260
252
261
private func targetListResultFromCollections( _ collections: [ PackageCollectionsModel . Collection ] ) -> PackageCollectionsModel . TargetListResult {
253
- var collectionGroups = [ PackageReference : ( package : PackageCollectionsModel . Collection . Package , collections: Set < PackageCollectionsModel . CollectionIdentifier > ) ] ( )
262
+ var packageCollections = [ PackageReference : ( package : PackageCollectionsModel . Collection . Package , collections: Set < PackageCollectionsModel . CollectionIdentifier > ) ] ( )
254
263
var targetsPackages = [ String : ( target: PackageCollectionsModel . PackageTarget , packages: Set < PackageReference > ) ] ( )
255
264
256
265
collections. forEach { collection in
257
266
collection. packages. forEach { package in
258
267
// Avoid copy-on-write: remove entry from dictionary before mutating
259
- var entry = collectionGroups . removeValue ( forKey: package . reference) ?? ( package , . init( ) )
268
+ var entry = packageCollections . removeValue ( forKey: package . reference) ?? ( package , . init( ) )
260
269
entry. collections. insert ( collection. identifier)
261
- collectionGroups [ package . reference] = entry
270
+ packageCollections [ package . reference] = entry
262
271
263
272
package . versions. forEach { version in
264
273
version. targets. forEach { target in
@@ -273,7 +282,7 @@ internal struct PackageCollections: PackageCollectionsProtocol {
273
282
274
283
return targetsPackages. map { _, pair in
275
284
let targetPackages = pair. packages
276
- . compactMap { collectionGroups [ $0] }
285
+ . compactMap { packageCollections [ $0] }
277
286
. map { pair -> PackageCollectionsModel . TargetListResult . Package in
278
287
let versions = pair. package . versions. map { PackageCollectionsModel . TargetListResult. PackageVersion ( version: $0. version, packageName: $0. packageName) }
279
288
return PackageCollectionsModel . TargetListResult. Package ( repository: pair. package . repository,
0 commit comments