You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2012/08/24 23:57:39 UTC

[46/50] [abbrv] js commit: [android] Separate start() from sendXhr() in callback.js

[android] Separate start() from sendXhr() in callback.js

Commit has no change in behaviour.


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/2e6cb6e2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/2e6cb6e2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/2e6cb6e2

Branch: refs/heads/master
Commit: 2e6cb6e27bceb0089a3c90ed6693f61dc20f70ce
Parents: eb6ba61
Author: Andrew Grieve <ag...@chromium.org>
Authored: Mon Aug 20 11:11:32 2012 -0400
Committer: Anis Kadri <an...@gmail.com>
Committed: Fri Aug 24 13:49:58 2012 -0700

----------------------------------------------------------------------
 lib/android/plugin/android/callback.js |  101 ++++++++++++++-------------
 1 files changed, 52 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/2e6cb6e2/lib/android/plugin/android/callback.js
----------------------------------------------------------------------
diff --git a/lib/android/plugin/android/callback.js b/lib/android/plugin/android/callback.js
index 4a5b7bf..127740b 100644
--- a/lib/android/plugin/android/callback.js
+++ b/lib/android/plugin/android/callback.js
@@ -2,60 +2,64 @@ var port = null,
     token = null,
     xmlhttp;
 
-module.exports = {
-    start: function callback() {
-        // cordova/exec depends on this module, so we can't require cordova/exec on the module level.
-        var exec = require('cordova/exec'),
-        xmlhttp = new XMLHttpRequest();
-
-        // Callback function when XMLHttpRequest is ready
-        xmlhttp.onreadystatechange=function(){
-            if (!xmlhttp) {
-                return;
-            }
-            if(xmlhttp.readyState === 4){
-                // If callback has JavaScript statement to execute
-                if (xmlhttp.status === 200) {
+function startXhr() {
+    // cordova/exec depends on this module, so we can't require cordova/exec on the module level.
+    var exec = require('cordova/exec'),
+    xmlhttp = new XMLHttpRequest();
 
-                    // Need to url decode the response
-                    var msg = decodeURIComponent(xmlhttp.responseText);
-                    setTimeout(function() {
-                        try {
-                            var t = eval(msg);
-                        }
-                        catch (e) {
-                            // If we're getting an error here, seeing the message will help in debugging
-                            console.log("JSCallback: Message from Server: " + msg);
-                            console.log("JSCallback Error: "+e);
-                        }
-                    }, 1);
-                    setTimeout(callback, 1);
-                }
+    // Callback function when XMLHttpRequest is ready
+    xmlhttp.onreadystatechange=function(){
+        if (!xmlhttp) {
+            return;
+        }
+        if (xmlhttp.readyState === 4){
+            // If callback has JavaScript statement to execute
+            if (xmlhttp.status === 200) {
 
-                // If callback ping (used to keep XHR request from timing out)
-                else if (xmlhttp.status === 404) {
-                    setTimeout(callback, 10);
-                }
+                // Need to url decode the response
+                var msg = decodeURIComponent(xmlhttp.responseText);
+                setTimeout(function() {
+                    try {
+                        var t = eval(msg);
+                    }
+                    catch (e) {
+                        // If we're getting an error here, seeing the message will help in debugging
+                        console.log("JSCallback: Message from Server: " + msg);
+                        console.log("JSCallback Error: "+e);
+                    }
+                }, 1);
+                setTimeout(startXhr, 1);
+            }
 
-                // 0 == Page is unloading.
-                // 400 == Bad request.
-                // 403 == invalid token.
-                // 503 == server stopped.
-                else {
-                    console.log("JSCallback Error: Request failed with status " + xmlhttp.status);
-                    exec.setNativeToJsBridgeMode(exec.nativeToJsModes.POLLING);
-                }
+            // If callback ping (used to keep XHR request from timing out)
+            else if (xmlhttp.status === 404) {
+                setTimeout(startXhr, 10);
             }
-        };
 
-        if (port === null) {
-            port = prompt("getPort", "gap_callbackServer:");
-        }
-        if (token === null) {
-            token = prompt("getToken", "gap_callbackServer:");
+            // 0 == Page is unloading.
+            // 400 == Bad request.
+            // 403 == invalid token.
+            // 503 == server stopped.
+            else {
+                console.log("JSCallback Error: Request failed with status " + xmlhttp.status);
+                exec.setNativeToJsBridgeMode(exec.nativeToJsModes.POLLING);
+            }
         }
-        xmlhttp.open("GET", "http://127.0.0.1:"+port+"/"+token , true);
-        xmlhttp.send();
+    };
+
+    if (port === null) {
+        port = prompt("getPort", "gap_callbackServer:");
+    }
+    if (token === null) {
+        token = prompt("getToken", "gap_callbackServer:");
+    }
+    xmlhttp.open("GET", "http://127.0.0.1:"+port+"/"+token , true);
+    xmlhttp.send();
+}
+
+module.exports = {
+    start: function() {
+        startXhr();
     },
 
     stop: function() {
@@ -69,6 +73,5 @@ module.exports = {
     isAvailable: function() {
         return ("true" != prompt("usePolling", "gap_callbackServer:"));
     }
-
 };