@@ -59,16 +59,16 @@ public final class DataLoader<Key: Hashable, Value> {
5959 do {
6060 _ = try batchLoadFunction ( [ key] ) . map { results in
6161 if results. isEmpty {
62- promise
63- . fail (
64- DataLoaderError
65- . noValueForKey ( " Did not return value for key: \( key) " )
62+ promise. fail (
63+ DataLoaderError . noValueForKey (
64+ " Did not return value for key: \( key) "
6665 )
66+ )
6767 } else {
6868 let result = results [ 0 ]
6969 switch result {
70- case let . success( value) : promise. succeed ( value)
71- case let . failure( error) : promise. fail ( error)
70+ case . success( let value) : promise. succeed ( value)
71+ case . failure( let error) : promise. fail ( error)
7272 }
7373 }
7474 }
@@ -179,10 +179,10 @@ public final class DataLoader<Key: Hashable, Value> {
179179 // If a maxBatchSize was provided and the queue is longer, then segment the
180180 // queue into multiple batches, otherwise treat the queue as a single batch.
181181 if let maxBatchSize = options. maxBatchSize, maxBatchSize > 0 , maxBatchSize < batch. count {
182- for i in 0 ... ( batch. count / maxBatchSize) {
182+ for i in 0 ... ( batch. count / maxBatchSize) {
183183 let startIndex = i * maxBatchSize
184184 let endIndex = ( i + 1 ) * maxBatchSize
185- let slicedBatch = batch [ startIndex ..< min ( endIndex, batch. count) ]
185+ let slicedBatch = batch [ startIndex..< min ( endIndex, batch. count) ]
186186 try executeBatch ( batch: Array ( slicedBatch) )
187187 }
188188 } else {
@@ -202,18 +202,17 @@ public final class DataLoader<Key: Hashable, Value> {
202202 do {
203203 _ = try batchLoadFunction ( keys) . flatMapThrowing { values in
204204 if values. count != keys. count {
205- throw DataLoaderError
206- . typeError (
207- " The function did not return an array of the same length as the array of keys. \n Keys count: \( keys. count) \n Values count: \( values. count) "
208- )
205+ throw DataLoaderError . typeError (
206+ " The function did not return an array of the same length as the array of keys. \n Keys count: \( keys. count) \n Values count: \( values. count) "
207+ )
209208 }
210209
211210 for entry in batch. enumerated ( ) {
212211 let result = values [ entry. offset]
213212
214213 switch result {
215- case let . failure( error) : entry. element. promise. fail ( error)
216- case let . success( value) : entry. element. promise. succeed ( value)
214+ case . failure( let error) : entry. element. promise. fail ( error)
215+ case . success( let value) : entry. element. promise. succeed ( value)
217216 }
218217 }
219218 } . recover { error in
@@ -238,25 +237,30 @@ public final class DataLoader<Key: Hashable, Value> {
238237 public typealias ConcurrentBatchLoadFunction < Key, Value> =
239238 @Sendable ( _ keys: [ Key ] ) async throws -> [ DataLoaderFutureValue < Value > ]
240239
241- public extension DataLoader {
240+ extension DataLoader {
242241 @available ( macOS 12 , iOS 15 , watchOS 8 , tvOS 15 , * )
243- convenience init (
242+ public convenience init (
244243 on eventLoop: EventLoop ,
245244 options: DataLoaderOptions < Key , Value > = DataLoaderOptions ( ) ,
246245 throwing asyncThrowingLoadFunction: @escaping ConcurrentBatchLoadFunction < Key , Value >
247246 ) {
248- self . init ( options: options, batchLoadFunction: { keys in
249- let promise = eventLoop. next ( ) . makePromise ( of: [ DataLoaderFutureValue < Value > ] . self)
250- promise. completeWithTask {
251- try await asyncThrowingLoadFunction ( keys)
247+ self . init (
248+ options: options,
249+ batchLoadFunction: { keys in
250+ let promise = eventLoop. next ( ) . makePromise (
251+ of: [ DataLoaderFutureValue < Value > ] . self
252+ )
253+ promise. completeWithTask {
254+ try await asyncThrowingLoadFunction ( keys)
255+ }
256+ return promise. futureResult
252257 }
253- return promise. futureResult
254- } )
258+ )
255259 }
256260
257261 /// Asynchronously loads a key, returning the value represented by that key.
258262 @available ( macOS 12 , iOS 15 , watchOS 8 , tvOS 15 , * )
259- func load( key: Key , on eventLoopGroup: EventLoopGroup ) async throws -> Value {
263+ public func load( key: Key , on eventLoopGroup: EventLoopGroup ) async throws -> Value {
260264 try await load ( key: key, on: eventLoopGroup) . get ( )
261265 }
262266
@@ -274,7 +278,8 @@ public final class DataLoader<Key: Hashable, Value> {
274278 /// let aAndB = try await a + b
275279 /// ```
276280 @available ( macOS 12 , iOS 15 , watchOS 8 , tvOS 15 , * )
277- func loadMany( keys: [ Key ] , on eventLoopGroup: EventLoopGroup ) async throws -> [ Value ] {
281+ public func loadMany( keys: [ Key ] , on eventLoopGroup: EventLoopGroup ) async throws -> [ Value ]
282+ {
278283 try await loadMany ( keys: keys, on: eventLoopGroup) . get ( )
279284 }
280285 }
0 commit comments