Code coverage report for src/operator/defaultIfEmpty.ts

Statements: 100% (25 / 25)      Branches: 100% (4 / 4)      Functions: 100% (8 / 8)      Lines: 100% (20 / 20)      Ignored: none     

All files » src/operator/ » defaultIfEmpty.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    1   7 5     1   5     1 5   1   1 5   5 5     1 4 4     1 4 2   4   1  
import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
 
export function defaultIfEmpty<T, R>(defaultValue: R = null): Observable<T> | Observable<R> {
  return this.lift(new DefaultIfEmptyOperator(defaultValue));
}
 
class DefaultIfEmptyOperator<T, R> implements Operator<T, R> {
 
  constructor(private defaultValue: R) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new DefaultIfEmptySubscriber(subscriber, this.defaultValue);
  }
}
 
class DefaultIfEmptySubscriber<T, R> extends Subscriber<T> {
  private isEmpty: boolean = true;
 
  constructor(destination: Subscriber<T>, private defaultValue: R) {
    super(destination);
  }
 
  _next(value: T): void {
    this.isEmpty = false;
    this.destination.next(value);
  }
 
  _complete(): void {
    if (this.isEmpty) {
      this.destination.next(this.defaultValue);
    }
    this.destination.complete();
  }
}