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+Async.h"
18
 
19
#import "FBLPromisePrivate.h"
20
 
21
@implementation FBLPromise (AsyncAdditions)
22
 
23
+ (instancetype)async:(FBLPromiseAsyncWorkBlock)work {
24
  return [self onQueue:self.defaultDispatchQueue async:work];
25
}
26
 
27
+ (instancetype)onQueue:(dispatch_queue_t)queue async:(FBLPromiseAsyncWorkBlock)work {
28
  NSParameterAssert(queue);
29
  NSParameterAssert(work);
30
 
31
  FBLPromise *promise = [[FBLPromise alloc] initPending];
32
  dispatch_group_async(FBLPromise.dispatchGroup, queue, ^{
33
    work(
34
        ^(id __nullable value) {
35
          if ([value isKindOfClass:[FBLPromise class]]) {
36
            [(FBLPromise *)value observeOnQueue:queue
37
                fulfill:^(id __nullable value) {
38
                  [promise fulfill:value];
39
                }
40
                reject:^(NSError *error) {
41
                  [promise reject:error];
42
                }];
43
          } else {
44
            [promise fulfill:value];
45
          }
46
        },
47
        ^(NSError *error) {
48
          [promise reject:error];
49
        });
50
  });
51
  return promise;
52
}
53
 
54
@end
55
 
56
@implementation FBLPromise (DotSyntax_AsyncAdditions)
57
 
58
+ (FBLPromise* (^)(FBLPromiseAsyncWorkBlock))async {
59
  return ^(FBLPromiseAsyncWorkBlock work) {
60
    return [self async:work];
61
  };
62
}
63
 
64
+ (FBLPromise* (^)(dispatch_queue_t, FBLPromiseAsyncWorkBlock))asyncOn {
65
  return ^(dispatch_queue_t queue, FBLPromiseAsyncWorkBlock work) {
66
    return [self onQueue:queue async:work];
67
  };
68
}
69
 
70
@end