From 91d6771f5eafdd2a9cc72821425247b8edc76e98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Fri, 24 Jul 2020 19:05:28 +0200 Subject: [PATCH] Make passings an object --- src/tam/realtime.js | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/src/tam/realtime.js b/src/tam/realtime.js index 1e28b81..4d8d02b 100644 --- a/src/tam/realtime.js +++ b/src/tam/realtime.js @@ -2,18 +2,6 @@ const tam = require('./sources/tam'); const util = require('../util'); 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 let nextUpdate = null; @@ -31,9 +19,8 @@ let currentCourses = null; * - `id`: Unique identifier for the course. * - `line`: Line number. * - `finalStop`: The final stop to which the course is headed. - * - `nextPassings`: Next passings of the vehicle, sorted by increasing - * arrival time, containing both the stop identifier (`stopId`) and the - * expected arrival timestamp (`arrivalTime`). + * - `nextPassings`: Next passings of the vehicle, as a dictionary associating + * each next stop to the passing timestamp. * * @return Mapping from active course IDs to information about each course. */ @@ -69,9 +56,9 @@ const getCourses = () => new Promise((res, rej) => } 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]; 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; res(currentCourses); return; @@ -113,12 +93,12 @@ const getCourses = () => new Promise((res, rej) => { courses[id] = { id, line, finalStop, - nextPassings: [{stopId, arrivalTime}], + nextPassings: {[stopId]: arrivalTime}, }; } else { - courses[id].nextPassings.push({stopId, arrivalTime}); + courses[id].nextPassings[stopId] = arrivalTime; } }); });