Skip to content

Commit

Permalink
I committed too late.
Browse files Browse the repository at this point in the history
  • Loading branch information
kahirokunn committed Aug 22, 2022
1 parent 6d87361 commit acce9c3
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/toolkit/src/query/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,39 @@ async function defaultBackoff(attempt: number = 0, maxRetries: number = 5) {
)
}

export interface RetryOptions {
type _RetryOptions = {
/**
* How many times the query will be retried (default: 5)
* Function used to determine delay between retries
*/
maxRetries?: number
backoff: (attempt: number, maxRetries: number) => Promise<void>

/**
* Function used to determine delay between retries
* How many times the query will be retried (default: 5)
*/
backoff?: (attempt: number, maxRetries: number) => Promise<void>
maxRetries: number

/**
* Callback to determine if a retry should be attempted.
* Return `true` for another retry and `false` to quit trying prematurely.
*/
retryCondition?: (error: FetchBaseQueryError, args: BaseQueryArg<BaseQueryFn>, extraArgs: {
retryCondition: (error: FetchBaseQueryError, args: BaseQueryArg<BaseQueryFn>, extraArgs: {
attempt: number
maxRetries: number
baseQueryApi: BaseQueryApi
extraOptions: BaseQueryExtraOptions<BaseQueryFn> & RetryOptions
}) => boolean
}

export type RetryOptions = {
backoff?: _RetryOptions['backoff']
} & ({
maxRetries?: _RetryOptions['maxRetries']
retryCondition?: never
} | {
maxRetries?: never
retryCondition?: _RetryOptions['retryCondition']
})

function fail(e: any): never {
throw Object.assign(new HandledError({ error: e }), {
throwImmediately: true,
Expand All @@ -58,7 +70,7 @@ const retryWithBackoff: BaseQueryEnhancer<
> = (baseQuery, defaultOptions) => async (args, api, extraOptions) => {
const defaultRetryCondition: Exclude<RetryOptions['retryCondition'], undefined> = (_, __, {attempt, maxRetries}) => attempt <= maxRetries

const options = {
const options: _RetryOptions = {
maxRetries: 5,
backoff: defaultBackoff,
retryCondition: defaultRetryCondition,
Expand Down

0 comments on commit acce9c3

Please sign in to comment.