const defaultMaxRetries = 3 ; const defaultRetryDelay = ( attemptIndex ) => Math . min ( 1000 * 2 ** attemptIndex , 30000 ) ; export type RetryOptions = { default maxRetries ? : number ; default retryDelay ? : number | ( ( attemptIndex : number ) => number ) ; } | boolean | number ; export interface RetryConfigState { default private attemptIndex ? : number ; /**
     * Function to be executed on approved retry attempt
     */ onRetry : ( attemptIndex : number ) => Promise < void > ; /**
     * Function to be executed on the last try
     */ onLastTry ? : ( attemptIndex : number ) => Promise < void > ; } export function doRetry ( options , state ) { const maxRetries = typeof options === 'number' ? options : ( typeof options === 'object' ? options . maxRetries : undefined ) ?? defaultMaxRetries ; const retryDelay = ( typeof options === 'object' ? options . retryDelay : undefined ) ?? defaultRetryDelay ; const { attemptIndex = 0 , onRetry , onLastTry } = state ; if ( onLastTry && attemptIndex === maxRetries - 1 ) { setTimeout ( ( ) => { onLastTry ( attemptIndex ) . catch ( console . error ) ; } , typeof retryDelay === 'function' ? retryDelay ( attemptIndex ) : retryDelay ) ; } else if ( attemptIndex < maxRetries ) { setTimeout ( ( ) => { onRetry ( attemptIndex ) . catch ( ( ) => { doRetry ( options , Object . assign ( { } , state , { attemptIndex : attemptIndex + 1 } ) ) ; } ) ; } , typeof retryDelay === 'function' ? retryDelay ( attemptIndex ) : retryDelay ) ; } }