diff --git a/src/console/progressBarCli.ts b/src/console/progressBarCli.ts index e6d4dcec9..441ec4c7e 100644 --- a/src/console/progressBarCli.ts +++ b/src/console/progressBarCli.ts @@ -25,7 +25,7 @@ export default class ProgressBarCLI extends ProgressBar { private static progressBars: ProgressBarCLI[] = []; - private static lastRedraw: [number, number] = [0, 0]; + private static lastRedraw: number = 0; private static logQueue: string[] = []; @@ -142,7 +142,7 @@ export default class ProgressBarCLI extends ProgressBar { } ProgressBarCLI.multiBar?.update(); - ProgressBarCLI.lastRedraw = process.hrtime(); + ProgressBarCLI.lastRedraw = TimePoly.hrtimeMillis(); ProgressBarCLI.RENDER_MUTEX.cancel(); // cancel all waiting locks, we just redrew }; @@ -152,8 +152,7 @@ export default class ProgressBarCLI extends ProgressBar { } // Limit the frequency of redrawing - const [elapsedSec, elapsedNano] = process.hrtime(ProgressBarCLI.lastRedraw); - const elapsedMs = (elapsedSec * 1_000_000_000 + elapsedNano) / 1_000_000; + const elapsedMs = TimePoly.hrtimeMillis(ProgressBarCLI.lastRedraw); if (elapsedMs < (1000 / ProgressBarCLI.FPS)) { return; } diff --git a/src/console/singleBarFormatted.ts b/src/console/singleBarFormatted.ts index 58bbd9029..cf94b62c6 100644 --- a/src/console/singleBarFormatted.ts +++ b/src/console/singleBarFormatted.ts @@ -6,6 +6,7 @@ import { linearRegression, linearRegressionLine } from 'simple-statistics'; import stripAnsi from 'strip-ansi'; import ConsolePoly from '../polyfill/consolePoly.js'; +import TimePoly from '../polyfill/timePoly.js'; import ProgressBarPayload from './progressBarPayload.js'; /** @@ -28,7 +29,7 @@ export default class SingleBarFormatted { private valueTimeBuffer: number[][] = []; - private lastEtaTime: [number, number] = [0, 0]; + private lastEtaTime: number = 0; private lastEtaValue = 'infinity'; @@ -165,12 +166,11 @@ export default class SingleBarFormatted { private getEtaFormatted(etaSeconds: number): string { // Rate limit how often the ETA can change // Update only every 5s if the ETA is >60s - const [elapsedSec, elapsedNano] = process.hrtime(this.lastEtaTime); - const elapsedMs = (elapsedSec * 1_000_000_000 + elapsedNano) / 1_000_000; + const elapsedMs = TimePoly.hrtimeMillis(this.lastEtaTime); if (etaSeconds > 60 && elapsedMs < 5000) { return this.lastEtaValue; } - this.lastEtaTime = process.hrtime(); + this.lastEtaTime = TimePoly.hrtimeMillis(); if (etaSeconds < 0) { this.lastEtaValue = 'infinity'; diff --git a/test/polyfill/timePoly.test.ts b/test/polyfill/timePoly.test.ts new file mode 100644 index 000000000..27468e16e --- /dev/null +++ b/test/polyfill/timePoly.test.ts @@ -0,0 +1,14 @@ +import TimePoly from '../../src/polyfill/timePoly.js'; + +describe('hrtimeMillis', () => { + test.each([ + [10], + [100], + [1000], + ])('should calculate the difference for %s ms', async (timeout) => { + const before = TimePoly.hrtimeMillis(); + await new Promise((resolve) => { setTimeout(resolve, timeout); }); + const after = TimePoly.hrtimeMillis(before); + expect(after).toBeGreaterThanOrEqual(timeout); + }); +});