Replace -[ProtocolClient sendCustomCommandWithFormat:...] with a block-based
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 10 Oct 2015 16:29:28 +0000 (12:29 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 10 Oct 2015 17:17:17 +0000 (13:17 -0400)
version.

Source/DebuggerBackEnd.m
Source/ProtocolClient.h
Source/ProtocolClient.m

index a06066deaa96bdb3aa977db834298abfc27587a9..c9c164d7cccd6ca6586560ad1cd5fd54a29928e1 100644 (file)
@@ -32,7 +32,6 @@
 - (void)rebuildStack:(NSXMLDocument*)response;
 - (void)getStackFrame:(NSXMLDocument*)response;
 - (void)propertiesReceived:(NSXMLDocument*)response;
-- (void)evalScriptReceived:(NSXMLDocument*)response;
 
 @end
 
 
   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 ////////////////////////////////////////////////////
   [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
index 3f9e91b983104578aefe335dc7890cf22db2cc94..9cfa539a454751db4caf9127f2b3c8366edcd288 100644 (file)
@@ -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;
index aa80cb228daabc4ff4b5de6a96fb924ff05b88dc..bb39ee34a923974dee7d987beb1462a5abfdd76f 100644 (file)
   [_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];
 }