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 96 97 98 99 100 101 102 1031 1     1 1                   1 48 44 44 44     1 44 44     1 44   1   1 44 44 44   1 48 48 48 48   48 73     48 5 5   43       1 44 44 44     1 49 49         1 83       83 83 83   83 49       1 45     1 3 3 3     1 38   1   1 83 83   1  
import {asap} from '../scheduler/asap';
import {isDate} from '../util/isDate';
import {Operator} from '../Operator';
import {Scheduler} from '../Scheduler';
import {Subscriber} from '../Subscriber';
import {Notification} from '../Notification';
import {Observable} from '../Observable';
 
/**
 * Returns an Observable that delays the emission of items from the source Observable
 * by a given timeout or until a given Date.
 * @param {number|Date} delay the timeout value or date until which the emission of the source items is delayed.
 * @param {Scheduler} [scheduler] the Scheduler to use for managing the timers that handle the timeout for each item.
 * @returns {Observable} an Observable that delays the emissions of the source Observable by the specified timeout or Date.
 */
export function delay<T>(delay: number|Date,
                         scheduler: Scheduler = asap): Observable<T> {
  const absoluteDelay = isDate(delay);
  const delayFor = absoluteDelay ? (+delay - scheduler.now()) : Math.abs(<number>delay);
  return this.lift(new DelayOperator(delayFor, scheduler));
}
 
class DelayOperator<T> implements Operator<T, T> {
  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: any): 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(scheduler.now() + this.delay, notification);
    this.queue.push(message);
 
    if (this.active === false) {
      this._schedule(scheduler);
    }
  }
 
  protected _next(value: T) {
    this.scheduleNotification(Notification.createNext(value));
  }
 
  protected _error(err: any) {
    this.errored = true;
    this.queue = [];
    this.destination.error(err);
  }
 
  protected _complete() {
    this.scheduleNotification(Notification.createComplete());
  }
}
 
class DelayMessage<T> {
  constructor(private time: number,
              private notification: any) {
  }
}