Updated Branches: refs/heads/462 7b6ae77e5 -> a58653cf0
[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/a58653cf Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/a58653cf Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/a58653cf Branch: refs/heads/462 Commit: a58653cf08ccc2980f92af48397b422918b21966 Parents: 7b6ae77 Author: Fil Maj <maj....@gmail.com> Authored: Fri May 18 10:54:52 2012 -0700 Committer: Fil Maj <maj....@gmail.com> Committed: Fri May 18 10:54:52 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/a58653cf/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); } } };