@@ -9,7 +9,7 @@ import React from 'react';
99
1010import Layers from '../../../utils/cesium/Layers' ;
1111import PropTypes from 'prop-types' ;
12- import { round , isNil , castArray } from 'lodash' ;
12+ import { round , isNil , castArray , isFunction } from 'lodash' ;
1313import { getResolutions } from '../../../utils/MapUtils' ;
1414import axios from '../../../libs/ajax' ;
1515import { getProxyCacheByUrl } from '../../../utils/ProxyUtils' ;
@@ -31,6 +31,7 @@ class CesiumLayer extends React.Component {
3131 // initial visibility should also take into account the visibility limits
3232 // in particular for detached layers (eg. Vector, WFS, 3D Tiles, ...)
3333 const visibility = this . getVisibilityOption ( this . props ) ;
34+ this . _isMounted = true ;
3435 this . createLayer ( this . props . type , { ...this . props . options , visibility } , this . props . position , this . props . map , this . props . securityToken ) ;
3536 if ( this . props . options && this . layer && visibility ) {
3637 this . addLayer ( this . props ) ;
@@ -76,6 +77,9 @@ class CesiumLayer extends React.Component {
7677 }
7778
7879 componentWillUnmount ( ) {
80+
81+ this . _isMounted = false ;
82+
7983 if ( this . layer && this . props . map && ! this . props . map . isDestroyed ( ) ) {
8084 // detached layers are layers that do not work through a provider
8185 // for this reason they cannot be added or removed from the map imageryProviders
@@ -233,6 +237,22 @@ class CesiumLayer extends React.Component {
233237 ...( this . _isProxy ? { forceProxy : this . _isProxy } : null )
234238 } ;
235239 this . layer = Layers . createLayer ( type , opts , map ) ;
240+ const isPromise = this . layer && isFunction ( this . layer . then ) ;
241+ if ( isPromise ) {
242+ this . layer . then ( ( resolvedLayer ) => {
243+ this . layer = resolvedLayer ;
244+ this . provider = map . imageryLayers . addImageryProvider ( resolvedLayer ) ;
245+ if ( this . layer ) {
246+ this . layer . layerName = options . name ;
247+ this . layer . layerId = options . id ;
248+ }
249+ if ( this . layer === null ) {
250+ this . props . onCreationError ( options ) ;
251+ }
252+ this . props . map . scene . requestRender ( ) ;
253+ } ) ;
254+ return ;
255+ }
236256 if ( this . layer ) {
237257 this . layer . layerName = options . name ;
238258 this . layer . layerId = options . id ;
@@ -264,11 +284,31 @@ class CesiumLayer extends React.Component {
264284 } ,
265285 this . props . map ) ;
266286 if ( newLayer ) {
287+ const isPromise = this . layer && isFunction ( this . layer . then ) ;
288+ if ( isPromise ) {
289+ this . layer . then ( ( resolvedLayer ) => {
290+ if ( this . _isMounted ) {
291+ this . removeLayer ( ) ;
292+ this . layer = resolvedLayer ;
293+
294+ this . provider = this . props . map . imageryLayers . addImageryProvider ( resolvedLayer ) ;
295+ this . provider . _position = this . props . position ;
296+ if ( newProps . options . opacity !== undefined ) {
297+ this . provider . alpha = newProps . options . opacity ;
298+ }
299+ this . props . onImageryLayersTreeUpdate ( ) ;
300+ newProps . map . scene . requestRender ( ) ;
301+ }
302+ } ) ;
303+ return ;
304+ }
305+
267306 this . removeLayer ( ) ;
268307 this . layer = newLayer ;
269308 if ( newProps . options . visibility ) {
270309 this . addLayer ( newProps ) ;
271310 }
311+
272312 }
273313 this . updateZIndex ( newProps . position ) ;
274314 newProps . map . scene . requestRender ( ) ;
@@ -278,6 +318,24 @@ class CesiumLayer extends React.Component {
278318 if ( newProps . options . useForElevation ) {
279319 this . props . map . terrainProvider = this . layer ;
280320 } else {
321+ const isPromise = this . layer && isFunction ( this . layer . then ) ;
322+ if ( isPromise ) {
323+ this . layer . then ( ( resolvedLayer ) => {
324+ if ( this . _isMounted ) {
325+ this . layer = resolvedLayer ;
326+
327+ this . provider = this . props . map . imageryLayers . addImageryProvider ( resolvedLayer ) ;
328+ this . provider . _position = this . props . position ;
329+ if ( newProps . options . opacity !== undefined ) {
330+ this . provider . alpha = newProps . options . opacity ;
331+ }
332+ this . props . onImageryLayersTreeUpdate ( ) ;
333+ newProps . map . scene . requestRender ( ) ;
334+ }
335+ } ) ;
336+ return ;
337+ }
338+
281339 this . provider = this . props . map . imageryLayers . addImageryProvider ( this . layer ) ;
282340 this . provider . _position = this . props . position ;
283341 if ( newProps . options . opacity !== undefined ) {
@@ -323,7 +381,7 @@ class CesiumLayer extends React.Component {
323381 }
324382
325383 removeLayer = ( provider ) => {
326- if ( this . layer . destroy ) {
384+ if ( this . layer ? .destroy ) {
327385 this . layer . destroy ( ) ;
328386 }
329387 const toRemove = provider || this . provider ;
0 commit comments