Skip to content

Commit c7f0b43

Browse files
committed
Flush performance track once we have no more pending chunks
1 parent 7cafeff commit c7f0b43

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ type Response = {
351351
_closedReason: mixed,
352352
_tempRefs: void | TemporaryReferenceSet, // the set temporary references can be resolved from
353353
_timeOrigin: number, // Profiling-only
354+
_pendingInitialRender: null | TimeoutID, // Profiling-only,
354355
_pendingChunks: number, // DEV-only
355356
_weakResponse: WeakResponse, // DEV-only
356357
_debugRootOwner?: null | ReactComponentInfo, // DEV-only
@@ -444,8 +445,13 @@ export function getRoot<T>(weakResponse: WeakResponse): Thenable<T> {
444445
function createPendingChunk<T>(response: Response): PendingChunk<T> {
445446
if (__DEV__) {
446447
// Retain a strong reference to the Response while we wait for the result.
447-
response._pendingChunks++;
448-
response._weakResponse.response = response;
448+
if (response._pendingChunks++ === 0) {
449+
response._weakResponse.response = response;
450+
if (response._pendingInitialRender !== null) {
451+
clearTimeout(response._pendingInitialRender);
452+
response._pendingInitialRender = null;
453+
}
454+
}
449455
}
450456
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
451457
return new ReactPromise(PENDING, null, null);
@@ -457,6 +463,14 @@ function releasePendingChunk(response: Response, chunk: SomeChunk<any>): void {
457463
// We're no longer waiting for any more chunks. We can release the strong reference
458464
// to the response. We'll regain it if we ask for any more data later on.
459465
response._weakResponse.response = null;
466+
// Wait a short period to see if any more chunks get asked for. E.g. by a React render.
467+
// These chunks might discover more pending chunks.
468+
// If we don't ask for more then we assume that those chunks weren't blocking initial
469+
// render and are excluded from the performance track.
470+
response._pendingInitialRender = setTimeout(
471+
flushInitialRenderPerformance.bind(null, response),
472+
100,
473+
);
460474
}
461475
}
462476
}
@@ -868,18 +882,6 @@ export function reportGlobalError(
868882
response._debugChannel = undefined;
869883
}
870884
}
871-
if (enableProfilerTimer && enableComponentPerformanceTrack) {
872-
if (response._replayConsole) {
873-
markAllTracksInOrder();
874-
flushComponentPerformance(
875-
response,
876-
getChunk(response, 0),
877-
0,
878-
-Infinity,
879-
-Infinity,
880-
);
881-
}
882-
}
883885
}
884886

885887
function nullRefGetter() {
@@ -2105,6 +2107,7 @@ function ResponseInstance(
21052107
this._tempRefs = temporaryReferences;
21062108
if (enableProfilerTimer && enableComponentPerformanceTrack) {
21072109
this._timeOrigin = 0;
2110+
this._pendingInitialRender = null;
21082111
}
21092112
if (__DEV__) {
21102113
this._pendingChunks = 0;
@@ -3703,6 +3706,20 @@ function flushComponentPerformance(
37033706
return result;
37043707
}
37053708

3709+
function flushInitialRenderPerformance(response: Response): void {
3710+
if (
3711+
enableProfilerTimer &&
3712+
enableComponentPerformanceTrack &&
3713+
response._replayConsole
3714+
) {
3715+
const rootChunk = getChunk(response, 0);
3716+
if (isArray(rootChunk._children)) {
3717+
markAllTracksInOrder();
3718+
flushComponentPerformance(response, rootChunk, 0, -Infinity, -Infinity);
3719+
}
3720+
}
3721+
}
3722+
37063723
function processFullBinaryRow(
37073724
response: Response,
37083725
id: number,

0 commit comments

Comments
 (0)