Skip to content

Commit 9df28ff

Browse files
authored
Merge pull request #471 from chrismayer/add-timeout-geocoding
Add HTTP timeout to geocoder
2 parents 5a0d818 + 9624e0b commit 9df28ff

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

docs/module-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Module identifier: `wgu-geocoder`
5555
| persistentHint | Forces hint to always be visible. | `"persistentHint": true` |
5656
| minChars | Minimum number of characters which has to be entered so the query is triggered | `"minChars": 2` |
5757
| queryDelay | Delay in MS before a query is triggered | `"queryDelay": 200` |
58+
| httpTimeout | Timeout in MS for underlying HTTP request. Defaults to `15000` | `"httpTimeout": 10000` |
5859
| debug | Boolean value to enable debug logs | `"debug": false` |
5960
| provider | Key defining which geocoder provider should be used. Could be `osm`, `photon` or `opencage` | `"provider": "osm"` |
6061
| providerOptions | Optional options which are passed to the geocoder provider | `"providerOptions": {"lang": "en-US", "countrycodes": "", "limit": 6}` |

src/components/geocoder/Geocoder.vue

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default {
5959
debug: { type: Boolean, required: false, default: false },
6060
minChars: { type: Number, required: false, default: 3 },
6161
queryDelay: { type: Number, required: false, default: 300 },
62+
httpTimeout: { type: Number, required: false, default: 15000 },
6263
provider: { type: String, required: false, default: 'osm' },
6364
providerOptions: { type: Object, required: false, default: function () { return {}; } }
6465
},
@@ -76,7 +77,7 @@ export default {
7677
selecting: false,
7778
selected: null,
7879
hideSearch: true,
79-
timeout: null
80+
debounceTimeout: null
8081
}
8182
},
8283
computed: {
@@ -98,12 +99,12 @@ export default {
9899
},
99100
mounted () {
100101
// Setup GeocoderController to which we delegate Provider and query-handling
101-
this.geocoderController = new GeocoderController(this.provider, this.providerOptions);
102+
this.geocoderController = new GeocoderController(this.provider, this.providerOptions, this.httpTimeout);
102103
},
103104
unmounted () {
104-
if (this.timeout) {
105-
clearTimeout(this.timeout);
106-
this.timeout = null;
105+
if (this.debounceTimeout) {
106+
clearTimeout(this.debounceTimeout);
107+
this.debounceTimeout = null;
107108
}
108109
109110
this.geocoderController.destroy();
@@ -118,10 +119,10 @@ export default {
118119
},
119120
// Query by string - should return list of selection items (adresses) for ComboBox
120121
querySelections (queryStr) {
121-
if (this.timeout) {
122-
clearTimeout(this.timeout);
122+
if (this.debounceTimeout) {
123+
clearTimeout(this.debounceTimeout);
123124
}
124-
this.timeout = setTimeout(() => {
125+
this.debounceTimeout = setTimeout(() => {
125126
// Let Geocoder Provider do the query
126127
// items (item.title fields) will be shown in combobox dropdown suggestions
127128
this.trace(`geocoderController.query: ${queryStr}`);
@@ -132,7 +133,7 @@ export default {
132133
this.onQueryError(err);
133134
}
134135
});
135-
this.timeout = null;
136+
this.debounceTimeout = null;
136137
}, this.queryDelay);
137138
},
138139
onQueryResults (results) {

src/components/geocoder/GeocoderController.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ export class GeocoderController {
4141
* @constructor
4242
* @param {String} providerName name of Provider.
4343
* @param {Object} options config options for Provider
44+
* @param {Number} timeout Timeout in MS for HTTP request
4445
*/
45-
constructor (providerName, options) {
46+
constructor (providerName, options, timeout) {
4647
this.options = options;
48+
this.timeout = timeout;
4749
this.abortController = null;
4850

4951
// Must have Provider class defined for name
@@ -80,7 +82,8 @@ export class GeocoderController {
8082
method: 'GET',
8183
url: parameters.url,
8284
params: parameters.params,
83-
signal: this.abortController?.signal
85+
signal: this.abortController?.signal,
86+
timeout: this.timeout
8487
};
8588

8689
const response = await axios(request);

tests/unit/specs/components/geocoder/Geocoder.spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe('geocoder/Geocoder.vue', () => {
6262
expect(vm.debug).to.be.false;
6363
expect(vm.minChars).to.equal(3);
6464
expect(vm.queryDelay).to.equal(300);
65+
expect(vm.httpTimeout).to.equal(15000);
6566
expect(vm.provider).to.equal('osm');
6667
expect(vm.providerOptions).to.be.an('object');
6768
});
@@ -84,7 +85,7 @@ describe('geocoder/Geocoder.vue', () => {
8485
expect(vm.selecting).to.be.false;
8586
expect(vm.selected).to.be.null;
8687
expect(vm.hideSearch).to.be.true;
87-
expect(vm.timeout).to.be.null;
88+
expect(vm.debounceTimeout).to.be.null;
8889
expect(vm.geocoderController).to.not.be.undefined;
8990
expect(vm.geocoderController.provider instanceof OpenStreetMap).to.be.true;
9091
});
@@ -100,6 +101,7 @@ describe('geocoder/Geocoder.vue', () => {
100101
target: 'toolbar',
101102
minChars: 5,
102103
queryDelay: 200,
104+
httpTimeout: 10000,
103105
debug: false,
104106
provider: 'photon'
105107
};
@@ -112,6 +114,7 @@ describe('geocoder/Geocoder.vue', () => {
112114
expect(vm.hideSearch).to.be.true;
113115
expect(vm.minChars).to.equal(5);
114116
expect(vm.queryDelay).to.equal(200);
117+
expect(vm.httpTimeout).to.equal(10000);
115118
expect(vm.geocoderController).to.not.be.undefined;
116119
expect(vm.geocoderController.provider instanceof Photon).to.be.true;
117120
});
@@ -127,6 +130,7 @@ describe('geocoder/Geocoder.vue', () => {
127130
target: 'toolbar',
128131
minChars: 6,
129132
queryDelay: 200,
133+
httpTimeout: 10000,
130134
debug: false,
131135
provider: 'opencage'
132136
};
@@ -139,6 +143,7 @@ describe('geocoder/Geocoder.vue', () => {
139143
expect(vm.hideSearch).to.be.true;
140144
expect(vm.minChars).to.equal(6);
141145
expect(vm.queryDelay).to.equal(200);
146+
expect(vm.httpTimeout).to.equal(10000);
142147
expect(vm.geocoderController).to.not.be.undefined;
143148
expect(vm.geocoderController.provider instanceof OpenCage).to.be.true;
144149
});

0 commit comments

Comments
 (0)