You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2012/11/05 18:54:05 UTC

[3/4] js commit: [ios] Tweak exec bridge logic to not send excess XHRs.

[ios] Tweak exec bridge logic to not send excess XHRs.


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

Branch: refs/heads/master
Commit: 86411246aba4292734b9bb46dc9128e28391e424
Parents: c3517e7
Author: Andrew Grieve <ag...@chromium.org>
Authored: Sun Nov 4 17:37:19 2012 -0800
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Mon Nov 5 12:53:37 2012 -0500

----------------------------------------------------------------------
 lib/ios/exec.js |   40 ++++++++++++++++++++--------------------
 1 files changed, 20 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/86411246/lib/ios/exec.js
----------------------------------------------------------------------
diff --git a/lib/ios/exec.js b/lib/ios/exec.js
index c6b8edd..7ce52d9 100644
--- a/lib/ios/exec.js
+++ b/lib/ios/exec.js
@@ -19,12 +19,12 @@
  *
 */
 
-    /**
-     * Creates a gap bridge iframe used to notify the native code about queued
-     * commands.
-     *
-     * @private
-     */
+/**
+ * Creates a gap bridge iframe used to notify the native code about queued
+ * commands.
+ *
+ * @private
+ */
 var cordova = require('cordova'),
     channel = require('cordova/channel'),
     utils = require('cordova/utils'),
@@ -34,10 +34,7 @@ var cordova = require('cordova'),
         XHR_WITH_PAYLOAD: 2,
         XHR_OPTIONAL_PAYLOAD: 3
     },
-    // XHR mode does not work on iOS 4.2, so default to IFRAME_NAV for such devices.
-    // XHR mode's main advantage is working around a bug in -webkit-scroll, which
-    // doesn't exist in 4.X devices anyways.
-    bridgeMode = navigator.userAgent.indexOf(' 4_') == -1 ? jsToNativeModes.XHR_NO_PAYLOAD : jsToNativeModes.IFRAME_NAV,
+    bridgeMode,
     execIframe,
     execXhr,
     requestCount = 0,
@@ -67,10 +64,11 @@ function shouldBundleCommandJson() {
 }
 
 function iOSExec() {
-    if (channel.onCordovaReady.state != 2) {
-        utils.alert("ERROR: Attempting to call cordova.exec()" +
-              " before 'deviceready'. Ignoring.");
-        return;
+    // XHR mode does not work on iOS 4.2, so default to IFRAME_NAV for such devices.
+    // XHR mode's main advantage is working around a bug in -webkit-scroll, which
+    // doesn't exist in 4.X devices anyways.
+    if (bridgeMode == undefined) {
+        bridgeMode = navigator.userAgent.indexOf(' 4_') == -1 ? jsToNativeModes.XHR_NO_PAYLOAD : jsToNativeModes.IFRAME_NAV;
     }
 
     var successCallback, failCallback, service, action, actionArgs, splitCommand;
@@ -111,16 +109,18 @@ function iOSExec() {
     // the command is executed.
     commandQueue.push(JSON.stringify(command));
 
-    if (!isInContextOfEvalJs) {
+    // If we're in the context of a stringByEvaluatingJavaScriptFromString call,
+    // then the queue will be flushed when it returns; no need for a poke.
+    // Also, if there is already a command in the queue, then we've already
+    // poked the native side, so there is no reason to do so again.
+    if (!isInContextOfEvalJs && commandQueue.length == 1) {
         if (bridgeMode != jsToNativeModes.IFRAME_NAV) {
-            // Re-using the XHR improves exec() performance by about 10%.
-            // It is possible for a native stringByEvaluatingJavascriptFromString call
-            // to cause us to reach this point when a request is already in progress,
-            // so we check the readyState to guard agains re-using an inprogress XHR.
-            // Refer to CB-1404.
+            // This prevents sending an XHR when there is already one being sent.
+            // This should happen only in rare circumstances (refer to unit tests).
             if (execXhr && execXhr.readyState != 4) {
                 execXhr = null;
             }
+            // Re-using the XHR improves exec() performance by about 10%.
             execXhr = execXhr || new XMLHttpRequest();
             // Changing this to a GET will make the XHR reach the URIProtocol on 4.2.
             // For some reason it still doesn't work though...