-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYPoller.m
48 lines (42 loc) · 1.27 KB
/
SSYPoller.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#import "SSYPoller.h"
@implementation SSYPoller
+ (BOOL)waitUntilInvocation:(NSInvocation*)invocation
initialBackoff:(NSTimeInterval)initialBackoff
backoffFactor:(CGFloat)backoffFactor
maxBackoff:(NSTimeInterval)maxBackoff
timeout:(NSTimeInterval)timeout
timeLimit:(NSTimeInterval)timeLimit
debugLabel:(NSString*)debugLabel
error_p:(NSError**)error_p {
NSTimeInterval backoff = initialBackoff ;
NSDate* deadline = [NSDate dateWithTimeIntervalSinceNow:timeout] ;
BOOL clearToGo ;
while ([(NSDate*)[NSDate date] compare:deadline] == NSOrderedAscending) {
[invocation invoke] ;
[invocation getReturnValue:&clearToGo] ;
if (clearToGo) {
return YES ;
}
else {
usleep( 1000000 * backoff ) ;
backoff = backoff * backoffFactor ;
backoff = MIN(backoff, maxBackoff) ;
}
}
// Timeout
if (error_p) {
NSString* msg = [NSString stringWithFormat:
@"Timeout %g secs exceeded waiting for %@",
timeout,
debugLabel // may be nil
] ;
*error_p = [NSError errorWithDomain:@"SSYPoller"
code:ETIME
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
msg, NSLocalizedDescriptionKey,
debugLabel, @"Waiting for", // may be nil ;
nil]] ;
}
return NO ;
}
@end