Make passings an object

This commit is contained in:
Mattéo Delabre 2020-07-24 19:05:28 +02:00
parent 5c4f219f7a
commit 91d6771f5e
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
1 changed files with 6 additions and 26 deletions

View File

@ -2,18 +2,6 @@ const tam = require('./sources/tam');
const util = require('../util'); const util = require('../util');
const network = require('./network.json'); const network = require('./network.json');
/**
* Comparison function between two stop passings.
*
* @param passing1 First stop passing.
* @param passing2 Second stop passing.
* @return Negative value if passing1 is sooner than passing2, positive
* otherwise, zero if they occur at the same time.
*/
const passingCompare = ({arrivalTime: time1}, {arrivalTime: time2}) => (
time1 - time2
);
// Time at which the course data needs to be updated next // Time at which the course data needs to be updated next
let nextUpdate = null; let nextUpdate = null;
@ -31,9 +19,8 @@ let currentCourses = null;
* - `id`: Unique identifier for the course. * - `id`: Unique identifier for the course.
* - `line`: Line number. * - `line`: Line number.
* - `finalStop`: The final stop to which the course is headed. * - `finalStop`: The final stop to which the course is headed.
* - `nextPassings`: Next passings of the vehicle, sorted by increasing * - `nextPassings`: Next passings of the vehicle, as a dictionary associating
* arrival time, containing both the stop identifier (`stopId`) and the * each next stop to the passing timestamp.
* expected arrival timestamp (`arrivalTime`).
* *
* @return Mapping from active course IDs to information about each course. * @return Mapping from active course IDs to information about each course.
*/ */
@ -69,9 +56,9 @@ const getCourses = () => new Promise((res, rej) =>
} }
else else
{ {
for (let passing of course.nextPassings) for (let stopId of Object.keys(course.nextPassings))
{ {
if (!(passing.stopId in network.stops)) if (!(stopId in network.stops))
{ {
delete courses[courseId]; delete courses[courseId];
break; break;
@ -80,13 +67,6 @@ const getCourses = () => new Promise((res, rej) =>
} }
} }
// End of courses information stream. Sort next stops by increasing
// arrival time in each course then save result in memory cache
for (let course of Object.values(courses))
{
course.nextPassings.sort(passingCompare);
}
currentCourses = courses; currentCourses = courses;
res(currentCourses); res(currentCourses);
return; return;
@ -113,12 +93,12 @@ const getCourses = () => new Promise((res, rej) =>
{ {
courses[id] = { courses[id] = {
id, line, finalStop, id, line, finalStop,
nextPassings: [{stopId, arrivalTime}], nextPassings: {[stopId]: arrivalTime},
}; };
} }
else else
{ {
courses[id].nextPassings.push({stopId, arrivalTime}); courses[id].nextPassings[stopId] = arrivalTime;
} }
}); });
}); });