| 1 |
/*--------------------------------------------------------------------------------------------- |
| 2 |
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
* Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
*--------------------------------------------------------------------------------------------*/ |
| 5 |
|
| 6 |
// fake definition so that the valid layers check won't trip on this |
| 7 |
declare const globalThis: { performance?: { now(): number } }; |
| 8 |
|
| 9 |
const hasPerformanceNow = (globalThis.performance && typeof globalThis.performance.now === 'function'); |
| 10 |
|
| 11 |
export class StopWatch { |
| 12 |
|
| 13 |
private _startTime: number; |
| 14 |
private _stopTime: number; |
| 15 |
|
| 16 |
private readonly _now: () => number; |
| 17 |
|
| 18 |
public static create(highResolution?: boolean): StopWatch { |
| 19 |
return new StopWatch(highResolution); |
| 20 |
} |
| 21 |
|
| 22 |
constructor(highResolution?: boolean) { |
| 23 |
this._now = hasPerformanceNow && highResolution === false ? Date.now : globalThis.performance!.now.bind(globalThis.performance); |
| 24 |
this._startTime = this._now(); |
| 25 |
this._stopTime = -1; |
| 26 |
} |
| 27 |
|
| 28 |
public stop(): void { |
| 29 |
this._stopTime = this._now(); |
| 30 |
} |
| 31 |
|
| 32 |
public reset(): void { |
| 33 |
this._startTime = this._now(); |
| 34 |
this._stopTime = -1; |
| 35 |
} |
| 36 |
|
| 37 |
public elapsed(): number { |
| 38 |
if (this._stopTime !== -1) { |
| 39 |
return this._stopTime - this._startTime; |
| 40 |
} |
| 41 |
return this._now() - this._startTime; |
| 42 |
} |
| 43 |
} |