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

visgl / react-map-gl / 18797483149

25 Oct 2025 03:24AM UTC coverage: 85.108% (+0.1%) from 84.961%
18797483149

Pull #2535

github

web-flow
Merge e405c1ccf into 6bf0a3771
Pull Request #2535: fix: max depth exceeded error when dynamically changing map settings

946 of 1167 branches covered (81.06%)

Branch coverage included in aggregate %.

5569 of 6488 relevant lines covered (85.84%)

61.72 hits per line

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

87.12
/modules/main/src/mapbox-legacy/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
  GeoJSONSourceImplementation,
1✔
9
  ImageSourceImplemtation,
1✔
10
  AnySourceImplementation
1✔
11
} from '../types/internal';
1✔
12
import type {
1✔
13
  SourceSpecification,
1✔
14
  ImageSourceSpecification,
1✔
15
  VectorSourceSpecification
1✔
16
} from '../types/style-spec';
1✔
17
import type {MapInstance} from '../types/lib';
1✔
18

1✔
19
export type SourceProps = SourceSpecification & {
1✔
20
  id?: string;
1✔
21
  children?: any;
1✔
22
};
1✔
23

1✔
24
let sourceCounter = 0;
1✔
25

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

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

10✔
44
  let changedKey = '';
10✔
45
  let changedKeyCount = 0;
10✔
46

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

10✔
54
  if (!changedKeyCount) {
10✔
55
    return;
9✔
56
  }
9✔
57

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

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

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

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

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

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

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

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