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         123 123 123     1 354     1 534 534 284   250     1 192 192 192 12   180 180 180     1 55   1   1 180 180     1 171 171 171 171   1   1     55 110 55     1 234 234 234 234 128   234   1   1     234 234 234 234 234     1 501     1 99 99     1 107 107     1 206 206 206 206 89 89 89 89       1 230 230 180   50 37 37 37 11 11       1  
import {Subject} from '../Subject';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
 
export class ConnectableObservable<T> extends Observable<T> {
 
  subject: Subject<T>;
  subscription: Subscription;
 
  constructor(public source: Observable<T>,
              protected subjectFactory: () => Subject<T>) {
    super();
  }
 
  protected _subscribe(subscriber: Subscriber<T>) {
    return this._getSubject().subscribe(subscriber);
  }
 
  _getSubject() {
    const subject = this.subject;
    if (subject && !subject.isUnsubscribed) {
      return subject;
    }
    return (this.subject = this.subjectFactory());
  }
 
  connect(): Subscription {
    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 extends Subscription {
  constructor(protected connectable: ConnectableObservable<any>) {
    super();
  }
 
  _unsubscribe() {
    const connectable = this.connectable;
    connectable.subject = null;
    connectable.subscription = null;
    this.connectable = null;
  }
}
 
class RefCountObservable<T> extends Observable<T> {
  connection: Subscription;
 
  constructor(protected connectable: ConnectableObservable<T>,
              Epublic refCount: number = 0) {
    super();
  }
 
  _subscribe(subscriber: Subscriber<T>) {
    const connectable = this.connectable;
    const refCountSubscriber: RefCountSubscriber<T> = 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;
 
  constructor(public destination: Subscriber<T>,
              private refCountObservable: RefCountObservable<T>) {
    super(null);
    this.connection = refCountObservable.connection;
    destination.add(this);
  }
 
  protected _next(value: T) {
    this.destination.next(value);
  }
 
  protected _error(err: any) {
    this._resetConnectable();
    this.destination.error(err);
  }
 
  protected _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 = null;
      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 = null;
      }
    }
  }
}