Skip to content

Commit

Permalink
Merge pull request #4956 from HSLdevcom/fix-geojson-race
Browse files Browse the repository at this point in the history
fix: geojson fetch race condition
  • Loading branch information
Antiik91 authored Feb 15, 2024
2 parents 909290d + 70f22df commit ccfc8c5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
5 changes: 4 additions & 1 deletion app/component/MapLayersDialogContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const mapLayersConfigShape = PropTypes.shape({
geoJson: PropTypes.shape({
layers: PropTypes.arrayOf(
PropTypes.shape({
url: PropTypes.string.isRequired,
url: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
]).isRequired,
name: PropTypes.shape({
en: PropTypes.string,
fi: PropTypes.string.isRequired,
Expand Down
18 changes: 10 additions & 8 deletions app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,16 @@ export function getNamedConfiguration(configName) {
// inject zone geoJson if necessary
const conf = configs[configName];
if (conf.useAssembledGeoJsonZones && allZones) {
const zoneLayer = {
...allZones,
isOffByDefault: conf.useAssembledGeoJsonZones === 'isOffByDefault',
};
if (!conf.geoJson) {
conf.geoJson = { layers: [zoneLayer] };
} else {
conf.geoJson.layers.push(zoneLayer);
if (!conf.geoJson?.layers?.find(l => l.name === allZones.name)) {
const zoneLayer = {
...allZones,
isOffByDefault: conf.useAssembledGeoJsonZones === 'isOffByDefault',
};
if (!conf.geoJson) {
conf.geoJson = { layers: [zoneLayer] };
} else {
conf.geoJson.layers.push(zoneLayer);
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions app/store/GeoJsonStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ class GeoJsonStore extends Store {
id = url;
urlArr = [url];
}
if (this.geoJsonData[id] === 'pending') {
for (let i = 0; i < 30; i++) {
/* eslint-disable no-await-in-loop, no-promise-executor-return */
await new Promise(resolve => setTimeout(resolve, 100));
if (this.geoJsonData[id] !== 'pending') {
break;
}
}
}
if (!this.geoJsonData[id]) {
this.geoJsonData[id] = 'pending';
const responses = await Promise.all(urlArr.map(u => getJson(u)));
const mapped = responses.map(r => {
if (metadata) {
Expand Down

0 comments on commit ccfc8c5

Please sign in to comment.