Debounce progress updates

Summary: This shows at max 5 progress updates per second on the terminal, which results in a minor speedup of reloads.

Reviewed By: bestander

Differential Revision: D3620164

fbshipit-source-id: d1a30f2f29f7088602d276b8ad3fc8ff1b74c79d
This commit is contained in:
David Aurelio 2016-07-26 07:15:42 -07:00 committed by Facebook Github Bot 3
parent a2fb703bbb
commit fe8eca43ac
1 changed files with 26 additions and 8 deletions

View File

@ -351,14 +351,13 @@ class Bundler {
if (!resolutionResponse) {
let onProgress = noop;
if (process.stdout.isTTY && !this._opts.silent) {
const bar = new ProgressBar(
'transformed :current/:total (:percent)',
{complete: '=', incomplete: ' ', width: 40, total: 1},
);
onProgress = (_, total) => {
bar.total = total;
bar.tick();
};
const bar = new ProgressBar('transformed :current/:total (:percent)', {
complete: '=',
incomplete: ' ',
width: 40,
total: 1,
});
onProgress = debouncedTick(bar);
}
resolutionResponse = this.getDependencies({
@ -708,4 +707,23 @@ function getMainModule({dependencies, numPrependedDependencies = 0}) {
return dependencies[numPrependedDependencies];
}
function debouncedTick(progressBar) {
let n = 0;
let start, total;
return (_, t) => {
total = t;
n += 1;
if (start) {
if (progressBar.curr + n >= total || Date.now() - start > 200) {
progressBar.total = total;
progressBar.tick(n);
start = n = 0;
}
} else {
start = Date.now();
}
};
}
module.exports = Bundler;