Skip to content

Commit 024a764

Browse files
author
Brian Vaughn
authored
Implemented Profiler onCommit() and onPostCommit() hooks (#17910)
* Implemented Profiler onCommit() and onPostCommit() hooks * Added enableProfilerCommitHooks feature flag for commit hooks * Moved onCommit and onPassiveCommit behind separate feature flag
1 parent d35f8a5 commit 024a764

15 files changed

+2795
-1060
lines changed

packages/react-reconciler/src/ReactFiber.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -826,13 +826,8 @@ function createFiberFromProfiler(
826826
key: null | string,
827827
): Fiber {
828828
if (__DEV__) {
829-
if (
830-
typeof pendingProps.id !== 'string' ||
831-
typeof pendingProps.onRender !== 'function'
832-
) {
833-
console.error(
834-
'Profiler must specify an "id" string and "onRender" function as props',
835-
);
829+
if (typeof pendingProps.id !== 'string') {
830+
console.error('Profiler must specify an "id" as a prop');
836831
}
837832
}
838833

@@ -842,6 +837,13 @@ function createFiberFromProfiler(
842837
fiber.type = REACT_PROFILER_TYPE;
843838
fiber.expirationTime = expirationTime;
844839

840+
if (enableProfilerTimer) {
841+
fiber.stateNode = {
842+
effectDuration: 0,
843+
passiveEffectDuration: 0,
844+
};
845+
}
846+
845847
return fiber;
846848
}
847849

packages/react-reconciler/src/ReactFiberBeginWork.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,12 @@ function updateProfiler(
580580
) {
581581
if (enableProfilerTimer) {
582582
workInProgress.effectTag |= Update;
583+
584+
// Reset effect durations for the next eventual effect phase.
585+
// These are reset during render to allow the DevTools commit hook a chance to read them,
586+
const stateNode = workInProgress.stateNode;
587+
stateNode.effectDuration = 0;
588+
stateNode.passiveEffectDuration = 0;
583589
}
584590
const nextProps = workInProgress.pendingProps;
585591
const nextChildren = nextProps.children;
@@ -2944,6 +2950,12 @@ function beginWork(
29442950
if (hasChildWork) {
29452951
workInProgress.effectTag |= Update;
29462952
}
2953+
2954+
// Reset effect durations for the next eventual effect phase.
2955+
// These are reset during render to allow the DevTools commit hook a chance to read them,
2956+
const stateNode = workInProgress.stateNode;
2957+
stateNode.effectDuration = 0;
2958+
stateNode.passiveEffectDuration = 0;
29472959
}
29482960
break;
29492961
case SuspenseComponent: {

0 commit comments

Comments
 (0)