You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ti...@apache.org on 2015/09/09 02:18:03 UTC

cordova-plugin-device-orientation git commit: CB-9426 Fix exception when using device orientation plugin on browser platform.

Repository: cordova-plugin-device-orientation
Updated Branches:
  refs/heads/master 4271adb2c -> eb976dc65


CB-9426 Fix exception when using device orientation plugin on browser platform.

The plugin's plugin.xml defines a general <js-module> with the name 'compass'. It defines another <js-module> for the browser platform (that merges with the general module) with the same name. Modules with the same name is not allowed, so Cordova throws an exception at runtime when trying to define the browser version of the module.

Since the only difference in the browser version of the module is to fake up deviceorientation events, I've deleted the browser version and added that logic to the general module, with the following change: rather than starting a new event timer everytime watchHeading() is called (which could happen multiple times resulting in multiple superfluous timers), we have a single event timer going as long as there are active watchers.


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/commit/eb976dc6
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/tree/eb976dc6
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/diff/eb976dc6

Branch: refs/heads/master
Commit: eb976dc650d954c721967e2a26808f1cd15635c5
Parents: 4271adb
Author: Tim Barham <ti...@microsoft.com>
Authored: Wed Jul 29 15:40:06 2015 +1000
Committer: Tim Barham <ti...@microsoft.com>
Committed: Wed Jul 29 15:40:06 2015 +1000

----------------------------------------------------------------------
 plugin.xml             |   4 --
 src/browser/compass.js | 109 --------------------------------------------
 www/compass.js         |  17 ++++++-
 3 files changed, 16 insertions(+), 114 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/eb976dc6/plugin.xml
----------------------------------------------------------------------
diff --git a/plugin.xml b/plugin.xml
index 87e9601..5a05cd0 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -169,9 +169,5 @@
         <js-module src="src/browser/CompassProxy.js" name="CompassProxy">
             <runs/>
         </js-module>
-
-        <js-module src="src/browser/compass.js" name="compass">
-            <merges target="navigator.compass" />
-        </js-module>
     </platform>
 </plugin>

http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/eb976dc6/src/browser/compass.js
----------------------------------------------------------------------
diff --git a/src/browser/compass.js b/src/browser/compass.js
deleted file mode 100644
index e705d77..0000000
--- a/src/browser/compass.js
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
-*/
-
-var argscheck = require('cordova/argscheck'),
-    exec = require('cordova/exec'),
-    utils = require('cordova/utils'),
-    CompassHeading = require('./CompassHeading'),
-    CompassError = require('./CompassError'),
-
-    timers = {},
-    compass = {
-        /**
-         * Asynchronously acquires the current heading.
-         * @param {Function} successCallback The function to call when the heading
-         * data is available
-         * @param {Function} errorCallback The function to call when there is an error
-         * getting the heading data.
-         * @param {CompassOptions} options The options for getting the heading data (not used).
-         */
-        getCurrentHeading:function(successCallback, errorCallback, options) {
-            argscheck.checkArgs('fFO', 'compass.getCurrentHeading', arguments);
-
-            var win = function(result) {
-                var ch = new CompassHeading(result.magneticHeading, result.trueHeading, result.headingAccuracy, result.timestamp);
-                successCallback(ch);
-            };
-            var fail = errorCallback && function(code) {
-                var ce = new CompassError(code);
-                errorCallback(ce);
-            };
-
-            // Get heading
-            exec(win, fail, "Compass", "getHeading", [options]);
-        },
-
-        /**
-         * Asynchronously acquires the heading repeatedly at a given interval.
-         * @param {Function} successCallback The function to call each time the heading
-         * data is available
-         * @param {Function} errorCallback The function to call when there is an error
-         * getting the heading data.
-         * @param {HeadingOptions} options The options for getting the heading data
-         * such as timeout and the frequency of the watch. For iOS, filter parameter
-         * specifies to watch via a distance filter rather than time.
-         */
-        watchHeading:function(successCallback, errorCallback, options) {
-            argscheck.checkArgs('fFO', 'compass.watchHeading', arguments);
-            // Default interval (100 msec)
-            var frequency = (options !== undefined && options.frequency !== undefined) ? options.frequency : 100;
-            var filter = (options !== undefined && options.filter !== undefined) ? options.filter : 0;
-
-            var id = utils.createUUID();
-            if (filter > 0) {
-                // is an iOS request for watch by filter, no timer needed
-                timers[id] = "iOS";
-                compass.getCurrentHeading(successCallback, errorCallback, options);
-            } else {
-                // Start watch timer to get headings
-                timers[id] = window.setInterval(function() {
-                    compass.getCurrentHeading(successCallback, errorCallback);
-                }, frequency);
-            }
-
-            // Continuously calls deviceorientation listener and gets new heading values
-            deviceorientation = new Event('deviceorientation');
-            window.setInterval(function() {
-                window.dispatchEvent(deviceorientation);
-            }, 200);
-
-            return id;
-        },
-
-        /**
-         * Clears the specified heading watch.
-         * @param {String} watchId The ID of the watch returned from #watchHeading.
-         */
-        clearWatch:function(id) {
-            // Stop javascript timer & remove from timer list
-            if (id && timers[id]) {
-                if (timers[id] != "iOS") {
-                    clearInterval(timers[id]);
-                } else {
-                    // is iOS watch by filter so call into device to stop
-                    exec(null, null, "Compass", "stopHeading", []);
-                }
-                delete timers[id];
-            }
-        }
-    };
-
-module.exports = compass;

http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/eb976dc6/www/compass.js
----------------------------------------------------------------------
diff --git a/www/compass.js b/www/compass.js
index d2be118..603e727 100644
--- a/www/compass.js
+++ b/www/compass.js
@@ -26,6 +26,7 @@ var argscheck = require('cordova/argscheck'),
     CompassError = require('./CompassError'),
 
     timers = {},
+    eventTimerId = null,
     compass = {
         /**
          * Asynchronously acquires the current heading.
@@ -79,12 +80,20 @@ var argscheck = require('cordova/argscheck'),
                 }, frequency);
             }
 
+            if (cordova.platformId === 'browser' && !eventTimerId) {
+                // Start firing deviceorientation events if haven't already
+                var deviceorientationEvent = new Event('deviceorientation');
+                eventTimerId = window.setInterval(function() {
+                    window.dispatchEvent(deviceorientationEvent);
+                }, 200);
+            }
+
             return id;
         },
 
         /**
          * Clears the specified heading watch.
-         * @param {String} watchId The ID of the watch returned from #watchHeading.
+         * @param {String} id The ID of the watch returned from #watchHeading.
          */
         clearWatch:function(id) {
             // Stop javascript timer & remove from timer list
@@ -96,6 +105,12 @@ var argscheck = require('cordova/argscheck'),
                     exec(null, null, "Compass", "stopHeading", []);
                 }
                 delete timers[id];
+
+                if (eventTimerId && Object.keys(timers).length === 0) {
+                    // No more watchers, so stop firing 'deviceorientation' events
+                    window.clearInterval(eventTimerId);
+                    eventTimerId = null;
+                }
             }
         }
     };


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org