import { createPollState, State, StateCatch, StateSuccess } from './state'; import { AbortablesAbort, createAbortabes } from './abortables'; import { createIntervalHandler } from './interval'; import { validateOptions } from './validateOptions'; import { MAX_ERRORS, MAX_POLLS, POLLING_INTERVAL } from './consts'; export type PollingSuccessContext = StateSuccess; export type PollingErrorContext = StateCatch; export type PollingContext = State; export type ReactionsProps = PollingSuccessContext | PollingErrorContext | PollingContext; export 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; }; export type PureOptions = { maxErrors?: number; maxPolls?: number; interval?: ((props: PollingContext) => number) | number; until?: (props: PollingSuccessContext) => boolean; breakIf?: (props: PollingSuccessContext) => boolean; breakIfError?: (props: PollingErrorContext) => boolean; }; export type Options = PureOptions & Reactions; export type Fetcher = ({ signal }: { signal: AbortSignal }) => Promise; export function createPolling(fetcher: Fetcher, options?: Options) { const { maxErrors = MAX_ERRORS, maxPolls = MAX_POLLS, interval = POLLING_INTERVAL, until = () => true, breakIf = () => false, breakIfError = () => false, onStart = () => {}, onBreak = () => {}, onComplete = () => {}, onNext = () => {}, onError = () => {}, onErrorBreak = () => {}, onTooManyAttempts = () => {}, onTooManyErrors = () => {}, onIntervalError = () => {}, onFinish = () => {}, } = validateOptions(options); const state = createPollState(); const abortables = createAbortabes(fetcher); const intervalHandler = createIntervalHandler(interval); const isTooMany = { attempts: () => state.get.attempt() >= maxPolls, errors: () => state.get.errorsCount() >= maxErrors, }; const poll = async () => { state.on.start(); onStart(); while (true) { if (abortables.isAborted()) { return state.get.all(); } try { const data = await abortables.fetcher(); state.on.newData(data); if (breakIf(state.get.success())) { onBreak(state.get.success()); break; } if (until(state.get.success())) { onComplete(state.get.success()); break; } if (isTooMany.attempts()) { onTooManyAttempts(state.get.success()); break; } const { error: intervalError, newInterval } = intervalHandler.get(state.get.all()); if (intervalError) { state.on.intervalCatch(intervalError); onIntervalError(state.get.all()); break; } onNext(state.get.success()); await abortables.wait(newInterval); } catch (e) { if (e.name === 'AbortError') { return state.get.all(); } state.on.newCatch(e); onError(state.get.catch()); if (breakIfError(state.get.catch())) { onErrorBreak(state.get.catch()); break; } if (isTooMany.errors()) { onTooManyErrors(state.get.catch()); break; } const { error: intervalError, newInterval } = intervalHandler.get(state.get.all()); if (intervalError) { state.on.intervalCatch(intervalError); onIntervalError(state.get.all()); break; } await abortables.wait(newInterval); } } onFinish(state.get.all()); return state.get.all(); }; const { init, abort } = createPollingHandlers(poll, abortables.abort); return { init, abort }; } function createPollingHandlers(poll: () => Promise>, abort: AbortablesAbort) { // this promise is going to be resolved only once and reused when handlers are called multiple times let pollPromise: Promise> | null = null; const initPolling = async () => { if (pollPromise) return pollPromise; pollPromise = poll(); return pollPromise; }; const abortPolling = async () => { if (pollPromise) { abort(); } return pollPromise; }; return { init: initPolling, abort: abortPolling }; }