Skip to content

Commit

Permalink
refactor: improve cleanup process
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmedhossamdev committed Nov 25, 2024
1 parent b49100e commit f8f2483
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/components/map/RouteMap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@
// used to clear interval api calls
let currentIntervalId = null;
let loadRouteDataPromise = null;
onMount(async () => {
await loadRouteData();
loadRouteDataPromise = loadRouteData();
await loadRouteDataPromise;
});
onDestroy(async () => {
isMounted = false;
mapProvider.removePolyline(await polyline);
mapProvider.removeStopMarkers();
mapProvider.cleanupInfoWindow();
clearInterval(currentIntervalId);
clearVehicleMarkersMap(mapProvider);
mapProvider.clearVehicleMarkers();
if (loadRouteDataPromise) {
await loadRouteDataPromise;
}
await Promise.all([
mapProvider.removePolyline(await polyline),
mapProvider.removeStopMarkers(),
mapProvider.cleanupInfoWindow(),
clearInterval(currentIntervalId),
clearVehicleMarkersMap(mapProvider),
mapProvider.clearVehicleMarkers()
]);
});
async function loadRouteData() {
Expand Down Expand Up @@ -56,6 +63,8 @@
}
}
currentIntervalId = await fetchAndUpdateVehicles(routeId, mapProvider);
if (routeId && isMounted) {
currentIntervalId = await fetchAndUpdateVehicles(routeId, mapProvider);
}
}
</script>

0 comments on commit f8f2483

Please sign in to comment.