Code coverage report for src/operator/bufferWhen.ts

Statements: 100% (56 / 56)      Branches: 90% (9 / 10)      Functions: 100% (12 / 12)      Lines: 100% (52 / 52)      Ignored: none     

All files » src/operator/ » bufferWhen.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      1 1 1   1 1                         1 17     1   17     1 17   1   1   17     17 17 17     1 62     1 7 7 7   7     1 17 17     1 18     1 3 1   2       1   37   37 20 20     37 37 20     37   37   37 1   36 36 36 36 36 36     1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
import {Subscription} from '../Subscription';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
 
/**
 * Opens a buffer immediately, then closes the buffer when the observable
 * returned by calling `closingSelector` emits a value. It that immediately
 * opens a new buffer and repeats the process.
 *
 * <img src="./img/bufferWhen.png" width="100%">
 *
 * @param {function} closingSelector a function that takes no arguments and
 * returns an Observable that signals buffer closure.
 * @returns {Observable<T[]>} an observable of arrays of buffered values.
 */
export function bufferWhen<T>(closingSelector: () => Observable<any>): Observable<T[]> {
  return this.lift(new BufferWhenOperator(closingSelector));
}
 
class BufferWhenOperator<T> implements Operator<T, T[]> {
 
  constructor(private closingSelector: () => Observable<any>) {
  }
 
  call(subscriber: Subscriber<T[]>): Subscriber<T> {
    return new BufferWhenSubscriber(subscriber, this.closingSelector);
  }
}
 
class BufferWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
  private buffer: T[];
  private subscribing: boolean = false;
  private closingSubscription: Subscription;
 
  constructor(destination: Subscriber<T[]>, private closingSelector: () => Observable<any>) {
    super(destination);
    this.openBuffer();
  }
 
  protected _next(value: T) {
    this.buffer.push(value);
  }
 
  protected _complete() {
    const buffer = this.buffer;
    Eif (buffer) {
      this.destination.next(buffer);
    }
    super._complete();
  }
 
  _unsubscribe() {
    this.buffer = null;
    this.subscribing = false;
  }
 
  notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number): void {
    this.openBuffer();
  }
 
  notifyComplete(): void {
    if (this.subscribing) {
      this.complete();
    } else {
      this.openBuffer();
    }
  }
 
  openBuffer() {
 
    let { closingSubscription } = this;
 
    if (closingSubscription) {
      this.remove(closingSubscription);
      closingSubscription.unsubscribe();
    }
 
    const buffer = this.buffer;
    if (this.buffer) {
      this.destination.next(buffer);
    }
 
    this.buffer = [];
 
    const closingNotifier = tryCatch(this.closingSelector)();
 
    if (closingNotifier === errorObject) {
      this.error(errorObject.e);
    } else {
      closingSubscription = new Subscription();
      this.closingSubscription = closingSubscription;
      this.add(closingSubscription);
      this.subscribing = true;
      closingSubscription.add(subscribeToResult(this, closingNotifier));
      this.subscribing = false;
    }
  }
}