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+Timeout.h"
|
|
|
18 |
|
|
|
19 |
#import "FBLPromisePrivate.h"
|
|
|
20 |
|
|
|
21 |
@implementation FBLPromise (TimeoutAdditions)
|
|
|
22 |
|
|
|
23 |
- (FBLPromise *)timeout:(NSTimeInterval)interval {
|
|
|
24 |
return [self onQueue:FBLPromise.defaultDispatchQueue timeout:interval];
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
- (FBLPromise *)onQueue:(dispatch_queue_t)queue timeout:(NSTimeInterval)interval {
|
|
|
28 |
NSParameterAssert(queue);
|
|
|
29 |
|
|
|
30 |
FBLPromise *promise = [[FBLPromise alloc] initPending];
|
|
|
31 |
[self observeOnQueue:queue
|
|
|
32 |
fulfill:^(id __nullable value) {
|
|
|
33 |
[promise fulfill:value];
|
|
|
34 |
}
|
|
|
35 |
reject:^(NSError *error) {
|
|
|
36 |
[promise reject:error];
|
|
|
37 |
}];
|
|
|
38 |
typeof(self) __weak weakPromise = promise;
|
|
|
39 |
dispatch_after(dispatch_time(0, (int64_t)(interval * NSEC_PER_SEC)), queue, ^{
|
|
|
40 |
NSError *timedOutError = [[NSError alloc] initWithDomain:FBLPromiseErrorDomain
|
|
|
41 |
code:FBLPromiseErrorCodeTimedOut
|
|
|
42 |
userInfo:nil];
|
|
|
43 |
[weakPromise reject:timedOutError];
|
|
|
44 |
});
|
|
|
45 |
return promise;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
@end
|
|
|
49 |
|
|
|
50 |
@implementation FBLPromise (DotSyntax_TimeoutAdditions)
|
|
|
51 |
|
|
|
52 |
- (FBLPromise* (^)(NSTimeInterval))timeout {
|
|
|
53 |
return ^(NSTimeInterval interval) {
|
|
|
54 |
return [self timeout:interval];
|
|
|
55 |
};
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
- (FBLPromise* (^)(dispatch_queue_t, NSTimeInterval))timeoutOn {
|
|
|
59 |
return ^(dispatch_queue_t queue, NSTimeInterval interval) {
|
|
|
60 |
return [self onQueue:queue timeout:interval];
|
|
|
61 |
};
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
@end
|