From a9db9b5d5c9334a7df83c704d2abf1cc93401954 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Thu, 20 Nov 2025 16:47:50 -0800 Subject: [PATCH] Pass down the new arguments into the shuttle repositories --- src/entities/InterchangeSystem.ts | 12 ++++++++++-- .../shuttle/RedisShuttleRepository.ts | 19 +++++++++++++++---- .../UnoptimizedInMemoryShuttleRepository.ts | 10 +++++++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/entities/InterchangeSystem.ts b/src/entities/InterchangeSystem.ts index b8cfbe1..398c619 100644 --- a/src/entities/InterchangeSystem.ts +++ b/src/entities/InterchangeSystem.ts @@ -23,6 +23,7 @@ import { RedisExternalSourceETARepository } from "../repositories/shuttle/eta/Re import { InMemorySelfUpdatingETARepository } from "../repositories/shuttle/eta/InMemorySelfUpdatingETARepository"; import { BaseRedisETARepository } from "../repositories/shuttle/eta/BaseRedisETARepository"; import { BaseInMemoryETARepository } from "../repositories/shuttle/eta/BaseInMemoryETARepository"; +import createRedisClientForRepository from "../helpers/createRedisClientForRepository"; export interface InterchangeSystemBuilderArguments { name: string; @@ -112,7 +113,11 @@ export class InterchangeSystem { } private static async buildRedisShuttleLoaderAndRepositories(args: InterchangeSystemBuilderArguments) { - const shuttleRepository = new RedisShuttleRepository(); + const shuttleRepository = new RedisShuttleRepository( + createRedisClientForRepository(), + args.shuttleStopArrivalDegreeDelta, + args.shuttleStopNearbyDegreeDelta, + ); await shuttleRepository.connect(); let etaRepository: BaseRedisETARepository; @@ -261,7 +266,10 @@ export class InterchangeSystem { } private static buildInMemoryShuttleLoaderAndRepositories(args: InterchangeSystemBuilderArguments) { - const shuttleRepository = new UnoptimizedInMemoryShuttleRepository(); + const shuttleRepository = new UnoptimizedInMemoryShuttleRepository( + args.shuttleStopArrivalDegreeDelta, + args.shuttleStopNearbyDegreeDelta, + ); let etaRepository: BaseInMemoryETARepository; let shuttleDataLoader: ApiBasedShuttleRepositoryLoader; diff --git a/src/repositories/shuttle/RedisShuttleRepository.ts b/src/repositories/shuttle/RedisShuttleRepository.ts index 3d8cf60..515f3cd 100644 --- a/src/repositories/shuttle/RedisShuttleRepository.ts +++ b/src/repositories/shuttle/RedisShuttleRepository.ts @@ -10,8 +10,18 @@ import { ShuttleTravelTimeDateFilterArguments } from "./ShuttleGetterRepository"; import { BaseRedisRepository } from "../BaseRedisRepository"; +import { RedisClientType } from "redis"; +import createRedisClientForRepository from "../../helpers/createRedisClientForRepository"; export class RedisShuttleRepository extends BaseRedisRepository implements ShuttleGetterSetterRepository { + constructor( + redisClient: RedisClientType = createRedisClientForRepository(), + readonly shuttleStopArrivalDegreeDelta: number = 0.001, + readonly shuttleStopNearbyDegreeDelta: number = 0.003, + ) { + super(redisClient); + } + get isReady() { return this.redisClient.isReady; } @@ -481,20 +491,21 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt * is on the shuttle's route. * * @param shuttle - * @param delta + * @param degreeDelta * @returns */ public async getArrivedStopIfExists( shuttle: IShuttle, - delta = 0.001, ): Promise { + const degreeDelta = this.shuttleStopArrivalDegreeDelta; + const lastStop = await this.getShuttleLastStopArrival(shuttle.id); if (lastStop) { const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId); const orderedStopAfter = lastOrderedStop?.nextStop; if (orderedStopAfter) { const stopAfter = await this.getStopById(orderedStopAfter.stopId); - if (stopAfter && shuttleHasArrivedAtStop(shuttle, stopAfter, delta)) { + if (stopAfter && shuttleHasArrivedAtStop(shuttle, stopAfter, degreeDelta)) { return stopAfter; } } @@ -503,7 +514,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt for (const orderedStop of orderedStops) { const stop = await this.getStopById(orderedStop.stopId); - if (stop != null && shuttleHasArrivedAtStop(shuttle, stop, delta)) { + if (stop != null && shuttleHasArrivedAtStop(shuttle, stop, degreeDelta)) { return stop; } } diff --git a/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts b/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts index ab34df6..8746bb0 100644 --- a/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts +++ b/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts @@ -20,6 +20,14 @@ import { export class UnoptimizedInMemoryShuttleRepository extends EventEmitter implements ShuttleGetterSetterRepository { + + constructor( + readonly shuttleStopArrivalDegreeDelta: number = 0.001, + readonly shuttleStopNearbyDegreeDelta: number = 0.003, + ) { + super() + } + public override on( event: T, listener: ShuttleRepositoryEventListener, @@ -252,8 +260,8 @@ export class UnoptimizedInMemoryShuttleRepository public async getArrivedStopIfExists( shuttle: IShuttle, - delta = 0.001, ): Promise { + const delta = this.shuttleStopArrivalDegreeDelta; const orderedStops = await this.getOrderedStopsByRouteId(shuttle.routeId); for (const orderedStop of orderedStops) {