Code coverage report for src/Notification.ts

Statements: 100% (40 / 40)      Branches: 100% (15 / 15)      Functions: 100% (9 / 9)      Lines: 100% (36 / 36)      Ignored: none     

All files » src/ » Notification.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  1   1     18066 18066     1 10068   7601   497   1970       1 6 6   2   2   2       1 6 3   3       1 3 3   1   1   1       1 1   1 16696 16550   146     1 1514     1 4059   1  
import {Observer} from './Observer';
import {Observable} from './Observable';
 
export class Notification<T> {
  hasValue: boolean;
 
  constructor(public kind: string, public value?: T, public exception?: any) {
    this.hasValue = kind === 'N';
  }
 
  observe(observer: Observer<T>): any {
    switch (this.kind) {
      case 'N':
        return observer.next(this.value);
      case 'E':
        return observer.error(this.exception);
      case 'C':
        return observer.complete();
    }
  }
 
  do(next: (value: T) => void, error?: (err: any) => void, complete?: () => void): any {
    const kind = this.kind;
    switch (kind) {
      case 'N':
        return next(this.value);
      case 'E':
        return error(this.exception);
      case 'C':
        return complete();
    }
  }
 
  accept(nextOrObserver: Observer<T> | ((value: T) => void), error?: (err: any) => void, complete?: () => void) {
    if (nextOrObserver && typeof (<Observer<T>>nextOrObserver).next === 'function') {
      return this.observe(<Observer<T>>nextOrObserver);
    } else {
      return this.do(<(value: T) => void>nextOrObserver, error, complete);
    }
  }
 
  toObservable(): Observable<T> {
    const kind = this.kind;
    switch (kind) {
      case 'N':
        return Observable.of(this.value);
      case 'E':
        return Observable.throw(this.exception);
      case 'C':
        return Observable.empty<T>();
    }
  }
 
  private static completeNotification: Notification<any> = new Notification('C');
  private static undefinedValueNotification: Notification<any> = new Notification('N', undefined);
 
  static createNext<T>(value: T): Notification<T> {
    if (typeof value !== 'undefined') {
      return new Notification('N', value);
    }
    return this.undefinedValueNotification;
  }
 
  static createError<T>(err?: any): Notification<T> {
    return new Notification('E', undefined, err);
  }
 
  static createComplete(): Notification<any> {
    return this.completeNotification;
  }
}