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+Race.h"
18
 
19
#import "FBLPromise+Async.h"
20
#import "FBLPromisePrivate.h"
21
 
22
@implementation FBLPromise (RaceAdditions)
23
 
24
+ (instancetype)race:(NSArray *)promises {
25
  return [self onQueue:self.defaultDispatchQueue race:promises];
26
}
27
 
28
+ (instancetype)onQueue:(dispatch_queue_t)queue race:(NSArray *)racePromises {
29
  NSParameterAssert(queue);
30
  NSAssert(racePromises.count > 0, @"No promises to observe");
31
 
32
  NSArray *promises = [racePromises copy];
33
  return [FBLPromise onQueue:queue
34
                       async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
35
                         for (id promise in promises) {
36
                           if (![promise isKindOfClass:self]) {
37
                             fulfill(promise);
38
                             return;
39
                           }
40
                         }
41
                         // Subscribe all, but only the first one to resolve will change
42
                         // the resulting promise's state.
43
                         for (FBLPromise *promise in promises) {
44
                           [promise observeOnQueue:queue fulfill:fulfill reject:reject];
45
                         }
46
                       }];
47
}
48
 
49
@end
50
 
51
@implementation FBLPromise (DotSyntax_RaceAdditions)
52
 
53
+ (FBLPromise * (^)(NSArray *))race {
54
  return ^(NSArray *promises) {
55
    return [self race:promises];
56
  };
57
}
58
 
59
+ (FBLPromise * (^)(dispatch_queue_t, NSArray *))raceOn {
60
  return ^(dispatch_queue_t queue, NSArray *promises) {
61
    return [self onQueue:queue race:promises];
62
  };
63
}
64
 
65
@end