Skip to content

Commit

Permalink
store the latest render data
Browse files Browse the repository at this point in the history
  • Loading branch information
pivanov committed Dec 16, 2024
1 parent 5cc8f43 commit 4b43622
Showing 1 changed file with 26 additions and 52 deletions.
78 changes: 26 additions & 52 deletions packages/scan/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,66 +220,40 @@ export const setOptions = (options: Options) => {
export const getOptions = () => ReactScanInternals.options;

export const reportRender = (fiber: Fiber, renders: Array<Render>) => {
let reportFiber: Fiber;
let prevRenderData: RenderData | undefined;

const currentFiberData = Store.reportData.get(fiber);
if (currentFiberData) {
reportFiber = fiber;
prevRenderData = currentFiberData;
} else if (!fiber.alternate) {
reportFiber = fiber;
prevRenderData = undefined;
} else {
reportFiber = fiber.alternate;
prevRenderData = Store.reportData.get(fiber.alternate);
}

const reportFiber = fiber;
const { selfTime } = getTimings(fiber);
const displayName = getDisplayName(fiber.type);

Store.lastReportTime.value = performance.now();

if (prevRenderData) {
prevRenderData.count = ((prevRenderData.count) ?? 0) + renders.length;
prevRenderData.renders.push(...renders);
if (renders[0]?.time) {
prevRenderData.time = ((prevRenderData.time) ?? 0) + (renders[0].time);
}
} else {
const { selfTime } = getTimings(fiber);

const reportData = {
count: renders.length,
time: selfTime,
renders,
displayName,
type: null,
};
const currentFiberData = Store.reportData.get(reportFiber) ?? {
count: 0,
time: 0,
renders: [],
displayName,
type: null,
};

Store.reportData.set(reportFiber, reportData);
}
currentFiberData.count += renders.length;
currentFiberData.time += selfTime;
currentFiberData.renders = renders;

Store.reportData.set(reportFiber, currentFiberData);

if (displayName && ReactScanInternals.options.value.report) {
const prevLegacyRenderData = Store.legacyReportData.get(displayName);
const existingLegacyData = Store.legacyReportData.get(displayName) ?? {
count: 0,
time: 0,
renders: [],
displayName: null,
type: getType(fiber.type) || fiber.type,
};

if (prevLegacyRenderData) {
prevLegacyRenderData.count = ((prevLegacyRenderData.count) ?? 0) + renders.length;
prevLegacyRenderData.renders.push(...renders);
if (renders[0]?.time) {
prevLegacyRenderData.time = ((prevLegacyRenderData.time) ?? 0) + (renders[0].time);
}
} else {
const { selfTime } = getTimings(fiber);

const reportData = {
count: renders.length,
time: selfTime,
renders,
displayName: null,
type: getType(fiber.type) || fiber.type,
};
Store.legacyReportData.set(displayName, reportData);
}
existingLegacyData.count += renders.length;
existingLegacyData.time += selfTime;
existingLegacyData.renders = renders;

Store.legacyReportData.set(displayName, existingLegacyData);
}
};

Expand Down

0 comments on commit 4b43622

Please sign in to comment.