From e0f6dfdadf10f928a35114d5ab4b11a3b9dad81c Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Tue, 26 Jul 2016 07:15:42 -0700 Subject: [PATCH] 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 --- react-packager/src/Bundler/index.js | 34 ++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/react-packager/src/Bundler/index.js b/react-packager/src/Bundler/index.js index 8ee861d2..8e7f21cd 100644 --- a/react-packager/src/Bundler/index.js +++ b/react-packager/src/Bundler/index.js @@ -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;