Code coverage report for src/observable/PromiseObservable.ts

Statements: 67.31% (35 / 52)      Branches: 42.31% (11 / 26)      Functions: 57.14% (8 / 14)      Lines: 62.79% (27 / 43)      Ignored: none     

All files » src/observable/ » PromiseObservable.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 901   1       1       18 9     59 58     72 72 72   72 72 2 2 2     70   42 42 42 41 41       28 13           1                                                         1   1             1          
import {root} from '../util/root';
import {Scheduler} from '../Scheduler';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
 
export class PromiseObservable<T> extends Observable<T> {
 
  public value: T;
 
  static create<T>(promise: Promise<T>, Escheduler: Scheduler = null): Observable<T> {
    return new PromiseObservable(promise, scheduler);
  }
 
  constructor(private promise: Promise<T>, public scheduler: Scheduler = null) {
    super();
  }
 
  protected _subscribe(subscriber: Subscriber<T>): Subscription | Function | void {
    const promise = this.promise;
    const scheduler = this.scheduler;
 
    Eif (scheduler == null) {
      if (this._isScalar) {
        Eif (!subscriber.isUnsubscribed) {
          subscriber.next(this.value);
          subscriber.complete();
        }
      } else {
        promise.then(
          (value) => {
            this.value = value;
            this._isScalar = true;
            if (!subscriber.isUnsubscribed) {
              subscriber.next(value);
              subscriber.complete();
            }
          },
          (err) => {
            if (!subscriber.isUnsubscribed) {
              subscriber.error(err);
            }
          }
        )
        .then(null, err => {
          // escape the promise trap, throw unhandled errors
          root.setTimeout(() => { throw err; });
        });
      }
    } else {
      if (this._isScalar) {
        if (!subscriber.isUnsubscribed) {
          return scheduler.schedule(dispatchNext, 0, { value: this.value, subscriber });
        }
      } else {
        promise.then(
          (value) => {
            this.value = value;
            this._isScalar = true;
            if (!subscriber.isUnsubscribed) {
              subscriber.add(scheduler.schedule(dispatchNext, 0, { value, subscriber }));
            }
          },
          (err) => {
            if (!subscriber.isUnsubscribed) {
              subscriber.add(scheduler.schedule(dispatchError, 0, { err, subscriber }));
            }
          })
          .then(null, (err) => {
            // escape the promise trap, throw unhandled errors
            root.setTimeout(() => { throw err; });
          });
      }
    }
  }
}
 
function dispatchNext({ value, subscriber }) {
  if (!subscriber.isUnsubscribed) {
    subscriber.next(value);
    subscriber.complete();
  }
}
 
function dispatchError({ err, subscriber }) {
  if (!subscriber.isUnsubscribed) {
    subscriber.error(err);
  }
}