Skip to content

Commit 25ba42b

Browse files
authored
Merge pull request #5471 from rldhont/fix-js-waitig-for-mainLizmap
[Fix JS] mainLizmap may not be ready when configs are loaded Fixes #5452
2 parents 3ca2ddd + e257531 commit 25ba42b

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

assets/src/legacy/map.js

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3176,7 +3176,15 @@ window.lizMap = function() {
31763176
}
31773177

31783178
// Request config and capabilities in parallel
3179-
Promise.allSettled([configRequest, keyValueConfigRequest, WMSRequest, WMTSRequest, WFSRequest, featureExtentRequest, getFeatureInfoRequest]).then(responses => {
3179+
Promise.allSettled([
3180+
configRequest,
3181+
keyValueConfigRequest,
3182+
WMSRequest,
3183+
WMTSRequest,
3184+
WFSRequest,
3185+
featureExtentRequest,
3186+
getFeatureInfoRequest,
3187+
]).then(async (responses) => {
31803188
// Raise an error when one those required requests fails
31813189
// Other requests can fail silently
31823190
const requiredRequests = [responses[0], responses[2], responses[3], responses[4]];
@@ -3202,13 +3210,46 @@ window.lizMap = function() {
32023210
}
32033211
}
32043212

3205-
self.events.triggerEvent("configsloaded", {
3206-
initialConfig: config,
3207-
wmsCapabilities: wmsCapaData,
3208-
wmtsCapabilities: wmtsCapaData,
3209-
wfsCapabilities: wfsCapaData,
3210-
startupFeatures: responses[5].value,
3213+
/**
3214+
* mainLizmap is loaded in another JS file
3215+
* and could not be available when `configsloaded` is fired
3216+
* in this case all the Lizmap is not build
3217+
* to be sur mainLizmap is ready when `configsloaded` is fired
3218+
* we have to wait until `lizMap.mainLizmap` is not `undefined`
3219+
*/
3220+
3221+
// sleep Promise
3222+
let sleep = ms => new Promise(r => setTimeout(r, ms));
3223+
// sleep step first value the *2
3224+
let sleepStep = 100;
3225+
// max wait to 10 seconds
3226+
const maxWait = 10000;
3227+
// waitFor function returns waiting time in milliseconds
3228+
let waitFor = async function waitFor(f){
3229+
let waitingTime = 0;
3230+
while(waitingTime < maxWait && !f()) {
3231+
await sleep(sleepStep);
3232+
waitingTime += sleepStep;
3233+
sleepStep *= 2;
3234+
}
3235+
return waitingTime;
3236+
};
3237+
// wait until lizMap.mainLizmap is not undefined
3238+
// lizMap.mainLizmap is defined when configsloaded is fired
3239+
const waitingFor = await waitFor(() => {
3240+
self.events.triggerEvent("configsloaded", {
3241+
initialConfig: config,
3242+
wmsCapabilities: wmsCapaData,
3243+
wmtsCapabilities: wmtsCapaData,
3244+
wfsCapabilities: wfsCapaData,
3245+
startupFeatures: responses[5].value,
3246+
});
3247+
return self.mainLizmap !== undefined;
32113248
});
3249+
// lizMap.mainLizmap is still undefined
3250+
if (self.mainLizmap === undefined) {
3251+
throw new Error('Until we wait '+waitingFor+' ms, mainLizmap has not been loaded!');
3252+
}
32123253

32133254
getFeatureInfo = responses[6].value;
32143255

@@ -3253,7 +3294,7 @@ window.lizMap = function() {
32533294

32543295
// Parse WFS capabilities
32553296
wfsCapabilities = domparser.parseFromString(wfsCapaData, "application/xml");
3256-
var featureTypes = lizMap.mainLizmap.initialConfig.vectorLayerFeatureTypeList;
3297+
var featureTypes = self.mainLizmap.initialConfig.vectorLayerFeatureTypeList;
32573298

32583299
for (const featureType of featureTypes) {
32593300
var typeName = featureType.Name;

0 commit comments

Comments
 (0)