tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

current-time-timer.js (2194B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 /**
      8 * In animation inspector, the scrubber and the progress bar moves along the current time
      9 * of animation. However, the processing which sync with actual animations is heavy since
     10 * we have to communication by the actor. The role of this class is to make the pseudo
     11 * current time in animation inspector to proceed.
     12 */
     13 class CurrentTimeTimer {
     14  /**
     15   * Constructor.
     16   *
     17   * @param {object} timeScale
     18   * @param {Bool} shouldStopAfterEndTime
     19   *               If need to stop the timer after animation end time, set true.
     20   * @param {window} win
     21   *                 Be used for requestAnimationFrame and performance.
     22   * @param {Function} onUpdated
     23   *                   Listener function to get updating.
     24   *                   This function is called with 2 parameters.
     25   *                   1st: current time
     26   *                   2nd: if shouldStopAfterEndTime is true and
     27   *                        the current time is over the end time, true is given.
     28   */
     29  constructor(timeScale, shouldStopAfterEndTime, win, onUpdated) {
     30    this.baseCurrentTime = timeScale.getCurrentTime();
     31    this.endTime = timeScale.getDuration();
     32    this.timerStartTime = win.performance.now();
     33 
     34    this.shouldStopAfterEndTime = shouldStopAfterEndTime;
     35    this.onUpdated = onUpdated;
     36    this.win = win;
     37    this.next = this.next.bind(this);
     38  }
     39 
     40  destroy() {
     41    this.stop();
     42    this.onUpdated = null;
     43    this.win = null;
     44  }
     45 
     46  /**
     47   * Proceed the pseudo current time.
     48   */
     49  next() {
     50    if (this.doStop) {
     51      return;
     52    }
     53 
     54    const currentTime =
     55      this.baseCurrentTime + this.win.performance.now() - this.timerStartTime;
     56 
     57    if (this.endTime < currentTime && this.shouldStopAfterEndTime) {
     58      this.onUpdated(this.endTime, true);
     59      return;
     60    }
     61 
     62    this.onUpdated(currentTime);
     63    this.win.requestAnimationFrame(this.next);
     64  }
     65 
     66  start() {
     67    this.next();
     68  }
     69 
     70  stop() {
     71    this.doStop = true;
     72  }
     73 }
     74 
     75 module.exports = CurrentTimeTimer;