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+Any.h"
|
|
|
18 |
|
|
|
19 |
#import "FBLPromise+Async.h"
|
|
|
20 |
#import "FBLPromisePrivate.h"
|
|
|
21 |
|
|
|
22 |
static NSArray *FBLPromiseCombineValuesAndErrors(NSArray<FBLPromise *> *promises) {
|
|
|
23 |
NSMutableArray *combinedValuesAndErrors = [[NSMutableArray alloc] init];
|
|
|
24 |
for (FBLPromise *promise in promises) {
|
|
|
25 |
if (promise.isFulfilled) {
|
|
|
26 |
[combinedValuesAndErrors addObject:promise.value ?: [NSNull null]];
|
|
|
27 |
continue;
|
|
|
28 |
}
|
|
|
29 |
if (promise.isRejected) {
|
|
|
30 |
[combinedValuesAndErrors addObject:promise.error];
|
|
|
31 |
continue;
|
|
|
32 |
}
|
|
|
33 |
assert(!promise.isPending);
|
|
|
34 |
};
|
|
|
35 |
return combinedValuesAndErrors;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
@implementation FBLPromise (AnyAdditions)
|
|
|
39 |
|
|
|
40 |
+ (FBLPromise<NSArray *> *)any:(NSArray *)promises {
|
|
|
41 |
return [self onQueue:FBLPromise.defaultDispatchQueue any:promises];
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
+ (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue any:(NSArray *)anyPromises {
|
|
|
45 |
NSParameterAssert(queue);
|
|
|
46 |
NSParameterAssert(anyPromises);
|
|
|
47 |
|
|
|
48 |
if (anyPromises.count == 0) {
|
|
|
49 |
return [[FBLPromise alloc] initWithResolution:@[]];
|
|
|
50 |
}
|
|
|
51 |
NSMutableArray *promises = [anyPromises mutableCopy];
|
|
|
52 |
return [FBLPromise
|
|
|
53 |
onQueue:queue
|
|
|
54 |
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
|
|
|
55 |
for (NSUInteger i = 0; i < promises.count; ++i) {
|
|
|
56 |
id promise = promises[i];
|
|
|
57 |
if ([promise isKindOfClass:self]) {
|
|
|
58 |
continue;
|
|
|
59 |
} else {
|
|
|
60 |
[promises replaceObjectAtIndex:i
|
|
|
61 |
withObject:[[FBLPromise alloc] initWithResolution:promise]];
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
for (FBLPromise *promise in promises) {
|
|
|
65 |
[promise observeOnQueue:queue
|
|
|
66 |
fulfill:^(id __unused _) {
|
|
|
67 |
// Wait until all are resolved.
|
|
|
68 |
for (FBLPromise *promise in promises) {
|
|
|
69 |
if (promise.isPending) {
|
|
|
70 |
return;
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
// If called multiple times, only the first one affects the result.
|
|
|
74 |
fulfill(FBLPromiseCombineValuesAndErrors(promises));
|
|
|
75 |
}
|
|
|
76 |
reject:^(NSError *error) {
|
|
|
77 |
BOOL atLeastOneIsFulfilled = NO;
|
|
|
78 |
for (FBLPromise *promise in promises) {
|
|
|
79 |
if (promise.isPending) {
|
|
|
80 |
return;
|
|
|
81 |
}
|
|
|
82 |
if (promise.isFulfilled) {
|
|
|
83 |
atLeastOneIsFulfilled = YES;
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
if (atLeastOneIsFulfilled) {
|
|
|
87 |
fulfill(FBLPromiseCombineValuesAndErrors(promises));
|
|
|
88 |
} else {
|
|
|
89 |
reject(error);
|
|
|
90 |
}
|
|
|
91 |
}];
|
|
|
92 |
}
|
|
|
93 |
}];
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
@end
|
|
|
97 |
|
|
|
98 |
@implementation FBLPromise (DotSyntax_AnyAdditions)
|
|
|
99 |
|
|
|
100 |
+ (FBLPromise<NSArray *> * (^)(NSArray *))any {
|
|
|
101 |
return ^(NSArray *promises) {
|
|
|
102 |
return [self any:promises];
|
|
|
103 |
};
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
+ (FBLPromise<NSArray *> * (^)(dispatch_queue_t, NSArray *))anyOn {
|
|
|
107 |
return ^(dispatch_queue_t queue, NSArray *promises) {
|
|
|
108 |
return [self onQueue:queue any:promises];
|
|
|
109 |
};
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
@end
|