Skip to content

Commit

Permalink
Remove layers before removing source
Browse files Browse the repository at this point in the history
  • Loading branch information
russrey committed Jan 21, 2018
1 parent 1432537 commit 30b8a30
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ export type ImageDefinitionWithOptions = [

export interface LayerCommonProps {
type?:
| 'symbol'
| 'line'
| 'fill'
| 'circle'
| 'raster'
| 'fill-extrusion'
| 'background'
| 'heatmap';
| 'symbol'
| 'line'
| 'fill'
| 'circle'
| 'raster'
| 'fill-extrusion'
| 'background'
| 'heatmap';
sourceId?: string;
images?:
| ImageDefinition
| ImageDefinition[]
| ImageDefinitionWithOptions
| ImageDefinitionWithOptions[];
| ImageDefinition
| ImageDefinition[]
| ImageDefinitionWithOptions
| ImageDefinitionWithOptions[];
before?: string;
paint?: Paint;
layout?: Layout;
Expand Down Expand Up @@ -216,7 +216,9 @@ export default class Layer extends React.Component<Props> {
return;
}

map.removeLayer(id);
if (map.getLayer(id)) {
map.removeLayer(id);
}
// if pointing to an existing source, don't remove
// as other layers may be dependent upon it
if (!this.props.sourceId) {
Expand Down Expand Up @@ -281,9 +283,9 @@ export default class Layer extends React.Component<Props> {
} else {
children = Array.isArray(children)
? (children as JSX.Element[][]).reduce(
(arr, next) => arr.concat(next),
[] as JSX.Element[]
)
(arr, next) => arr.concat(next),
[] as JSX.Element[]
)
: [children] as JSX.Element[];
}

Expand Down
8 changes: 8 additions & 0 deletions src/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ export default class Source extends React.Component<Props> {
map.off('styledata', this.onStyleDataChange);

if (map.getSource(this.id)) {
const { layers } = map.getStyle()

if (layers) {
layers
.filter(layer => layer.source === this.id)
.map(layer => map.removeLayer(layer.id))
}

map.removeSource(this.id);
}
}
Expand Down

0 comments on commit 30b8a30

Please sign in to comment.