Code coverage report for src/operator/groupBy-support.ts

Statements: 100% (48 / 48)      Branches: 94.44% (17 / 18)      Functions: 100% (10 / 10)      Lines: 100% (40 / 40)      Ignored: none     

All files » src/operator/ » groupBy-support.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 651     1   1   37 37   1 37     1 37     1 39 36 36 33 33       1   1 171 171 171 171     1 150 150 84   150 150   1   1 84 84 84     1 84 84 84 84 3 3       1    
import {Subscription} from '../Subscription';
import {Subject} from '../Subject';
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
 
export class RefCountSubscription<T> extends Subscription<T> {
  primary: Subscription<T>;
  attemptedToUnsubscribePrimary: boolean = false;
  count: number = 0;
 
  constructor() {
    super();
  }
 
  setPrimary(subscription: Subscription<T>) {
    this.primary = subscription;
  }
 
  unsubscribe() {
    if (!this.isUnsubscribed && !this.attemptedToUnsubscribePrimary) {
      this.attemptedToUnsubscribePrimary = true;
      if (this.count === 0) {
        super.unsubscribe();
        this.primary.unsubscribe();
      }
    }
  }
}
 
export class GroupedObservable<T> extends Observable<T> {
  constructor(public key: string,
              private groupSubject: Subject<T>,
              private refCountSubscription?: RefCountSubscription<T>) {
    super();
  }
 
  _subscribe(subscriber: Subscriber<T>) {
    const subscription = new Subscription();
    if (this.refCountSubscription && !this.refCountSubscription.isUnsubscribed) {
      subscription.add(new InnerRefCountSubscription(this.refCountSubscription));
    }
    subscription.add(this.groupSubject.subscribe(subscriber));
    return subscription;
  }
}
 
export class InnerRefCountSubscription<T> extends Subscription<T> {
  constructor(private parent: RefCountSubscription<T>) {
    super();
    parent.count++;
  }
 
  unsubscribe() {
    Eif (!this.parent.isUnsubscribed && !this.isUnsubscribed) {
      super.unsubscribe();
      this.parent.count--;
      if (this.parent.count === 0 && this.parent.attemptedToUnsubscribePrimary) {
        this.parent.unsubscribe();
        this.parent.primary.unsubscribe();
      }
    }
  }
}