Apply the same events to UnoptimizedInMemoryShuttleRepository

This commit is contained in:
2025-11-11 18:39:24 -08:00
parent a3bfa4894e
commit 7497c1d004

View File

@@ -3,6 +3,7 @@ import { ShuttleGetterSetterRepository } from "./ShuttleGetterSetterRepository";
import { IOrderedStop, IRoute, IShuttle, IStop, shuttleHasArrivedAtStop } from "../../entities/ShuttleRepositoryEntities"; import { IOrderedStop, IRoute, IShuttle, IStop, shuttleHasArrivedAtStop } from "../../entities/ShuttleRepositoryEntities";
import { IEntityWithId } from "../../entities/SharedEntities"; import { IEntityWithId } from "../../entities/SharedEntities";
import { import {
ShuttleRepositoryEvent,
ShuttleRepositoryEventListener, ShuttleRepositoryEventListener,
ShuttleRepositoryEventName, ShuttleRepositoryEventName,
ShuttleRepositoryEventPayloads, ShuttleRepositoryEventPayloads,
@@ -138,7 +139,6 @@ export class UnoptimizedInMemoryShuttleRepository
public async addOrUpdateShuttle( public async addOrUpdateShuttle(
shuttle: IShuttle, shuttle: IShuttle,
travelTimeTimestamp = Date.now(), travelTimeTimestamp = Date.now(),
referenceCurrentTime = new Date(),
): Promise<void> { ): Promise<void> {
const index = this.shuttles.findIndex((s) => s.id === shuttle.id); const index = this.shuttles.findIndex((s) => s.id === shuttle.id);
if (index !== -1) { if (index !== -1) {
@@ -147,6 +147,8 @@ export class UnoptimizedInMemoryShuttleRepository
this.shuttles.push(shuttle); this.shuttles.push(shuttle);
} }
this.emit(ShuttleRepositoryEvent.SHUTTLE_UPDATED, shuttle);
await this.updateLastStopArrival(shuttle, travelTimeTimestamp); await this.updateLastStopArrival(shuttle, travelTimeTimestamp);
} }
@@ -175,17 +177,19 @@ export class UnoptimizedInMemoryShuttleRepository
const arrivedStop = await this.getArrivedStopIfExists(shuttle); const arrivedStop = await this.getArrivedStopIfExists(shuttle);
if (arrivedStop != undefined) { if (arrivedStop != undefined) {
await this.updateShuttleLastStopArrival(shuttle.id, { const shuttleArrival = {
stopId: arrivedStop.id, stopId: arrivedStop.id,
timestamp: new Date(travelTimeTimestamp), timestamp: new Date(travelTimeTimestamp),
shuttleId: shuttle.id, shuttleId: shuttle.id,
}); };
await this.updateShuttleLastStopArrival(shuttleArrival);
this.emit(ShuttleRepositoryEvent.SHUTTLE_ARRIVED_AT_STOP, shuttleArrival);
} }
} }
private async updateShuttleLastStopArrival(shuttleId: string, lastStopArrival: ShuttleStopArrival) { private async updateShuttleLastStopArrival(lastStopArrival: ShuttleStopArrival) {
this.shuttleLastStopArrivals.set(shuttleId, lastStopArrival); this.shuttleLastStopArrivals.set(lastStopArrival.shuttleId, lastStopArrival);
} }
public async getAverageTravelTimeSeconds( public async getAverageTravelTimeSeconds(
@@ -253,7 +257,11 @@ export class UnoptimizedInMemoryShuttleRepository
} }
public async removeShuttleIfExists(shuttleId: string): Promise<IShuttle | null> { public async removeShuttleIfExists(shuttleId: string): Promise<IShuttle | null> {
return await this.removeEntityByIdIfExists(shuttleId, this.shuttles); const shuttle = await this.removeEntityByIdIfExists(shuttleId, this.shuttles);
if (shuttle != null) {
this.emit(ShuttleRepositoryEvent.SHUTTLE_REMOVED, shuttle);
}
return shuttle;
} }
public async removeStopIfExists(stopId: string): Promise<IStop | null> { public async removeStopIfExists(stopId: string): Promise<IStop | null> {