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

visgl / react-map-gl / 24371188141

13 Apr 2026 10:57PM UTC coverage: 85.349% (+0.2%) from 85.103%
24371188141

Pull #2573

github

web-flow
Merge ffe693798 into a4050bc65
Pull Request #2573: Call setMinZoom, setMaxZoom, setMinPitch and setMaxPitch in the right order to avoid error in Maplibre

958 of 1181 branches covered (81.12%)

Branch coverage included in aggregate %.

133 of 134 new or added lines in 2 files covered. (99.25%)

2 existing lines in 1 file now uncovered.

5712 of 6634 relevant lines covered (86.1%)

61.11 hits per line

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

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

1✔
7
import type {
1✔
8
  SourceSpecification,
1✔
9
  CanvasSourceSpecification,
1✔
10
  ImageSourceSpecification,
1✔
11
  VectorSourceSpecification
1✔
12
} from '../types/style-spec';
1✔
13
import type {MapInstance} from '../types/lib';
1✔
14
import type {
1✔
15
  GeoJSONSourceImplementation,
1✔
16
  ImageSourceImplementation,
1✔
17
  AnySourceImplementation
1✔
18
} from '../types/internal';
1✔
19

1✔
20
export type SourceProps = (SourceSpecification | CanvasSourceSpecification) & {
1✔
21
  id?: string;
1✔
22
  children?: any;
1✔
23
};
1✔
24

1✔
25
let sourceCounter = 0;
1✔
26

1✔
27
function createSource(map: MapInstance, id: string, props: SourceProps) {
4✔
28
  // @ts-ignore
4✔
29
  if (map.style && map.style._loaded) {
4✔
30
    const options = {...props};
4✔
31
    delete options.id;
4✔
32
    delete options.children;
4✔
33
    // @ts-ignore
4✔
34
    map.addSource(id, options);
4✔
35
    return map.getSource(id);
4✔
36
  }
4!
UNCOV
37
  return null;
×
UNCOV
38
}
×
39

1✔
40
/* eslint-disable complexity */
1✔
41
function updateSource(source: AnySourceImplementation, props: SourceProps, prevProps: SourceProps) {
11✔
42
  assert(props.id === prevProps.id, 'source id changed');
11✔
43
  assert(props.type === prevProps.type, 'source type changed');
11✔
44

11✔
45
  let changedKey = '';
11✔
46
  let changedKeyCount = 0;
11✔
47

11✔
48
  for (const key in props) {
11✔
49
    if (key !== 'children' && key !== 'id' && !deepEqual(prevProps[key], props[key])) {
39✔
50
      changedKey = key;
1✔
51
      changedKeyCount++;
1✔
52
    }
1✔
53
  }
39✔
54

11✔
55
  if (!changedKeyCount) {
11✔
56
    return;
10✔
57
  }
10✔
58

1✔
59
  const type = props.type;
1✔
60

1✔
61
  if (type === 'geojson') {
1✔
62
    (source as GeoJSONSourceImplementation).setData(props.data);
1✔
63
  } else if (type === 'image') {
11!
64
    (source as ImageSourceImplementation).updateImage({
×
65
      url: props.url,
×
66
      coordinates: props.coordinates
×
67
    });
×
68
  } else if ('setCoordinates' in source && changedKeyCount === 1 && changedKey === 'coordinates') {
×
69
    source.setCoordinates((props as unknown as ImageSourceSpecification).coordinates);
×
70
  } else if ('setUrl' in source && changedKey === 'url') {
×
71
    source.setUrl((props as VectorSourceSpecification).url);
×
72
  } else if ('setTiles' in source && changedKey === 'tiles') {
×
73
    source.setTiles((props as VectorSourceSpecification).tiles);
×
74
  } else {
×
75
    // eslint-disable-next-line
×
76
    console.warn(`Unable to update <Source> prop: ${changedKey}`);
×
77
  }
×
78
}
11✔
79
/* eslint-enable complexity */
1✔
80

1✔
81
export function Source(props: SourceProps) {
1✔
82
  const map = useContext(MapContext).map.getMap();
15✔
83
  const propsRef = useRef(props);
15✔
84
  const [, setStyleLoaded] = useState(0);
15✔
85

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

15✔
88
  useEffect(() => {
15✔
89
    if (map) {
2✔
90
      /* global setTimeout */
2✔
91
      const forceUpdate = () => setTimeout(() => setStyleLoaded(version => version + 1), 0);
2✔
92
      map.on('styledata', forceUpdate);
2✔
93
      forceUpdate();
2✔
94

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

15✔
118
  // @ts-ignore
15✔
119
  let source = map && map.style && map.getSource(id);
15✔
120
  if (source) {
15✔
121
    updateSource(source, props, propsRef.current);
11✔
122
  } else {
15✔
123
    source = createSource(map, id, props);
4✔
124
  }
4✔
125
  propsRef.current = props;
15✔
126

15✔
127
  return (
15✔
128
    (source &&
15✔
129
      React.Children.map(
15✔
130
        props.children,
15✔
131
        child =>
15✔
132
          child &&
8✔
133
          cloneElement(child, {
8✔
134
            source: id
8✔
135
          })
8✔
136
      )) ||
15✔
137
    null
7✔
138
  );
15✔
139
}
15✔
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