@@ -28,34 +28,14 @@ extension Collection {
28
28
29
29
extension Dictionary where Value : FutureType {
30
30
func flatten( on eventLoopGroup: EventLoopGroup ) -> Future < [ Key : Value . Expectation ] > {
31
- let queue = DispatchQueue ( label: " org.graphQL.elementQueue " )
32
- var elements : [ Key : Value . Expectation ] = [ : ]
33
-
34
- guard self . count > 0 else {
35
- return eventLoopGroup. next ( ) . makeSucceededFuture ( elements)
31
+ // create array of futures with (key,value) tuple
32
+ let futures : [ Future < ( Key , Value . Expectation ) > ] = self . map { element in
33
+ element. value. map ( file: #file, line: #line) { ( key: element. key, value: $0) }
36
34
}
37
-
38
- let promise : EventLoopPromise < [ Key : Value . Expectation ] > = eventLoopGroup. next ( ) . makePromise ( )
39
- elements. reserveCapacity ( self . count)
40
-
41
- for (key, value) in self {
42
- value. whenSuccess { expectation in
43
- // Control access to elements to avoid thread conflicts
44
- queue. async {
45
- elements [ key] = expectation
46
-
47
- if elements. count == self . count {
48
- promise. succeed ( elements)
49
- }
50
- }
51
- }
52
-
53
- value. whenFailure { error in
54
- promise. fail ( error)
55
- }
35
+ // when all futures have succeeded convert tuple array back to dictionary
36
+ return EventLoopFuture . whenAllSucceed ( futures, on: eventLoopGroup. next ( ) ) . map {
37
+ . init( uniqueKeysWithValues: $0)
56
38
}
57
-
58
- return promise. futureResult
59
39
}
60
40
}
61
41
extension Future {
@@ -86,6 +66,7 @@ public protocol FutureType {
86
66
associatedtype Expectation
87
67
func whenSuccess( _ callback: @escaping ( Expectation ) -> Void )
88
68
func whenFailure( _ callback: @escaping ( Error ) -> Void )
69
+ func map< NewValue> ( file: StaticString , line: UInt , _ callback: @escaping ( Expectation ) -> ( NewValue ) ) -> EventLoopFuture < NewValue >
89
70
}
90
71
91
72
extension Future : FutureType {
0 commit comments