Skip to content

Commit 6d83a76

Browse files
committed
Add duration property to Stopwatch class
1 parent 29223e2 commit 6d83a76

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

apps/rush-lib/src/utilities/Stopwatch.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ export class Stopwatch {
8181
if (this._state === StopwatchState.Stopped && this._startTime === undefined) {
8282
return '0.00 seconds (stopped)';
8383
}
84-
85-
const curTime: number = this._endTime !== undefined
86-
? this._endTime
87-
: this._getTime();
88-
89-
const totalSeconds: number = ((curTime - this._startTime) / 1000.0);
84+
const totalSeconds: number = this._getTotalSeconds();
9085

9186
if (totalSeconds > 60) {
9287
const minutes: number = Math.floor(totalSeconds / 60);
@@ -98,4 +93,22 @@ export class Stopwatch {
9893
return `${totalSeconds.toFixed(2)} seconds`;
9994
}
10095
}
96+
97+
/**
98+
* Get the duration in seconds.
99+
*/
100+
public get duration(): number {
101+
return this._getTotalSeconds();
102+
}
103+
104+
private _getTotalSeconds(): number {
105+
if (this._startTime === undefined) {
106+
return 0;
107+
}
108+
const curTime: number = this._endTime !== undefined
109+
? this._endTime
110+
: this._getTime();
111+
112+
return ((curTime - this._startTime) / 1000.0);
113+
}
101114
}

apps/rush-lib/src/utilities/test/Stopwatch.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ describe('Stopwatch', () => {
5454
watch.start();
5555
watch.reset();
5656
assert.equal(watch.toString(), '0.00 seconds (stopped)');
57+
assert.equal(watch.duration, 0);
5758
done();
5859
});
5960

6061
it('gives 0.00 seconds when not running', (done: MochaDone) => {
6162
const watch: Stopwatch = new Stopwatch();
6263
assert.equal(watch.toString(), '0.00 seconds (stopped)');
64+
assert.equal(watch.duration, 0);
6365
done();
6466
});
6567

@@ -103,4 +105,26 @@ describe('Stopwatch', () => {
103105
assert.equal(watch.toString(), '1 minute 1.3 seconds');
104106
done();
105107
});
108+
109+
it('uses the latest time when the clock is not stopped', (done: MochaDone) => {
110+
const watch: Stopwatch = new Stopwatch(pseudoTimeSeconds([0, 1, 2]));
111+
watch.start();
112+
assert.equal(watch.toString(), '1.00 seconds');
113+
assert.equal(watch.toString(), '2.00 seconds');
114+
done();
115+
});
116+
117+
it('returns duration when the clock is stopped', () => {
118+
const watch: Stopwatch = new Stopwatch(pseudoTimeSeconds([0, 61.25]));
119+
watch.start();
120+
watch.stop();
121+
assert.equal(watch.duration, 61.25);
122+
});
123+
124+
it('returns duration using the latest time when the clock is not stopped', () => {
125+
const watch: Stopwatch = new Stopwatch(pseudoTimeSeconds([0, 1, 2]));
126+
watch.start();
127+
assert.equal(watch.duration, 1);
128+
assert.equal(watch.duration, 2);
129+
});
106130
});

common/reviews/api/rush-lib.api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ module RushConstants {
271271
class Stopwatch {
272272
// (undocumented)
273273
constructor(getTime: () => number = Utilities.getTimeInMs);
274+
public readonly duration: number;
274275
public reset(): Stopwatch;
275276
public static start(): Stopwatch;
276277
// (undocumented)

0 commit comments

Comments
 (0)