Skip to content

Commit 3f37798

Browse files
committed
fix: timeout error
1 parent 5206b0d commit 3f37798

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

www/android/geolocation.js

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ const PositionError = require('./PositionError');
2727
// So we use additional map and own ids to return watch id synchronously.
2828
const pluginToNativeWatchMap = {};
2929

30-
const timers = {}; // list of timers in use
31-
3230
// Returns a timeout failure, closed over a specified timeout value and error callback.
3331
function createTimeout (errorCallback, timeout) {
3432
let t = setTimeout(function () {
@@ -42,8 +40,37 @@ function createTimeout (errorCallback, timeout) {
4240
return t;
4341
}
4442

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+
4570
module.exports = {
4671
getCurrentPosition: function (success, error, args) {
72+
args = parseParameters(args);
73+
4774
// Timer var that will fire an error callback if no position is retrieved from native
4875
// before the "timeout" param provided expires
4976
const timeoutTimer = { timer: null };
@@ -54,17 +81,19 @@ module.exports = {
5481
if (typeof args === 'undefined') args = {};
5582
args.enableHighAccuracy = true;
5683
}
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.
6391
// 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+
}
6897
};
6998
const fail = function () {
7099
clearTimeout(timeoutTimer.timer);
@@ -74,11 +103,11 @@ module.exports = {
74103
}
75104
};
76105

77-
if (options.timeout !== Infinity) {
106+
if (args.timeout !== Infinity) {
78107
// If the timeout value was not set to Infinity (default), then
79108
// set up a timeout function that will fire the error callback
80109
// if no successful position was retrieved before timeout expired.
81-
timeoutTimer.timer = createTimeout(fail, options.timeout);
110+
timeoutTimer.timer = createTimeout(error, args.timeout);
82111
} else {
83112
// This is here so the check in the win function doesn't mess stuff up
84113
// may seem weird but this guarantees timeoutTimer is

0 commit comments

Comments
 (0)