diff --git a/src/repositories/shuttle/RedisShuttleRepository.ts b/src/repositories/shuttle/RedisShuttleRepository.ts index 91c1f62..86a8d5d 100644 --- a/src/repositories/shuttle/RedisShuttleRepository.ts +++ b/src/repositories/shuttle/RedisShuttleRepository.ts @@ -398,13 +398,24 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt shuttle: IShuttle, travelTimeTimestamp = Date.now(), ) { + const isAtStop = await this.checkIfShuttleIsAtStop(shuttle.id); const arrivedStop = await this.getArrivedStopIfExists(shuttle); + // Will not fire *any* events if the same stop + const lastStop = await this.getShuttleLastStopArrival(shuttle.id); + if (lastStop?.stopId === arrivedStop?.id) return; + + if (isAtStop) { + if (lastStop) { + this.emit(ShuttleRepositoryEvent.SHUTTLE_WILL_LEAVE_STOP, { + stopArrivalThatShuttleIsLeaving: lastStop, + }); + } + await this.markShuttleAsNotAtStop(shuttle.id); + } + if (arrivedStop) { // stop if same stop - const lastStop = await this.getShuttleLastStopArrival(shuttle.id); - if (lastStop?.stopId === arrivedStop.id) return; - const shuttleArrival = { stopId: arrivedStop.id, timestamp: new Date(travelTimeTimestamp), diff --git a/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts b/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts index 8203170..ab34df6 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"; /** @@ -74,6 +74,7 @@ export class UnoptimizedInMemoryShuttleRepository private orderedStops: IOrderedStop[] = []; private shuttleLastStopArrivals: Map = new Map(); private travelTimeData: Map> = new Map(); + private shuttlesAtStop: Set = new Set(); public async getStops(): Promise { return this.stops; @@ -174,13 +175,24 @@ export class UnoptimizedInMemoryShuttleRepository shuttle: IShuttle, travelTimeTimestamp = Date.now(), ) { + const isAtStop = await this.checkIfShuttleIsAtStop(shuttle.id); const arrivedStop = await this.getArrivedStopIfExists(shuttle); - if (arrivedStop != undefined) { - // stop if same stop - const lastStop = await this.getShuttleLastStopArrival(shuttle.id); - if (lastStop?.stopId === arrivedStop.id) return; + // Will not fire *any* events if the same stop + const lastStop = await this.getShuttleLastStopArrival(shuttle.id); + if (lastStop?.stopId === arrivedStop?.id) return; + if (isAtStop) { + if (lastStop) { + this.emit(ShuttleRepositoryEvent.SHUTTLE_WILL_LEAVE_STOP, { + stopArrivalThatShuttleIsLeaving: lastStop, + }); + } + await this.markShuttleAsNotAtStop(shuttle.id); + } + + if (arrivedStop) { + // stop if same stop const shuttleArrival = { stopId: arrivedStop.id, timestamp: new Date(travelTimeTimestamp), @@ -190,10 +202,23 @@ export class UnoptimizedInMemoryShuttleRepository lastStopArrival: lastStop, willArriveAt: shuttleArrival, }); + await this.markShuttleAsAtStop(shuttleArrival.shuttleId); await this.updateShuttleLastStopArrival(shuttleArrival); } } + private async markShuttleAsAtStop(shuttleId: string) { + this.shuttlesAtStop.add(shuttleId); + } + + private async markShuttleAsNotAtStop(shuttleId: string) { + this.shuttlesAtStop.delete(shuttleId); + } + + private async checkIfShuttleIsAtStop(shuttleId: string) { + return this.shuttlesAtStop.has(shuttleId); + } + private async updateShuttleLastStopArrival(lastStopArrival: ShuttleStopArrival) { this.shuttleLastStopArrivals.set(lastStopArrival.shuttleId, lastStopArrival); @@ -267,6 +292,7 @@ export class UnoptimizedInMemoryShuttleRepository const shuttle = await this.removeEntityByIdIfExists(shuttleId, this.shuttles); if (shuttle != null) { this.emit(ShuttleRepositoryEvent.SHUTTLE_REMOVED, shuttle); + this.shuttlesAtStop.delete(shuttleId); await this.removeShuttleLastStopIfExists(shuttleId); } return shuttle; @@ -289,6 +315,7 @@ export class UnoptimizedInMemoryShuttleRepository public async clearShuttleData(): Promise { this.shuttles = []; + this.shuttlesAtStop.clear(); await this.clearShuttleLastStopData(); }