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

visgl / react-map-gl / 14338424105

08 Apr 2025 04:11PM UTC coverage: 84.959% (-0.05%) from 85.008%
14338424105

push

github

web-flow
Use name of defined component (#2522)

942 of 1167 branches covered (80.72%)

Branch coverage included in aggregate %.

5565 of 6492 relevant lines covered (85.72%)

68.7 hits per line

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

87.27
/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
  if (map.isStyleLoaded()) {
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) {
11✔
41
  assert(props.id === prevProps.id, 'source id changed');
11✔
42
  assert(props.type === prevProps.type, 'source type changed');
11✔
43

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

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

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

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

1✔
60
  if (type === 'geojson') {
1✔
61
    (source as GeoJSONSourceImplementation).setData(props.data);
1✔
62
  } else if (type === 'image') {
11!
63
    (source as ImageSourceImplementation).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
}
11✔
78
/* eslint-enable complexity */
1✔
79

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

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

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

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

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

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