Code coverage report for src/operator/zip.ts

Statements: 96.69% (146 / 151)      Branches: 87.5% (35 / 40)      Functions: 93.55% (29 / 31)      Lines: 96.45% (136 / 141)      Ignored: none     

All files » src/operator/ » zip.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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246  1 1     1 1 1 1 1 1   102 44 44     292 88 88 18   88     1       1 139     1 139   1   1 139     139 139   1   278 139 139 139     1 295 295 295 30 265 6   259       1 138 138 138 138 293 293 257   36         1 30 30 5       1 449 449 449     449 877 877 257       192 192 192 424 424       424 20     424         424     192 192 91 91 6   85     101     192 17     1             1     6 6     1 21     1 21 21 21     1 21 21   1   1 30 30   30 30     1       1 18 18 18     1 21     1 18   1   1 259 259 259   1 259 259 259 259     1           1 385 385     385       1 835     1 385     1 93 30 30   63       1 449 449     1 257   1  
import {Observable} from '../Observable';
import {ArrayObservable} from '../observable/ArrayObservable';
import {isArray} from '../util/isArray';
import {Operator} from '../Operator';
import {Observer} from '../Observer';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
import {SymbolShim} from '../util/SymbolShim';
 
export function zipProto<R>(...observables: Array<Observable<any> | ((...values: Array<any>) => R)>): Observable<R> {
  observables.unshift(this);
  return zipStatic.apply(this, observables);
}
 
export function zipStatic<T, R>(...observables: Array<Observable<any> | ((...values: Array<any>) => R)>): Observable<R> {
  const project = <((...ys: Array<any>) => R)> observables[observables.length - 1];
  if (typeof project === 'function') {
    observables.pop();
  }
  return new ArrayObservable(observables).lift<T, R>(new ZipOperator<T, R>(project));
}
 
export class ZipOperator<T, R> implements Operator<T, R> {
 
  project: (...values: Array<any>) => R;
 
  constructor(project?: (...values: Array<any>) => R) {
    this.project = project;
  }
 
  call(subscriber: Subscriber<R>): Subscriber<T> {
    return new ZipSubscriber(subscriber, this.project);
  }
}
 
export class ZipSubscriber<T, R> extends Subscriber<T> {
  private index = 0;
  private values: any;
  private project: (...values: Array<any>) => R;
  private iterators: LookAheadIterator<any>[] = [];
  private active = 0;
 
  constructor(destination: Subscriber<R>,
              project?: (...values: Array<any>) => R,
              Evalues: any = Object.create(null)) {
    super(destination);
    this.project = (typeof project === 'function') ? project : null;
    this.values = values;
  }
 
  protected _next(value: any) {
    const iterators = this.iterators;
    const index = this.index++;
    if (isArray(value)) {
      iterators.push(new StaticArrayIterator(value));
    } else if (typeof value[SymbolShim.iterator] === 'function') {
      iterators.push(new StaticIterator(value[SymbolShim.iterator]()));
    } else {
      iterators.push(new ZipBufferIterator(this.destination, this, value, index));
    }
  }
 
  protected _complete() {
    const iterators = this.iterators;
    const len = iterators.length;
    this.active = len;
    for (let i = 0; i < len; i++) {
      let iterator: ZipBufferIterator<any, any> = <any>iterators[i];
      if (iterator.stillUnsubscribed) {
        this.add(iterator.subscribe(iterator, i));
      } else {
        this.active--; // not an observable
      }
    }
  }
 
  notifyInactive() {
    this.active--;
    if (this.active === 0) {
      this.destination.complete();
    }
  }
 
  checkIterators() {
    const iterators = this.iterators;
    const len = iterators.length;
    const destination = this.destination;
 
    // abort if not all of them have values
    for (let i = 0; i < len; i++) {
      let iterator = iterators[i];
      if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) {
        return;
      }
    }
 
    let shouldComplete = false;
    const args: any[] = [];
    for (let i = 0; i < len; i++) {
      let iterator = iterators[i];
      let result = iterator.next();
 
      // check to see if it's completed now that you've gotten
      // the next value.
      if (iterator.hasCompleted()) {
        shouldComplete = true;
      }
 
      Iif (result.done) {
        destination.complete();
        return;
      }
 
      args.push(result.value);
    }
 
    const project = this.project;
    if (project) {
      let result = tryCatch(project).apply(this, args);
      if (result === errorObject) {
        destination.error(errorObject.e);
      } else {
        destination.next(result);
      }
    } else {
      destination.next(args);
    }
 
    if (shouldComplete) {
      destination.complete();
    }
  }
}
 
interface LookAheadIterator<T> extends Iterator<T> {
  hasValue(): boolean;
  hasCompleted(): boolean;
}
 
class StaticIterator<T> implements LookAheadIterator<T> {
  private nextResult: IteratorResult<T>;
 
  constructor(private iterator: Iterator<T>) {
    this.nextResult = iterator.next();
  }
 
  hasValue() {
    return true;
  }
 
  next(): IteratorResult<T> {
    const result = this.nextResult;
    this.nextResult = this.iterator.next();
    return result;
  }
 
  hasCompleted() {
    const nextResult = this.nextResult;
    return nextResult && nextResult.done;
  }
}
 
class StaticArrayIterator<T> implements LookAheadIterator<T> {
  private index = 0;
  private length = 0;
 
  constructor(private array: T[]) {
    this.length = array.length;
  }
 
  [SymbolShim.iterator]() {
    return this;
  }
 
  next(value?: any): IteratorResult<T> {
    const i = this.index++;
    const array = this.array;
    return i < this.length ? { value: array[i], done: false } : { done: true };
  }
 
  hasValue() {
    return this.array.length > this.index;
  }
 
  hasCompleted() {
    return this.array.length === this.index;
  }
}
 
class ZipBufferIterator<T, R> extends OuterSubscriber<T, R> implements LookAheadIterator<T> {
  stillUnsubscribed = true;
  buffer: T[] = [];
  isComplete = false;
 
  constructor(destination: Observer<T>,
              private parent: ZipSubscriber<T, R>,
              private observable: Observable<T>,
              private index: number) {
    super(destination);
  }
 
  [SymbolShim.iterator]() {
    return this;
  }
 
  // NOTE: there is actually a name collision here with Subscriber.next and Iterator.next
  //    this is legit because `next()` will never be called by a subscription in this case.
  next(): IteratorResult<T> {
    const buffer = this.buffer;
    Iif (buffer.length === 0 && this.isComplete) {
      return { done: true };
    } else {
      return { value: buffer.shift(), done: false };
    }
  }
 
  hasValue() {
    return this.buffer.length > 0;
  }
 
  hasCompleted() {
    return this.buffer.length === 0 && this.isComplete;
  }
 
  notifyComplete() {
    if (this.buffer.length > 0) {
      this.isComplete = true;
      this.parent.notifyInactive();
    } else {
      this.destination.complete();
    }
  }
 
  notifyNext(outerValue: any, innerValue: any, outerIndex: number, innerIndex: number) {
    this.buffer.push(innerValue);
    this.parent.checkIterators();
  }
 
  subscribe(value: any, index: number) {
    return subscribeToResult<any, any>(this, this.observable, this, index);
  }
}