Code coverage report for src/scheduler/QueueScheduler.ts

Statements: 100% (25 / 25)      Branches: 100% (8 / 8)      Functions: 100% (6 / 6)      Lines: 100% (23 / 23)      Ignored: none     

All files » src/scheduler/ » QueueScheduler.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  1   1     2 3 3 3   1 318     1 144 106   38 38 38 147   38     60 55         1 30     1 21   1  
import {Scheduler} from '../Scheduler';
import {QueueAction} from './QueueAction';
import {Subscription} from '../Subscription';
import {FutureAction} from './FutureAction';
import {Action} from './Action';
 
export class QueueScheduler implements Scheduler {
  public active: boolean = false;
  public actions: QueueAction<any>[] = [];
  public scheduledId: number = null;
 
  now() {
    return Date.now();
  }
 
  flush() {
    if (this.active || this.scheduledId) {
      return;
    }
    this.active = true;
    const actions = this.actions;
    for (let action: QueueAction<any>; action = actions.shift(); ) {
      action.execute();
    }
    this.active = false;
  }
 
  schedule<T>(work: (x?: any) => Subscription | void, delay: number = 0, state?: any): Subscription {
    return (delay <= 0) ?
      this.scheduleNow(work, state) :
      this.scheduleLater(work, delay, state);
  }
 
  scheduleNow<T>(work: (x?: any) => Subscription | void, state?: any): Action {
    return new QueueAction(this, work).schedule(state);
  }
 
  scheduleLater<T>(work: (x?: any) => Subscription | void, delay: number, state?: any): Action {
    return new FutureAction(this, work).schedule(state, delay);
  }
}