diff --git a/src/utils/RouteUtilsV3.js b/src/utils/RouteUtilsV3.js index 46c78a1..083b709 100644 --- a/src/utils/RouteUtilsV3.js +++ b/src/utils/RouteUtilsV3.js @@ -254,6 +254,42 @@ async function getSectionedRoutes( isArriveBy, ); + // Iterates over each route in finalBusRoutes, calculates total delay from bus segments, + // and applies this delay to the 'walk' segment if it follows a delayed bus segment. + finalBusRoutes.forEach((route) => { + let totalDelay = 0; + let firstDelay = null; + let foundFirstDelay = false; + let firstDelayIndex = 0; + + const { directions } = route; + + for (let i = 0; i < directions.length; i++) { + const segment = directions[i]; + const { delay } = segment; + if (!foundFirstDelay && delay !== null) { + firstDelay = delay; + foundFirstDelay = true; + firstDelayIndex = i; + } + + if (segment.type === 'walk' && totalDelay > 0) { + segment.delay = totalDelay; + } else if (delay !== null) { + totalDelay += delay; + } + } + + // assign delay to walking route before first bus delay + let i = 0; + if (foundFirstDelay) { + while (directions[i].type === 'walk' && firstDelay != null && i < firstDelayIndex) { + directions[i].delay = firstDelay; + i += 1; + } + } + }); + finalBusRoutes.forEach((route) => { if (originBusStopName !== null && route.directions