@@ -3176,7 +3176,15 @@ window.lizMap = function() {
3176
3176
}
3177
3177
3178
3178
// 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 ) => {
3180
3188
// Raise an error when one those required requests fails
3181
3189
// Other requests can fail silently
3182
3190
const requiredRequests = [ responses [ 0 ] , responses [ 2 ] , responses [ 3 ] , responses [ 4 ] ] ;
@@ -3202,13 +3210,46 @@ window.lizMap = function() {
3202
3210
}
3203
3211
}
3204
3212
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 ;
3211
3248
} ) ;
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
+ }
3212
3253
3213
3254
getFeatureInfo = responses [ 6 ] . value ;
3214
3255
@@ -3253,7 +3294,7 @@ window.lizMap = function() {
3253
3294
3254
3295
// Parse WFS capabilities
3255
3296
wfsCapabilities = domparser . parseFromString ( wfsCapaData , "application/xml" ) ;
3256
- var featureTypes = lizMap . mainLizmap . initialConfig . vectorLayerFeatureTypeList ;
3297
+ var featureTypes = self . mainLizmap . initialConfig . vectorLayerFeatureTypeList ;
3257
3298
3258
3299
for ( const featureType of featureTypes ) {
3259
3300
var typeName = featureType . Name ;
0 commit comments