Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 8x 8x 8x 8x 8x 8x 58x 58x 54x 54x 54x 34x 34x 34x 4x 4x 20x 20x 4x 4x | export type Options<TFetcherReturn> = { fetcher: () => TFetcherReturn; onPoll: (data: Awaited<TFetcherReturn>) => boolean; interval: number; pollAttempts: number; }; export class DataPoller<TFetcherReturn> { private options: Options<TFetcherReturn>; constructor(options: Options<TFetcherReturn>) { this.options = options; } async startPollingData() { return new Promise<{ maxAttemptsReached: boolean; correctDataStateFound: boolean; error?: string; }>((resolve, reject) => { let timesPolled = 0; let errorMessage: string | null = null; const intervalId = setInterval(async () => { timesPolled++; if (timesPolled <= this.options.pollAttempts) { errorMessage = null; try { const data = await this.options.fetcher(); Eif (data) { const stopPoll = this.options.onPoll(data); if (stopPoll) { resolve({ correctDataStateFound: true, maxAttemptsReached: false, }); clearInterval(intervalId); } } } catch (error) { const message = error instanceof Error ? (error as Error).message : error; errorMessage = `Error fetching data: ${message}`; } } else { reject({ correctDataStateFound: false, maxAttemptsReached: true, message: errorMessage || "Error polling data: Correct data state not found, after max attempts reached", }); clearInterval(intervalId); } }, this.options.interval); }); } } |