Code coverage report for src/Subscription.ts

Statements: 95.92% (47 / 49)      Branches: 91.43% (32 / 35)      Functions: 100% (6 / 6)      Lines: 95.83% (46 / 48)      Ignored: none     

All files » src/ » Subscription.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 1011 1 1   1 1 1 1     29785   1 29785 3083       1   18482 130     18352   18352   18352   18352 7244     18352   6356 6356   6356 12660 12660 12660           1         18313     3291     15022   15022   9   15022 234 14788 121   14667   14788           1           1508           1508   1508 1428 1428 831       1  
import {isArray} from './util/isArray';
import {isObject} from './util/isObject';
import {isFunction} from './util/isFunction';
 
export class Subscription {
  public static EMPTY: Subscription = (function(empty: any){
    empty.isUnsubscribed = true;
    return empty;
  }(new Subscription()));
 
  public isUnsubscribed: boolean = false;
 
  constructor(_unsubscribe?: () => void) {
    if (_unsubscribe) {
      (<any> this)._unsubscribe = _unsubscribe;
    }
  }
 
  unsubscribe(): void {
 
    if (this.isUnsubscribed) {
      return;
    }
 
    this.isUnsubscribed = true;
 
    const { _unsubscribe, _subscriptions } = (<any> this);
 
    (<any> this)._subscriptions = null;
 
    if (isFunction(_unsubscribe)) {
      _unsubscribe.call(this);
    }
 
    if (isArray(_subscriptions)) {
 
      let index = -1;
      const len = _subscriptions.length;
 
      while (++index < len) {
        const subscription = _subscriptions[index];
        Eif (isObject(subscription)) {
          subscription.unsubscribe();
        }
      }
    }
  }
 
  add(subscription: Subscription | Function | void): void {
    // return early if:
    //  1. the subscription is null
    //  2. we're attempting to add our this
    //  3. we're attempting to add the static `empty` Subscription
    if (!subscription || (
        subscription === this) || (
        subscription === Subscription.EMPTY)) {
      return;
    }
 
    let sub = (<Subscription> subscription);
 
    switch (typeof subscription) {
      case 'function':
        sub = new Subscription(<(() => void) > subscription);
      case 'object':
        if (sub.isUnsubscribed || typeof sub.unsubscribe !== 'function') {
          break;
        } else if (this.isUnsubscribed) {
            sub.unsubscribe();
        } else {
          ((<any> this)._subscriptions || ((<any> this)._subscriptions = [])).push(sub);
        }
        break;
      default:
        throw new Error('Unrecognized subscription ' + subscription + ' added to Subscription.');
    }
  }
 
  remove(subscription: Subscription): void {
 
    // return early if:
    //  1. the subscription is null
    //  2. we're attempting to remove ourthis
    //  3. we're attempting to remove the static `empty` Subscription
    Iif (subscription == null   || (
        subscription === this) || (
        subscription === Subscription.EMPTY)) {
      return;
    }
 
    const subscriptions = (<any> this)._subscriptions;
 
    if (subscriptions) {
      const subscriptionIndex = subscriptions.indexOf(subscription);
      if (subscriptionIndex !== -1) {
        subscriptions.splice(subscriptionIndex, 1);
      }
    }
  }
}