Code coverage report for src/observable/ArrayObservable.ts

Statements: 97.96% (48 / 49)      Branches: 94.44% (17 / 18)      Functions: 100% (6 / 6)      Lines: 97.73% (43 / 44)      Ignored: none     

All files » src/observable/ » ArrayObservable.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  1 1 1   1     1   1 15     1910 753 753 37   716     753 753 209 544 542   2       1   208   208 53 51     155   153       153   153           537 537 537 1 1       1 551 551 551 551   551 59       492 1195   492     1  
import {Scheduler} from '../Scheduler';
import {Observable} from '../Observable';
import {ScalarObservable} from './ScalarObservable';
import {EmptyObservable} from './EmptyObservable';
import {Subscriber} from '../Subscriber';
import {isScheduler} from '../util/isScheduler';
import {Subscription} from '../Subscription';
 
export class ArrayObservable<T> extends Observable<T> {
 
  static create<T>(array: T[], scheduler?: Scheduler) {
    return new ArrayObservable(array, scheduler);
  }
 
  static of<T>(...array: Array<T | Scheduler>): Observable<T> {
    let scheduler = <Scheduler>array[array.length - 1];
    if (isScheduler(scheduler)) {
      array.pop();
    } else {
      scheduler = null;
    }
 
    const len = array.length;
    if (len > 1) {
      return new ArrayObservable<T>(<any>array, scheduler);
    } else if (len === 1) {
      return new ScalarObservable<T>(<any>array[0], scheduler);
    } else {
      return new EmptyObservable<T>(scheduler);
    }
  }
 
  static dispatch(state: any) {
 
    const { array, index, count, subscriber } = state;
 
    if (index >= count) {
      subscriber.complete();
      return;
    }
 
    subscriber.next(array[index]);
 
    Iif (subscriber.isUnsubscribed) {
      return;
    }
 
    state.index = index + 1;
 
    (<any> this).schedule(state);
  }
 
  // value used if Array has one value and _isScalar
  value: any;
 
  constructor(public array: T[], public scheduler?: Scheduler) {
    super();
    if (!scheduler && array.length === 1) {
      this._isScalar = true;
      this.value = array[0];
    }
  }
 
  protected _subscribe(subscriber: Subscriber<T>): Subscription | Function | void {
    let index = 0;
    const array = this.array;
    const count = array.length;
    const scheduler = this.scheduler;
 
    if (scheduler) {
      return scheduler.schedule(ArrayObservable.dispatch, 0, {
        array, index, count, subscriber
      });
    } else {
      for (let i = 0; i < count && !subscriber.isUnsubscribed; i++) {
        subscriber.next(array[i]);
      }
      subscriber.complete();
    }
  }
}