• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visgl / react-map-gl / 12408321118

19 Dec 2024 07:38AM UTC coverage: 81.65% (+0.3%) from 81.383%
12408321118

push

github

web-flow
Improves Source#updateSource to use setTiles function if available (#2355)

264 of 321 branches covered (82.24%)

Branch coverage included in aggregate %.

0 of 4 new or added lines in 1 file covered. (0.0%)

2032 of 2491 relevant lines covered (81.57%)

12.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.66
/src/components/source.ts
1
import * as React from 'react';
1✔
2
import {useContext, useEffect, useMemo, useState, useRef} from 'react';
1✔
3
import {cloneElement} from 'react';
1✔
4
import {MapContext} from './map';
1✔
5
import assert from '../utils/assert';
1✔
6
import {deepEqual} from '../utils/deep-equal';
1✔
7

1✔
8
import type {
1✔
9
  MapInstance,
1✔
10
  ISource,
1✔
11
  CustomSource,
1✔
12
  GeoJSONSourceImplementation,
1✔
13
  ImageSourceImplemtation,
1✔
14
  AnySourceImplementation
1✔
15
} from '../types';
1✔
16
import type {GeoJSONSourceRaw, ImageSourceRaw, VectorSourceRaw} from '../types/style-spec-maplibre';
1✔
17

1✔
18
export type SourceProps<SourceT> = (SourceT | CustomSource) & {
1✔
19
  id?: string;
1✔
20
  children?: any;
1✔
21
};
1✔
22

1✔
23
let sourceCounter = 0;
1✔
24

1✔
25
function createSource<SourceT extends ISource>(
6✔
26
  map: MapInstance,
6✔
27
  id: string,
6✔
28
  props: SourceProps<SourceT>
6✔
29
) {
6✔
30
  // @ts-ignore
6✔
31
  if (map.style && map.style._loaded) {
6✔
32
    const options = {...props};
4✔
33
    delete options.id;
4✔
34
    delete options.children;
4✔
35
    // @ts-ignore
4✔
36
    map.addSource(id, options);
4✔
37
    return map.getSource(id);
4✔
38
  }
4✔
39
  return null;
2✔
40
}
2✔
41

1✔
42
/* eslint-disable complexity */
1✔
43
function updateSource<SourceT extends ISource>(
6✔
44
  source: AnySourceImplementation,
6✔
45
  props: SourceProps<SourceT>,
6✔
46
  prevProps: SourceProps<SourceT>
6✔
47
) {
6✔
48
  assert(props.id === prevProps.id, 'source id changed');
6✔
49
  assert(props.type === prevProps.type, 'source type changed');
6✔
50

6✔
51
  let changedKey = '';
6✔
52
  let changedKeyCount = 0;
6✔
53

6✔
54
  for (const key in props) {
6✔
55
    if (key !== 'children' && key !== 'id' && !deepEqual(prevProps[key], props[key])) {
21✔
56
      changedKey = key;
1✔
57
      changedKeyCount++;
1✔
58
    }
1✔
59
  }
21✔
60

6✔
61
  if (!changedKeyCount) {
6✔
62
    return;
5✔
63
  }
5✔
64

1✔
65
  const type = props.type;
1✔
66

1✔
67
  if (type === 'geojson') {
1✔
68
    (source as GeoJSONSourceImplementation).setData(
1✔
69
      (props as unknown as GeoJSONSourceRaw).data as any
1✔
70
    );
1✔
71
  } else if (type === 'image') {
6!
72
    (source as ImageSourceImplemtation).updateImage({
×
73
      url: (props as unknown as ImageSourceRaw).url,
×
74
      coordinates: (props as unknown as ImageSourceRaw).coordinates
×
75
    });
×
76
  } else if ('setCoordinates' in source && changedKeyCount === 1 && changedKey === 'coordinates') {
×
77
    source.setCoordinates((props as ImageSourceRaw).coordinates);
×
NEW
78
  } else if ('setUrl' in source && changedKey === 'url') {
×
NEW
79
    source.setUrl((props as VectorSourceRaw).url);
×
NEW
80
  } else if ('setTiles' in source && changedKey === 'tiles') {
×
NEW
81
    source.setTiles((props as VectorSourceRaw).tiles);
×
82
  } else {
×
83
    // eslint-disable-next-line
×
84
    console.warn(`Unable to update <Source> prop: ${changedKey}`);
×
85
  }
×
86
}
6✔
87
/* eslint-enable complexity */
1✔
88

1✔
89
function Source<SourceT extends ISource>(props: SourceProps<SourceT>) {
12✔
90
  const map = useContext(MapContext).map.getMap();
12✔
91
  const propsRef = useRef(props);
12✔
92
  const [, setStyleLoaded] = useState(0);
12✔
93

12✔
94
  const id = useMemo(() => props.id || `jsx-source-${sourceCounter++}`, []);
12!
95

12✔
96
  useEffect(() => {
12✔
97
    if (map) {
2✔
98
      /* global setTimeout */
2✔
99
      const forceUpdate = () => setTimeout(() => setStyleLoaded(version => version + 1), 0);
2✔
100
      map.on('styledata', forceUpdate);
2✔
101
      forceUpdate();
2✔
102

2✔
103
      return () => {
2✔
104
        map.off('styledata', forceUpdate);
2✔
105
        // @ts-ignore
2✔
106
        if (map.style && map.style._loaded && map.getSource(id)) {
2✔
107
          // Parent effects are destroyed before child ones, see
2✔
108
          // https://github.com/facebook/react/issues/16728
2✔
109
          // Source can only be removed after all child layers are removed
2✔
110
          const allLayers = map.getStyle()?.layers;
2✔
111
          if (allLayers) {
2✔
112
            for (const layer of allLayers) {
2✔
113
              // @ts-ignore (2339) source does not exist on all layer types
1✔
114
              if (layer.source === id) {
1✔
115
                map.removeLayer(layer.id);
1✔
116
              }
1✔
117
            }
1✔
118
          }
2✔
119
          map.removeSource(id);
2✔
120
        }
2✔
121
      };
2✔
122
    }
2✔
123
    return undefined;
×
124
  }, [map]);
12✔
125

12✔
126
  // @ts-ignore
12✔
127
  let source = map && map.style && map.getSource(id);
12✔
128
  if (source) {
12✔
129
    updateSource(source, props, propsRef.current);
6✔
130
  } else {
6✔
131
    source = createSource(map, id, props);
6✔
132
  }
6✔
133
  propsRef.current = props;
12✔
134

12✔
135
  return (
12✔
136
    (source &&
12✔
137
      React.Children.map(
10✔
138
        props.children,
10✔
139
        child =>
10✔
140
          child &&
5✔
141
          cloneElement(child, {
5✔
142
            source: id
5✔
143
          })
5✔
144
      )) ||
12✔
145
    null
7✔
146
  );
12✔
147
}
12✔
148

1✔
149
export default Source;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc