You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.6 KiB

5 years ago
  1. /**
  2. Copyright 2018 Google Inc. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at:
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #import "FBLPromise+Any.h"
  14. #import "FBLPromise+Async.h"
  15. #import "FBLPromisePrivate.h"
  16. static NSArray *FBLPromiseCombineValuesAndErrors(NSArray<FBLPromise *> *promises) {
  17. NSMutableArray *combinedValuesAndErrors = [[NSMutableArray alloc] init];
  18. for (FBLPromise *promise in promises) {
  19. if (promise.isFulfilled) {
  20. [combinedValuesAndErrors addObject:promise.value ?: [NSNull null]];
  21. continue;
  22. }
  23. if (promise.isRejected) {
  24. [combinedValuesAndErrors addObject:promise.error];
  25. continue;
  26. }
  27. assert(!promise.isPending);
  28. };
  29. return combinedValuesAndErrors;
  30. }
  31. @implementation FBLPromise (AnyAdditions)
  32. + (FBLPromise<NSArray *> *)any:(NSArray *)promises {
  33. return [self onQueue:FBLPromise.defaultDispatchQueue any:promises];
  34. }
  35. + (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue any:(NSArray *)anyPromises {
  36. NSParameterAssert(queue);
  37. NSParameterAssert(anyPromises);
  38. if (anyPromises.count == 0) {
  39. return [[FBLPromise alloc] initWithResolution:@[]];
  40. }
  41. NSMutableArray *promises = [anyPromises mutableCopy];
  42. return [FBLPromise
  43. onQueue:queue
  44. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
  45. for (NSUInteger i = 0; i < promises.count; ++i) {
  46. id promise = promises[i];
  47. if ([promise isKindOfClass:self]) {
  48. continue;
  49. } else {
  50. [promises replaceObjectAtIndex:i
  51. withObject:[[FBLPromise alloc] initWithResolution:promise]];
  52. }
  53. }
  54. for (FBLPromise *promise in promises) {
  55. [promise observeOnQueue:queue
  56. fulfill:^(id __unused _) {
  57. // Wait until all are resolved.
  58. for (FBLPromise *promise in promises) {
  59. if (promise.isPending) {
  60. return;
  61. }
  62. }
  63. // If called multiple times, only the first one affects the result.
  64. fulfill(FBLPromiseCombineValuesAndErrors(promises));
  65. }
  66. reject:^(NSError *error) {
  67. BOOL atLeastOneIsFulfilled = NO;
  68. for (FBLPromise *promise in promises) {
  69. if (promise.isPending) {
  70. return;
  71. }
  72. if (promise.isFulfilled) {
  73. atLeastOneIsFulfilled = YES;
  74. }
  75. }
  76. if (atLeastOneIsFulfilled) {
  77. fulfill(FBLPromiseCombineValuesAndErrors(promises));
  78. } else {
  79. reject(error);
  80. }
  81. }];
  82. }
  83. }];
  84. }
  85. @end
  86. @implementation FBLPromise (DotSyntax_AnyAdditions)
  87. + (FBLPromise<NSArray *> * (^)(NSArray *))any {
  88. return ^(NSArray *promises) {
  89. return [self any:promises];
  90. };
  91. }
  92. + (FBLPromise<NSArray *> * (^)(dispatch_queue_t, NSArray *))anyOn {
  93. return ^(dispatch_queue_t queue, NSArray *promises) {
  94. return [self onQueue:queue any:promises];
  95. };
  96. }
  97. @end