Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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+All.h"
18
 
19
#import "FBLPromise+Async.h"
20
#import "FBLPromisePrivate.h"
21
 
22
@implementation FBLPromise (AllAdditions)
23
 
24
+ (FBLPromise<NSArray *> *)all:(NSArray *)promises {
25
  return [self onQueue:self.defaultDispatchQueue all:promises];
26
}
27
 
28
+ (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue all:(NSArray *)allPromises {
29
  NSParameterAssert(queue);
30
  NSParameterAssert(allPromises);
31
 
32
  if (allPromises.count == 0) {
33
    return [[FBLPromise alloc] initWithResolution:@[]];
34
  }
35
  NSMutableArray *promises = [allPromises mutableCopy];
36
  return [FBLPromise
37
      onQueue:queue
38
        async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
39
          for (NSUInteger i = 0; i < promises.count; ++i) {
40
            id promise = promises[i];
41
            if ([promise isKindOfClass:self]) {
42
              continue;
43
            } else if ([promise isKindOfClass:[NSError class]]) {
44
              reject(promise);
45
              return;
46
            } else {
47
              [promises replaceObjectAtIndex:i
48
                                  withObject:[[FBLPromise alloc] initWithResolution:promise]];
49
            }
50
          }
51
          for (FBLPromise *promise in promises) {
52
            [promise observeOnQueue:queue
53
                fulfill:^(id __unused _) {
54
                  // Wait until all are fulfilled.
55
                  for (FBLPromise *promise in promises) {
56
                    if (!promise.isFulfilled) {
57
                      return;
58
                    }
59
                  }
60
                  // If called multiple times, only the first one affects the result.
61
                  fulfill([promises valueForKey:NSStringFromSelector(@selector(value))]);
62
                }
63
                reject:^(NSError *error) {
64
                  reject(error);
65
                }];
66
          }
67
        }];
68
}
69
 
70
@end
71
 
72
@implementation FBLPromise (DotSyntax_AllAdditions)
73
 
74
+ (FBLPromise<NSArray *> * (^)(NSArray *))all {
75
  return ^(NSArray<FBLPromise *> *promises) {
76
    return [self all:promises];
77
  };
78
}
79
 
80
+ (FBLPromise<NSArray *> * (^)(dispatch_queue_t, NSArray *))allOn {
81
  return ^(dispatch_queue_t queue, NSArray<FBLPromise *> *promises) {
82
    return [self onQueue:queue all:promises];
83
  };
84
}
85
 
86
@end