Code coverage report for src/operator/repeat.ts

Statements: 100% (60 / 60)      Branches: 91.67% (11 / 12)      Functions: 100% (16 / 16)      Lines: 100% (53 / 53)      Ignored: none     

All files » src/operator/ » repeat.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  1   1     41 36 4   32       1 32 32     1 35   1   1     35 35 35 35 35 35     1 75     1 3     1 29 29       1 67 67 59   8       1 80 80 80 80 24   56 56 56     1   1 56 56 56     1 91     1 1     1 51 51   1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
import {EmptyObservable} from '../observable/empty';
import {Subscription} from '../Subscription';
 
export function repeat<T>(count: number = -1): Observable<T> {
  if (count === 0) {
    return new EmptyObservable();
  } else {
    return this.lift(new RepeatOperator(count, this));
  }
}
 
class RepeatOperator<T, R> implements Operator<T, R> {
  constructor(private count: number,
              private source: Observable<T>) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new FirstRepeatSubscriber(subscriber, this.count, this.source);
  }
}
 
class FirstRepeatSubscriber<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): void {
    this.destination.next(value);
  }
 
  _error(err: any): void {
    this.destination.error(err);
  }
 
  complete(): void {
    Eif (!this.isUnsubscribed) {
      this.resubscribe(this.count);
    }
  }
 
  unsubscribe(): void {
    const lastSubscription = this.lastSubscription;
    if (lastSubscription === this) {
      super.unsubscribe();
    } else {
      lastSubscription.unsubscribe();
    }
  }
 
  resubscribe(count: number): void {
    const { destination, lastSubscription } = this;
    destination.remove(lastSubscription);
    lastSubscription.unsubscribe();
    if (count - 1 === 0) {
      destination.complete();
    } else {
      const nextSubscriber = new MoreRepeatSubscriber(this, count - 1);
      this.lastSubscription = this.source.subscribe(nextSubscriber);
      destination.add(this.lastSubscription);
    }
  }
}
 
class MoreRepeatSubscriber<T> extends Subscriber<T> {
  constructor(private parent: FirstRepeatSubscriber<T>,
              private count: number) {
    super();
  }
 
  _next(value: T): void {
    this.parent.destination.next(value);
  }
 
  _error(err: any): void {
    this.parent.destination.error(err);
  }
 
  _complete(): void {
    const count = this.count;
    this.parent.resubscribe(count < 0 ? -1 : count);
  }
}