Skip to content

Commit 639dd59

Browse files
author
Brian Vaughn
committed
Scheduling Profiler should not warn about long updates when they are transition priority. This is the purpose of the transition lane and APIs.
1 parent b81de86 commit 639dd59

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

packages/react-devtools-scheduling-profiler/src/import-worker/__tests__/preprocessData-test.internal.js

+136
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,142 @@ describe('preprocessData', () => {
15811581
);
15821582
}
15831583
});
1584+
1585+
it('should not warn about transition updates scheduled during commit phase', async () => {
1586+
function Component() {
1587+
const [value, setValue] = React.useState(0);
1588+
const [isPending, startTransition] = React.useTransition();
1589+
1590+
Scheduler.unstable_yieldValue(
1591+
`Component rendered with value ${value}`,
1592+
);
1593+
1594+
// Fake a long render
1595+
if (value !== 0) {
1596+
Scheduler.unstable_yieldValue('Long render');
1597+
startTime += 20000;
1598+
}
1599+
1600+
React.useLayoutEffect(() => {
1601+
startTransition(() => {
1602+
setValue(1);
1603+
});
1604+
}, []);
1605+
1606+
return value;
1607+
}
1608+
1609+
if (gate(flags => flags.enableSchedulingProfiler)) {
1610+
const cpuProfilerSample = creactCpuProfilerSample();
1611+
1612+
const root = ReactDOM.createRoot(document.createElement('div'));
1613+
act(() => {
1614+
root.render(<Component />);
1615+
});
1616+
1617+
expect(Scheduler).toHaveYielded([
1618+
'Component rendered with value 0',
1619+
'Component rendered with value 0',
1620+
'Component rendered with value 1',
1621+
'Long render',
1622+
]);
1623+
1624+
const testMarks = [];
1625+
clearedMarks.forEach(markName => {
1626+
if (markName === '--component-render-start-Component') {
1627+
// Fake a long running render
1628+
startTime += 20000;
1629+
}
1630+
1631+
testMarks.push({
1632+
pid: ++pid,
1633+
tid: ++tid,
1634+
ts: ++startTime,
1635+
args: {data: {}},
1636+
cat: 'blink.user_timing',
1637+
name: markName,
1638+
ph: 'R',
1639+
});
1640+
});
1641+
1642+
const data = await preprocessData([
1643+
cpuProfilerSample,
1644+
...createBoilerplateEntries(),
1645+
...testMarks,
1646+
]);
1647+
1648+
data.schedulingEvents.forEach(event => {
1649+
expect(event.warning).toBeNull();
1650+
});
1651+
}
1652+
});
1653+
1654+
it('should not warn about deferred value updates scheduled during commit phase', async () => {
1655+
function Component() {
1656+
const [value, setValue] = React.useState(0);
1657+
const deferredValue = React.useDeferredValue(value);
1658+
1659+
Scheduler.unstable_yieldValue(
1660+
`Component rendered with value ${value} and deferredValue ${deferredValue}`,
1661+
);
1662+
1663+
// Fake a long render
1664+
if (deferredValue !== 0) {
1665+
Scheduler.unstable_yieldValue('Long render');
1666+
startTime += 20000;
1667+
}
1668+
1669+
React.useLayoutEffect(() => {
1670+
setValue(1);
1671+
}, []);
1672+
1673+
return value + deferredValue;
1674+
}
1675+
1676+
if (gate(flags => flags.enableSchedulingProfiler)) {
1677+
const cpuProfilerSample = creactCpuProfilerSample();
1678+
1679+
const root = ReactDOM.createRoot(document.createElement('div'));
1680+
act(() => {
1681+
root.render(<Component />);
1682+
});
1683+
1684+
expect(Scheduler).toHaveYielded([
1685+
'Component rendered with value 0 and deferredValue 0',
1686+
'Component rendered with value 1 and deferredValue 0',
1687+
'Component rendered with value 1 and deferredValue 1',
1688+
'Long render',
1689+
]);
1690+
1691+
const testMarks = [];
1692+
clearedMarks.forEach(markName => {
1693+
if (markName === '--component-render-start-Component') {
1694+
// Fake a long running render
1695+
startTime += 20000;
1696+
}
1697+
1698+
testMarks.push({
1699+
pid: ++pid,
1700+
tid: ++tid,
1701+
ts: ++startTime,
1702+
args: {data: {}},
1703+
cat: 'blink.user_timing',
1704+
name: markName,
1705+
ph: 'R',
1706+
});
1707+
});
1708+
1709+
const data = await preprocessData([
1710+
cpuProfilerSample,
1711+
...createBoilerplateEntries(),
1712+
...testMarks,
1713+
]);
1714+
1715+
data.schedulingEvents.forEach(event => {
1716+
expect(event.warning).toBeNull();
1717+
});
1718+
}
1719+
});
15841720
});
15851721

15861722
describe('errors thrown while rendering', () => {

packages/react-devtools-scheduling-profiler/src/import-worker/preprocessData.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,16 @@ export default async function preprocessData(
970970
// See how long the subsequent batch of React work was.
971971
const [startTime, stopTime] = getBatchRange(batchUID, profilerData);
972972
if (stopTime - startTime > NESTED_UPDATE_DURATION_THRESHOLD) {
973-
schedulingEvent.warning = WARNING_STRINGS.NESTED_UPDATE;
973+
// Don't warn about transition updates scheduled during the commit phase.
974+
// e.g. useTransition, useDeferredValue
975+
// These are allowed to be long-running.
976+
if (
977+
!schedulingEvent.lanes.some(
978+
lane => profilerData.laneToLabelMap.get(lane) === 'Transition',
979+
)
980+
) {
981+
schedulingEvent.warning = WARNING_STRINGS.NESTED_UPDATE;
982+
}
974983
}
975984
});
976985
state.potentialSuspenseEventsOutsideOfTransition.forEach(

0 commit comments

Comments
 (0)