Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle updates to Source tileJsonSource prop more gracefully #914

Merged
merged 5 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"supercluster": "^7.0.0"
},
"peerDependencies": {
"mapbox-gl": "^1.10.1",
"mapbox-gl": "^1.12.0",
"prop-types": "^15.6.2",
"react": "^16.11.0",
"react-dom": "^16.11.0"
Expand All @@ -84,7 +84,7 @@
"@types/enzyme-adapter-react-16": "^1.0.3",
"@types/geojson": "7946.0.4",
"@types/jest": "24.0.19",
"@types/mapbox-gl": "^1.10.2",
"@types/mapbox-gl": "^1.12.8",
"@types/node": "8.0.29",
"@types/prettier": "1.10.0",
"@types/prop-types": "15.5.6",
Expand All @@ -95,7 +95,7 @@
"enzyme-adapter-react-16": "1.6.0",
"husky": "^0.14.3",
"jest": "24.9.0",
"mapbox-gl": "^1.10.1",
"mapbox-gl": "^1.12.0",
"prettier": "1.10.2",
"prop-types": "15.6.2",
"react": "^16.11.0",
Expand Down
25 changes: 14 additions & 11 deletions src/geojson-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,20 @@ export class GeoJSONLayer extends React.Component<Props> {
visibility
};

map.addLayer(
{
id: layerId,
source: this.id,
type,
paint,
layout,
...layerOptions
},
before
);
const layer: MapboxGL.Layer = {
id: layerId,
source: this.id,
// TODO: Fix mapbox-gl types
// tslint:disable-next-line:no-any
type: type as any,
// TODO: Fix mapbox-gl types
// tslint:disable-next-line:no-any
paint: paint as any,
layout,
...layerOptions
};

map.addLayer(layer, before);

this.mapLayerMouseHandlers(type);
};
Expand Down
4 changes: 3 additions & 1 deletion src/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ export default class Layer extends React.Component<Props> {
// tslint:disable-next-line:no-any
type: type as any,
layout,
paint,
// TODO: Fix mapbox-gl types
// tslint:disable-next-line:no-any
paint: paint as any,
metadata
};

Expand Down
38 changes: 30 additions & 8 deletions src/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export interface Props {
onSourceLoaded?: (source: GeoJSONSource | TilesJson) => void;
}

export interface LayerWithBefore extends Layer {
before?: string;
}
export type LayerWithBefore = Layer & { before?: string };

export class Source extends React.Component<Props> {
private id = this.props.id;
Expand Down Expand Up @@ -79,7 +77,7 @@ export class Source extends React.Component<Props> {
let { layers = [] } = map.getStyle();

layers = layers
.map((layer, idx) => {
.map((layer, idx): LayerWithBefore => {
const { id: before } = layers[idx + 1] || { id: undefined };
return { ...layer, before };
})
Expand Down Expand Up @@ -108,13 +106,37 @@ export class Source extends React.Component<Props> {

public componentDidUpdate(prevProps: Props) {
const { geoJsonSource, tileJsonSource, map } = prevProps;
const source = map.getSource(this.id);

// Update tilesJsonSource
if (tileJsonSource && this.props.tileJsonSource) {
let urlUpdated = false;
let tilesUpdated = false;

if (source && source.type === 'vector') {
const hasNewSourceUrl =
tileJsonSource.url !== this.props.tileJsonSource.url;

if (hasNewSourceUrl && this.props.tileJsonSource.url) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't permit unsetting the URL – not sure if there would be a valid use case for it, but is it intentional here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Mapbox types seem to indicate that the url cannot be "unset", i.e the functions don't take undefined as an argument - for that I guess one would have to change the entire source. I did now however make this check more explicit about undefined.

source.setUrl(this.props.tileJsonSource.url);
urlUpdated = true;
}

const hasNewSourceTiles =
tileJsonSource.tiles !== this.props.tileJsonSource.tiles;
henrinormak marked this conversation as resolved.
Show resolved Hide resolved
if (hasNewSourceTiles && this.props.tileJsonSource.tiles) {
source.setTiles(this.props.tileJsonSource.tiles);
tilesUpdated = true;
}
}

// Prefer the more targetted updates, but fallback to swapping out the entire source
// This applies to raster tile sources, for example
const hasNewTilesSource =
tileJsonSource.url !== this.props.tileJsonSource.url ||
(!urlUpdated && tileJsonSource.url !== this.props.tileJsonSource.url) ||
// Check for reference equality on tiles array
tileJsonSource.tiles !== this.props.tileJsonSource.tiles ||
(!tilesUpdated &&
tileJsonSource.tiles !== this.props.tileJsonSource.tiles) ||
tileJsonSource.minzoom !== this.props.tileJsonSource.minzoom ||
tileJsonSource.maxzoom !== this.props.tileJsonSource.maxzoom;

Expand All @@ -132,9 +154,9 @@ export class Source extends React.Component<Props> {
this.props.geoJsonSource &&
this.props.geoJsonSource.data !== geoJsonSource.data &&
this.props.geoJsonSource.data &&
map.getSource(this.id)
source &&
source.type === 'geojson'
) {
const source = map.getSource(this.id) as GeoJSONSource;
source.setData(this.props.geoJsonSource.data);
}
}
Expand Down