Skip to content

Commit 34ca6a6

Browse files
vzaidmanmeta-codesync[bot]
authored andcommitted
sync bundling progress between Metro cli and app hints
Summary: Changelog: [Fix] sync bundling progress between Metro cli and app hints Reviewed By: hoxyq Differential Revision: D93737737 fbshipit-source-id: 683c70b1c73c6dfc2e20a9369ba5ca33f7c52f0b
1 parent 1f3e259 commit 34ca6a6

5 files changed

Lines changed: 76 additions & 33 deletions

File tree

packages/metro/src/Server.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import getRamBundleInfo from './DeltaBundler/Serializers/getRamBundleInfo';
5252
import {sourceMapStringNonBlocking} from './DeltaBundler/Serializers/sourceMapString';
5353
import IncrementalBundler from './IncrementalBundler';
5454
import ResourceNotFoundError from './IncrementalBundler/ResourceNotFoundError';
55+
import {calculateBundleProgressRatio} from './lib/bundleProgressUtils';
5556
import bundleToString from './lib/bundleToString';
5657
import formatBundlingError from './lib/formatBundlingError';
5758
import getGraphId from './lib/getGraphId';
@@ -857,25 +858,23 @@ export default class Server {
857858
const mres = MultipartResponse.wrapIfSupported(req, res);
858859

859860
let onProgress = null;
860-
let lastProgress = -1;
861+
let lastRatio = -1;
861862
if (this._config.reporter) {
862863
onProgress = (transformedFileCount: number, totalFileCount: number) => {
863-
const currentProgress = parseInt(
864-
(transformedFileCount / totalFileCount) * 100,
865-
10,
864+
const newRatio = calculateBundleProgressRatio(
865+
transformedFileCount,
866+
totalFileCount,
867+
lastRatio,
866868
);
867869

868-
// We want to throttle the updates so that we only show meaningful
869-
// UI updates slow enough for the client to actually handle them. For
870-
// that, we check the percentage, and only send percentages that are
871-
// actually different and that have increased from the last one we sent.
872-
if (currentProgress > lastProgress || totalFileCount < 10) {
870+
if (newRatio > lastRatio) {
873871
if (mres instanceof MultipartResponse) {
874872
mres.writeChunk(
875873
{'Content-Type': 'application/json'},
876874
JSON.stringify({
877875
done: transformedFileCount,
878876
total: totalFileCount,
877+
percent: Math.floor(newRatio * 100),
879878
}),
880879
);
881880
}
@@ -891,7 +890,7 @@ export default class Server {
891890
res.socket.uncork();
892891
}
893892

894-
lastProgress = currentProgress;
893+
lastRatio = newRatio;
895894
}
896895

897896
this._reporter.update({

packages/metro/src/lib/TerminalReporter.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {BundleDetails, ReportableEvent} from './reporting';
1313
import type {Terminal} from 'metro-core';
1414
import type {HealthCheckResult, WatcherStatus} from 'metro-file-map';
1515

16+
import {calculateBundleProgressRatio} from './bundleProgressUtils';
1617
import logToConsole from './logToConsole';
1718
import * as reporting from './reporting';
1819
import chalk from 'chalk';
@@ -132,7 +133,7 @@ export default class TerminalReporter {
132133
chalk.bgWhite.white(
133134
LIGHT_BLOCK_CHAR.repeat(MAX_PROGRESS_BAR_CHAR_WIDTH - filledBar),
134135
) +
135-
chalk.bold(` ${(100 * ratio).toFixed(1)}% `) +
136+
chalk.bold(` ${Math.floor(100 * ratio)}% `) +
136137
chalk.dim(`(${transformedFileCount}/${totalFileCount})`)
137138
: '';
138139

@@ -352,14 +353,6 @@ export default class TerminalReporter {
352353
});
353354
}
354355
355-
/**
356-
* Because we know the `totalFileCount` is going to progressively increase
357-
* starting with 1:
358-
* - We use Math.max(totalFileCount, 10) to prevent the ratio to raise too
359-
* quickly when the total file count is low. (e.g 1/2 5/6)
360-
* - We prevent the ratio from going backwards.
361-
* - Instead, we use Math.pow(ratio, 2) to as a conservative measure of progress.
362-
*/
363356
_updateBundleProgress({
364357
buildID,
365358
transformedFileCount,
@@ -375,12 +368,10 @@ export default class TerminalReporter {
375368
return;
376369
}
377370
378-
const ratio = Math.min(
379-
Math.max(
380-
Math.pow(transformedFileCount / Math.max(totalFileCount, 10), 2),
381-
currentProgress.ratio,
382-
),
383-
0.999, // make sure not to go above 99.9% to not get rounded to 100%,
371+
const ratio = calculateBundleProgressRatio(
372+
transformedFileCount,
373+
totalFileCount,
374+
currentProgress.ratio,
384375
);
385376
386377
// $FlowFixMe[unsafe-object-assign]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
/**
13+
* Calculates a conservative progress ratio for bundle building.
14+
*
15+
* Because we know the `totalFileCount` is going to progressively increase
16+
* starting with 1:
17+
* - We use Math.max(totalFileCount, 10) to prevent the ratio from raising too
18+
* quickly when the total file count is low. (e.g 1/2 5/6)
19+
* - We use Math.pow(ratio, 2) as a conservative measure of progress.
20+
* - The ratio is capped at 0.999 to ensure we don't display 100% until done.
21+
* - If previousRatio is provided, the ratio will not go backwards.
22+
*/
23+
export function calculateBundleProgressRatio(
24+
transformedFileCount: number,
25+
totalFileCount: number,
26+
previousRatio?: number,
27+
): number {
28+
const baseRatio = Math.pow(
29+
transformedFileCount / Math.max(totalFileCount, 10),
30+
2,
31+
);
32+
const ratio =
33+
previousRatio != null ? Math.max(baseRatio, previousRatio) : baseRatio;
34+
return Math.min(ratio, 0.999);
35+
}

packages/metro/types/lib/TerminalReporter.d.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,6 @@ declare class TerminalReporter {
8989
*/
9090
_logBundlingError(error: SnippetError): void;
9191
_logWorkerChunk(origin: 'stdout' | 'stderr', chunk: string): void;
92-
/**
93-
* Because we know the `totalFileCount` is going to progressively increase
94-
* starting with 1:
95-
* - We use Math.max(totalFileCount, 10) to prevent the ratio to raise too
96-
* quickly when the total file count is low. (e.g 1/2 5/6)
97-
* - We prevent the ratio from going backwards.
98-
* - Instead, we use Math.pow(ratio, 2) to as a conservative measure of progress.
99-
*/
10092
_updateBundleProgress($$PARAM_0$$: {
10193
buildID: string;
10294
transformedFileCount: number;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @oncall react_native
9+
*/
10+
11+
/**
12+
* Calculates a conservative progress ratio for bundle building.
13+
*
14+
* Because we know the `totalFileCount` is going to progressively increase
15+
* starting with 1:
16+
* - We use Math.max(totalFileCount, 10) to prevent the ratio from raising too
17+
* quickly when the total file count is low. (e.g 1/2 5/6)
18+
* - We use Math.pow(ratio, 2) as a conservative measure of progress.
19+
* - The ratio is capped at 0.999 to ensure we don't display 100% until done.
20+
* - If previousRatio is provided, the ratio will not go backwards.
21+
*/
22+
export declare function calculateBundleProgressRatio(
23+
transformedFileCount: number,
24+
totalFileCount: number,
25+
previousRatio?: number,
26+
): number;

0 commit comments

Comments
 (0)