Skip to content

Commit

Permalink
front: fix a crash that would happen when editing a train
Browse files Browse the repository at this point in the history
the two rtk-query calls postV2TrainSchedule & postV2TrainScheduleSimulationSummary do not happen during the same react cycle.

if we update a train, one is going to re-fetch first and the 2 are out of sync during a few cycles, which fails an assert.
  • Loading branch information
anisometropie committed Aug 23, 2024
1 parent 1484faf commit 6d020d8
Showing 1 changed file with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,33 @@ const useLazyLoadTrains = ({
},
}).unwrap();

// launch the projection of the trains
setTrainIdsToProject((prev) => [...prev, ...packageToFetch]);

// format the summaries to display them in the timetable
const newFormattedSummaries = formatTrainScheduleSummaries(
packageToFetch,
rawSummaries,
trainSchedulesById,
rollingStocks
);

// as formattedSummaries is a dictionary, we replace the previous values with the new ones
setTrainScheduleSummariesById((prev) => concatMap(prev, newFormattedSummaries));
// the two rtk-query calls postV2TrainSchedule & postV2TrainScheduleSimulationSummary
// do not happen during the same react cycle.
// if we update a train, one is going to re-fetch first and the 2 are out of sync during a few cycles.
// these cycles do not make sense to render.
const outOfSync = [...trainSchedulesById.values()].some((trainShedule) => {
const summary = rawSummaries[trainShedule.id];
if (summary?.status === 'success') {
return trainShedule.path.length !== summary.path_item_times_final.length;
}
return false;
});

if (!outOfSync) {
// launch the projection of the trains
setTrainIdsToProject((prev) => [...prev, ...packageToFetch]);

// format the summaries to display them in the timetable
const newFormattedSummaries = formatTrainScheduleSummaries(
packageToFetch,
rawSummaries,
trainSchedulesById,
rollingStocks
);

// as formattedSummaries is a dictionary, we replace the previous values with the new ones
setTrainScheduleSummariesById((prev) => concatMap(prev, newFormattedSummaries));
}
}

setTrainIdsToFetch([]);
Expand Down

0 comments on commit 6d020d8

Please sign in to comment.