Code coverage report for src/observable/ConnectableObservable.ts

Statements: 100% (90 / 90)      Branches: 96.15% (25 / 26)      Functions: 100% (19 / 19)      Lines: 100% (80 / 80)      Ignored: none     

All files » src/observable/ » ConnectableObservable.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  1 1 1   1         107 107 107     1 280     1 404 404 212   192     1 132 132 132 8   124 124 124     1 44   1   1 124 124     1 115 115 115 115   1   1     44 88 44     1 175 175 175 175 73   175   1   1     175 175 175 175 175     1 321     1 76 76     1 72 72     1 148 148 148 148 51 51 51 51       1 171 171 139   32 19 19 19 10 10       1  
import {Subject} from '../Subject';
import {Observable} from '../Observable';
import {Subscription} from '../Subscription';
import {Subscriber} from '../Subscriber';
 
export class ConnectableObservable<T> extends Observable<T> {
 
  subject: Subject<T>;
  subscription: Subscription<T>;
 
  constructor(public source: Observable<T>,
              protected subjectFactory: () => Subject<T>) {
    super();
  }
 
  _subscribe(subscriber) {
    return this._getSubject().subscribe(subscriber);
  }
 
  _getSubject() {
    const subject = this.subject;
    if (subject && !subject.isUnsubscribed) {
      return subject;
    }
    return (this.subject = this.subjectFactory());
  }
 
  connect(): Subscription<T> {
    const source = this.source;
    let subscription = this.subscription;
    if (subscription && !subscription.isUnsubscribed) {
      return subscription;
    }
    subscription = source.subscribe(this._getSubject());
    subscription.add(new ConnectableSubscription(this));
    return (this.subscription = subscription);
  }
 
  refCount(): Observable<T> {
    return new RefCountObservable(this);
  }
}
 
class ConnectableSubscription<T> extends Subscription<T> {
  constructor(protected connectable: ConnectableObservable<T>) {
    super();
  }
 
  _unsubscribe() {
    const connectable = this.connectable;
    connectable.subject = void 0;
    connectable.subscription = void 0;
    this.connectable = void 0;
  }
}
 
class RefCountObservable<T> extends Observable<T> {
  connection: Subscription<T>;
 
  constructor(protected connectable: ConnectableObservable<T>,
              Epublic refCount: number = 0) {
    super();
  }
 
  _subscribe(subscriber) {
    const connectable = this.connectable;
    const refCountSubscriber = new RefCountSubscriber(subscriber, this);
    const subscription = connectable.subscribe(refCountSubscriber);
    if (!subscription.isUnsubscribed && ++this.refCount === 1) {
      refCountSubscriber.connection = this.connection = connectable.connect();
    }
    return subscription;
  }
}
 
class RefCountSubscriber<T> extends Subscriber<T> {
  connection: Subscription<T>;
 
  constructor(public destination: Subscriber<T>,
              private refCountObservable: RefCountObservable<T>) {
    super(null);
    this.connection = refCountObservable.connection;
    destination.add(this);
  }
 
  _next(value: T) {
    this.destination.next(value);
  }
 
  _error(err: any) {
    this._resetConnectable();
    this.destination.error(err);
  }
 
  _complete() {
    this._resetConnectable();
    this.destination.complete();
  }
 
  _resetConnectable() {
    const observable = this.refCountObservable;
    const obsConnection = observable.connection;
    const subConnection = this.connection;
    if (subConnection && subConnection === obsConnection) {
      observable.refCount = 0;
      obsConnection.unsubscribe();
      observable.connection = void 0;
      this.unsubscribe();
    }
  }
 
  _unsubscribe() {
    const observable = this.refCountObservable;
    if (observable.refCount === 0) {
      return;
    }
    if (--observable.refCount === 0) {
      const obsConnection = observable.connection;
      const subConnection = this.connection;
      if (subConnection && subConnection === obsConnection) {
        obsConnection.unsubscribe();
        observable.connection = void 0;
      }
    }
  }
}