Code coverage report for src/Subscriber.ts

Statements: 98.67% (74 / 75)      Branches: 92.68% (38 / 41)      Functions: 100% (15 / 15)      Lines: 98.61% (71 / 72)      Ignored: none     

All files » src/ » Subscriber.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 1301 1 1     1 1   1   10462   1 1     1 139857 139857   51709   88148         14686 14686       14686       1     2038 2038 2038 2038 2038     10462 10463   10462 8406   2056 2056 102 1954 1934       1   16073 16073 3708   12365       1   2307 1062   1245       1 7563 1749 5814 1592   4222       1 50 50 50       1 177 177 177       1 228 228 226       1 36569 32795       1 1252 1223 1218       1 7383 7325 3543     1
import {noop} from './util/noop';
import {throwError} from './util/throwError';
import {tryOrOnError} from './util/tryOrOnError';
 
import {Observer} from './Observer';
import {Subscription} from './Subscription';
import {rxSubscriber} from './symbol/rxSubscriber';
 
export class Subscriber<T> extends Subscription<T> implements Observer<T> {
  protected _subscription: Subscription<T>;
  protected _isUnsubscribed: boolean = false;
 
  [rxSubscriber]() {
    return this;
  }
 
  get isUnsubscribed(): boolean {
    const subscription = this._subscription;
    if (subscription) {
      // route to the shared Subscription if it exists
      return this._isUnsubscribed || subscription.isUnsubscribed;
    } else {
      return this._isUnsubscribed;
    }
  }
 
  set isUnsubscribed(value: boolean) {
    const subscription = this._subscription;
    Iif (subscription) {
      // route to the shared Subscription if it exists
      subscription.isUnsubscribed = Boolean(value);
    } else {
      this._isUnsubscribed = Boolean(value);
    }
  }
 
  static create<T>(next?: (x?: T) => void,
                   error?: (e?: any) => void,
                   complete?: () => void): Subscriber<T> {
    const subscriber = new Subscriber<T>();
    subscriber._next = (typeof next === 'function') && tryOrOnError(next) || noop;
    subscriber._error = (typeof error === 'function') && error || throwError;
    subscriber._complete = (typeof complete === 'function') && complete || noop;
    return subscriber;
  }
 
  constructor(protected destination?: Observer<any>) {
    super();
 
    if (!this.destination) {
      return;
    }
    const subscription = (<any> destination)._subscription;
    if (subscription) {
      this._subscription = subscription;
    } else if (destination instanceof Subscriber) {
      this._subscription = (<Subscription<T>> destination);
    }
  }
 
  add(sub: Subscription<T> | Function | void): void {
    // route add to the shared Subscription if it exists
    const _subscription = this._subscription;
    if (_subscription) {
      _subscription.add(sub);
    } else {
      super.add(sub);
    }
  }
 
  remove(sub: Subscription<T>): void {
    // route remove to the shared Subscription if it exists
    if (this._subscription) {
      this._subscription.remove(sub);
    } else {
      super.remove(sub);
    }
  }
 
  unsubscribe(): void {
    if (this._isUnsubscribed) {
      return;
    } else if (this._subscription) {
      this._isUnsubscribed = true;
    } else {
      super.unsubscribe();
    }
  }
 
  _next(value: T): void {
    const destination = this.destination;
    Eif (destination.next) {
      destination.next(value);
    }
  }
 
  _error(err: any): void {
    const destination = this.destination;
    Eif (destination.error) {
      destination.error(err);
    }
  }
 
  _complete(): void {
    const destination = this.destination;
    if (destination.complete) {
      destination.complete();
    }
  }
 
  next(value?: T): void {
    if (!this.isUnsubscribed) {
      this._next(value);
    }
  }
 
  error(err?: any): void {
    if (!this.isUnsubscribed) {
      this._error(err);
      this.unsubscribe();
    }
  }
 
  complete(): void {
    if (!this.isUnsubscribed) {
      this._complete();
      this.unsubscribe();
    }
  }
}