Skip to content

Commit

Permalink
[Transition Tracing] Fix excess calls to the transition start callback (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
lunaruan authored Jun 29, 2022
1 parent 2e1c884 commit d1432ba
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/react-reconciler/src/ReactFiberLane.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,9 +846,9 @@ export function addTransitionToLanesMap(
const index = laneToIndex(lane);
let transitions = transitionLanesMap[index];
if (transitions === null) {
transitions = [];
transitions = new Set();
}
transitions.push(transition);
transitions.add(transition);

transitionLanesMap[index] = transitions;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-reconciler/src/ReactFiberLane.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,9 @@ export function addTransitionToLanesMap(
const index = laneToIndex(lane);
let transitions = transitionLanesMap[index];
if (transitions === null) {
transitions = [];
transitions = new Set();
}
transitions.push(transition);
transitions.add(transition);

transitionLanesMap[index] = transitions;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactInternalTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export type TransitionTracingCallbacks = {
// The following fields are only used in transition tracing in Profile builds
type TransitionTracingOnlyFiberRootProperties = {|
transitionCallbacks: null | TransitionTracingCallbacks,
transitionLanes: Array<Array<Transition> | null>,
transitionLanes: Array<Set<Transition> | null>,
// Transitions on the root can be represented as a bunch of tracing markers.
// Each entangled group of transitions can be treated as a tracing marker.
// It will have a set of pending suspense boundaries. These transitions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,69 @@ describe('ReactInteractionTracing', () => {
});
});

// @gate enableTransitionTracing
it('multiple updates in transition callback should only result in one transitionStart/transitionComplete call', async () => {
const transitionCallbacks = {
onTransitionStart: (name, startTime) => {
Scheduler.unstable_yieldValue(
`onTransitionStart(${name}, ${startTime})`,
);
},
onTransitionComplete: (name, startTime, endTime) => {
Scheduler.unstable_yieldValue(
`onTransitionComplete(${name}, ${startTime}, ${endTime})`,
);
},
};

let navigateToPageTwo;
let setText;
function App() {
const [navigate, setNavigate] = useState(false);
const [text, _setText] = useState('hide');
navigateToPageTwo = () => setNavigate(true);
setText = () => _setText('show');

return (
<div>
{navigate ? (
<Text text={`Page Two: ${text}`} />
) : (
<Text text={`Page One: ${text}`} />
)}
</div>
);
}

const root = ReactNoop.createRoot({transitionCallbacks});
await act(async () => {
root.render(<App />);
ReactNoop.expire(1000);
await advanceTimers(1000);

expect(Scheduler).toFlushAndYield(['Page One: hide']);

await act(async () => {
startTransition(
() => {
navigateToPageTwo();
setText();
},
{name: 'page transition'},
);

ReactNoop.expire(1000);
await advanceTimers(1000);

expect(Scheduler).toFlushAndYield([
'Page Two: show',
'onTransitionStart(page transition, 1000)',
'onTransitionComplete(page transition, 1000, 2000)',
]);
});
});
});

// @gate enableTransitionTracing
it('should correctly trace interactions for async roots', async () => {
const transitionCallbacks = {
Expand Down

0 comments on commit d1432ba

Please sign in to comment.