commit 583097dbb0086064ad19cb2eb50b615706286d86
parent 9d3c908e4737c9e60b71e73b37078ef4f710cc05
Author: Sergey Orlov <wladimirych@gmail.com>
Date: Fri, 22 May 2020 10:13:02 +0200
elevations: split large request in chunks
Diffstat:
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/src/lib/elevations/index.js b/src/lib/elevations/index.js
@@ -2,14 +2,22 @@ import config from '~/config';
import {fetch} from '~/lib/xhr-promise';
class ElevationProvider {
+ chunkSize = 10000;
+
constructor(serverUrl = config.elevationsServer) {
this.url = serverUrl;
}
async get(latlngs) {
- const request = latlngs.map((ll) => `${ll.lat.toFixed(6)} ${ll.lng.toFixed(6)}`).join('\n');
- const xhr = await fetch(this.url, {method: 'POST', data: request, withCredentials: true});
- return xhr.responseText.split('\n').map((line) => (line === 'NULL' ? null : parseFloat(line)));
+ const result = [];
+ for (let i = 0; i < latlngs.length; i += this.chunkSize) {
+ const chunk = latlngs.slice(i, i + this.chunkSize);
+ const request = chunk.map((ll) => `${ll.lat.toFixed(6)} ${ll.lng.toFixed(6)}`).join('\n');
+ const xhr = await fetch(this.url, {method: 'POST', data: request, withCredentials: true});
+ const respValues = xhr.responseText.split('\n').map((line) => (line === 'NULL' ? null : parseFloat(line)));
+ result.push(...respValues);
+ }
+ return result;
}
}