Skip to content

Commit

Permalink
Merge pull request #47 from toggle-corp/fix/map-destroyed-ref
Browse files Browse the repository at this point in the history
Fix mapDestroyedRef value not being set properly
  • Loading branch information
tnagorra authored Aug 13, 2023
2 parents 1909228 + 54621ad commit ecb5453
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ function Map(props: Props) {
...initialMapOptions,
});

mapDestroyedRef.current = false;
mapRef.current = mapboxglMap;
// FIXME: we shouldn't always set cursor to pointer
// mapboxglMap.getCanvas().style.cursor = 'pointer';
Expand Down
26 changes: 11 additions & 15 deletions src/MapImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import { MapChildContext } from './context';

type AddImageParams = Parameters<Map['addImage']>;

// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};

type Name = NonNullable<AddImageParams[0]>;
type Img = NonNullable<AddImageParams[1]>;
type ImageOptions = NonNullable<AddImageParams[2]>;
Expand Down Expand Up @@ -59,31 +56,30 @@ function MapImage<NAME extends Name>(props: Props<NAME>) {

useEffect(
() => {
if (!map || !mapStyle) {
return noop;
if (!map || !mapStyle || (!initialUrl && !initialImage)) {
return undefined;
}

if (map.hasImage(initialName)) {
// eslint-disable-next-line no-console
console.error(`An image with name '${initialName}' already exists`);
} else if (initialUrl) {
if (onLoad) {
onLoad(false, initialName);
}
return undefined;
}

if (initialUrl) {
map.loadImage(
initialUrl,
(error: unknown, loadedImage: Img) => {
if (!mountedRef.current) {
return;
}
if (isMapDestroyed()) {
return;
}
if (error) {
// eslint-disable-next-line no-console
console.error(error);
return;
}

if (!mountedRef.current || isMapDestroyed()) {
return;
}

map.addImage(initialName, loadedImage, initialImageOptions);
if (onLoad) {
onLoad(true, initialName);
Expand Down

0 comments on commit ecb5453

Please sign in to comment.