Code coverage report for src/operator/last.ts

Statements: 100% (55 / 55)      Branches: 100% (14 / 14)      Functions: 100% (8 / 8)      Lines: 100% (52 / 52)      Ignored: none     

All files » src/operator/ » last.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    1 1 1 1   1     11     1 11 11 11 11     1 11   1   1   11 11   1 11 11 11 11 11 11 2 2       1 19 19   19 12 12 1 1     11 4 2 2 1 1   1   2   3     7 7       1 7 7 5 5   2     1  
import {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import {EmptyError} from '../util/EmptyError';
 
export function last<T, R>(predicate?: (value: T, index: number, source: Observable<T>) => boolean,
                           resultSelector?: (value: T, index: number) => R,
                           defaultValue?: any): Observable<T> | Observable<R> {
  return this.lift(new LastOperator(predicate, resultSelector, defaultValue, this));
}
 
class LastOperator<T, R> implements Operator<T, R> {
  constructor(private predicate?: (value: T, index: number, source: Observable<T>) => boolean,
              private resultSelector?: (value: T, index: number) => R,
              private defaultValue?: any,
              private source?: Observable<T>) {
  }
 
  call(observer: Subscriber<R>): Subscriber<T> {
    return new LastSubscriber(observer, this.predicate, this.resultSelector, this.defaultValue, this.source);
  }
}
 
class LastSubscriber<T, R> extends Subscriber<T> {
  private lastValue: T;
  private hasValue: boolean = false;
  private index: number = 0;
 
  constructor(destination: Subscriber<R>,
              private predicate?: (value: T, index: number, source: Observable<T>) => boolean,
              private resultSelector?: (value: T, index: number) => R,
              private defaultValue?: any,
              private source?: Observable<T>) {
    super(destination);
    if (typeof defaultValue !== 'undefined') {
      this.lastValue = defaultValue;
      this.hasValue = true;
    }
  }
 
  _next(value: T): void {
    const { predicate, resultSelector, destination } = this;
    const index = this.index++;
 
    if (predicate) {
      let found = tryCatch(predicate)(value, index, this.source);
      if (found === errorObject) {
        destination.error(errorObject.e);
        return;
      }
 
      if (found) {
        if (resultSelector) {
          let result = tryCatch(resultSelector)(value, index);
          if (result === errorObject) {
            destination.error(errorObject.e);
            return;
          }
          this.lastValue = result;
        } else {
          this.lastValue = value;
        }
        this.hasValue = true;
      }
    } else {
      this.lastValue = value;
      this.hasValue = true;
    }
  }
 
  _complete(): void {
    const destination = this.destination;
    if (this.hasValue) {
      destination.next(this.lastValue);
      destination.complete();
    } else {
      destination.error(new EmptyError);
    }
  }
}