Add CDVCommandDelegate::sendPluginResult and use it in CDVEcho.

This new function optimizes the case where a plugin callback calls
exec().


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/ba7d7713
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/ba7d7713
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/ba7d7713

Branch: refs/heads/master
Commit: ba7d771306a51c180da7a5541c4738f08a8cac4d
Parents: 9e1aa3d
Author: Andrew Grieve <agri...@chromium.org>
Authored: Thu Oct 4 11:13:24 2012 -0400
Committer: Andrew Grieve <agri...@chromium.org>
Committed: Thu Oct 4 14:57:50 2012 -0400

----------------------------------------------------------------------
 CordovaLib/Classes/CDVCommandDelegate.h |    4 ++++
 CordovaLib/Classes/CDVEcho.m            |    4 ++--
 CordovaLib/Classes/CDVPlugin.h          |    3 ++-
 CordovaLib/Classes/CDVViewController.m  |   18 ++++++++++++++++++
 4 files changed, 26 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/ba7d7713/CordovaLib/Classes/CDVCommandDelegate.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCommandDelegate.h 
b/CordovaLib/Classes/CDVCommandDelegate.h
index 116f35c..7acf3e4 100644
--- a/CordovaLib/Classes/CDVCommandDelegate.h
+++ b/CordovaLib/Classes/CDVCommandDelegate.h
@@ -20,6 +20,7 @@
 #import "CDVInvokedUrlCommand.h"
 
 @class CDVPlugin;
+@class CDVPluginResult;
 
 @protocol CDVCommandDelegate <NSObject>
 
@@ -31,5 +32,8 @@
 // being made. Instead, they should use getCommandInstance and call methods
 // directly.
 - (BOOL)execute:(CDVInvokedUrlCommand*)command;
+// Sends a plugin result to the JS. The success/failure is inferred from the
+// plugin's status.
+- (void)sendPluginResult:(CDVPluginResult*)result 
callbackId:(NSString*)callbackId;
 
 @end

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/ba7d7713/CordovaLib/Classes/CDVEcho.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVEcho.m b/CordovaLib/Classes/CDVEcho.m
index 6247dd3..9cda957 100644
--- a/CordovaLib/Classes/CDVEcho.m
+++ b/CordovaLib/Classes/CDVEcho.m
@@ -26,12 +26,12 @@
 {
     CDVPluginResult* pluginResult = [CDVPluginResult 
resultWithStatus:CDVCommandStatus_OK messageAsString:[command.arguments 
objectAtIndex:0]];
 
-    [self success:pluginResult callbackId:command.callbackId];
+    [self.commandDelegate sendPluginResult:pluginResult 
callbackId:command.callbackId];
 }
 
 - (void)echoAsyncHelper:(NSArray*)args
 {
-    [self success:[args objectAtIndex:0] callbackId:[args objectAtIndex:1]];
+    [self.commandDelegate sendPluginResult:[args objectAtIndex:0] 
callbackId:[args objectAtIndex:1]];
 }
 
 - (void)echoAsync:(CDVInvokedUrlCommand*)command

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/ba7d7713/CordovaLib/Classes/CDVPlugin.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVPlugin.h b/CordovaLib/Classes/CDVPlugin.h
index 59d3a5c..16334c8 100644
--- a/CordovaLib/Classes/CDVPlugin.h
+++ b/CordovaLib/Classes/CDVPlugin.h
@@ -41,7 +41,7 @@
 - (void)handleOpenURL:(NSNotification*)notification;
 - (void)onAppTerminate;
 - (void)onMemoryWarning;
-- (void) onReset;
+- (void)onReset;
 
 /*
  // see initWithWebView implementation
@@ -53,6 +53,7 @@
 
 - (id)appDelegate;
 
+// TODO(agrieve): Deprecate these in favour of using CDVCommandDelegate 
directly.
 - (NSString*)writeJavascript:(NSString*)javascript;
 - (NSString*)success:(CDVPluginResult*)pluginResult 
callbackId:(NSString*)callbackId;
 - (NSString*)error:(CDVPluginResult*)pluginResult 
callbackId:(NSString*)callbackId;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/ba7d7713/CordovaLib/Classes/CDVViewController.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVViewController.m 
b/CordovaLib/Classes/CDVViewController.m
index ddf9253..c6251c2 100644
--- a/CordovaLib/Classes/CDVViewController.m
+++ b/CordovaLib/Classes/CDVViewController.m
@@ -795,6 +795,24 @@ BOOL gSplashScreenShown = NO;
 
 #pragma mark CordovaCommands
 
+- (void)sendPluginResult:(CDVPluginResult*)result 
callbackId:(NSString*)callbackId
+{
+    int status = [result.status intValue];
+    BOOL keepCallback = [result.keepCallback boolValue];
+    id message = result.message == nil ?[NSNull null] : result.message;
+
+    // Use an array to encode the message as JSON.
+    message = [NSArray arrayWithObject:message];
+    NSString* encodedMessage = [message cdvjk_JSONString];
+    // And then strip off the outer []s.
+    encodedMessage = [encodedMessage substringWithRange:NSMakeRange(1, 
[encodedMessage length] - 2)];
+    NSString* js = [NSString 
stringWithFormat:@"cordova.require('cordova/exec').nativeCallback('%@',%d,%@,%d)",
+        callbackId, status, encodedMessage, keepCallback];
+
+    NSString* commandsJSON = [self.webView 
stringByEvaluatingJavaScriptFromString:js];
+    [_commandQueue enqueCommandBatch:commandsJSON];
+}
+
 - (BOOL)execute:(CDVInvokedUrlCommand*)command
 {
     return [_commandQueue execute:command];

Reply via email to