Skip to content

Commit d1432ba

Browse files
authored
[Transition Tracing] Fix excess calls to the transition start callback (#24806)
This PR fixes a bug where we would add a transition to the lanes map every time an update occurs. However, we didn't factor in that there might be multiple updates in a transition, which would cause the transition to be added multiple times to the transitionLanes map. This changes the transitionLanes object from an Array of Arrays to an Array of Sets so that we only add a transition if it hasn't been added before, avoiding duplicates
1 parent 2e1c884 commit d1432ba

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

packages/react-reconciler/src/ReactFiberLane.new.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,9 +846,9 @@ export function addTransitionToLanesMap(
846846
const index = laneToIndex(lane);
847847
let transitions = transitionLanesMap[index];
848848
if (transitions === null) {
849-
transitions = [];
849+
transitions = new Set();
850850
}
851-
transitions.push(transition);
851+
transitions.add(transition);
852852

853853
transitionLanesMap[index] = transitions;
854854
}

packages/react-reconciler/src/ReactFiberLane.old.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,9 @@ export function addTransitionToLanesMap(
813813
const index = laneToIndex(lane);
814814
let transitions = transitionLanesMap[index];
815815
if (transitions === null) {
816-
transitions = [];
816+
transitions = new Set();
817817
}
818-
transitions.push(transition);
818+
transitions.add(transition);
819819

820820
transitionLanesMap[index] = transitions;
821821
}

packages/react-reconciler/src/ReactInternalTypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ export type TransitionTracingCallbacks = {
328328
// The following fields are only used in transition tracing in Profile builds
329329
type TransitionTracingOnlyFiberRootProperties = {|
330330
transitionCallbacks: null | TransitionTracingCallbacks,
331-
transitionLanes: Array<Array<Transition> | null>,
331+
transitionLanes: Array<Set<Transition> | null>,
332332
// Transitions on the root can be represented as a bunch of tracing markers.
333333
// Each entangled group of transitions can be treated as a tracing marker.
334334
// It will have a set of pending suspense boundaries. These transitions

packages/react-reconciler/src/__tests__/ReactTransitionTracing-test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,69 @@ describe('ReactInteractionTracing', () => {
212212
});
213213
});
214214

215+
// @gate enableTransitionTracing
216+
it('multiple updates in transition callback should only result in one transitionStart/transitionComplete call', async () => {
217+
const transitionCallbacks = {
218+
onTransitionStart: (name, startTime) => {
219+
Scheduler.unstable_yieldValue(
220+
`onTransitionStart(${name}, ${startTime})`,
221+
);
222+
},
223+
onTransitionComplete: (name, startTime, endTime) => {
224+
Scheduler.unstable_yieldValue(
225+
`onTransitionComplete(${name}, ${startTime}, ${endTime})`,
226+
);
227+
},
228+
};
229+
230+
let navigateToPageTwo;
231+
let setText;
232+
function App() {
233+
const [navigate, setNavigate] = useState(false);
234+
const [text, _setText] = useState('hide');
235+
navigateToPageTwo = () => setNavigate(true);
236+
setText = () => _setText('show');
237+
238+
return (
239+
<div>
240+
{navigate ? (
241+
<Text text={`Page Two: ${text}`} />
242+
) : (
243+
<Text text={`Page One: ${text}`} />
244+
)}
245+
</div>
246+
);
247+
}
248+
249+
const root = ReactNoop.createRoot({transitionCallbacks});
250+
await act(async () => {
251+
root.render(<App />);
252+
ReactNoop.expire(1000);
253+
await advanceTimers(1000);
254+
255+
expect(Scheduler).toFlushAndYield(['Page One: hide']);
256+
257+
await act(async () => {
258+
startTransition(
259+
() => {
260+
navigateToPageTwo();
261+
setText();
262+
},
263+
{name: 'page transition'},
264+
);
265+
266+
ReactNoop.expire(1000);
267+
await advanceTimers(1000);
268+
269+
expect(Scheduler).toFlushAndYield([
270+
'Page Two: show',
271+
'onTransitionStart(page transition, 1000)',
272+
'onTransitionComplete(page transition, 1000, 2000)',
273+
]);
274+
});
275+
});
276+
});
277+
215278
// @gate enableTransitionTracing
216279
it('should correctly trace interactions for async roots', async () => {
217280
const transitionCallbacks = {

0 commit comments

Comments
 (0)