(props: MapProps, ref: React.Ref<MapRef>)
| 35 | }; |
| 36 | |
| 37 | function _Map(props: MapProps, ref: React.Ref<MapRef>) { |
| 38 | const mountedMapsContext = useContext(MountedMapsContext); |
| 39 | const [mapInstance, setMapInstance] = useState<Mapbox>(null); |
| 40 | const containerRef = useRef(); |
| 41 | |
| 42 | const {current: contextValue} = useRef<MapContextValue>({mapLib: null, map: null}); |
| 43 | |
| 44 | useEffect(() => { |
| 45 | const mapLib = props.mapLib; |
| 46 | let isMounted = true; |
| 47 | let mapbox: Mapbox; |
| 48 | |
| 49 | Promise.resolve(mapLib || import('mapbox-gl')) |
| 50 | .then((module: MapLib | {default: MapLib}) => { |
| 51 | if (!isMounted) { |
| 52 | return; |
| 53 | } |
| 54 | if (!module) { |
| 55 | throw new Error('Invalid mapLib'); |
| 56 | } |
| 57 | const mapboxgl = 'Map' in module ? module : module.default; |
| 58 | if (!mapboxgl.Map) { |
| 59 | throw new Error('Invalid mapLib'); |
| 60 | } |
| 61 | |
| 62 | setGlobals(mapboxgl, props); |
| 63 | if (props.reuseMaps) { |
| 64 | mapbox = Mapbox.reuse(props, containerRef.current); |
| 65 | } |
| 66 | if (!mapbox) { |
| 67 | mapbox = new Mapbox(mapboxgl.Map, props, containerRef.current); |
| 68 | } |
| 69 | contextValue.map = createRef(mapbox); |
| 70 | contextValue.mapLib = mapboxgl; |
| 71 | |
| 72 | setMapInstance(mapbox); |
| 73 | mountedMapsContext?.onMapMount(contextValue.map, props.id); |
| 74 | }) |
| 75 | .catch(error => { |
| 76 | const {onError} = props; |
| 77 | if (onError) { |
| 78 | onError({ |
| 79 | type: 'error', |
| 80 | target: null, |
| 81 | error |
| 82 | }); |
| 83 | } else { |
| 84 | console.error(error); // eslint-disable-line |
| 85 | } |
| 86 | }); |
| 87 | |
| 88 | return () => { |
| 89 | isMounted = false; |
| 90 | if (mapbox) { |
| 91 | mountedMapsContext?.onMapUnmount(props.id); |
| 92 | if (props.reuseMaps) { |
| 93 | mapbox.recycle(); |
| 94 | } else { |
nothing calls this directly
no test coverage detected
searching dependent graphs…