Code coverage report for src/operator/retryWhen.ts

Statements: 100% (62 / 62)      Branches: 90% (9 / 10)      Functions: 100% (9 / 9)      Lines: 100% (59 / 59)      Ignored: none     

All files » src/operator/ » retryWhen.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113      1   1 1   1 1                               1 21     1 21 21     1 21   1   1           1 21 21 21     1 24   24 24 24   24 14 14 14 1   13   10 10     23 23   23 23 23   23       1 55 55 11 11   55 11 11   55     1   14 14 14 14   14 14 14   14 14 14   14   1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
import {Subject} from '../Subject';
import {Subscription} from '../Subscription';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
 
/**
 * Returns an Observable that emits the same values as the source observable with the exception of an `error`.
 * An `error` will cause the emission of the Throwable that cause the error to the Observable returned from
 * notificationHandler. If that Observable calls onComplete or `error` then retry will call `complete` or `error`
 * on the child subscription. Otherwise, this Observable will resubscribe to the source observable, on a particular
 * Scheduler.
 *
 * <img src="./img/retryWhen.png" width="100%">
 *
 * @param {notificationHandler} receives an Observable of notifications with which a user can `complete` or `error`,
 * aborting the retry.
 * @param {scheduler} the Scheduler on which to subscribe to the source Observable.
 * @returns {Observable} the source Observable modified with retry logic.
 */
export function retryWhen<T>(notifier: (errors: Observable<any>) => Observable<any>): Observable<T> {
  return this.lift(new RetryWhenOperator(notifier, this));
}
 
class RetryWhenOperator<T> implements Operator<T, T> {
  constructor(protected notifier: (errors: Observable<any>) => Observable<any>,
              protected source: Observable<T>) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new RetryWhenSubscriber(subscriber, this.notifier, this.source);
  }
}
 
class RetryWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
 
  private errors: Subject<any>;
  private retries: Observable<any>;
  private retriesSubscription: Subscription;
 
  constructor(destination: Subscriber<R>,
              private notifier: (errors: Observable<any>) => Observable<any>,
              private source: Observable<T>) {
    super(destination);
  }
 
  error(err: any) {
    Eif (!this.isStopped) {
 
      let errors = this.errors;
      let retries: any = this.retries;
      let retriesSubscription = this.retriesSubscription;
 
      if (!retries) {
        errors = new Subject();
        retries = tryCatch(this.notifier)(errors);
        if (retries === errorObject) {
          return super.error(errorObject.e);
        }
        retriesSubscription = subscribeToResult(this, retries);
      } else {
        this.errors = null;
        this.retriesSubscription = null;
      }
 
      this.unsubscribe();
      this.isUnsubscribed = false;
 
      this.errors = errors;
      this.retries = retries;
      this.retriesSubscription = retriesSubscription;
 
      errors.next(err);
    }
  }
 
  _unsubscribe() {
    const { errors, retriesSubscription } = this;
    if (errors) {
      errors.unsubscribe();
      this.errors = null;
    }
    if (retriesSubscription) {
      retriesSubscription.unsubscribe();
      this.retriesSubscription = null;
    }
    this.retries = null;
  }
 
  notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number): void {
 
    const { errors, retries, retriesSubscription } = this;
    this.errors = null;
    this.retries = null;
    this.retriesSubscription = null;
 
    this.unsubscribe();
    this.isStopped = false;
    this.isUnsubscribed = false;
 
    this.errors = errors;
    this.retries = retries;
    this.retriesSubscription = retriesSubscription;
 
    this.source.subscribe(this);
  }
}