From 6d4d26e5ebf79d9173e2e1848cdbc1c78010d43a Mon Sep 17 00:00:00 2001
From: Robert Sesek <rsesek@bluestatic.org>
Date: Sat, 10 Oct 2015 12:40:56 -0400
Subject: [PATCH] Refactor -[DebuggerController fetchChildProperties:].

---
 Source/DebuggerBackEnd.h    | 12 ++++------
 Source/DebuggerBackEnd.m    | 48 ++++++++++++++++---------------------
 Source/DebuggerController.h |  4 ----
 Source/DebuggerController.m | 20 ++++------------
 4 files changed, 29 insertions(+), 55 deletions(-)

diff --git a/Source/DebuggerBackEnd.h b/Source/DebuggerBackEnd.h
index 7c2839c..081f025 100644
--- a/Source/DebuggerBackEnd.h
+++ b/Source/DebuggerBackEnd.h
@@ -86,10 +86,11 @@
 // Evaluates a given string in the current execution context.
 - (void)evalScript:(NSString*)str;
 
-// Gets a property by name from the debugger engine. Returns a transaction ID
-// which used in the delegate callback. Properties must be retrieved at a
-// certain stack depth.
-- (NSInteger)getChildrenOfProperty:(VariableNode*)property atDepth:(NSInteger)depth;
+// Gets a property by name from the debugger engine. Properties must be
+// retrieved at a certain stack depth.
+- (void)getChildrenOfProperty:(VariableNode*)property
+                      atDepth:(NSInteger)depth
+                     callback:(void (^)(NSArray*))callback;
 
 // Takes a partially loaded stack frame and fetches the rest of the information.
 - (void)loadStackFrame:(StackFrame*)frame;
@@ -120,9 +121,6 @@
 // TODO: rename to |-frameUpdated:|.
 - (void)sourceUpdated:(StackFrame*)frame;
 
-// Callback from |-getProperty:|.
-- (void)receivedProperties:(NSArray*)properties forTransaction:(NSInteger)transaction;
-
 // Callback for the result of |-evalScript:|.
 - (void)scriptWasEvaluatedWithResult:(NSString*)result;
 
diff --git a/Source/DebuggerBackEnd.m b/Source/DebuggerBackEnd.m
index c9c164d..2be9ce6 100644
--- a/Source/DebuggerBackEnd.m
+++ b/Source/DebuggerBackEnd.m
@@ -31,7 +31,6 @@
 - (void)debuggerStep:(NSXMLDocument*)response;
 - (void)rebuildStack:(NSXMLDocument*)response;
 - (void)getStackFrame:(NSXMLDocument*)response;
-- (void)propertiesReceived:(NSXMLDocument*)response;
 
 @end
 
@@ -176,11 +175,27 @@
  * Tells the debugger engine to get a specifc property. This also takes in the NSXMLElement
  * that requested it so that the child can be attached.
  */
-- (NSInteger)getChildrenOfProperty:(VariableNode*)property atDepth:(NSInteger)depth;
+- (void)getChildrenOfProperty:(VariableNode*)property
+                      atDepth:(NSInteger)depth
+                     callback:(void (^)(NSArray*))callback
 {
-  NSNumber* tx = [client_ sendCommandWithFormat:@"property_get -d %d -n %@", depth, [property fullName]];
-  [self recordCallback:@selector(propertiesReceived:) forTransaction:tx];
-  return [tx intValue];
+  ProtocolClientMessageHandler handler = ^(NSXMLDocument* message) {
+    /*
+     <response>
+       <property> <!-- this is the one we requested -->
+         <property ... /> <!-- these are what we want -->
+       </property>
+     </repsonse>
+     */
+
+    // Detach all the children so we can insert them into another document.
+    NSXMLElement* parent = (NSXMLElement*)[[message rootElement] childAtIndex:0];
+    NSArray* children = [parent children];
+    [parent setChildren:nil];
+
+    callback(children);
+  };
+  [client_ sendCommandWithFormat:@"property_get -d %d -n %@" handler:handler, depth, [property fullName]];
 }
 
 - (void)loadStackFrame:(StackFrame*)frame
@@ -473,29 +488,6 @@
   }
 }
 
-/**
- * Callback from a |-getProperty:| request.
- */
-- (void)propertiesReceived:(NSXMLDocument*)response
-{
-  NSInteger transaction = [client_ transactionIDFromResponse:response];
-  
-  /*
-   <response>
-     <property> <!-- this is the one we requested -->
-       <property ... /> <!-- these are what we want -->
-     </property>
-   </repsonse>
-   */
-  
-  // Detach all the children so we can insert them into another document.
-  NSXMLElement* parent = (NSXMLElement*)[[response rootElement] childAtIndex:0];
-  NSArray* children = [parent children];
-  [parent setChildren:nil];
-  
-  [delegate receivedProperties:children forTransaction:transaction];
-}
-
 // Private /////////////////////////////////////////////////////////////////////
 
 - (void)recordCallback:(SEL)callback forTransaction:(NSNumber*)txn
diff --git a/Source/DebuggerController.h b/Source/DebuggerController.h
index 864332e..7ea29a2 100644
--- a/Source/DebuggerController.h
+++ b/Source/DebuggerController.h
@@ -29,10 +29,6 @@
   // so upon receipt of the first new stack frame.
   BOOL aboutToClobber_;
 
-  // Dictionary of transcations to tree nodes that are used when properties
-  // are requested from the backend.
-  NSMutableDictionary* pendingProperties_;
-
   IBOutlet NSButton* attachedCheckbox_;
 
   StackController* stackController;
diff --git a/Source/DebuggerController.m b/Source/DebuggerController.m
index 8aa5518..541ad28 100644
--- a/Source/DebuggerController.m
+++ b/Source/DebuggerController.m
@@ -41,7 +41,6 @@
   if (self = [super initWithWindowNibName:@"Debugger"])
   {
     stackController = [[StackController alloc] init];
-    pendingProperties_ = [[NSMutableDictionary alloc] init];
 
     NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
 
@@ -65,7 +64,6 @@
   [connection release];
   [expandedVariables release];
   [stackController release];
-  [pendingProperties_ release];
   [super dealloc];
 }
 
@@ -233,8 +231,10 @@
     return;
   assert([selection count] == 1);
   NSInteger depth = [[selection objectAtIndex:0] index];
-  NSInteger txn = [connection getChildrenOfProperty:node atDepth:depth];
-  [pendingProperties_ setObject:node forKey:[NSNumber numberWithInt:txn]];
+  [connection getChildrenOfProperty:node atDepth:depth callback:^(NSArray* properties) {
+    [node setChildrenFromXMLChildren:properties];
+    [variablesTreeController rearrangeObjects];
+  }];
 }
 
 /**
@@ -372,7 +372,6 @@
 - (void)clobberStack
 {
   aboutToClobber_ = YES;
-  [pendingProperties_ removeAllObjects];
 }
 
 - (void)newStackFrame:(StackFrame*)frame
@@ -392,17 +391,6 @@
   [self updateSourceViewer];
 }
 
-- (void)receivedProperties:(NSArray*)properties forTransaction:(NSInteger)transaction
-{
-  NSNumber* key = [NSNumber numberWithInt:transaction];
-  VariableNode* node = [pendingProperties_ objectForKey:key];
-  if (node) {
-    [node setChildrenFromXMLChildren:properties];
-    [variablesTreeController rearrangeObjects];
-    [pendingProperties_ removeObjectForKey:key];
-  }
-}
-
 - (void)scriptWasEvaluatedWithResult:(NSString*)result
 {
   [EvalController scriptWasEvaluatedWithResult:result];
-- 
2.43.5