all files / src/ scheduler.ts

100% Statements 89/89
76.47% Branches 26/34
100% Functions 14/14
100% Lines 78/78
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                                            15× 15× 13× 13×       15× 15× 15×                           16× 17× 17×                       13× 13×                                                      
/**
 * Created by thinhtran on 5/24/17.
 */
 
export interface IScheduleStep {
  at?: number;
  duration?: number;
  run: Function;
}
 
/**
 * Scheduler helps to call function with specific time and flexible configuration.
 */
export class Scheduler {
  endTime = 0;
  steps = [];
  startTime = 0;
  isRunning = false;
  timeouts = [];
  delay = 0;
  repeat = 0;
 
  constructor() {
 
  }
 
  /**
   *
   * @param delay milliseconds
   * @param repeat integer
   */
  public start(delay = 0, repeat = 0) {
    this.stop();
    this.delay = delay !== undefined ? delay : this.delay;
    this.repeat = repeat !== undefined ? repeat : this.repeat;
    this.isRunning = true;
    this.endTime = 0;
    for (let step of this.steps) {
      let timeout = setTimeout(() => {
        Eif (this.isRunning) {
          step.run();
        }
      }, this.delay + step.at);
 
      let duration = step.duration || 0;
      this.endTime = Math.max(this.endTime, this.delay + step.at + duration);
      this.timeouts.push(timeout);
    }
 
    this.scheduleEndStep();
 
    this.startTime = Date.now();
  }
 
  private scheduleEndStep() {
    let timeout = setTimeout(() => {
      this.repeat = this.repeat > 0 ? this.repeat - 1 : this.repeat;
      if (this.repeat > 0 || this.repeat === -1) {
        this.start();
      } else {
        this.stop();
      }
    }, this.endTime + 1);
    this.timeouts.push(timeout);
  }
 
  /**
   * Stop all scheduled steps
   */
  public stop(Eclean = true) {
    this.isRunning = false;
    Eif (clean) {
      for (let timeout of this.timeouts) {
        clearTimeout(timeout);
      }
    }
    this.timeouts = [];
    this.startTime = 0;
  }
 
  /**
   *
   * @param at - starting time (make sure: at - elapsed >= 10ms, which elapsed is elapsed time from starting)
   * @param run - executive function
   */
  public pushStep(at, run): void {
    let step: IScheduleStep = {at, run};
    this.steps.push(step);
    this.scheduleNewStep(step);
  }
 
  private scheduleNewStep(step: IScheduleStep): void {
    let elapsed = Date.now() - this.startTime;
    if (step.at > elapsed) {
      let timeout = setTimeout(step.run, step.at - elapsed);
      this.timeouts.push(timeout);
 
      let duration = step.duration || 0;
      let endTime = step.at - elapsed + duration;
      Eif (step.at > this.endTime) {
        Eif (this.timeouts.length > 1) {
          let endtimeout = this.timeouts.splice(this.timeouts.length - 2, 1)[0];
          clearTimeout(endtimeout);
        }
        this.endTime = endTime;
        this.scheduleEndStep();
      }
    }
  }
 
  public pushSteps(steps: Array<IScheduleStep>): void {
    Eif (steps && steps.length) {
      this.steps = this.steps.concat(steps);
      for (let step of steps) {
        this.scheduleNewStep(step);
      }
    }
  }
 
  public pushDurationSteps(durationSteps: Array<IScheduleStep>): void {
    let totalTime = 0;
    for (let durationStep of durationSteps) {
      let step = {at: totalTime, run: durationStep.run}
      totalTime += durationStep.duration;
      this.steps.push(step);
      this.scheduleNewStep(step);
    }
  }
}
 
export class SchedulerStatic {
 
  /**
   * Create new schedule with initial steps
   * @param steps - Syntax: [{at: <time at milliseconds>, run: <executive function>}, ...]
   * @returns Scheduler
   */
  public config(steps: Array<IScheduleStep>): Scheduler {
    let scheduler = new Scheduler();
    scheduler.pushSteps(steps);
    return scheduler;
  }
 
  /**
   *
   * @param durationSteps - Syntax: [{duration: <time at milliseconds>, run: <executive function>}, ...]
   * @returns {Scheduler}
   */
  public sequence(durationSteps: Array<IScheduleStep>): Scheduler {
    let scheduler = new Scheduler();
    scheduler.pushDurationSteps(durationSteps);
    return scheduler;
  }
}