@@ -27,17 +27,20 @@ const PositionError = require('./PositionError');
2727// So we use additional map and own ids to return watch id synchronously.
2828const pluginToNativeWatchMap = { } ;
2929
30+ // list of timers in use
31+ const timers = { } ;
32+
3033// Returns a timeout failure, closed over a specified timeout value and error callback.
31- function createTimeout ( errorCallback , timeout ) {
32- let t = setTimeout ( function ( ) {
33- clearTimeout ( t ) ;
34- t = null ;
34+ function createTimeout ( errorCallback , timeout , id ) {
35+ timers [ id ] . timer = setTimeout ( function ( ) {
36+ clearTimeout ( timers [ id ] . timer ) ;
37+ timers [ id ] . timer = null ;
3538 errorCallback ( {
3639 code : PositionError . TIMEOUT ,
3740 message : 'Position retrieval timed out.'
3841 } ) ;
3942 } , timeout ) ;
40- return t ;
43+ return timers [ id ] . timer ;
4144}
4245
4346// Returns default params, overrides if provided with values
@@ -71,33 +74,39 @@ module.exports = {
7174 getCurrentPosition : function ( success , error , args ) {
7275 args = parseParameters ( args ) ;
7376
77+ const id = utils . createUUID ( ) ;
78+
7479 // Timer var that will fire an error callback if no position is retrieved from native
7580 // before the "timeout" param provided expires
76- const timeoutTimer = { timer : null } ;
81+ timers [ id ] = { timer : null } ;
7782
7883 const win = function ( deviceApiLevel ) {
84+ if ( ! timers [ id ] . timer ) {
85+ // Timeout already happened, or native fired error callback for
86+ // this geo request.
87+ // Don't continue with success callback.
88+ return ;
89+ }
7990 // Workaround for bug specific to API 31 where requesting `enableHighAccuracy: false` results in TIMEOUT error.
8091 if ( deviceApiLevel === 31 ) {
8192 if ( typeof args === 'undefined' ) args = { } ;
8293 args . enableHighAccuracy = true ;
8394 }
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.
95+ const geo = cordova . require ( 'cordova/modulemapper' ) . getOriginalSymbol ( window , 'navigator.geolocation' ) ; // eslint-disable-line no-undef
96+ geo . getCurrentPosition ( ( position ) => {
97+ clearTimeout ( timers [ id ] . timer ) ;
98+ if ( ! timers [ id ] . timer ) {
99+ // Timeout already happened, or native fired error callback for
100+ // this geo request.
91101 // Don't continue with success callback.
92- if ( timeoutTimer . timer ) {
93- success ( position ) ;
94- }
95- } , error , args ) ;
96- }
102+ return ;
103+ }
104+ success ( position ) ;
105+ } , error , args ) ;
97106 } ;
98107 const fail = function ( ) {
99- clearTimeout ( timeoutTimer . timer ) ;
100- timeoutTimer . timer = null ;
108+ clearTimeout ( timers [ id ] . timer ) ;
109+ timers [ id ] . timer = null ;
101110 if ( error ) {
102111 error ( new PositionError ( PositionError . PERMISSION_DENIED , 'Illegal Access' ) ) ;
103112 }
@@ -107,16 +116,16 @@ module.exports = {
107116 // If the timeout value was not set to Infinity (default), then
108117 // set up a timeout function that will fire the error callback
109118 // if no successful position was retrieved before timeout expired.
110- timeoutTimer . timer = createTimeout ( error , args . timeout ) ;
119+ timers [ id ] . timer = createTimeout ( error , args . timeout , id ) ;
111120 } else {
112121 // This is here so the check in the win function doesn't mess stuff up
113122 // may seem weird but this guarantees timeoutTimer is
114123 // always truthy before we call into native
115- timeoutTimer . timer = true ;
124+ timers [ id ] . timer = true ;
116125 }
117126 const enableHighAccuracy = typeof args === 'object' && ! ! args . enableHighAccuracy ;
118127 exec ( win , fail , 'Geolocation' , 'getPermission' , [ enableHighAccuracy ] ) ;
119- return timeoutTimer ;
128+ return timers [ id ] ;
120129 } ,
121130
122131 watchPosition : function ( success , error , args ) {
0 commit comments