1 |
efrain |
1 |
/**
|
|
|
2 |
Copyright 2018 Google Inc. All rights reserved.
|
|
|
3 |
|
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
you may not use this file except in compliance with the License.
|
|
|
6 |
You may obtain a copy of the License at:
|
|
|
7 |
|
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
|
|
|
10 |
Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
See the License for the specific language governing permissions and
|
|
|
14 |
limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#import "FBLPromise+Reduce.h"
|
|
|
18 |
|
|
|
19 |
#import "FBLPromisePrivate.h"
|
|
|
20 |
|
|
|
21 |
@implementation FBLPromise (ReduceAdditions)
|
|
|
22 |
|
|
|
23 |
- (FBLPromise *)reduce:(NSArray *)items combine:(FBLPromiseReducerBlock)reducer {
|
|
|
24 |
return [self onQueue:FBLPromise.defaultDispatchQueue reduce:items combine:reducer];
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
|
|
|
28 |
reduce:(NSArray *)items
|
|
|
29 |
combine:(FBLPromiseReducerBlock)reducer {
|
|
|
30 |
NSParameterAssert(queue);
|
|
|
31 |
NSParameterAssert(items);
|
|
|
32 |
NSParameterAssert(reducer);
|
|
|
33 |
|
|
|
34 |
FBLPromise *promise = self;
|
|
|
35 |
for (id item in items) {
|
|
|
36 |
promise = [promise chainOnQueue:queue
|
|
|
37 |
chainedFulfill:^id(id value) {
|
|
|
38 |
return reducer(value, item);
|
|
|
39 |
}
|
|
|
40 |
chainedReject:nil];
|
|
|
41 |
}
|
|
|
42 |
return promise;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
@end
|
|
|
46 |
|
|
|
47 |
@implementation FBLPromise (DotSyntax_ReduceAdditions)
|
|
|
48 |
|
|
|
49 |
- (FBLPromise * (^)(NSArray *, FBLPromiseReducerBlock))reduce {
|
|
|
50 |
return ^(NSArray *items, FBLPromiseReducerBlock reducer) {
|
|
|
51 |
return [self reduce:items combine:reducer];
|
|
|
52 |
};
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
- (FBLPromise * (^)(dispatch_queue_t, NSArray *, FBLPromiseReducerBlock))reduceOn {
|
|
|
56 |
return ^(dispatch_queue_t queue, NSArray *items, FBLPromiseReducerBlock reducer) {
|
|
|
57 |
return [self onQueue:queue reduce:items combine:reducer];
|
|
|
58 |
};
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
@end
|