Code coverage report for src/operator/delay.ts

Statements: 98.39% (61 / 62)      Branches: 91.67% (11 / 12)      Functions: 100% (14 / 14)      Lines: 98.28% (57 / 58)      Ignored: none     

All files » src/operator/ » delay.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    1 1 1 1   1 40 37 37 37     1 37 37     1 37   1   1 37 37 37   1 43 43 43 43   43 62     43 5 5   38       1 37 37 37     1 41 41         1 67       67 67 67   67 41       1 35     1 3 3 3     1 32   1   1 67 67   1  
import {Operator} from '../Operator';
import {Scheduler} from '../Scheduler';
import {Subscriber} from '../Subscriber';
import {Notification} from '../Notification';
import {queue} from '../scheduler/queue';
import {isDate} from '../util/isDate';
 
export function delay<T>(delay: number|Date,
                         scheduler: Scheduler = queue) {
  const absoluteDelay = isDate(delay);
  const delayFor = absoluteDelay ? (+delay - scheduler.now()) : <number>delay;
  return this.lift(new DelayOperator(delayFor, scheduler));
}
 
class DelayOperator<T, R> implements Operator<T, R> {
  constructor(private delay: number,
              private scheduler: Scheduler) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new DelaySubscriber(subscriber, this.delay, this.scheduler);
  }
}
 
class DelaySubscriber<T> extends Subscriber<T> {
  private queue: Array<any> = [];
  private active: boolean = false;
  private errored: boolean = false;
 
  private static dispatch(state): void {
    const source = state.source;
    const queue = source.queue;
    const scheduler = state.scheduler;
    const destination = state.destination;
 
    while (queue.length > 0 && (queue[0].time - scheduler.now()) <= 0) {
      queue.shift().notification.observe(destination);
    }
 
    if (queue.length > 0) {
      const delay = Math.max(0, queue[0].time - scheduler.now());
      (<any> this).schedule(state, delay);
    } else {
      source.active = false;
    }
  }
 
  constructor(destination: Subscriber<T>,
              private delay: number,
              private scheduler: Scheduler) {
    super(destination);
  }
 
  private _schedule(scheduler: Scheduler): void {
    this.active = true;
    this.add(scheduler.schedule(DelaySubscriber.dispatch, this.delay, {
      source: this, destination: this.destination, scheduler: scheduler
    }));
  }
 
  private scheduleNotification(notification: Notification<any>): void {
    Iif (this.errored === true) {
      return;
    }
 
    const scheduler = this.scheduler;
    const message = new DelayMessage<T>(scheduler.now() + this.delay, notification);
    this.queue.push(message);
 
    if (this.active === false) {
      this._schedule(scheduler);
    }
  }
 
  _next(value: T) {
    this.scheduleNotification(Notification.createNext(value));
  }
 
  _error(err) {
    this.errored = true;
    this.queue = [];
    this.destination.error(err);
  }
 
  _complete() {
    this.scheduleNotification(Notification.createComplete());
  }
}
 
class DelayMessage<T> {
  constructor(private time: number,
              private notification: any) {
  }
}