Skip to content

Commit 012a749

Browse files
committed
fetch GeoJSON URLs found in calcdata before updateData()
1 parent 70878b7 commit 012a749

File tree

1 file changed

+58
-11
lines changed

1 file changed

+58
-11
lines changed

src/plots/mapbox/mapbox.js

+58-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
'use strict';
1010

11+
/* global PlotlyGeoAssets:false */
12+
1113
var mapboxgl = require('mapbox-gl');
14+
var d3 = require('d3');
1215

1316
var Fx = require('../../components/fx');
1417
var Lib = require('../../lib');
@@ -111,7 +114,15 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
111114

112115
self.rejectOnError(reject);
113116

114-
map.once('load', function() {
117+
var promises = [];
118+
119+
promises.push(new Promise(function(resolve) {
120+
map.once('load', resolve);
121+
}));
122+
123+
promises = promises.concat(self.fetchMapData(calcData, fullLayout));
124+
125+
Promise.all(promises).then(function() {
115126
self.updateData(calcData);
116127
self.updateLayout(fullLayout);
117128
self.resolveOnRender(resolve);
@@ -232,32 +243,68 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
232243
};
233244
};
234245

246+
proto.fetchMapData = function(calcData) {
247+
var promises = [];
248+
249+
function fetch(url) {
250+
return new Promise(function(resolve, reject) {
251+
d3.json(url, function(err, d) {
252+
if(err) {
253+
var msg = err.status === 404 ?
254+
('GeoJSON at URL ' + url + ' does not exist.') :
255+
('Unexpected error while fetching from ' + url);
256+
return reject(new Error(msg));
257+
}
258+
259+
PlotlyGeoAssets[url] = d;
260+
resolve(d);
261+
});
262+
});
263+
}
264+
265+
for(var i = 0; i < calcData.length; i++) {
266+
var trace = calcData[i][0].trace;
267+
var url = trace.geojson;
268+
269+
if(typeof url === 'string' && !PlotlyGeoAssets[url]) {
270+
PlotlyGeoAssets[url] = 'pending';
271+
promises.push(fetch(url));
272+
}
273+
}
274+
275+
return promises;
276+
};
277+
235278
proto.updateMap = function(calcData, fullLayout, resolve, reject) {
236279
var self = this;
237280
var map = self.map;
238281
var opts = fullLayout[this.id];
239282

240283
self.rejectOnError(reject);
241284

285+
var promises = [];
242286
var styleObj = getStyleObj(opts.style);
243287

244288
if(self.styleObj.id !== styleObj.id) {
245289
self.styleObj = styleObj;
246290
map.setStyle(styleObj.style);
247291

248-
map.once('styledata', function() {
249-
// need to rebuild trace layers on reload
250-
// to avoid 'lost event' errors
251-
self.traceHash = {};
252-
self.updateData(calcData);
253-
self.updateLayout(fullLayout);
254-
self.resolveOnRender(resolve);
255-
});
256-
} else {
292+
// need to rebuild trace layers on reload
293+
// to avoid 'lost event' errors
294+
self.traceHash = {};
295+
296+
promises.push(new Promise(function(resolve) {
297+
map.once('styledata', resolve);
298+
}));
299+
}
300+
301+
promises = promises.concat(self.fetchMapData(calcData, fullLayout));
302+
303+
Promise.all(promises).then(function() {
257304
self.updateData(calcData);
258305
self.updateLayout(fullLayout);
259306
self.resolveOnRender(resolve);
260-
}
307+
});
261308
};
262309

263310
proto.updateData = function(calcData) {

0 commit comments

Comments
 (0)