import { Observable , of , throwError } from 'rxjs' ; import { delay , mergeMap , retryWhen } from 'rxjs/operators' ; const getErrorMessage = ( maxRetry ) => `Tried to load Resource over XHR for ${ maxRetry } times without success. Giving up.` ; const DEFAULT_MAX_RETRIES = 5 ; const DEFAULT_BACKOFF = 1000 ; export function retryWithBackoff < T > ( delayMs , maxRetry = DEFAULT_BACKOFF , backoffMs = DEFAULT_BACKOFF ) { let retries = maxRetry ; return ( src$ ) : Observable < T > => src$ . pipe ( retryWhen ( ( errors ) => errors . pipe ( mergeMap ( ( error ) => { if ( retries -- > 0 ) { const backoffTime = delayMs + ( maxRetry - retries ) * backoffMs ; return of ( error ) . pipe ( delay ( backoffTime ) ) ; } return throwError ( getErrorMessage ( maxRetry ) ) ; } ) ) ) ) ; }