mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-16 23:40:32 +00:00
Add logic to fire the SHUTTLE_WILL_LEAVE_STOP event
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -74,6 +74,7 @@ export class UnoptimizedInMemoryShuttleRepository
|
||||
private orderedStops: IOrderedStop[] = [];
|
||||
private shuttleLastStopArrivals: Map<string, ShuttleStopArrival> = new Map();
|
||||
private travelTimeData: Map<string, Array<{ timestamp: number; seconds: number }>> = new Map();
|
||||
private shuttlesAtStop: Set<string> = new Set();
|
||||
|
||||
public async getStops(): Promise<IStop[]> {
|
||||
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
|
||||
// Will not fire *any* events if the same stop
|
||||
const lastStop = await this.getShuttleLastStopArrival(shuttle.id);
|
||||
if (lastStop?.stopId === arrivedStop.id) return;
|
||||
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<void> {
|
||||
this.shuttles = [];
|
||||
this.shuttlesAtStop.clear();
|
||||
await this.clearShuttleLastStopData();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user