Updated Branches:
  refs/heads/master 72bf85c23 -> 2d15c4045

[all] Refactor network.js a bit. No functional change.


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

Branch: refs/heads/master
Commit: 2d15c4045aab52f8b2d728bd90244894258b3b47
Parents: 557dab7
Author: Andrew Grieve <[email protected]>
Authored: Wed Nov 7 15:15:31 2012 -0500
Committer: Andrew Grieve <[email protected]>
Committed: Wed Nov 7 15:17:56 2012 -0500

----------------------------------------------------------------------
 lib/common/plugin/network.js |   88 +++++++++++++++++-------------------
 1 files changed, 42 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/2d15c404/lib/common/plugin/network.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/network.js b/lib/common/plugin/network.js
index adaba5a..63736a9 100644
--- a/lib/common/plugin/network.js
+++ b/lib/common/plugin/network.js
@@ -33,49 +33,9 @@ if (typeof navigator != 'undefined') {
     });
 }
 
-var NetworkConnection = function () {
-    this.type = null;
-    this._firstRun = true;
-    this._timer = null;
-    this.timeout = 500;
-
-    var me = this;
-
-    channel.onCordovaReady.subscribe(function() {
-        me.getInfo(function (info) {
-            me.type = info;
-            if (info === "none") {
-                // set a timer if still offline at the end of timer send the 
offline event
-                me._timer = setTimeout(function(){
-                    cordova.fireDocumentEvent("offline");
-                    me._timer = null;
-                    }, me.timeout);
-            } else {
-                // If there is a current offline event pending clear it
-                if (me._timer !== null) {
-                    clearTimeout(me._timer);
-                    me._timer = null;
-                }
-                cordova.fireDocumentEvent("online");
-            }
-
-            // should only fire this once
-            if (me._firstRun) {
-                me._firstRun = false;
-                channel.onCordovaConnectionReady.fire();
-            }
-        },
-        function (e) {
-            // If we can't get the network info we should still tell Cordova
-            // to fire the deviceready event.
-            if (me._firstRun) {
-                me._firstRun = false;
-                channel.onCordovaConnectionReady.fire();
-            }
-            console.log("Error initializing Network Connection: " + e);
-        });
-    });
-};
+function NetworkConnection() {
+    this.type = 'unknown';
+}
 
 /**
  * Get connection info
@@ -83,9 +43,45 @@ var NetworkConnection = function () {
  * @param {Function} successCallback The function to call when the Connection 
data is available
  * @param {Function} errorCallback The function to call when there is an error 
getting the Connection data. (OPTIONAL)
  */
-NetworkConnection.prototype.getInfo = function (successCallback, 
errorCallback) {
-    // Get info
+NetworkConnection.prototype.getInfo = function(successCallback, errorCallback) 
{
     exec(successCallback, errorCallback, "NetworkStatus", "getConnectionInfo", 
[]);
 };
 
-module.exports = new NetworkConnection();
+var me = new NetworkConnection();
+var timerId = null;
+var timeout = 500;
+
+channel.onCordovaReady.subscribe(function() {
+    me.getInfo(function(info) {
+        me.type = info;
+        if (info === "none") {
+            // set a timer if still offline at the end of timer send the 
offline event
+            timerId = setTimeout(function(){
+                cordova.fireDocumentEvent("offline");
+                timerId = null;
+            }, timeout);
+        } else {
+            // If there is a current offline event pending clear it
+            if (timerId !== null) {
+                clearTimeout(timerId);
+                timerId = null;
+            }
+            cordova.fireDocumentEvent("online");
+        }
+
+        // should only fire this once
+        if (channel.onCordovaConnectionReady.state !== 2) {
+            channel.onCordovaConnectionReady.fire();
+        }
+    },
+    function (e) {
+        // If we can't get the network info we should still tell Cordova
+        // to fire the deviceready event.
+        if (channel.onCordovaConnectionReady.state !== 2) {
+            channel.onCordovaConnectionReady.fire();
+        }
+        console.log("Error initializing Network Connection: " + e);
+    });
+});
+
+module.exports = me;

Reply via email to