Skip to content

Commit

Permalink
Improve bugfix for map flashing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
braddf committed Feb 23, 2024
1 parent 56b4d57 commit a8189d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
24 changes: 15 additions & 9 deletions apps/nowcasting-app/components/helpers/mapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,36 @@ export const safelyUpdateMapData = (
!map.isStyleLoaded()
) {
if (!map.isStyleLoaded()) {
// Check if we've already set a timeout for this map
console.warn("📍map style not loaded yet, skipping update");
// -- Check if we've already set a timeout for this map and therefore a check is already pending
const existingTimeout = localStorage.getItem(
`MapTimeoutId-${map.getContainer().dataset.title}`
);
// If we have, clear it
// -- If we have, skip the update and return
if (existingTimeout) {
// console.log(`clearing existing map timeout for ${map.getContainer().dataset.title}`);
clearTimeout(Number(existingTimeout));
console.debug("existing timeout running, skipping");
return;
}
// Set a new timeout to check if the map is ready and update the data
// console.log(`setting new map timeout for ${map.getContainer().dataset.title}`, newTimeout);
// -- Set a new timeout to check whether the map is ready and update the data
console.debug(`setting new map timeout for ${map.getContainer().dataset.title}`);
const newTimeout = setTimeout(() => {
// console.warn("map is not style loaded, trying again");
safelyUpdateMapData(map, updateMapData);
// console.log(`clearing new map timeout for ${map.getContainer().dataset.title}`);
localStorage.removeItem(`MapTimeoutId-${map.getContainer().dataset.title}`);
}, 500);
// Save the timeout id to local storage
// -- Save the timeout id to local storage
console.debug(
`saving new map timeout id for ${map.getContainer().dataset.title}`,
newTimeout
);
localStorage.setItem(
`MapTimeoutId-${map.getContainer().dataset.title}`,
newTimeout.toString()
);
}
return;
} else {
console.warn("🎉 map is ready, updating data");
console.debug("🎉 map is ready, updating data");
updateMapData(map);
}
};
10 changes: 10 additions & 0 deletions apps/nowcasting-app/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ export default function Home({ dashboardModeServer }: { dashboardModeServer: str
});
}, [view, maps]);

useEffect(() => {
// Clear any previous map timeouts on initial page load
for (const map of maps) {
localStorage.getItem(`MapTimeoutId-${map.getContainer().dataset.title}`) &&
clearTimeout(
Number(localStorage.getItem(`MapTimeoutId-${map.getContainer().dataset.title}`))
);
}
}, [maps]);

useEffect(() => {
maps.forEach((map) => {
console.log("-- -- -- resizing map");
Expand Down

0 comments on commit a8189d1

Please sign in to comment.