Code coverage report for src/subject/AsyncSubject.ts

Statements: 100% (28 / 28)      Branches: 100% (6 / 6)      Functions: 100% (4 / 4)      Lines: 100% (26 / 26)      Ignored: none     

All files » src/subject/ » AsyncSubject.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 481       48 47 47   1 74 6     74     1 63 63     1 28 28 28       28   28 25 24 24 24     3 3       28   28   1  
import {Subject} from '../Subject';
import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
 
export class AsyncSubject<T> extends Subject<T> {
  value: T = null;
  hasNext: boolean = false;
 
  protected _subscribe(subscriber: Subscriber<any>): Subscription|Function|void {
    if (this.hasCompleted && this.hasNext) {
      subscriber.next(this.value);
    }
 
    return super._subscribe(subscriber);
  }
 
  protected _next(value: T): void {
    this.value = value;
    this.hasNext = true;
  }
 
  protected _complete(): void {
    let index = -1;
    const observers = this.observers;
    const len = observers.length;
 
    // optimization to block our SubjectSubscriptions from
    // splicing themselves out of the observers list one by one.
    this.isUnsubscribed = true;
 
    if (this.hasNext) {
      while (++index < len) {
        let o = observers[index];
        o.next(this.value);
        o.complete();
      }
    } else {
      while (++index < len) {
        observers[index].complete();
      }
    }
 
    this.isUnsubscribed = false;
 
    this.unsubscribe();
  }
}