Skip to content

Commit 68d2522

Browse files
committed
Build timeline recursively
1 parent 95a3111 commit 68d2522

File tree

1 file changed

+59
-44
lines changed
  • packages/react-devtools-shared/src/devtools

1 file changed

+59
-44
lines changed

packages/react-devtools-shared/src/devtools/store.js

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,9 @@ export default class Store extends EventEmitter<{
897897
getSuspendableDocumentOrderSuspense(
898898
uniqueSuspendersOnly: boolean,
899899
): $ReadOnlyArray<SuspenseTimelineStep> {
900+
const target: Array<SuspenseTimelineStep> = [];
900901
const roots = this.roots;
901-
if (roots.length === 0) {
902-
return [];
903-
}
904-
905-
const list: SuspenseTimelineStep[] = [];
902+
let rootStep: null | SuspenseTimelineStep = null;
906903
for (let i = 0; i < roots.length; i++) {
907904
const rootID = roots[i];
908905
const root = this.getElementByID(rootID);
@@ -913,50 +910,68 @@ export default class Store extends EventEmitter<{
913910

914911
const suspense = this.getSuspenseByID(rootID);
915912
if (suspense !== null) {
916-
if (list.length === 0) {
917-
// start with an arbitrary root that will allow inspection of the Screen
918-
list.push({
913+
const environments = suspense.environments;
914+
const environmentName =
915+
environments.length > 0
916+
? environments[environments.length - 1]
917+
: null;
918+
if (rootStep === null) {
919+
// Arbitrarily use the first root as the root step id.
920+
rootStep = {
919921
id: suspense.id,
920-
environment: null,
921-
});
922-
}
923-
924-
const stack = [suspense];
925-
while (stack.length > 0) {
926-
const current = stack.pop();
927-
if (current === undefined) {
928-
continue;
929-
}
930-
// Ignore any suspense boundaries that has no visual representation as this is not
931-
// part of the visible loading sequence.
932-
// TODO: Consider making visible meta data and other side-effects get virtual rects.
933-
const hasRects =
934-
current.rects !== null &&
935-
current.rects.length > 0 &&
936-
current.rects.some(isNonZeroRect);
937-
if (
938-
hasRects &&
939-
(!uniqueSuspendersOnly || current.hasUniqueSuspenders) &&
940-
// Roots are already included as part of the Screen
941-
current.id !== rootID
942-
) {
943-
list.push({
944-
id: current.id,
945-
environment: null,
946-
});
947-
}
948-
// Add children in reverse order to maintain document order
949-
for (let j = current.children.length - 1; j >= 0; j--) {
950-
const childSuspense = this.getSuspenseByID(current.children[j]);
951-
if (childSuspense !== null) {
952-
stack.push(childSuspense);
953-
}
954-
}
922+
environment: environmentName,
923+
};
924+
target.push(rootStep);
925+
} else if (rootStep.environment === null) {
926+
// If any root has an environment name, then let's use it.
927+
rootStep.environment = environmentName;
955928
}
929+
this.pushTimelineStepsInDocumentOrder(
930+
suspense.children,
931+
target,
932+
uniqueSuspendersOnly,
933+
environments,
934+
);
956935
}
957936
}
958937

959-
return list;
938+
return target;
939+
}
940+
941+
pushTimelineStepsInDocumentOrder(
942+
children: Array<SuspenseNode['id']>,
943+
target: Array<SuspenseTimelineStep>,
944+
uniqueSuspendersOnly: boolean,
945+
parentEnvironments: Array<string>,
946+
): void {
947+
for (let i = 0; i < children.length; i++) {
948+
const child = this.getSuspenseByID(children[i]);
949+
if (child === null) {
950+
continue;
951+
}
952+
// Ignore any suspense boundaries that has no visual representation as this is not
953+
// part of the visible loading sequence.
954+
// TODO: Consider making visible meta data and other side-effects get virtual rects.
955+
const hasRects =
956+
child.rects !== null &&
957+
child.rects.length > 0 &&
958+
child.rects.some(isNonZeroRect);
959+
const environments = child.environments;
960+
const environmentName =
961+
environments.length > 0 ? environments[environments.length - 1] : null;
962+
if (hasRects && (!uniqueSuspendersOnly || child.hasUniqueSuspenders)) {
963+
target.push({
964+
id: child.id,
965+
environment: environmentName,
966+
});
967+
}
968+
this.pushTimelineStepsInDocumentOrder(
969+
child.children,
970+
target,
971+
uniqueSuspendersOnly,
972+
environments,
973+
);
974+
}
960975
}
961976

962977
getRendererIDForElement(id: number): number | null {

0 commit comments

Comments
 (0)