Code coverage report for src/operator/retryWhen.ts

Statements: 100% (90 / 90)      Branches: 80% (8 / 10)      Functions: 100% (24 / 24)      Lines: 100% (82 / 82)      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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138  1   1   1 1   1 20     1 20 20     1 20   1   1           20 20 20 20 20 20     1 33     1 13 13 13 13 13 13 13 1   12 12 12 12     13       1 3 3     1 4     1 9 9     1 41 41 37   4       1 16 16 16 16 11       1 12 12 12 12 12 12   1   1 12 12     1 21     1 9     1 1   1   1 12 12     1 12     1 3     1 4   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';
 
export function retryWhen<T>(notifier: (errors: Observable<any>) => Observable<any>) {
  return this.lift(new RetryWhenOperator(notifier, this));
}
 
class RetryWhenOperator<T, R> implements Operator<T, R> {
  constructor(protected notifier: (errors: Observable<any>) => Observable<any>,
              protected source: Observable<T>) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new FirstRetryWhenSubscriber<T>(subscriber, this.notifier, this.source);
  }
}
 
class FirstRetryWhenSubscriber<T> extends Subscriber<T> {
  lastSubscription: Subscription<T>;
  notificationSubscription: Subscription<T>;
  errors: Subject<any>;
  retryNotifications: Observable<any>;
 
  constructor(public destination: Subscriber<T>,
              public notifier: (errors: Observable<any>) => Observable<any>,
              public source: Observable<T>) {
    super();
    destination.add(this);
    this.lastSubscription = this;
  }
 
  _next(value: T) {
    this.destination.next(value);
  }
 
  error(err?) {
    const destination = this.destination;
    Eif (!this.isUnsubscribed) {
      super.unsubscribe();
      Eif (!this.retryNotifications) {
        this.errors = new Subject();
        const notifications = tryCatch(this.notifier).call(this, this.errors);
        if (notifications === errorObject) {
          destination.error(errorObject.e);
        } else {
          this.retryNotifications = notifications;
          const notificationSubscriber = new RetryNotificationSubscriber(this);
          this.notificationSubscription = notifications.subscribe(notificationSubscriber);
          destination.add(this.notificationSubscription);
        }
      }
      this.errors.next(err);
    }
  }
 
  destinationError(err: any) {
    this.tearDown();
    this.destination.error(err);
  }
 
  _complete() {
    this.destinationComplete();
  }
 
  destinationComplete() {
    this.tearDown();
    this.destination.complete();
  }
 
  unsubscribe() {
    const lastSubscription = this.lastSubscription;
    if (lastSubscription === this) {
      super.unsubscribe();
    } else {
      this.tearDown();
    }
  }
 
  tearDown() {
    super.unsubscribe();
    this.lastSubscription.unsubscribe();
    const notificationSubscription = this.notificationSubscription;
    if (notificationSubscription) {
      notificationSubscription.unsubscribe();
    }
  }
 
  resubscribe() {
    const { destination, lastSubscription } = this;
    destination.remove(lastSubscription);
    lastSubscription.unsubscribe();
    const nextSubscriber = new MoreRetryWhenSubscriber(this);
    this.lastSubscription = this.source.subscribe(nextSubscriber);
    destination.add(this.lastSubscription);
  }
}
 
class MoreRetryWhenSubscriber<T> extends Subscriber<T> {
  constructor(private parent: FirstRetryWhenSubscriber<T>) {
    super(null);
  }
 
  _next(value: T) {
    this.parent.destination.next(value);
  }
 
  _error(err: any) {
    this.parent.errors.next(err);
  }
 
  _complete() {
    this.parent.destinationComplete();
  }
}
 
class RetryNotificationSubscriber<T> extends Subscriber<T> {
  constructor(public parent: FirstRetryWhenSubscriber<any>) {
    super(null);
  }
 
  _next(value: T) {
    this.parent.resubscribe();
  }
 
  _error(err: any) {
    this.parent.destinationError(err);
  }
 
  _complete() {
    this.parent.destinationComplete();
  }
}