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