@@ -27,8 +27,6 @@ const PositionError = require('./PositionError');
27
27
// So we use additional map and own ids to return watch id synchronously.
28
28
const pluginToNativeWatchMap = { } ;
29
29
30
- const timers = { } ; // list of timers in use
31
-
32
30
// Returns a timeout failure, closed over a specified timeout value and error callback.
33
31
function createTimeout ( errorCallback , timeout ) {
34
32
let t = setTimeout ( function ( ) {
@@ -42,8 +40,37 @@ function createTimeout (errorCallback, timeout) {
42
40
return t ;
43
41
}
44
42
43
+ // Returns default params, overrides if provided with values
44
+ function parseParameters ( options ) {
45
+ const opt = {
46
+ maximumAge : 0 ,
47
+ enableHighAccuracy : false ,
48
+ timeout : Infinity
49
+ } ;
50
+
51
+ if ( options ) {
52
+ if ( options . maximumAge !== undefined && ! isNaN ( options . maximumAge ) && options . maximumAge > 0 ) {
53
+ opt . maximumAge = options . maximumAge ;
54
+ }
55
+ if ( options . enableHighAccuracy !== undefined ) {
56
+ opt . enableHighAccuracy = options . enableHighAccuracy ;
57
+ }
58
+ if ( options . timeout !== undefined && ! isNaN ( options . timeout ) ) {
59
+ if ( options . timeout < 0 ) {
60
+ opt . timeout = 0 ;
61
+ } else {
62
+ opt . timeout = options . timeout ;
63
+ }
64
+ }
65
+ }
66
+
67
+ return opt ;
68
+ }
69
+
45
70
module . exports = {
46
71
getCurrentPosition : function ( success , error , args ) {
72
+ args = parseParameters ( args ) ;
73
+
47
74
// Timer var that will fire an error callback if no position is retrieved from native
48
75
// before the "timeout" param provided expires
49
76
const timeoutTimer = { timer : null } ;
@@ -54,17 +81,19 @@ module.exports = {
54
81
if ( typeof args === 'undefined' ) args = { } ;
55
82
args . enableHighAccuracy = true ;
56
83
}
57
- const geo = cordova . require ( 'cordova/modulemapper' ) . getOriginalSymbol ( window , 'navigator.geolocation' ) ; // eslint-disable-line no-undef
58
- geo . getCurrentPosition ( ( position ) => {
59
- clearTimeout ( timeoutTimer . timer ) ;
60
- if ( ! timeoutTimer . timer ) {
61
- // Timeout already happened, or native fired error callback for
62
- // this geo request.
84
+ // Timeout already happened, or native fired error callback for this geo request.
85
+ // Don't continue with success callback.
86
+ if ( timeoutTimer . timer ) {
87
+ const geo = cordova . require ( 'cordova/modulemapper' ) . getOriginalSymbol ( window , 'navigator.geolocation' ) ; // eslint-disable-line no-undef
88
+ geo . getCurrentPosition ( ( position ) => {
89
+ clearTimeout ( timeoutTimer . timer ) ;
90
+ // Timeout already happened, or native fired error callback for this geo request.
63
91
// Don't continue with success callback.
64
- return ;
65
- }
66
- success ( position ) ;
67
- } , error , args ) ;
92
+ if ( timeoutTimer . timer ) {
93
+ success ( position ) ;
94
+ }
95
+ } , error , args ) ;
96
+ }
68
97
} ;
69
98
const fail = function ( ) {
70
99
clearTimeout ( timeoutTimer . timer ) ;
@@ -74,11 +103,11 @@ module.exports = {
74
103
}
75
104
} ;
76
105
77
- if ( options . timeout !== Infinity ) {
106
+ if ( args . timeout !== Infinity ) {
78
107
// If the timeout value was not set to Infinity (default), then
79
108
// set up a timeout function that will fire the error callback
80
109
// if no successful position was retrieved before timeout expired.
81
- timeoutTimer . timer = createTimeout ( fail , options . timeout ) ;
110
+ timeoutTimer . timer = createTimeout ( error , args . timeout ) ;
82
111
} else {
83
112
// This is here so the check in the win function doesn't mess stuff up
84
113
// may seem weird but this guarantees timeoutTimer is
0 commit comments