import { RepositoryLoader } from "./RepositoryLoader"; // 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; constructor( public loader: RepositoryLoader, public readonly timeoutMs: number = 10000, ) { this.startFetchDataAndUpdate = this.startFetchDataAndUpdate.bind(this); } public async start() { if (this.shouldBeRunning) { console.warn("DataLoader timer is already running"); return; } this.shouldBeRunning = true; await this.startFetchDataAndUpdate(); } public stop() { this.shouldBeRunning = false; } private async startFetchDataAndUpdate() { if (!this.shouldBeRunning) return; try { await this.loader.fetchAndUpdateAll(); } catch (e) { console.error(e); } this.timer = setTimeout(this.startFetchDataAndUpdate, this.timeoutMs); } }