@@ -50,9 +50,10 @@ export class Deck2gisLayer<LayerT extends Layer> implements DeckCustomLayer {
5050 renderingMode : '2d' | '3d' ;
5151 map : Map | null ;
5252 deck : Deck | null ;
53- props : LayerProps < LayerT > ;
53+ props : LayerProps < LayerT > | undefined ;
5454 gl ?: WebGLRenderingContext | WebGL2RenderingContext ;
5555 antialiasing : boolean ;
56+ isDestroyed : boolean ;
5657
5758 /**
5859 * Initializes deck.gl properties for working with the MapGL map.
@@ -87,7 +88,7 @@ export class Deck2gisLayer<LayerT extends Layer> implements DeckCustomLayer {
8788 if ( ! props . id ) {
8889 throw new Error ( 'Layer must have a unique id' ) ;
8990 }
90-
91+ this . isDestroyed = false ;
9192 this . id = props . id ;
9293 this . type = 'custom' ;
9394 this . renderingMode = props . renderingMode || '3d' ;
@@ -103,7 +104,7 @@ export class Deck2gisLayer<LayerT extends Layer> implements DeckCustomLayer {
103104 * MapGL calls this method after adding a layer to a map.
104105 */
105106 public onAdd = ( ) => {
106- if ( ! this . map && this . props . deck ) {
107+ if ( ! this . map && this . props ? .deck && ! this . isDestroyed ) {
107108 const map = ( this . props . deck . props as CustomRenderProps ) . _2gisData . _2gisMap ;
108109 this . map = map ;
109110 const gl = ( this . gl = map . getWebGLContext ( ) ) ;
@@ -128,12 +129,13 @@ export class Deck2gisLayer<LayerT extends Layer> implements DeckCustomLayer {
128129 this . frameBuffer . unbind ( gl ) ;
129130 this . deck = prepareDeckInstance ( { map, gl, deck : this . props . deck , renderTarget } ) ;
130131 }
131-
132- this . program = ( this . deck as any ) . props . _2glProgram ;
133- this . vao = ( this . deck as any ) . props . _2glVao ;
132+ if ( this . deck ) {
133+ this . program = ( this . deck as any ) . props . _2glProgram ;
134+ this . vao = ( this . deck as any ) . props . _2glVao ;
135+ }
134136 }
135137
136- if ( this . deck ) {
138+ if ( this . deck && ! this . isDestroyed ) {
137139 addLayer ( this . deck , this ) ;
138140 }
139141 } ;
@@ -154,28 +156,47 @@ export class Deck2gisLayer<LayerT extends Layer> implements DeckCustomLayer {
154156 * @param props deck.gl layer properties.
155157 */
156158 public setProps ( props : Partial < LayerProps < LayerT > > ) {
157- // id cannot be changed
158- Object . assign ( this . props , props , { id : this . id } ) ;
159- this . antialiasing = Boolean ( props . antialiasing ) ;
160- // safe guard in case setProps is called before onAdd
161- if ( this . deck ) {
162- updateLayer ( this . deck , this ) ;
159+ if ( ! this . isDestroyed && this . props ) {
160+ // id cannot be changed
161+ Object . assign ( this . props , props , { id : this . id } ) ;
162+ this . antialiasing = Boolean ( props . antialiasing ) ;
163+ // safe guard in case setProps is called before onAdd
164+ if ( this . deck ) {
165+ updateLayer ( this . deck , this ) ;
166+ }
163167 }
164168 }
165169
170+ /**
171+ * Destroys the layer and frees all related resources.
172+ */
173+ public destroy = ( ) => {
174+ this . deck = null ;
175+ this . map = null ;
176+ this . frameBuffer = undefined ;
177+ this . program = undefined ;
178+ this . vao = undefined ;
179+ this . gl = undefined ;
180+ this . isDestroyed = true ;
181+ this . props = undefined ;
182+ } ;
183+
166184 /**
167185 * @hidden
168186 * @internal
169187 * MapGL calls this method on each map frame rendering.
170188 */
171189 public render = ( ) => {
172190 if (
191+ this . isDestroyed ||
173192 ! this . deck ||
193+ ! ( this . deck as any ) . layerManager ||
174194 ! this . map ||
175195 ! this . frameBuffer ||
176196 ! this . program ||
177197 ! this . vao ||
178- ! this . gl
198+ ! this . gl ||
199+ ! this . props
179200 ) {
180201 return ;
181202 }
0 commit comments