update timed loader to use class composition

This commit is contained in:
2025-04-11 16:52:27 -07:00
parent d1a47baea6
commit a0e0c19ca3
3 changed files with 35 additions and 38 deletions

View File

@@ -1,6 +1,5 @@
import { ShuttleRepositoryLoader } from "../loaders/ShuttleRepositoryLoader";
import { ETANotificationScheduler } from "../notifications/schedulers/ETANotificationScheduler";
import { TimedApiBasedShuttleRepositoryLoader } from "../loaders/TimedApiBasedShuttleRepositoryLoader";
import { TimedApiBasedRepositoryLoader } from "../loaders/TimedApiBasedRepositoryLoader";
import { UnoptimizedInMemoryShuttleRepository } from "../repositories/UnoptimizedInMemoryShuttleRepository";
import { RedisNotificationRepository } from "../repositories/RedisNotificationRepository";
import { NotificationRepository } from "../repositories/NotificationRepository";
@@ -27,7 +26,7 @@ export class InterchangeSystem {
constructor(
public name: string,
public id: string,
public shuttleDataLoader: ShuttleRepositoryLoader,
public shuttleTimedDataLoader: TimedApiBasedRepositoryLoader,
public shuttleRepository: ShuttleGetterSetterRepository,
public notificationScheduler: ETANotificationScheduler,
public notificationRepository: NotificationRepository,
@@ -43,12 +42,15 @@ export class InterchangeSystem {
args: InterchangeSystemBuilderArguments,
) {
const shuttleRepository = new UnoptimizedInMemoryShuttleRepository();
const shuttleDataLoader = new TimedApiBasedShuttleRepositoryLoader(
const shuttleDataLoader = new ApiBasedShuttleRepositoryLoader(
args.passioSystemId,
args.id,
shuttleRepository
);
await shuttleDataLoader.start();
const timedShuttleDataLoader = new TimedApiBasedRepositoryLoader(
shuttleDataLoader,
);
await timedShuttleDataLoader.start();
const notificationRepository = new RedisNotificationRepository();
await notificationRepository.connect();
@@ -62,7 +64,7 @@ export class InterchangeSystem {
return new InterchangeSystem(
args.name,
args.id,
shuttleDataLoader,
timedShuttleDataLoader,
shuttleRepository,
notificationScheduler,
notificationRepository,
@@ -84,6 +86,11 @@ export class InterchangeSystem {
args.id,
shuttleRepository
);
// Note that this loader should not be started,
// so the test data doesn't get overwritten
const timedShuttleLoader = new TimedApiBasedRepositoryLoader(
shuttleDataLoader,
);
const notificationRepository = new InMemoryNotificationRepository();
const notificationScheduler = new ETANotificationScheduler(
@@ -96,7 +103,7 @@ export class InterchangeSystem {
return new InterchangeSystem(
args.name,
args.id,
shuttleDataLoader,
timedShuttleLoader,
shuttleRepository,
notificationScheduler,
notificationRepository,

View File

@@ -1,5 +1,4 @@
import { ShuttleGetterSetterRepository } from "../repositories/ShuttleGetterSetterRepository";
import { ApiBasedShuttleRepositoryLoader } from "./ApiBasedShuttleRepositoryLoader";
import { RepositoryLoader } from "./RepositoryLoader";
// Ideas to break this into smaller pieces in the future:
// Have one repository data loader running for each supported system
@@ -15,18 +14,15 @@ import { ApiBasedShuttleRepositoryLoader } from "./ApiBasedShuttleRepositoryLoad
// - OrderedStops: reload every few minutes
// - Systems: reload once a day
export class TimedApiBasedShuttleRepositoryLoader extends ApiBasedShuttleRepositoryLoader {
export class TimedApiBasedRepositoryLoader {
private shouldBeRunning: boolean = false;
private timer: any;
readonly timeout = 10000;
constructor(
public passioSystemId: string,
public systemIdForConstructedData: string,
repository: ShuttleGetterSetterRepository,
public loader: RepositoryLoader,
) {
super(passioSystemId, systemIdForConstructedData, repository);
this.startFetchDataAndUpdate = this.startFetchDataAndUpdate.bind(this);
}
@@ -48,14 +44,7 @@ export class TimedApiBasedShuttleRepositoryLoader extends ApiBasedShuttleReposit
if (!this.shouldBeRunning) return;
try {
await this.fetchAndUpdateRouteDataForSystem();
await this.fetchAndUpdateStopAndPolylineDataForRoutesInSystem();
await this.fetchAndUpdateShuttleDataForSystem();
// Because ETA method doesn't support pruning yet,
// add a call to the clear method here
await this.repository.clearEtaData();
await this.fetchAndUpdateEtaDataForExistingStopsForSystem();
await this.loader.fetchAndUpdateAll();
} catch (e) {
console.error(e);
}