Code coverage report for src/operator/bufferWhen.ts

Statements: 100% (56 / 56)      Branches: 100% (6 / 6)      Functions: 100% (15 / 15)      Lines: 100% (50 / 50)      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  1     1 1               1 16     1   16     1 16   1   1       16 16 16     1 58     1 5 5     1 6 6 6 6     1 3815 3815 19 19     3815 3815 3799   3815   3815 3815 1 1 1   3814     1   1 3813 3814     1 17     1 3     1 3782   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';
 
/**
 * 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
 * @param {function} 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, R> implements Operator<T, R> {
 
  constructor(private closingSelector: () => Observable<any>) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new BufferWhenSubscriber(subscriber, this.closingSelector);
  }
}
 
class BufferWhenSubscriber<T> extends Subscriber<T> {
  private buffer: T[];
  private closingNotification: Subscription<any>;
 
  constructor(destination: Subscriber<T>, private closingSelector: () => Observable<any>) {
    super(destination);
    this.openBuffer();
  }
 
  _next(value: T) {
    this.buffer.push(value);
  }
 
  _error(err: any) {
    this.buffer = null;
    this.destination.error(err);
  }
 
  _complete() {
    const buffer = this.buffer;
    this.destination.next(buffer);
    this.buffer = null;
    this.destination.complete();
  }
 
  openBuffer() {
    const prevClosingNotification = this.closingNotification;
    if (prevClosingNotification) {
      this.remove(prevClosingNotification);
      prevClosingNotification.unsubscribe();
    }
 
    const buffer = this.buffer;
    if (buffer) {
      this.destination.next(buffer);
    }
    this.buffer = [];
 
    let closingNotifier = tryCatch(this.closingSelector)();
    if (closingNotifier === errorObject) {
      const err = closingNotifier.e;
      this.buffer = null;
      this.destination.error(err);
    } else {
      this.add(this.closingNotification = closingNotifier._subscribe(new BufferClosingNotifierSubscriber(this)));
    }
  }
}
 
class BufferClosingNotifierSubscriber<T> extends Subscriber<T> {
  constructor(private parent: BufferWhenSubscriber<any>) {
    super(null);
  }
 
  _next() {
    this.parent.openBuffer();
  }
 
  _error(err) {
    this.parent.error(err);
  }
 
  _complete() {
    this.parent.openBuffer();
  }
}