mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Apply the same events to UnoptimizedInMemoryShuttleRepository
This commit is contained in:
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user