Code coverage report for src/subject/BehaviorSubject.ts

Statements: 100% (28 / 28)      Branches: 100% (8 / 8)      Functions: 100% (7 / 7)      Lines: 100% (25 / 25)      Ignored: none     

All files » src/subject/ » BehaviorSubject.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 441     1 1   1   31 31     1 8 1 7 1   6       1 3     1 61 61 42   61     1 74     1 4 4   1  
import {Subject} from '../Subject';
import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
import {throwError} from '../util/throwError';
import {ObjectUnsubscribedError} from '../util/ObjectUnsubscribedError';
 
export class BehaviorSubject<T> extends Subject<T> {
 
  constructor(private _value: T) {
    super();
  }
 
  getValue(): T {
    if (this.hasErrored) {
      throwError(this.errorValue);
    } else if (this.isUnsubscribed) {
      throwError(new ObjectUnsubscribedError());
    } else {
      return this._value;
    }
  }
 
  get value(): T {
    return this.getValue();
  }
 
  protected _subscribe(subscriber: Subscriber<T>): Subscription | Function | void {
    const subscription = super._subscribe(subscriber);
    if (subscription && !(<Subscription> subscription).isUnsubscribed) {
      subscriber.next(this._value);
    }
    return subscription;
  }
 
  protected _next(value: T): void {
    super._next(this._value = value);
  }
 
  protected _error(err: any): void {
    this.hasErrored = true;
    super._error(this.errorValue = err);
  }
}