Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Objective-C convenience methods for abstracting timing dependency
- (void)example
{
NSTextView * console = [self xcodeConsoleIn: IDEWindow];
NSInteger baseLength = [[console string] length] + [command length];
[_log appendFormat: @"\n Writing into console: %@", command];
[console insertText: command];
[console insertNewline:nil];
[self waitFor:@"the console to write" waitBlock:^BOOL{
// Wait until the console text has grown to include the command
NSInteger newLength = [[console string] length];
if (newLength < baseLength)
return NO;
// Wait until the console has printed (lldb) indicating it's
// ready for the next input.
if ([[[console string] substringFromIndex: baseLength] rangeOfString: @"(lldb)"].location == NSNotFound)
return NO;
// proceed to ready block
return YES;
} readyBlock:^{
NSString * newText = [self contentsOf:console after: baseLength];
[_log appendFormat: @"\n Read from console: %@", newText];
callback(newText);
}];
}
- (void)waitFor:(NSString*)description waitBlock:(BOOL(^)())conditionalBlock readyBlock:(void(^)())finalBlock
{
[self waitFor:description waitBlock:conditionalBlock readyBlock:finalBlock tries: 40];
}
- (void)waitFor:(NSString*)description waitBlock:(BOOL(^)())conditionalBlock readyBlock:(void(^)())finalBlock tries:(int)remaining
{
if (remaining == 0)
return [self showAlert: [NSString stringWithFormat: @"Setup did not complete successfully. The Spark Inspector timed out waiting for %@.", description]];
[self after:INTERFACE_POLL_INTERVAL execute:^{
if (conditionalBlock()) {
if (finalBlock)
finalBlock();
return;
}
[self waitFor:description waitBlock:conditionalBlock readyBlock:finalBlock tries: remaining - 1];
}];
}
- (void)after:(NSTimeInterval)interval execute:(void(^)())block
{
double delayInSeconds = interval;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
block();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment