2016-04-18 17:20:30 -04:00
|
|
|
/**
|
2016-09-12 13:09:01 -04:00
|
|
|
* @fileoverview
|
|
|
|
* A utility for accurately measuring time.
|
|
|
|
* To use:
|
|
|
|
* ---
|
|
|
|
* var timer = new Timer();
|
|
|
|
* timer.start();
|
|
|
|
* ... pass some time ...
|
|
|
|
* var timeDifference = timer.timeElapsed();
|
|
|
|
* ---
|
2020-05-18 01:39:39 -04:00
|
|
|
* Or, you can use the `time` function
|
2016-09-12 13:09:01 -04:00
|
|
|
* to do some measurement yourself.
|
2016-04-18 17:20:30 -04:00
|
|
|
*/
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
class Timer {
|
2018-12-04 10:35:42 -05:00
|
|
|
constructor (nowObj = Timer.nowObj) {
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Used to store the start time of a timer action.
|
|
|
|
* Updated when calling `timer.start`.
|
|
|
|
*/
|
|
|
|
this.startTime = 0;
|
2018-12-04 10:35:42 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to pass custom logic for determining the value for "now",
|
|
|
|
* which is sometimes useful for compatibility with Scratch 2
|
|
|
|
*/
|
|
|
|
this.nowObj = nowObj;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-09-12 13:09:01 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Disable use of self.performance for now as it results in lower performance
|
|
|
|
* However, instancing it like below (caching the self.performance to a local variable) negates most of the issues.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
static get USE_PERFORMANCE () {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-08 03:11:10 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Legacy object to allow for us to call now to get the old style date time (for backwards compatibility)
|
|
|
|
* @deprecated This is only called via the nowObj.now() if no other means is possible...
|
|
|
|
*/
|
|
|
|
static get legacyDateCode () {
|
|
|
|
return {
|
|
|
|
now: function () {
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
};
|
2017-02-08 03:11:10 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Use this object to route all time functions through single access points.
|
|
|
|
*/
|
|
|
|
static get nowObj () {
|
|
|
|
if (Timer.USE_PERFORMANCE && typeof self !== 'undefined' && self.performance && 'now' in self.performance) {
|
|
|
|
return self.performance;
|
|
|
|
} else if (Date.now) {
|
|
|
|
return Date;
|
|
|
|
}
|
|
|
|
return Timer.legacyDateCode;
|
|
|
|
}
|
2017-02-08 03:11:10 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return the currently known absolute time, in ms precision.
|
|
|
|
* @returns {number} ms elapsed since 1 January 1970 00:00:00 UTC.
|
|
|
|
*/
|
|
|
|
time () {
|
2018-12-04 10:35:42 -05:00
|
|
|
return this.nowObj.now();
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-04-18 17:20:30 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Start a timer for measuring elapsed time,
|
|
|
|
* at the most accurate precision possible.
|
|
|
|
*/
|
|
|
|
start () {
|
2018-12-04 10:35:42 -05:00
|
|
|
this.startTime = this.nowObj.now();
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-04-18 17:20:30 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
timeElapsed () {
|
2018-12-04 10:35:42 -05:00
|
|
|
return this.nowObj.now() - this.startTime;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2019-02-07 19:03:45 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call a handler function after a specified amount of time has elapsed.
|
|
|
|
* @param {function} handler - function to call after the timeout
|
|
|
|
* @param {number} timeout - number of milliseconds to delay before calling the handler
|
2019-02-11 15:14:44 -05:00
|
|
|
* @returns {number} - the ID of the new timeout
|
2019-02-07 19:03:45 -05:00
|
|
|
*/
|
|
|
|
setTimeout (handler, timeout) {
|
2019-02-11 15:14:44 -05:00
|
|
|
return global.setTimeout(handler, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear a timeout from the pending timeout pool.
|
|
|
|
* @param {number} timeoutId - the ID returned by `setTimeout()`
|
|
|
|
* @memberof Timer
|
|
|
|
*/
|
|
|
|
clearTimeout (timeoutId) {
|
|
|
|
global.clearTimeout(timeoutId);
|
2019-02-07 19:03:45 -05:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-04-18 17:20:30 -04:00
|
|
|
|
|
|
|
module.exports = Timer;
|