Skip to content

Commit aeaa06d

Browse files
committed
fix potential race condition in fetchTraceGeoData
1 parent 02d6143 commit aeaa06d

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/lib/geo_location_utils.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,39 @@ function fetchTraceGeoData(calcData) {
326326
}
327327

328328
PlotlyGeoAssets[url] = d;
329-
resolve(d);
329+
return resolve(d);
330330
});
331331
});
332332
}
333333

334+
function wait(url) {
335+
return new Promise(function(resolve, reject) {
336+
var cnt = 0;
337+
var interval = setInterval(function() {
338+
if(PlotlyGeoAssets[url] && PlotlyGeoAssets[url] !== 'pending') {
339+
clearInterval(interval);
340+
return resolve(PlotlyGeoAssets[url]);
341+
}
342+
if(cnt > 100) {
343+
clearInterval(interval);
344+
return reject('Unexpected error while fetching from ' + url);
345+
}
346+
cnt++;
347+
}, 50);
348+
});
349+
}
350+
334351
for(var i = 0; i < calcData.length; i++) {
335352
var trace = calcData[i][0].trace;
336353
var url = trace.geojson;
337354

338-
if(typeof url === 'string' && !PlotlyGeoAssets[url]) {
339-
PlotlyGeoAssets[url] = 'pending';
340-
promises.push(fetch(url));
355+
if(typeof url === 'string') {
356+
if(!PlotlyGeoAssets[url]) {
357+
PlotlyGeoAssets[url] = 'pending';
358+
promises.push(fetch(url));
359+
} else if(PlotlyGeoAssets[url] === 'pending') {
360+
promises.push(wait(url));
361+
}
341362
}
342363
}
343364

0 commit comments

Comments
 (0)