@@ -27,17 +27,20 @@ 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
+ // list of timers in use
31
+ const timers = { } ;
32
+
30
33
// 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 ;
35
38
errorCallback ( {
36
39
code : PositionError . TIMEOUT ,
37
40
message : 'Position retrieval timed out.'
38
41
} ) ;
39
42
} , timeout ) ;
40
- return t ;
43
+ return timers [ id ] . timer ;
41
44
}
42
45
43
46
// Returns default params, overrides if provided with values
@@ -71,33 +74,39 @@ module.exports = {
71
74
getCurrentPosition : function ( success , error , args ) {
72
75
args = parseParameters ( args ) ;
73
76
77
+ const id = utils . createUUID ( ) ;
78
+
74
79
// Timer var that will fire an error callback if no position is retrieved from native
75
80
// before the "timeout" param provided expires
76
- const timeoutTimer = { timer : null } ;
81
+ timers [ id ] = { timer : null } ;
77
82
78
83
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
+ }
79
90
// Workaround for bug specific to API 31 where requesting `enableHighAccuracy: false` results in TIMEOUT error.
80
91
if ( deviceApiLevel === 31 ) {
81
92
if ( typeof args === 'undefined' ) args = { } ;
82
93
args . enableHighAccuracy = true ;
83
94
}
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.
91
101
// 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 ) ;
97
106
} ;
98
107
const fail = function ( ) {
99
- clearTimeout ( timeoutTimer . timer ) ;
100
- timeoutTimer . timer = null ;
108
+ clearTimeout ( timers [ id ] . timer ) ;
109
+ timers [ id ] . timer = null ;
101
110
if ( error ) {
102
111
error ( new PositionError ( PositionError . PERMISSION_DENIED , 'Illegal Access' ) ) ;
103
112
}
@@ -107,16 +116,16 @@ module.exports = {
107
116
// If the timeout value was not set to Infinity (default), then
108
117
// set up a timeout function that will fire the error callback
109
118
// 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 ) ;
111
120
} else {
112
121
// This is here so the check in the win function doesn't mess stuff up
113
122
// may seem weird but this guarantees timeoutTimer is
114
123
// always truthy before we call into native
115
- timeoutTimer . timer = true ;
124
+ timers [ id ] . timer = true ;
116
125
}
117
126
const enableHighAccuracy = typeof args === 'object' && ! ! args . enableHighAccuracy ;
118
127
exec ( win , fail , 'Geolocation' , 'getPermission' , [ enableHighAccuracy ] ) ;
119
- return timeoutTimer ;
128
+ return timers [ id ] ;
120
129
} ,
121
130
122
131
watchPosition : function ( success , error , args ) {
0 commit comments