mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
make timeoutMs a settable property
This commit is contained in:
@@ -1,27 +1,16 @@
|
|||||||
import { RepositoryLoader } from "./RepositoryLoader";
|
import { RepositoryLoader } from "./RepositoryLoader";
|
||||||
|
|
||||||
// Ideas to break this into smaller pieces in the future:
|
// To break down timed loading in the future:
|
||||||
// Have one repository data loader running for each supported system
|
// Add flags to the repository indicating which data users are subscribed to
|
||||||
// Each data loader independently updates data based on frequency of usage
|
// In the loader's `fetchAll` method, check flags and update only needed data
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
export class TimedApiBasedRepositoryLoader {
|
export class TimedApiBasedRepositoryLoader {
|
||||||
private shouldBeRunning: boolean = false;
|
private shouldBeRunning: boolean = false;
|
||||||
private timer: any;
|
private timer: any;
|
||||||
|
|
||||||
readonly timeout = 10000;
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public loader: RepositoryLoader,
|
public loader: RepositoryLoader,
|
||||||
|
public timeoutMs: number = 10000,
|
||||||
) {
|
) {
|
||||||
this.startFetchDataAndUpdate = this.startFetchDataAndUpdate.bind(this);
|
this.startFetchDataAndUpdate = this.startFetchDataAndUpdate.bind(this);
|
||||||
}
|
}
|
||||||
@@ -49,6 +38,6 @@ export class TimedApiBasedRepositoryLoader {
|
|||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.timer = setTimeout(this.startFetchDataAndUpdate, this.timeout);
|
this.timer = setTimeout(this.startFetchDataAndUpdate, this.timeoutMs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ describe("TimedApiBasedRepositoryLoader", () => {
|
|||||||
expect(spy).toHaveBeenCalled();
|
expect(spy).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), timedLoader.timeout);
|
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), timedLoader.timeoutMs);
|
||||||
expect(timedLoader.timeout).not.toBeUndefined();
|
expect(timedLoader.timeoutMs).not.toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does nothing if timer is already running", async () => {
|
it("does nothing if timer is already running", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user