All files / src/time timer.ts

97.05% Statements 33/34
71.42% Branches 10/14
90% Functions 9/10
97.05% Lines 33/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                      1x 1x 1x 1x 1x                                                                                                           3x 3x 3x   3x 3x                 1x     7x 4x     7x 7x 7x       8x 6x     6x 4x   2x           1x   1x 1x   1x         1x 1x 1x 1x       3x               2x 2x 2x        
import {type EventDispatcher, EventManager} from '../ui/events';
import * as time from './time';
 
export interface TimerConfig {
  // Duration in ms.
  duration: number;
  // Whether to repeat the timer.  If false, runs
  // only once.  Defaults to false.
  repeat?: boolean;
}
 
enum TimerEvent {
  START = 'START',
  PAUSED = 'PAUSED',
  UNPAUSED = 'UNPAUSED',
  TRIGGERED = 'TRIGGERED',
}
 
/**
 * A subscribable timer.
 *
 * Basic Usage:
 *
 * ```ts
 * const myTimer = new Timer({
 *    duration: 5000,
 *    repeat: false
 * })
 *
 * // Called on timer is triggered
 * myTimer.on(Timer.Event.TRIGGERED, ()=> {
 *    console.log('timer is done');
 * })
 *
 * // Start it.
 * myTimer.start();
 *
 * // Later dispose it.
 * myTimer.dispose();
 * ```
 *
 * Advanced Usage - repeating timer and pause / unpausing.
 *
 * ```ts
 * const myTimer = new Timer({
 *    duration: 5000,
 *    repeat: true
 * })
 *
 * // Called every 5000ms.
 * myTimer.on(Timer.Event.TRIGGERED, ()=> {
 *    console.log('timer is done');
 * })
 *
 * myTimer.start();
 *
 * // Later
 * myTimer.pause();
 * myTimer.unpause();
 *
 * // Call this on page disposal to prevent any timers running later.
 * myTimer.dispose();
 *
 * ```
 *
 */
export class Timer implements EventDispatcher {
  private eventManager: EventManager;
  private settings: TimerConfig;
  private timerId: number | null = null;
  private startTime: number | null = null;
  private timeRemaining: number | null = null;
  constructor(config?: TimerConfig) {
    this.eventManager = new EventManager();
    this.settings = {
      ...{
        duration: 5000,
        repeat: false,
      },
      ...(config || {}),
    };
  }
 
  static Event = TimerEvent;
 
  start() {
    if (this.timerId) {
      window.clearTimeout(this.timerId);
    }
 
    this.startTime = Date.now();
    this.eventManager.dispatch(Timer.Event.START);
    this.createTimer(this.settings.duration);
  }
 
  private createTimer(duration: number) {
    this.timerId = window.setTimeout(() => {
      this.eventManager.dispatch(Timer.Event.TRIGGERED);
 
      // If repeating start again.
      if (this.settings.repeat) {
        this.start();
      } else {
        this.dispose();
      }
    }, duration);
  }
 
  pause() {
    this.eventManager.dispatch(Timer.Event.PAUSED);
 
    Eif (this.timerId) {
      window.clearTimeout(this.timerId);
    }
    this.timeRemaining =
      this.settings.duration - (Date.now() - (this.startTime || 0));
  }
 
  unpause() {
    const timePast = this.settings.duration - this.timeRemaining!;
    this.startTime = Date.now() - timePast;
    this.eventManager.dispatch(Timer.Event.UNPAUSED);
    this.createTimer(this.timeRemaining!);
  }
 
  on(event: string, callback: Function) {
    this.eventManager.on(event, callback);
  }
 
  off(event: string, callback: Function) {
    this.eventManager.off(event, callback);
  }
 
  dispose() {
    this.eventManager.dispose();
    Eif (this.timerId) {
      window.clearTimeout(this.timerId);
    }
  }
}