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

visgl / react-map-gl / 18231345194

03 Oct 2025 07:04PM UTC coverage: 84.961% (-0.04%) from 85.0%
18231345194

push

github

web-flow
Update what's new for 8.1 release (#2562)

944 of 1169 branches covered (80.75%)

Branch coverage included in aggregate %.

5564 of 6491 relevant lines covered (85.72%)

67.96 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!
37
  return null;
×
38
}
×
39

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

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

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

10✔
55
  if (!changedKeyCount) {
10✔
56
    return;
9✔
57
  }
9✔
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') {
10!
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
}
10✔
79
/* eslint-enable complexity */
1✔
80

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

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

14✔
88
  useEffect(() => {
14✔
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]);
14✔
117

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

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