@rushstack/operation-graph
Version:
Library for managing and executing operations in a directed acyclic graph.
93 lines • 2.76 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
/**
* Represents a typical timer/stopwatch which keeps track
* of elapsed time in between two events.
*
* @public
*/
export class Stopwatch {
constructor() {
this._startTime = undefined;
this._endTime = undefined;
this._running = false;
}
/**
* Static helper function which creates a stopwatch which is immediately started
*/
static start() {
return new Stopwatch().start();
}
get isRunning() {
return this._running;
}
/**
* Starts the stopwatch. Note that if end() has been called,
* reset() should be called before calling start() again.
*/
start() {
if (this._startTime !== undefined) {
throw new Error('Call reset() before starting the Stopwatch');
}
this._startTime = performance.now();
this._endTime = undefined;
this._running = true;
return this;
}
/**
* Stops executing the stopwatch and saves the current timestamp
*/
stop() {
this._endTime = this._startTime !== undefined ? performance.now() : undefined;
this._running = false;
return this;
}
/**
* Resets all values of the stopwatch back to the original
*/
reset() {
this._endTime = this._startTime = undefined;
this._running = false;
return this;
}
/**
* Displays how long the stopwatch has been executing in a human readable format.
*/
toString() {
if (!this._running && this._startTime === undefined) {
return '0.00 seconds (stopped)';
}
const totalSeconds = this.duration;
if (totalSeconds > 60) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60.0;
return `${minutes.toFixed(0)} minute${minutes === 1 ? '' : 's'} ${seconds.toFixed(1)} seconds`;
}
else {
return `${totalSeconds.toFixed(2)} seconds`;
}
}
/**
* Get the duration in seconds.
*/
get duration() {
if (this._startTime === undefined) {
return 0;
}
const curTime = this._endTime !== undefined ? this._endTime : performance.now();
return (curTime - this._startTime) / 1000.0;
}
/**
* Return the start time of the most recent stopwatch run.
*/
get startTime() {
return this._startTime;
}
/**
* Return the end time of the most recent stopwatch run.
*/
get endTime() {
return this._endTime;
}
}
//# sourceMappingURL=Stopwatch.js.map