Skip to content

Commit 294c33f

Browse files
authored
[Flight] Always initialize a debug info array for each Chunk (#34419)
I'm about to add info for pretty much all of these anyway since they all depend on the data stream itself.
1 parent 3fb190f commit 294c33f

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ type PendingChunk<T> = {
169169
reason: null | Array<InitializationReference | (mixed => mixed)>,
170170
_children: Array<SomeChunk<any>> | ProfilingResult, // Profiling-only
171171
_debugChunk: null | SomeChunk<ReactDebugInfoEntry>, // DEV-only
172-
_debugInfo: null | ReactDebugInfo, // DEV-only
172+
_debugInfo: ReactDebugInfo, // DEV-only
173173
then(resolve: (T) => mixed, reject?: (mixed) => mixed): void,
174174
};
175175
type BlockedChunk<T> = {
@@ -178,7 +178,7 @@ type BlockedChunk<T> = {
178178
reason: null | Array<InitializationReference | (mixed => mixed)>,
179179
_children: Array<SomeChunk<any>> | ProfilingResult, // Profiling-only
180180
_debugChunk: null, // DEV-only
181-
_debugInfo: null | ReactDebugInfo, // DEV-only
181+
_debugInfo: ReactDebugInfo, // DEV-only
182182
then(resolve: (T) => mixed, reject?: (mixed) => mixed): void,
183183
};
184184
type ResolvedModelChunk<T> = {
@@ -187,7 +187,7 @@ type ResolvedModelChunk<T> = {
187187
reason: Response,
188188
_children: Array<SomeChunk<any>> | ProfilingResult, // Profiling-only
189189
_debugChunk: null | SomeChunk<ReactDebugInfoEntry>, // DEV-only
190-
_debugInfo: null | ReactDebugInfo, // DEV-only
190+
_debugInfo: ReactDebugInfo, // DEV-only
191191
then(resolve: (T) => mixed, reject?: (mixed) => mixed): void,
192192
};
193193
type ResolvedModuleChunk<T> = {
@@ -196,7 +196,7 @@ type ResolvedModuleChunk<T> = {
196196
reason: null,
197197
_children: Array<SomeChunk<any>> | ProfilingResult, // Profiling-only
198198
_debugChunk: null, // DEV-only
199-
_debugInfo: null | ReactDebugInfo, // DEV-only
199+
_debugInfo: ReactDebugInfo, // DEV-only
200200
then(resolve: (T) => mixed, reject?: (mixed) => mixed): void,
201201
};
202202
type InitializedChunk<T> = {
@@ -205,7 +205,7 @@ type InitializedChunk<T> = {
205205
reason: null | FlightStreamController,
206206
_children: Array<SomeChunk<any>> | ProfilingResult, // Profiling-only
207207
_debugChunk: null, // DEV-only
208-
_debugInfo: null | ReactDebugInfo, // DEV-only
208+
_debugInfo: ReactDebugInfo, // DEV-only
209209
then(resolve: (T) => mixed, reject?: (mixed) => mixed): void,
210210
};
211211
type InitializedStreamChunk<
@@ -216,7 +216,7 @@ type InitializedStreamChunk<
216216
reason: FlightStreamController,
217217
_children: Array<SomeChunk<any>> | ProfilingResult, // Profiling-only
218218
_debugChunk: null, // DEV-only
219-
_debugInfo: null | ReactDebugInfo, // DEV-only
219+
_debugInfo: ReactDebugInfo, // DEV-only
220220
then(resolve: (ReadableStream) => mixed, reject?: (mixed) => mixed): void,
221221
};
222222
type ErroredChunk<T> = {
@@ -225,7 +225,7 @@ type ErroredChunk<T> = {
225225
reason: mixed,
226226
_children: Array<SomeChunk<any>> | ProfilingResult, // Profiling-only
227227
_debugChunk: null, // DEV-only
228-
_debugInfo: null | ReactDebugInfo, // DEV-only
228+
_debugInfo: ReactDebugInfo, // DEV-only
229229
then(resolve: (T) => mixed, reject?: (mixed) => mixed): void,
230230
};
231231
type HaltedChunk<T> = {
@@ -234,7 +234,7 @@ type HaltedChunk<T> = {
234234
reason: null,
235235
_children: Array<SomeChunk<any>> | ProfilingResult, // Profiling-only
236236
_debugChunk: null, // DEV-only
237-
_debugInfo: null | ReactDebugInfo, // DEV-only
237+
_debugInfo: ReactDebugInfo, // DEV-only
238238
then(resolve: (T) => mixed, reject?: (mixed) => mixed): void,
239239
};
240240
type SomeChunk<T> =
@@ -256,7 +256,7 @@ function ReactPromise(status: any, value: any, reason: any) {
256256
}
257257
if (__DEV__) {
258258
this._debugChunk = null;
259-
this._debugInfo = null;
259+
this._debugInfo = [];
260260
}
261261
}
262262
// We subclass Promise.prototype so that we get other methods like .catch
@@ -798,12 +798,10 @@ function resolveModuleChunk<T>(
798798
resolvedChunk.value = value;
799799
if (__DEV__) {
800800
const debugInfo = getModuleDebugInfo(value);
801-
if (debugInfo !== null && resolvedChunk._debugInfo != null) {
801+
if (debugInfo !== null) {
802802
// Add to the live set if it was already initialized.
803803
// $FlowFixMe[method-unbinding]
804804
resolvedChunk._debugInfo.push.apply(resolvedChunk._debugInfo, debugInfo);
805-
} else {
806-
resolvedChunk._debugInfo = debugInfo;
807805
}
808806
}
809807
if (resolveListeners !== null) {
@@ -842,7 +840,7 @@ function initializeDebugChunk(
842840
): void {
843841
const debugChunk = chunk._debugChunk;
844842
if (debugChunk !== null) {
845-
const debugInfo = chunk._debugInfo || (chunk._debugInfo = []);
843+
const debugInfo = chunk._debugInfo;
846844
try {
847845
if (debugChunk.status === RESOLVED_MODEL) {
848846
// Find the index of this debug info by walking the linked list.
@@ -1303,10 +1301,8 @@ function createLazyChunkWrapper<T>(
13031301
_init: readChunk,
13041302
};
13051303
if (__DEV__) {
1306-
// Ensure we have a live array to track future debug info.
1307-
const chunkDebugInfo: ReactDebugInfo =
1308-
chunk._debugInfo || (chunk._debugInfo = ([]: ReactDebugInfo));
1309-
lazyType._debugInfo = chunkDebugInfo;
1304+
// Forward the live array
1305+
lazyType._debugInfo = chunk._debugInfo;
13101306
// Initialize a store for key validation by the JSX runtime.
13111307
lazyType._store = {validated: validated};
13121308
}
@@ -1508,9 +1504,7 @@ function rejectReference(
15081504
// $FlowFixMe[cannot-write]
15091505
erroredComponent.debugTask = element._debugTask;
15101506
}
1511-
const chunkDebugInfo: ReactDebugInfo =
1512-
chunk._debugInfo || (chunk._debugInfo = []);
1513-
chunkDebugInfo.push(erroredComponent);
1507+
chunk._debugInfo.push(erroredComponent);
15141508
}
15151509
}
15161510

@@ -1750,9 +1744,7 @@ function loadServerReference<A: Iterable<any>, T>(
17501744
// $FlowFixMe[cannot-write]
17511745
erroredComponent.debugTask = element._debugTask;
17521746
}
1753-
const chunkDebugInfo: ReactDebugInfo =
1754-
chunk._debugInfo || (chunk._debugInfo = []);
1755-
chunkDebugInfo.push(erroredComponent);
1747+
chunk._debugInfo.push(erroredComponent);
17561748
}
17571749
}
17581750

@@ -1770,7 +1762,7 @@ function transferReferencedDebugInfo(
17701762
referencedChunk: SomeChunk<any>,
17711763
referencedValue: mixed,
17721764
): void {
1773-
if (__DEV__ && referencedChunk._debugInfo) {
1765+
if (__DEV__) {
17741766
const referencedDebugInfo = referencedChunk._debugInfo;
17751767
// If we have a direct reference to an object that was rendered by a synchronous
17761768
// server component, it might have some debug info about how it was rendered.
@@ -1784,24 +1776,29 @@ function transferReferencedDebugInfo(
17841776
referencedValue !== null &&
17851777
(isArray(referencedValue) ||
17861778
typeof referencedValue[ASYNC_ITERATOR] === 'function' ||
1787-
referencedValue.$$typeof === REACT_ELEMENT_TYPE) &&
1788-
!referencedValue._debugInfo
1779+
referencedValue.$$typeof === REACT_ELEMENT_TYPE)
17891780
) {
17901781
// We should maybe use a unique symbol for arrays but this is a React owned array.
17911782
// $FlowFixMe[prop-missing]: This should be added to elements.
1792-
Object.defineProperty((referencedValue: any), '_debugInfo', {
1793-
configurable: false,
1794-
enumerable: false,
1795-
writable: true,
1796-
value: referencedDebugInfo,
1797-
});
1783+
const existingDebugInfo: ?ReactDebugInfo =
1784+
(referencedValue._debugInfo: any);
1785+
if (existingDebugInfo == null) {
1786+
Object.defineProperty((referencedValue: any), '_debugInfo', {
1787+
configurable: false,
1788+
enumerable: false,
1789+
writable: true,
1790+
value: referencedDebugInfo.slice(0), // Clone so that pushing later isn't going into the original
1791+
});
1792+
} else {
1793+
// $FlowFixMe[method-unbinding]
1794+
existingDebugInfo.push.apply(existingDebugInfo, referencedDebugInfo);
1795+
}
17981796
}
17991797
// We also add it to the initializing chunk since the resolution of that promise is
18001798
// also blocked by these. By adding it to both we can track it even if the array/element
18011799
// is extracted, or if the root is rendered as is.
18021800
if (parentChunk !== null) {
1803-
const parentDebugInfo =
1804-
parentChunk._debugInfo || (parentChunk._debugInfo = []);
1801+
const parentDebugInfo = parentChunk._debugInfo;
18051802
// $FlowFixMe[method-unbinding]
18061803
parentDebugInfo.push.apply(parentDebugInfo, referencedDebugInfo);
18071804
}

0 commit comments

Comments
 (0)