From fdd9f64cf78a0f9ae3b3efc9928d84f455337c32 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 10 Oct 2015 12:29:28 -0400 Subject: [PATCH] Replace -[ProtocolClient sendCustomCommandWithFormat:...] with a block-based version. --- Source/DebuggerBackEnd.m | 20 ++++++-------------- Source/ProtocolClient.h | 4 +++- Source/ProtocolClient.m | 18 +++++++++++------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/Source/DebuggerBackEnd.m b/Source/DebuggerBackEnd.m index a06066d..c9c164d 100644 --- a/Source/DebuggerBackEnd.m +++ b/Source/DebuggerBackEnd.m @@ -32,7 +32,6 @@ - (void)rebuildStack:(NSXMLDocument*)response; - (void)getStackFrame:(NSXMLDocument*)response; - (void)propertiesReceived:(NSXMLDocument*)response; -- (void)evalScriptReceived:(NSXMLDocument*)response; @end @@ -252,10 +251,13 @@ char* encodedString = malloc(modp_b64_encode_len([str length])); modp_b64_encode(encodedString, [str UTF8String], [str length]); - NSNumber* tx = [client_ sendCustomCommandWithFormat:@"eval -i {txn} -- %s", encodedString]; + ProtocolClientMessageHandler handler = ^(NSXMLDocument* message) { + NSXMLElement* parent = (NSXMLElement*)[[message rootElement] childAtIndex:0]; + NSString* value = [parent base64DecodedValue]; + [self.delegate scriptWasEvaluatedWithResult:value]; + }; + [client_ sendCustomCommandWithFormat:@"eval -i {txn} -- %s" handler:handler, encodedString]; free(encodedString); - - [self recordCallback:@selector(evalScriptReceived:) forTransaction:tx]; } // Protocol Client Delegate //////////////////////////////////////////////////// @@ -494,16 +496,6 @@ [delegate receivedProperties:children forTransaction:transaction]; } -/** - * Callback from a |-evalScript:| request. - */ -- (void)evalScriptReceived:(NSXMLDocument*)response -{ - NSXMLElement* parent = (NSXMLElement*)[[response rootElement] childAtIndex:0]; - NSString* value = [parent base64DecodedValue]; - [delegate scriptWasEvaluatedWithResult:value]; -} - // Private ///////////////////////////////////////////////////////////////////// - (void)recordCallback:(SEL)callback forTransaction:(NSNumber*)txn diff --git a/Source/ProtocolClient.h b/Source/ProtocolClient.h index 3f9e91b..9cfa539 100644 --- a/Source/ProtocolClient.h +++ b/Source/ProtocolClient.h @@ -48,7 +48,9 @@ typedef void (^ProtocolClientMessageHandler)(NSXMLDocument*); // Sends a command to the debugger. The command must have a substring |{txn}| // within it, which will be replaced with the transaction ID. Use this if // |-sendCommandWithFormat:|'s insertion of the transaction ID is incorrect. -- (NSNumber*)sendCustomCommandWithFormat:(NSString*)format, ...; +- (void)sendCustomCommandWithFormat:(NSString*)format + handler:(ProtocolClientMessageHandler)handler, + ...; - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response; - (NSInteger)transactionIDFromCommand:(NSString*)command; diff --git a/Source/ProtocolClient.m b/Source/ProtocolClient.m index aa80cb2..bb39ee3 100644 --- a/Source/ProtocolClient.m +++ b/Source/ProtocolClient.m @@ -97,21 +97,25 @@ [_messageQueue sendMessage:taggedCommand]; } -- (NSNumber*)sendCustomCommandWithFormat:(NSString*)format, ... { +- (void)sendCustomCommandWithFormat:(NSString*)format + handler:(ProtocolClientMessageHandler)handler, ... { // Collect varargs and format command. va_list args; - va_start(args, format); - NSString* command = [[[NSString alloc] initWithFormat:format arguments:args] autorelease]; + va_start(args, handler); + NSString* command = [[NSString alloc] initWithFormat:format arguments:args]; va_end(args); - NSNumber* callbackKey = [NSNumber numberWithInt:_nextID++]; - NSString* taggedCommand = [command stringByReplacingOccurrencesOfString:@"{txn}" - withString:[callbackKey stringValue]]; + int transaction = _nextID++; + NSString* taggedCommand = + [command stringByReplacingOccurrencesOfString:@"{txn}" + withString:[NSString stringWithFormat:@"%d", transaction]]; + assert(_messageQueue); + [_dispatchTable setObject:[[handler copy] autorelease] forKey:@(transaction)]; [_messageQueue sendMessage:taggedCommand]; - return callbackKey; } + - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response { return [[[[response rootElement] attributeForName:@"transaction_id"] stringValue] intValue]; } -- 2.22.5