import { Observable , timer , Subscriber , TeardownLogic , Operator , SchedulerLike , asyncScheduler , Subscription , } from 'rxjs' ; import { retryWhen , switchMap , mergeMap , tap } from 'rxjs/operators' ; param maxTries param initialWait export const retryExponentialBackoff = ( maxTries = - 1 , initialWait = 1 , scheduler = asyncScheduler ) => < T > ( source ) => { return new Observable < T > ( subscriber => { let count = 1 ; const subscription = new Subscription ( ) ; const subscribe = ( ) => subscription . add ( source . subscribe ( { next ( value ) { count = 1 ; subscriber . next ( value ) ; } , error ( err ) { if ( count <= maxTries ) { subscription . add ( scheduler . schedule ( subscribe , initialWait * Math . pow ( 2 , count ++ ) ) ) ; } } , complete ( ) { subscriber . complete ( ) ; } , } ) ) ; subscribe ( ) ; return subscription ; } ) ; } ; export const retryWhenExponentialbackoff = ( maxTries = - 1 , initialWait = 1 ) => < T > ( source ) => { return source . pipe ( ( innerSource ) => { let count = 1 ; return innerSource . pipe ( retryWhen ( ( errors ) => { return errors . pipe ( switchMap ( error => { return timer ( initialWait * Math . pow ( count ++ , 2 ) ) ; } ) ) ; } ) , tap ( ( ) => count = 1 ) ) ; } ) ; } ;