Skip to content

Commit

Permalink
Merge pull request #18 from 2gis/TILES-4922
Browse files Browse the repository at this point in the history
Tiles 4922
  • Loading branch information
Kuznecoff authored Feb 17, 2023
2 parents f5a428d + 1647ee5 commit f6871c9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@2gis/deck2gis-layer",
"version": "1.1.7",
"version": "1.1.8",
"description": "",
"main": "dist/deck2gislayer.js",
"typings": "dist/types/index.d.ts",
Expand Down
49 changes: 35 additions & 14 deletions src/deckgl2gisLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ export class Deck2gisLayer<LayerT extends Layer> implements DeckCustomLayer {
renderingMode: '2d' | '3d';
map: Map | null;
deck: Deck | null;
props: LayerProps<LayerT>;
props: LayerProps<LayerT> | undefined;
gl?: WebGLRenderingContext | WebGL2RenderingContext;
antialiasing: boolean;
isDestroyed: boolean;

/**
* Initializes deck.gl properties for working with the MapGL map.
Expand Down Expand Up @@ -87,7 +88,7 @@ export class Deck2gisLayer<LayerT extends Layer> implements DeckCustomLayer {
if (!props.id) {
throw new Error('Layer must have a unique id');
}

this.isDestroyed = false;
this.id = props.id;
this.type = 'custom';
this.renderingMode = props.renderingMode || '3d';
Expand All @@ -103,7 +104,7 @@ export class Deck2gisLayer<LayerT extends Layer> implements DeckCustomLayer {
* MapGL calls this method after adding a layer to a map.
*/
public onAdd = () => {
if (!this.map && this.props.deck) {
if (!this.map && this.props?.deck && !this.isDestroyed) {
const map = (this.props.deck.props as CustomRenderProps)._2gisData._2gisMap;
this.map = map;
const gl = (this.gl = map.getWebGLContext());
Expand All @@ -128,12 +129,13 @@ export class Deck2gisLayer<LayerT extends Layer> implements DeckCustomLayer {
this.frameBuffer.unbind(gl);
this.deck = prepareDeckInstance({ map, gl, deck: this.props.deck, renderTarget });
}

this.program = (this.deck as any).props._2glProgram;
this.vao = (this.deck as any).props._2glVao;
if (this.deck) {
this.program = (this.deck as any).props._2glProgram;
this.vao = (this.deck as any).props._2glVao;
}
}

if (this.deck) {
if (this.deck && !this.isDestroyed) {
addLayer(this.deck, this);
}
};
Expand All @@ -154,28 +156,47 @@ export class Deck2gisLayer<LayerT extends Layer> implements DeckCustomLayer {
* @param props deck.gl layer properties.
*/
public setProps(props: Partial<LayerProps<LayerT>>) {
// id cannot be changed
Object.assign(this.props, props, { id: this.id });
this.antialiasing = Boolean(props.antialiasing);
// safe guard in case setProps is called before onAdd
if (this.deck) {
updateLayer(this.deck, this);
if (!this.isDestroyed && this.props) {
// id cannot be changed
Object.assign(this.props, props, { id: this.id });
this.antialiasing = Boolean(props.antialiasing);
// safe guard in case setProps is called before onAdd
if (this.deck) {
updateLayer(this.deck, this);
}
}
}

/**
* Destroys the layer and frees all related resources.
*/
public destroy = () => {
this.deck = null;
this.map = null;
this.frameBuffer = undefined;
this.program = undefined;
this.vao = undefined;
this.gl = undefined;
this.isDestroyed = true;
this.props = undefined;
};

/**
* @hidden
* @internal
* MapGL calls this method on each map frame rendering.
*/
public render = () => {
if (
this.isDestroyed ||
!this.deck ||
!(this.deck as any).layerManager ||
!this.map ||
!this.frameBuffer ||
!this.program ||
!this.vao ||
!this.gl
!this.gl ||
!this.props
) {
return;
}
Expand Down
39 changes: 23 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function prepareDeckInstance({
map.on('move', () => onMapMove(deckInstance, map));
}

if (deck) {
if (deck?.['layerManager']) {
deckInstance = deck as Deck;
deckInstance.setProps(deckProps);
} else {
Expand Down Expand Up @@ -106,6 +106,9 @@ export function addLayer(deck: Deck, layer: Deck2gisLayer<any>): void {
export function removeLayer(deck: Deck, layer: Deck2gisLayer<any>): void {
(deck.props as CustomRenderProps)._2gisData._2gisCustomLayers.delete(layer);
updateLayers(deck);
if ((deck.props as CustomRenderProps)._2gisData._2gisCustomLayers.size === 0) {
(deck.props as CustomRenderProps)._2gisData._2gisMap.__deck = undefined;
}
}

/**
Expand Down Expand Up @@ -159,13 +162,15 @@ function getViewport(map: Map): MapglMercatorViewport | undefined {
* @internal
*/
function onMapMove(deck: Deck, map: Map): void {
deck.setProps({
viewState: getViewState(map),
});
// Camera changed, will trigger a map repaint right after this
// Clear any change flag triggered by setting viewState so that deck does not request
// a second repaint
deck.needsRedraw({ clearRedrawFlags: true });
if (deck['layerManager']) {
deck.setProps({
viewState: getViewState(map),
});
// Camera changed, will trigger a map repaint right after this
// Clear any change flag triggered by setting viewState so that deck does not request
// a second repaint
deck.needsRedraw({ clearRedrawFlags: true });
}
}

/**
Expand All @@ -187,14 +192,16 @@ export function onMapResize(map: Map, deck: Deck, renderTarget: RenderTarget) {
* @internal
*/
function updateLayers(deck: Deck): void {
const layers: Layer<any>[] = [];
let layerIndex = 0;
(deck.props as CustomRenderProps)._2gisData._2gisCustomLayers.forEach((deckLayer) => {
const LayerType = deckLayer.props.type;
const layer = new LayerType(deckLayer.props, { _offset: layerIndex++ });
layers.push(layer);
});
deck.setProps({ layers });
if (deck['layerManager']) {
const layers: Layer<any>[] = [];
let layerIndex = 0;
(deck.props as CustomRenderProps)._2gisData._2gisCustomLayers.forEach((deckLayer) => {
const LayerType = deckLayer.props.type;
const layer = new LayerType(deckLayer.props, { _offset: layerIndex++ });
layers.push(layer);
});
deck.setProps({ layers });
}
}

/**
Expand Down

0 comments on commit f6871c9

Please sign in to comment.