Updated Branches: refs/heads/master 6315a3247 -> aece7e97f
[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/aece7e97 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/aece7e97 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/aece7e97 Branch: refs/heads/master Commit: aece7e97fd214818269175978ea1240c5d14cb2c Parents: ff0fd30 Author: Andrew Grieve <agri...@chromium.org> Authored: Mon Aug 20 11:11:32 2012 -0400 Committer: Andrew Grieve <agri...@chromium.org> Committed: Mon Aug 20 11:11:32 2012 -0400 ---------------------------------------------------------------------- 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/aece7e97/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:")); } - };