Change the event name to SHUTTLE_WILL_ARRIVE_AT_STOP, and emit it before the stop is updated

This commit is contained in:
2025-11-11 19:10:13 -08:00
parent b9fefcc6a9
commit 9af50ddfe9
5 changed files with 12 additions and 11 deletions

View File

@@ -401,8 +401,8 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
timestamp: new Date(travelTimeTimestamp), timestamp: new Date(travelTimeTimestamp),
shuttleId: shuttle.id, shuttleId: shuttle.id,
}; };
this.emit(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, shuttleArrival);
await this.updateShuttleLastStopArrival(shuttleArrival); await this.updateShuttleLastStopArrival(shuttleArrival);
this.emit(ShuttleRepositoryEvent.SHUTTLE_ARRIVED_AT_STOP, shuttleArrival);
} }
} }

View File

@@ -4,7 +4,7 @@ import type EventEmitter from "node:events";
export const ShuttleRepositoryEvent = { export const ShuttleRepositoryEvent = {
SHUTTLE_UPDATED: "shuttleUpdated", SHUTTLE_UPDATED: "shuttleUpdated",
SHUTTLE_REMOVED: "shuttleRemoved", SHUTTLE_REMOVED: "shuttleRemoved",
SHUTTLE_ARRIVED_AT_STOP: "shuttleArrivedAtStop", SHUTTLE_WILL_ARRIVE_AT_STOP: "shuttleArrivedAtStop",
} as const; } as const;
export type ShuttleRepositoryEventName = typeof ShuttleRepositoryEvent[keyof typeof ShuttleRepositoryEvent]; export type ShuttleRepositoryEventName = typeof ShuttleRepositoryEvent[keyof typeof ShuttleRepositoryEvent];
@@ -15,7 +15,7 @@ export type EtaDataClearedEventPayload = IEta[];
export interface ShuttleRepositoryEventPayloads { export interface ShuttleRepositoryEventPayloads {
[ShuttleRepositoryEvent.SHUTTLE_UPDATED]: IShuttle, [ShuttleRepositoryEvent.SHUTTLE_UPDATED]: IShuttle,
[ShuttleRepositoryEvent.SHUTTLE_REMOVED]: IShuttle, [ShuttleRepositoryEvent.SHUTTLE_REMOVED]: IShuttle,
[ShuttleRepositoryEvent.SHUTTLE_ARRIVED_AT_STOP]: ShuttleStopArrival, [ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP]: ShuttleStopArrival,
} }
export type ShuttleRepositoryEventListener<T extends ShuttleRepositoryEventName> = ( export type ShuttleRepositoryEventListener<T extends ShuttleRepositoryEventName> = (

View File

@@ -182,8 +182,8 @@ export class UnoptimizedInMemoryShuttleRepository
timestamp: new Date(travelTimeTimestamp), timestamp: new Date(travelTimeTimestamp),
shuttleId: shuttle.id, shuttleId: shuttle.id,
}; };
this.emit(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, shuttleArrival);
await this.updateShuttleLastStopArrival(shuttleArrival); await this.updateShuttleLastStopArrival(shuttleArrival);
this.emit(ShuttleRepositoryEvent.SHUTTLE_ARRIVED_AT_STOP, shuttleArrival);
} }
} }

View File

@@ -712,12 +712,12 @@ describe.each(repositoryImplementations)('$name', (holder) => {
}); });
}); });
describe("SHUTTLE_ARRIVED_AT_STOP event", () => { describe("SHUTTLE_WILL_ARRIVE_AT_STOP event", () => {
test("emits SHUTTLE_ARRIVED_AT_STOP event when shuttle arrives at a stop", async () => { test("emits SHUTTLE_WILL_ARRIVE_AT_STOP event before shuttle arrives at a stop", async () => {
const { route, systemId, stop1 } = await setupRouteAndOrderedStops(); const { route, systemId, stop1 } = await setupRouteAndOrderedStops();
const listener = jest.fn(); const listener = jest.fn();
repository.on(ShuttleRepositoryEvent.SHUTTLE_ARRIVED_AT_STOP, listener); repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener);
const shuttle = { const shuttle = {
id: "sh1", id: "sh1",
@@ -743,7 +743,7 @@ describe.each(repositoryImplementations)('$name', (holder) => {
const { route, systemId } = await setupRouteAndOrderedStops(); const { route, systemId } = await setupRouteAndOrderedStops();
const listener = jest.fn(); const listener = jest.fn();
repository.on(ShuttleRepositoryEvent.SHUTTLE_ARRIVED_AT_STOP, listener); repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener);
const shuttle = { const shuttle = {
id: "sh1", id: "sh1",
@@ -764,7 +764,7 @@ describe.each(repositoryImplementations)('$name', (holder) => {
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops(); const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops();
const listener = jest.fn(); const listener = jest.fn();
repository.on(ShuttleRepositoryEvent.SHUTTLE_ARRIVED_AT_STOP, listener); repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener);
const shuttle = { const shuttle = {
id: "sh1", id: "sh1",

View File

@@ -26,12 +26,12 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple
startListeningForUpdates() { startListeningForUpdates() {
this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_UPDATED, this.handleShuttleUpdate); this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_UPDATED, this.handleShuttleUpdate);
this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_ARRIVED_AT_STOP, this.handleShuttleArriveAtStop) this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleArriveAtStop)
} }
stopListeningForUpdates() { stopListeningForUpdates() {
this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_UPDATED, this.handleShuttleUpdate); this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_UPDATED, this.handleShuttleUpdate);
this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_ARRIVED_AT_STOP, this.handleShuttleArriveAtStop); this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleArriveAtStop);
} }
private handleShuttleUpdate(shuttle: IShuttle) { private handleShuttleUpdate(shuttle: IShuttle) {
@@ -40,5 +40,6 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple
private handleShuttleArriveAtStop(shuttleArrival: ShuttleStopArrival) { private handleShuttleArriveAtStop(shuttleArrival: ShuttleStopArrival) {
// TODO: handle shuttle arrive at stop // TODO: handle shuttle arrive at stop
} }
} }