-
Notifications
You must be signed in to change notification settings - Fork 536
/
source.ts
173 lines (139 loc) · 4.85 KB
/
source.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as React from 'react';
import { Map, GeoJSONSource, GeoJSONSourceRaw, Layer } from 'mapbox-gl';
import { TilesJson } from './util/types';
import { withMap } from './context';
export interface Props {
id: string;
geoJsonSource?: GeoJSONSourceRaw;
tileJsonSource?: TilesJson;
map: Map;
onSourceAdded?: (source: GeoJSONSource | TilesJson) => void;
onSourceLoaded?: (source: GeoJSONSource | TilesJson) => void;
}
export type LayerWithBefore = Layer & { before?: string };
export class Source extends React.Component<Props> {
private id = this.props.id;
private onStyleDataChange = () => {
// if the style of the map has been updated we won't have any sources anymore,
// add it back to the map and force re-rendering to redraw it
if (!this.props.map.getLayer(this.id)) {
this.initialize();
this.forceUpdate();
}
};
public componentDidMount() {
const { map } = this.props;
map.on('styledata', this.onStyleDataChange);
this.initialize();
}
private initialize = () => {
const { map } = this.props;
const { geoJsonSource, tileJsonSource, onSourceAdded } = this.props;
if (!map.getSource(this.id) && (geoJsonSource || tileJsonSource)) {
if (geoJsonSource) {
map.addSource(this.id, geoJsonSource);
} else if (tileJsonSource) {
map.addSource(this.id, tileJsonSource);
}
map.on('sourcedata', this.onData);
if (onSourceAdded) {
onSourceAdded(map.getSource(this.id) as GeoJSONSource | TilesJson);
}
}
};
private onData = () => {
const { map } = this.props;
const source = map.getSource(this.props.id) as GeoJSONSource;
if (!source || !map.isSourceLoaded(this.props.id)) {
return;
}
const { onSourceLoaded } = this.props;
if (source && onSourceLoaded) {
onSourceLoaded(source);
}
// Will fix datasource being empty
if (source && this.props.geoJsonSource && this.props.geoJsonSource.data) {
source.setData(this.props.geoJsonSource.data);
}
map.off('sourcedata', this.onData);
};
public removeSource(): LayerWithBefore[] {
const { map } = this.props;
if (map.getSource(this.id)) {
let { layers = [] } = map.getStyle();
layers = layers
.map((layer, idx): LayerWithBefore => {
const { id: before } = layers[idx + 1] || { id: undefined };
return { ...layer, before };
})
.filter(layer => layer.source === this.id);
layers.forEach(layer => map.removeLayer(layer.id));
map.removeSource(this.id);
return layers.reverse();
}
return [];
}
public componentWillUnmount() {
const { map } = this.props;
if (!map || !map.getStyle()) {
return;
}
map.off('styledata', this.onStyleDataChange);
this.removeSource();
}
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 !== undefined) {
source.setUrl(this.props.tileJsonSource.url);
urlUpdated = true;
}
const hasNewSourceTiles =
tileJsonSource.tiles !== this.props.tileJsonSource.tiles;
if (
hasNewSourceTiles &&
this.props.tileJsonSource.tiles !== undefined
) {
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 =
(!urlUpdated && tileJsonSource.url !== this.props.tileJsonSource.url) ||
// Check for reference equality on tiles array
(!tilesUpdated &&
tileJsonSource.tiles !== this.props.tileJsonSource.tiles) ||
tileJsonSource.minzoom !== this.props.tileJsonSource.minzoom ||
tileJsonSource.maxzoom !== this.props.tileJsonSource.maxzoom;
if (hasNewTilesSource) {
const layers = this.removeSource();
map.addSource(this.id, this.props.tileJsonSource);
layers.forEach(layer => map.addLayer(layer, layer.before));
}
}
// Update geoJsonSource data
if (
geoJsonSource &&
this.props.geoJsonSource &&
this.props.geoJsonSource.data !== geoJsonSource.data &&
this.props.geoJsonSource.data &&
source &&
source.type === 'geojson'
) {
source.setData(this.props.geoJsonSource.data);
}
}
public render() {
return null;
}
}
export default withMap(Source);