From 44095e711a09c0882580b8360252d6317c11ac98 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Fri, 11 Apr 2025 16:57:47 -0700 Subject: [PATCH] make timeoutMs a settable property --- src/loaders/TimedApiBasedRepositoryLoader.ts | 21 +++++-------------- ...TimedApiBasedRepositoryLoaderTests.test.ts | 4 ++-- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/loaders/TimedApiBasedRepositoryLoader.ts b/src/loaders/TimedApiBasedRepositoryLoader.ts index 5add5a7..63d869f 100644 --- a/src/loaders/TimedApiBasedRepositoryLoader.ts +++ b/src/loaders/TimedApiBasedRepositoryLoader.ts @@ -1,27 +1,16 @@ import { RepositoryLoader } from "./RepositoryLoader"; -// Ideas to break this into smaller pieces in the future: -// Have one repository data loader running for each supported system -// Each data loader independently updates data based on frequency of usage - -// Notes on this: we only need to reload ETA data frequently -// Other data can be reloaded periodically -// Detailed list: -// - ETA: reload frequently or switch to write-through approach -// - Shuttles: reload every minute -// - Routes: reload every few minutes -// - Stops: reload every few minutes -// - OrderedStops: reload every few minutes -// - Systems: reload once a day +// To break down timed loading in the future: +// Add flags to the repository indicating which data users are subscribed to +// In the loader's `fetchAll` method, check flags and update only needed data export class TimedApiBasedRepositoryLoader { private shouldBeRunning: boolean = false; private timer: any; - readonly timeout = 10000; - constructor( public loader: RepositoryLoader, + public timeoutMs: number = 10000, ) { this.startFetchDataAndUpdate = this.startFetchDataAndUpdate.bind(this); } @@ -49,6 +38,6 @@ export class TimedApiBasedRepositoryLoader { console.error(e); } - this.timer = setTimeout(this.startFetchDataAndUpdate, this.timeout); + this.timer = setTimeout(this.startFetchDataAndUpdate, this.timeoutMs); } } diff --git a/test/loaders/TimedApiBasedRepositoryLoaderTests.test.ts b/test/loaders/TimedApiBasedRepositoryLoaderTests.test.ts index accafd2..e62f6ad 100644 --- a/test/loaders/TimedApiBasedRepositoryLoaderTests.test.ts +++ b/test/loaders/TimedApiBasedRepositoryLoaderTests.test.ts @@ -48,8 +48,8 @@ describe("TimedApiBasedRepositoryLoader", () => { expect(spy).toHaveBeenCalled(); }); - expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), timedLoader.timeout); - expect(timedLoader.timeout).not.toBeUndefined(); + expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), timedLoader.timeoutMs); + expect(timedLoader.timeoutMs).not.toBeUndefined(); }); it("does nothing if timer is already running", async () => {