Skip to content

Commit

Permalink
Merge branch 'emmercm/3.0.0-feature' into emmercm/remove-prefer-ntsc-pal
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Aug 26, 2024
2 parents 49407c4 + 7aa70d6 commit 0d737ce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
7 changes: 3 additions & 4 deletions src/console/progressBarCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];

Expand Down Expand Up @@ -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
};

Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/console/singleBarFormatted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -28,7 +29,7 @@ export default class SingleBarFormatted {

private valueTimeBuffer: number[][] = [];

private lastEtaTime: [number, number] = [0, 0];
private lastEtaTime: number = 0;

private lastEtaValue = 'infinity';

Expand Down Expand Up @@ -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';
Expand Down
14 changes: 14 additions & 0 deletions test/polyfill/timePoly.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 0d737ce

Please sign in to comment.