From ee10daf957d19af24b8ac5e6477491b8339ddbe2 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Fri, 21 Nov 2025 11:42:42 -0800 Subject: [PATCH] Rename getArrivedStopIfExists to getArrivedStopIfNextStop, and add more documentation --- .../shuttle/RedisShuttleRepository.ts | 20 +++++++++---------- .../shuttle/ShuttleGetterRepository.ts | 6 ++++-- .../UnoptimizedInMemoryShuttleRepository.ts | 20 +++++++++---------- .../ShuttleRepositorySharedTests.test.ts | 18 ++++++++--------- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/repositories/shuttle/RedisShuttleRepository.ts b/src/repositories/shuttle/RedisShuttleRepository.ts index d4669bc..86af512 100644 --- a/src/repositories/shuttle/RedisShuttleRepository.ts +++ b/src/repositories/shuttle/RedisShuttleRepository.ts @@ -1,13 +1,13 @@ import { ShuttleGetterSetterRepository } from "./ShuttleGetterSetterRepository"; import { IEta, IOrderedStop, IRoute, IShuttle, IStop, shuttleHasArrivedAtStop } from "../../entities/ShuttleRepositoryEntities"; import { - ShuttleRepositoryEvent, - ShuttleRepositoryEventListener, - ShuttleRepositoryEventName, - ShuttleRepositoryEventPayloads, - ShuttleStopArrival, - ShuttleTravelTimeDataIdentifier, - ShuttleTravelTimeDateFilterArguments + ShuttleRepositoryEvent, + ShuttleRepositoryEventListener, + ShuttleRepositoryEventName, + ShuttleRepositoryEventPayloads, + ShuttleStopArrival, + ShuttleTravelTimeDataIdentifier, + ShuttleTravelTimeDateFilterArguments } from "./ShuttleGetterRepository"; import { BaseRedisRepository } from "../BaseRedisRepository"; import { RedisClientType } from "redis"; @@ -375,9 +375,9 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt if (isAtStop) { // Allow retrieval of the shuttle's current stop // Will still return undefined when the shuttle leaves the stop - arrivedStop = await this.getArrivedStopIfExists(shuttle, true); + arrivedStop = await this.getArrivedStopIfNextStop(shuttle, true); } else { - arrivedStop = await this.getArrivedStopIfExists(shuttle, false); + arrivedStop = await this.getArrivedStopIfNextStop(shuttle, false); } // Will not fire *any* events if the same stop @@ -452,7 +452,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt } } - public async getArrivedStopIfExists( + public async getArrivedStopIfNextStop( shuttle: IShuttle, canReturnShuttleCurrentStop: boolean = false, ): Promise { diff --git a/src/repositories/shuttle/ShuttleGetterRepository.ts b/src/repositories/shuttle/ShuttleGetterRepository.ts index 44815ec..91f51e3 100644 --- a/src/repositories/shuttle/ShuttleGetterRepository.ts +++ b/src/repositories/shuttle/ShuttleGetterRepository.ts @@ -104,7 +104,9 @@ export interface ShuttleGetterRepository extends EventEmitter { checkIfShuttleIsAtStop(shuttleId: string): Promise; /** - * Get the stop that the shuttle is currently at, if it exists. + * Get the stop that the shuttle is currently at, if it's the shuttle's + * next stop based on the "last stop" the shuttle was at. If there was no + * "last stop" for the shuttle, it may return any stop on the shuttle's route. * * @param shuttle * @param canReturnShuttleCurrentStop If set to true, and the shuttle's "last stop" @@ -113,5 +115,5 @@ export interface ShuttleGetterRepository extends EventEmitter { * This flag has no effect if the shuttle has not had a "last stop". * @returns */ - getArrivedStopIfExists(shuttle: IShuttle, canReturnShuttleCurrentStop: boolean): Promise; + getArrivedStopIfNextStop(shuttle: IShuttle, canReturnShuttleCurrentStop: boolean): Promise; } diff --git a/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts b/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts index 38c7c02..c3f192c 100644 --- a/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts +++ b/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts @@ -3,13 +3,13 @@ import { ShuttleGetterSetterRepository } from "./ShuttleGetterSetterRepository"; import { IOrderedStop, IRoute, IShuttle, IStop, shuttleHasArrivedAtStop } from "../../entities/ShuttleRepositoryEntities"; import { IEntityWithId } from "../../entities/SharedEntities"; import { - ShuttleRepositoryEvent, - ShuttleRepositoryEventListener, - ShuttleRepositoryEventName, - ShuttleRepositoryEventPayloads, - ShuttleStopArrival, - ShuttleTravelTimeDataIdentifier, - ShuttleTravelTimeDateFilterArguments, + ShuttleRepositoryEvent, + ShuttleRepositoryEventListener, + ShuttleRepositoryEventName, + ShuttleRepositoryEventPayloads, + ShuttleStopArrival, + ShuttleTravelTimeDataIdentifier, + ShuttleTravelTimeDateFilterArguments, } from "./ShuttleGetterRepository"; /** @@ -190,9 +190,9 @@ export class UnoptimizedInMemoryShuttleRepository if (isAtStop) { // Allow retrieval of the shuttle's current stop // Will still return undefined when the shuttle leaves the stop - arrivedStop = await this.getArrivedStopIfExists(shuttle, true); + arrivedStop = await this.getArrivedStopIfNextStop(shuttle, true); } else { - arrivedStop = await this.getArrivedStopIfExists(shuttle, false); + arrivedStop = await this.getArrivedStopIfNextStop(shuttle, false); } @@ -268,7 +268,7 @@ export class UnoptimizedInMemoryShuttleRepository return sum / filteredPoints.length; } - public async getArrivedStopIfExists( + public async getArrivedStopIfNextStop( shuttle: IShuttle, canReturnShuttleCurrentStop: boolean = false, ): Promise { diff --git a/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts b/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts index 59745e7..8520999 100644 --- a/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts +++ b/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts @@ -4,10 +4,10 @@ import { UnoptimizedInMemoryShuttleRepository } from "../UnoptimizedInMemoryShut import { ShuttleGetterSetterRepository } from "../ShuttleGetterSetterRepository"; import { RedisShuttleRepository } from "../RedisShuttleRepository"; import { - generateMockOrderedStops, - generateMockRoutes, - generateMockShuttles, - generateMockStops, + generateMockOrderedStops, + generateMockRoutes, + generateMockShuttles, + generateMockStops, } from "../../../../testHelpers/mockDataGenerators"; import { RepositoryHolder } from "../../../../testHelpers/RepositoryHolder"; import { setupRouteAndOrderedStopsForShuttleRepository } from "../../../../testHelpers/setupRouteAndOrderedStopsForShuttleRepository"; @@ -577,7 +577,7 @@ describe.each(repositoryImplementations)('$name', (holder) => { test("gets any stop that the shuttle is currently at, if the shuttle has not had a last stop", async () => { const { sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); - const result = await repository.getArrivedStopIfExists(shuttle, false); + const result = await repository.getArrivedStopIfNextStop(shuttle, false); expect(result).toBeDefined(); expect(result?.id).toBe("st2"); @@ -588,7 +588,7 @@ describe.each(repositoryImplementations)('$name', (holder) => { const { sampleShuttleNotInRepository } = await setupRouteAndOrderedStops(); const shuttle = { ...sampleShuttleNotInRepository, coordinates: { latitude: 12.5, longitude: 22.5 } }; // Not at any stop - const result = await repository.getArrivedStopIfExists(shuttle, false); + const result = await repository.getArrivedStopIfNextStop(shuttle, false); expect(result).toBeUndefined(); }); @@ -599,11 +599,11 @@ describe.each(repositoryImplementations)('$name', (holder) => { shuttle.coordinates = stop1.coordinates; await repository.addOrUpdateShuttle(shuttle); - let result = await repository.getArrivedStopIfExists(shuttle, false); + let result = await repository.getArrivedStopIfNextStop(shuttle, false); expect(result).toBeUndefined(); shuttle.coordinates = stop2.coordinates; - result = await repository.getArrivedStopIfExists(shuttle, false); + result = await repository.getArrivedStopIfNextStop(shuttle, false); expect(result).not.toBeUndefined(); }); @@ -613,7 +613,7 @@ describe.each(repositoryImplementations)('$name', (holder) => { shuttle.coordinates = stop1.coordinates; await repository.addOrUpdateShuttle(shuttle); - const result = await repository.getArrivedStopIfExists(shuttle, true); + const result = await repository.getArrivedStopIfNextStop(shuttle, true); expect(result?.id === stop1.id) });