type Timestamped = { readonly value: T; readonly timestamp: number; }; /** Handles managing time and scheduling functions to run after a delay. */ export interface PollScheduler { schedule: (callback: () => Promise, delay: number) => T; cancelSchedule: (schedule: T) => void; getCurrentTimestamp: () => number; } /** Handles periodically fetching a value so it's always available in advance. */ export declare class Polled { readonly pollInterval: number; readonly retryInterval: number | null; readonly maxRetries: number; readonly requireInitSuccess: boolean; private readonly _fetch; private readonly _scheduler; private readonly _onError; private _initialized; private _data; private _failedAttemptCount; private _runningSchedule; constructor({ fetch, scheduler, pollInterval, retryInterval, maxRetries, onError, requireInitSuccess, }: { /** The function that does the expensive fetch. */ fetch: () => Promise; /** * The poll scheduler, which manages time and allows functions to be run * after a given delay. The timestamp and delay can be measured in any unit * of time, as long as they match the unit used for the poll interval, and * the retry interval. */ scheduler: PollScheduler; /** How often to poll for fresh data. */ pollInterval: number; /** * How often to retry after a failed fetch. Default: null (i.e. don't retry, * just wait for the next poll). */ retryInterval?: number | null; /** * How many times to use the retry interval before reverting to the poll * interval. The retry interval will not be used again until a successful * fetch is made. */ maxRetries?: number; /** Called when a fetch fails (except the first fetch, which throws). */ onError?: ((error: unknown) => void) | null; /** Whether to throw if the fetch during init() fails. Default: true. */ requireInitSuccess?: boolean; }); /** * Fetch the value and start polling. Throws if the first fetch fails, unless * {@link requireInitSuccess} is false. */ init(): Promise; /** Stops polling for the value. Can be started again by calling init(). */ dispose(): void; /** * Get the value. Can be null if {@link requireInitSuccess} is set to false. * Use {@link require} to guarantee a non-null value. */ get(): Timestamped | null; /** * Get the value. Throw if {@link requireInitSuccess} is false and the value * has never been successfully fetched. */ require(): Timestamped; /** * Ignore any polled value and fetch again. Throws if the fetch fails. As * calling this method refreshes the value, the next poll will be rescheduled * to avoid unnecessary fetches. */ fetch(): Promise>; private _poll; private _fetchAndStore; private _schedulePoll; private _assertInitialized; } export {};