Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useRef, useEffect, useCallback, useState } from 'react';

import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import type {
LightRollingStockWithLiveries,
TrainScheduleSimulationSummaryResult,
Expand Down Expand Up @@ -47,13 +48,19 @@ export default function useLazySimulateTrains({
const [simulatedTrainsById, setSimulatedTrainsById] = useState<
Map<number, TrainScheduleWithDetails>
>(new Map());
const [allSimulationsDone, setAllSimulationsDone] = useState(false);

const onProgressRef = useRef<UseLazySimulateTrainsOptions['onProgress']>(null);
onProgressRef.current = onProgress;

useEffect(() => {
if (!rollingStocks) return undefined;

const idsToSimulate = [...trainSchedulesByIdRef.current.keys()];
if (idsToSimulate.length > 0) {
setAllSimulationsDone(false);
}

const loader = new TrainSimulationLazyLoader({
dispatch,
infraId,
Expand All @@ -67,10 +74,14 @@ export default function useLazySimulateTrains({
);
setSimulatedTrainsById((prev) => new Map([...prev.entries(), ...summaries.entries()]));
if (onProgressRef.current) onProgressRef.current(summaries);
if (loader.pending.length === 0) {
setAllSimulationsDone(true);
dispatch(osrdEditoastApi.util.invalidateTags(['conflicts']));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obtaining new simulations shouldn't invalidate any previously fetched conflicts.

Invalidating conflicts means that conflicts have changed. Obtaining simulations doesn't mutate any train schedule, so that can't invalidate conflicts.

}
},
});

loader.simulateTrainSchedules([...trainSchedulesByIdRef.current.keys()]);
loader.simulateTrainSchedules(idsToSimulate);

loaderRef.current = loader;
return () => {
Expand All @@ -84,6 +95,11 @@ export default function useLazySimulateTrains({
trainSchedulesByIdRef.current.set(trainSchedule.id, trainSchedule);
}

if (trainSchedules.length === 0) {
setAllSimulationsDone(true);
return;
}
setAllSimulationsDone(false);
loaderRef.current?.simulateTrainSchedules(trainSchedules.map(({ id }) => id));
}, []);

Expand Down Expand Up @@ -124,6 +140,6 @@ export default function useLazySimulateTrains({
simulateTrainSchedules,
removeSimulatedTrainSchedules,
updateSimulatedTrainScheduleDepartureTime,
allTrainsSimulated: loaderRef.current && loaderRef.current.pending.length === 0,
allTrainsSimulated: allSimulationsDone,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ const useScenarioData = (scenario: ScenarioWithDetails, infraId: number, timetab
}).unwrap();

setTrainScheduleDepartureTime(trainScheduleId, newDeparture);
dispatch(osrdEditoastApi.util.invalidateTags(['conflicts']));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is doing manually something that rtk-query should be doing automatically. Do we have a good reason to do so?

},
[trainSchedules]
);
Expand Down
6 changes: 5 additions & 1 deletion front/src/common/api/osrdEditoastApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ const osrdEditoastApi = generatedEditoastApi
}),
})
.enhanceEndpoints({
addTagTypes: ['conflicts'] as const,
endpoints: {
getLightRollingStock: {
transformResponse: (response: GetLightRollingStockApiResponse) => ({
Expand All @@ -281,8 +282,11 @@ const osrdEditoastApi = generatedEditoastApi
// As we always use all get train_schedule endpoints after updating the timetable,
// we don't want to invalidate the train_schedule tags here to prevent multiple calls

getTimetableByIdConflicts: {
providesTags: ['conflicts'],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the current tags, ['timetable']. This is an issue because deleteTrainSchedules is not the only request which should invalidate conflicts. For instance, updating a train schedule's path may also result in a new conflict or remove an existing conflict.

(Note, I don't understand why we need a new tag type.)

},
deleteTrainSchedules: {
invalidatesTags: ['timetable', 'scenarios'],
invalidatesTags: ['timetable', 'scenarios', 'conflicts'],
},
postTrainScheduleSetsByIdTrainSchedules: {
invalidatesTags: ['train_schedule_set', 'scenarios', 'timetable'],
Expand Down