declare function createPollState(): { get: { attempt: () => number; errorsCount: () => number; common: () => { attempt: number; attemptsDuration: number[]; errorsCount: number; duration: number; }; success: () => { data: T; attempt: number; attemptsDuration: number[]; errorsCount: number; duration: number; }; catch: () => { error: any; attempt: number; attemptsDuration: number[]; errorsCount: number; duration: number; }; all: () => { data: T; error: any; attempt: number; attemptsDuration: number[]; errorsCount: number; duration: number; }; }; on: { start: () => void; newAttempt: () => void; newData: (d: T) => void; newCatch: (e: any) => void; intervalCatch: (e: any) => void; }; }; type State = ReturnType>['get']['all']>; type StateSuccess = ReturnType>['get']['success']>; type StateCatch = ReturnType>['get']['catch']>; type PollingSuccessContext = StateSuccess; type PollingErrorContext = StateCatch; type PollingContext = State; type ReactionsProps = PollingSuccessContext | PollingErrorContext | PollingContext; type Reactions = { onStart?: () => void; onFinish?: (props: PollingContext) => void; onComplete?: (props: PollingSuccessContext) => void; onBreak?: (props: PollingSuccessContext) => void; onNext?: (props: PollingSuccessContext) => void; onTooManyAttempts?: (props: PollingSuccessContext) => void; onError?: (props: PollingErrorContext) => void; onErrorBreak?: (props: PollingErrorContext) => void; onTooManyErrors?: (props: PollingErrorContext) => void; onIntervalError?: (props: PollingContext) => void; }; type PureOptions = { maxErrors?: number; maxPolls?: number; interval?: ((props: PollingContext) => number) | number; until?: (props: PollingSuccessContext) => boolean; breakIf?: (props: PollingSuccessContext) => boolean; breakIfError?: (props: PollingErrorContext) => boolean; }; type Options = PureOptions & Reactions; type Fetcher = ({ signal }: { signal: AbortSignal; }) => Promise; /** * @description * this function allows you to do polling with retries and errors count. * It is useful when you want to keep going with the request until you get the result you want or until you reach the max retries or max errors count. * @example * const { init, abort } = await doPolling(fetchStuff, * { * // max retries count. If maxPolls is reached, onTooManyAttempts will be called * maxPolls: 10, // default is Infinity * // max errors count. If maxErrors is reached, onTooManyErrors will be called * maxErrors: 5, // default is 5 * // interval between retries. Can be a number or function that is called on every poll * interval: 1000, // default is 2000 * // polling will be stopped if condition is true. If condition is not provided, polling will be stopped after one successful request * until: ({ data }) => data.status === 'SUCCESS', // default is () => true * // onStart will be called before polling * onStart: () => {}, * // onNext will be called after each successful poll, except the last one * onNext: ({ data, attempt, errorsCount }) => {}, * // onComplete will be called after polling is completed * onComplete: ({ data, attempt, errorsCount }) => {}, * // onFinish will be called after polling is finished with whichever result * onFinish: ({ data, attempt, errorsCount }) => {}, * // onError will be called after each failed poll * onError: ({ retry, errorsCount, error }) => {}, * // onTooManyAttempts will be called if maxPolls is reached. * onTooManyAttempts: () => {}, * // onTooManyErrors will be called if maxErrors is reached. * onTooManyErrors: ({ retry, errorsCount, error }) => {}, * // onIntervalError will be called if the interval function throws an error * onIntervalError({ data, error, attempt, attemptsDuration, errorsCount, duration }) => {} * // polling will be stopped if breakIf is true. This is useful when you want to stop polling if you know that you will never get the result you want. * breakIf: ({ data }) => data.received !== total, * // onBreak will be called if breakIf is true * onBreak: ({ data, attempt, errorsCount }) => {}, * // breakIfError acts the same as breakIf, but for errors. It is useful when you want to stop polling if you receive a specific error type. * breakIfError: ({ error }) => error.code === 404, * // onBreakError will be called if breakIfError is true * onBreakError: ({ error, attempt, errorsCount }) => {}, * } * ); * * // now simple call init to start polling * init().then(({ error, data }: {data: T}) => { * // do something with the result * }); * * // you can also abort the polling from the outside * addEventListener('click', abort); * @throws if maxErrors is less than 0 * @throws if interval is less than 0 or not a function * @throws if passed callbacks are not functions */ declare function doPolling(fetcher: Fetcher, options?: Options): { init: () => Promise>; abort: () => Promise>; }; /** * @description * this function allows you to subscribe to the polling with retries and errors count. * It is useful when you want to keep going with the request until you get the result you want or until you reach the max retries or max errors count. * @example * // someService.ts * import { subscribePolling } from '@ilyagershman/easy-poll'; * * export const { subscribe, init, abort } = subscribePolling(fetchStuff, { * // max retries count. If maxPolls is reached, onTooManyAttempts will be called * maxPolls: 10, * // max errors count. If maxErrors is reached, onTooManyErrors will be called * maxErrors: 5, * // interval between retries. Can be a number or function that is called on every poll * interval: () => getRandomInt(10000), * // polling will be stopped if the condition is true. If the condition is not provided, * // polling will be stopped after one successful request * until: ({ data }) => data.status === 'SUCCESS', * // polling will be stopped if breakIf is true. * // This is useful when you want to stop polling if you know that you will never get the result you want. * breakIf: ({ data }) => data.received !== total, * // breakIfError acts the same as breakIf, but for errors. It is useful when you want to stop polling if you receive a specific error type. * breakIfError: ({ error }) => error.code === 404, * }); * * /// somewhere.ts in your code react on the polling events. * import { subscribe, init } from './somewhere'; * import { EVENTS } from '@ilyagershman/easy-poll'; * * init().then({ data, error } => { * // same as doPolling * })); * * subscribe(props => { * if (props.event === EVENTS.ON_COMPLETE) onComplete(props); * if (props.event === EVENTS.ON_BREAK) onBreak(props); * if (props.event === EVENTS.ON_NEXT) onNext(props); * if (props.event === EVENTS.ON_ERROR) onError(props); * if (props.event === EVENTS.ON_FINISH) onFinish(props); * if (props.event === EVENTS.ON_TOOMANYERRORS) onTooManyErrors(props); * if (props.event === EVENTS.ON_TOOMANYATTEMPTS) onTooManyAttempts(props); * // ... * }); * * /// somewhereElse.ts in your code react on the polling events. * import { abort } from './somewhere'; * * // this will abort the polling after some time * setTimeout(abort, 4200); */ declare function subscribePolling(fetcher: Fetcher, pureOptions?: PureOptions): { subscribe: (callback: (props: { event: keyof typeof EVENTS; props?: ReactionsProps; }) => void, key?: string) => { unsubscribe: () => void; }; init: () => Promise>; abort: () => Promise>; }; declare const EVENTS: { readonly ON_START: "ON_START"; readonly ON_BREAK: "ON_BREAK"; readonly ON_FINISH: "ON_FINISH"; readonly ON_COMPLETE: "ON_COMPLETE"; readonly ON_NEXT: "ON_NEXT"; readonly ON_TOOMANYATTEMPTS: "ON_TOOMANYATTEMPTS"; readonly ON_ERROR: "ON_ERROR"; readonly ON_ERRORBREAK: "ON_ERRORBREAK"; readonly ON_TOOMANYERRORS: "ON_TOOMANYERRORS"; readonly ON_INTERVALERROR: "ON_INTERVALERROR"; }; export { EVENTS, doPolling, subscribePolling };