Skip to content

Commit f6871c9

Browse files
authored
Merge pull request #18 from 2gis/TILES-4922
Tiles 4922
2 parents f5a428d + 1647ee5 commit f6871c9

File tree

4 files changed

+61
-33
lines changed

4 files changed

+61
-33
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@2gis/deck2gis-layer",
3-
"version": "1.1.7",
3+
"version": "1.1.8",
44
"description": "",
55
"main": "dist/deck2gislayer.js",
66
"typings": "dist/types/index.d.ts",

src/deckgl2gisLayer.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/utils.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function prepareDeckInstance({
7474
map.on('move', () => onMapMove(deckInstance, map));
7575
}
7676

77-
if (deck) {
77+
if (deck?.['layerManager']) {
7878
deckInstance = deck as Deck;
7979
deckInstance.setProps(deckProps);
8080
} else {
@@ -106,6 +106,9 @@ export function addLayer(deck: Deck, layer: Deck2gisLayer<any>): void {
106106
export function removeLayer(deck: Deck, layer: Deck2gisLayer<any>): void {
107107
(deck.props as CustomRenderProps)._2gisData._2gisCustomLayers.delete(layer);
108108
updateLayers(deck);
109+
if ((deck.props as CustomRenderProps)._2gisData._2gisCustomLayers.size === 0) {
110+
(deck.props as CustomRenderProps)._2gisData._2gisMap.__deck = undefined;
111+
}
109112
}
110113

111114
/**
@@ -159,13 +162,15 @@ function getViewport(map: Map): MapglMercatorViewport | undefined {
159162
* @internal
160163
*/
161164
function onMapMove(deck: Deck, map: Map): void {
162-
deck.setProps({
163-
viewState: getViewState(map),
164-
});
165-
// Camera changed, will trigger a map repaint right after this
166-
// Clear any change flag triggered by setting viewState so that deck does not request
167-
// a second repaint
168-
deck.needsRedraw({ clearRedrawFlags: true });
165+
if (deck['layerManager']) {
166+
deck.setProps({
167+
viewState: getViewState(map),
168+
});
169+
// Camera changed, will trigger a map repaint right after this
170+
// Clear any change flag triggered by setting viewState so that deck does not request
171+
// a second repaint
172+
deck.needsRedraw({ clearRedrawFlags: true });
173+
}
169174
}
170175

171176
/**
@@ -187,14 +192,16 @@ export function onMapResize(map: Map, deck: Deck, renderTarget: RenderTarget) {
187192
* @internal
188193
*/
189194
function updateLayers(deck: Deck): void {
190-
const layers: Layer<any>[] = [];
191-
let layerIndex = 0;
192-
(deck.props as CustomRenderProps)._2gisData._2gisCustomLayers.forEach((deckLayer) => {
193-
const LayerType = deckLayer.props.type;
194-
const layer = new LayerType(deckLayer.props, { _offset: layerIndex++ });
195-
layers.push(layer);
196-
});
197-
deck.setProps({ layers });
195+
if (deck['layerManager']) {
196+
const layers: Layer<any>[] = [];
197+
let layerIndex = 0;
198+
(deck.props as CustomRenderProps)._2gisData._2gisCustomLayers.forEach((deckLayer) => {
199+
const LayerType = deckLayer.props.type;
200+
const layer = new LayerType(deckLayer.props, { _offset: layerIndex++ });
201+
layers.push(layer);
202+
});
203+
deck.setProps({ layers });
204+
}
198205
}
199206

200207
/**

0 commit comments

Comments
 (0)