Code coverage report for src/operator/retry.ts

Statements: 100% (59 / 59)      Branches: 83.33% (10 / 12)      Functions: 100% (15 / 15)      Lines: 100% (50 / 50)      Ignored: none     

All files » src/operator/ » retry.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  1       32 25     1 25 25     1 33   1   1     33 33 33 33 33 33     1 73     1 29 29 29       1 2 2     134 105 105 105 105 105 105   1   1 105 105 105 105     1 84     1 99 99 99   99 23   76       1 2   1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
import {Subscription} from '../Subscription';
 
export function retry<T>(count: number = 0): Observable<T> {
  return this.lift(new RetryOperator(count, this));
}
 
class RetryOperator<T, R> implements Operator<T, R> {
  constructor(private count: number,
              protected source: Observable<T>) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new FirstRetrySubscriber<T>(subscriber, this.count, this.source);
  }
}
 
class FirstRetrySubscriber<T> extends Subscriber<T> {
  private lastSubscription: Subscription<T>;
 
  constructor(public destination: Subscriber<T>,
              private count: number,
              private source: Observable<T>) {
    super();
    destination.add(this);
    this.lastSubscription = this;
  }
 
  _next(value: T) {
    this.destination.next(value);
  }
 
  error(error?) {
    Eif (!this.isUnsubscribed) {
      this.unsubscribe();
      this.resubscribe();
    }
  }
 
  _complete() {
    this.unsubscribe();
    this.destination.complete();
  }
 
  resubscribe(retried: number = 0) {
    const { lastSubscription, destination } = this;
    destination.remove(lastSubscription);
    lastSubscription.unsubscribe();
    const nextSubscriber = new RetryMoreSubscriber(this, this.count, retried + 1);
    this.lastSubscription = this.source.subscribe(nextSubscriber);
    destination.add(this.lastSubscription);
  }
}
 
class RetryMoreSubscriber<T> extends Subscriber<T> {
  constructor(private parent: FirstRetrySubscriber<T>,
              private count: number,
              Iprivate retried: number = 0) {
    super(null);
  }
 
  _next(value: T) {
    this.parent.destination.next(value);
  }
 
  _error(err: any) {
    const parent = this.parent;
    const retried = this.retried;
    const count = this.count;
 
    if (count && retried === count) {
      parent.destination.error(err);
    } else {
      parent.resubscribe(retried);
    }
  }
 
  _complete() {
    this.parent.destination.complete();
  }
}