incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fil...@apache.org
Subject [1/7] js commit: [CB-462] rewrote accel _again_. weaknesses in my initial draft of it brought to light
Date Fri, 18 May 2012 22:11:21 GMT
Updated Branches:
  refs/heads/master b1673e3e1 -> 58a213de4


[CB-462] rewrote accel _again_. weaknesses in my initial draft of it brought to light


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

Branch: refs/heads/master
Commit: cef430190bf902c80625f8a77859f1a019b1d7f7
Parents: 2d0cef3
Author: Fil Maj <maj.fil@gmail.com>
Authored: Fri May 18 10:54:52 2012 -0700
Committer: Fil Maj <maj.fil@gmail.com>
Committed: Fri May 18 15:14:37 2012 -0700

----------------------------------------------------------------------
 lib/common/plugin/accelerometer.js |   94 ++++++++++++++++++++++++-------
 1 files changed, 74 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/cef43019/lib/common/plugin/accelerometer.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/accelerometer.js b/lib/common/plugin/accelerometer.js
index f0841e0..ae10f6b 100644
--- a/lib/common/plugin/accelerometer.js
+++ b/lib/common/plugin/accelerometer.js
@@ -6,13 +6,57 @@ var utils = require("cordova/utils"),
     exec = require("cordova/exec"),
     Acceleration = require('cordova/plugin/Acceleration');
 
+// Is the accel sensor running?
+var running = false;
 
 // Keeps reference to watchAcceleration calls.
 var timers = {};
 
+// Array of listeners; used to keep track of when we should call start and stop.
+var listeners = [];
+
 // Last returned acceleration object from native
 var accel = null;
 
+// Tells native to start.
+function start() {
+    exec(function(a) {
+        var tempListeners = listeners.slice(0);
+        accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
+        for (var i = 0, l = tempListeners.length; i < l; i++) {
+            tempListeners[i].win(accel);
+        }
+    }, function(e) {
+        var tempListeners = listeners.slice(0);
+        for (var i = 0, l = tempListeners.length; i < l; i++) {
+            tempListeners[i].fail(e);
+        }
+    }, "Accelerometer", "start", []);
+    running = true;
+}
+
+// Tells native to stop.
+function stop() {
+    exec(null, null, "Accelerometer", "stop", []);
+    running = false;
+}
+
+// Adds a callback pair to the listeners array
+function createCallbackPair(win, fail) {
+    return {win:win, fail:fail};
+}
+
+// Removes a win/fail listener pair from the listeners array
+function removeListeners(l) {
+    var idx = listeners.indexOf(l);
+    if (l > -1) {
+        listeners.splice(idx, 1);
+        if (listeners.length === 0) {
+            stop();
+        }
+    }
+}
+
 var accelerometer = {
     /**
      * Asynchronously aquires the current acceleration.
@@ -27,13 +71,22 @@ var accelerometer = {
             throw "getCurrentAcceleration must be called with at least a success callback
function as first parameter.";
         }
 
+        var p;
         var win = function(a) {
-            accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
-            successCallback(accel);
+            successCallback(a);
+            removeListeners(p);
+        };
+        var fail = function(e) {
+            errorCallback(e);
+            removeListeners(p);
         };
 
-        // Get acceleration
-        exec(win, errorCallback, "Accelerometer", "getAcceleration", []);
+        p = createCallbackPair(win, fail);
+        listeners.push(p);
+
+        if (!running) {
+            start();
+        }
     },
 
     /**
@@ -55,24 +108,25 @@ var accelerometer = {
 
         // Keep reference to watch id, and report accel readings as often as defined in frequency
         var id = utils.createUUID();
-        timers[id] = window.setInterval(function() {
-            if (accel) {
-                successCallback(accel);
-            }
-        }, frequency);
 
-        // Success callback from native just updates the accel object.
-        var win = function(a) {
-            accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
-        };
+        var p = createCallbackPair(successCallback, errorCallback);
+        listeners.push(p);
 
-        // Fail callback clears the watch and sends an error back.
-        var fail = function(err) {
-            accelerometer.clearWatch(id);
-            errorCallback(err);
+        timers[id] = {
+            timer:window.setInterval(function() {
+                if (accel) {
+                    successCallback(accel);
+                }
+            }, frequency),
+            listeners:p
         };
 
-        exec(win, fail, "Accelerometer", "addWatch", [id, frequency]);
+        if (running) {
+            // If we're already running then immediately invoke the success callback
+            successCallback(accel);
+        } else {
+            start();
+        }
 
         return id;
     },
@@ -85,9 +139,9 @@ var accelerometer = {
     clearWatch: function(id) {
         // Stop javascript timer & remove from timer list
         if (id && timers[id]) {
-            window.clearInterval(timers[id]);
+            window.clearInterval(timers[id].timer);
             delete timers[id];
-            exec(null, null, "Accelerometer", "clearWatch", [id]);
+            removeListeners(timers[id].listeners);
         }
     }
 };


Mime
View raw message