Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release_3_9] [Fix JS] mainLizmap may not be ready when configs are loaded #5475

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 49 additions & 8 deletions assets/src/legacy/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3166,7 +3166,15 @@ window.lizMap = function() {
}

// Request config and capabilities in parallel
Promise.allSettled([configRequest, keyValueConfigRequest, WMSRequest, WMTSRequest, WFSRequest, featureExtentRequest, getFeatureInfoRequest]).then(responses => {
Promise.allSettled([
configRequest,
keyValueConfigRequest,
WMSRequest,
WMTSRequest,
WFSRequest,
featureExtentRequest,
getFeatureInfoRequest,
]).then(async (responses) => {
// Raise an error when one those required requests fails
// Other requests can fail silently
const requiredRequests = [responses[0], responses[2], responses[3], responses[4]];
Expand All @@ -3192,13 +3200,46 @@ window.lizMap = function() {
}
}

self.events.triggerEvent("configsloaded", {
initialConfig: config,
wmsCapabilities: wmsCapaData,
wmtsCapabilities: wmtsCapaData,
wfsCapabilities: wfsCapaData,
startupFeatures: responses[5].value,
/**
* mainLizmap is loaded in another JS file
* and could not be available when `configsloaded` is fired
* in this case all the Lizmap is not build
* to be sur mainLizmap is ready when `configsloaded` is fired
* we have to wait until `lizMap.mainLizmap` is not `undefined`
*/

// sleep Promise
let sleep = ms => new Promise(r => setTimeout(r, ms));
// sleep step first value the *2
let sleepStep = 100;
// max wait to 10 seconds
const maxWait = 10000;
// waitFor function returns waiting time in milliseconds
let waitFor = async function waitFor(f){
let waitingTime = 0;
while(waitingTime < maxWait && !f()) {
await sleep(sleepStep);
waitingTime += sleepStep;
sleepStep *= 2;
}
return waitingTime;
};
// wait until lizMap.mainLizmap is not undefined
// lizMap.mainLizmap is defined when configsloaded is fired
const waitingFor = await waitFor(() => {
self.events.triggerEvent("configsloaded", {
initialConfig: config,
wmsCapabilities: wmsCapaData,
wmtsCapabilities: wmtsCapaData,
wfsCapabilities: wfsCapaData,
startupFeatures: responses[5].value,
});
return self.mainLizmap !== undefined;
});
// lizMap.mainLizmap is still undefined
if (self.mainLizmap === undefined) {
throw new Error('Until we wait '+waitingFor+' ms, mainLizmap has not been loaded!');
}

getFeatureInfo = responses[6].value;

Expand Down Expand Up @@ -3243,7 +3284,7 @@ window.lizMap = function() {

// Parse WFS capabilities
wfsCapabilities = domparser.parseFromString(wfsCapaData, "application/xml");
var featureTypes = lizMap.mainLizmap.initialConfig.vectorLayerFeatureTypeList;
var featureTypes = self.mainLizmap.initialConfig.vectorLayerFeatureTypeList;

for (const featureType of featureTypes) {
var typeName = featureType.Name;
Expand Down
Loading