Skip to content

Commit 9859189

Browse files
committed
Move onPostCommit to actual passive phase
Original PR: facebook#19862
1 parent 3b18e04 commit 9859189

File tree

3 files changed

+50
-91
lines changed

3 files changed

+50
-91
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import {
5959
ContentReset,
6060
DidCapture,
6161
Update,
62+
Passive,
6263
Ref,
6364
Deletion,
6465
ChildDeletion,
@@ -673,7 +674,7 @@ function updateProfiler(
673674
renderLanes: Lanes,
674675
) {
675676
if (enableProfilerTimer) {
676-
workInProgress.flags |= Update;
677+
workInProgress.flags |= Update | Passive;
677678

678679
// Reset effect durations for the next eventual effect phase.
679680
// These are reset during render to allow the DevTools commit hook a chance to read them,
@@ -3099,7 +3100,7 @@ function beginWork(
30993100
workInProgress.childLanes,
31003101
);
31013102
if (hasChildWork) {
3102-
workInProgress.flags |= Update;
3103+
workInProgress.flags |= Update | Passive;
31033104
}
31043105

31053106
// Reset effect durations for the next eventual effect phase.

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

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ import {
127127
captureCommitPhaseError,
128128
resolveRetryWakeable,
129129
markCommitTimeOfFallback,
130-
enqueuePendingPassiveProfilerEffect,
131130
} from './ReactFiberWorkLoop.new';
132131
import {
133132
NoFlags as NoHookEffect,
@@ -412,63 +411,6 @@ function commitHookEffectListMount(tag: number, finishedWork: Fiber) {
412411
}
413412
}
414413

415-
export function commitPassiveEffectDurations(
416-
finishedRoot: FiberRoot,
417-
finishedWork: Fiber,
418-
): void {
419-
if (enableProfilerTimer && enableProfilerCommitHooks) {
420-
// Only Profilers with work in their subtree will have an Update effect scheduled.
421-
if ((finishedWork.flags & Update) !== NoFlags) {
422-
switch (finishedWork.tag) {
423-
case Profiler: {
424-
const {passiveEffectDuration} = finishedWork.stateNode;
425-
const {id, onPostCommit} = finishedWork.memoizedProps;
426-
427-
// This value will still reflect the previous commit phase.
428-
// It does not get reset until the start of the next commit phase.
429-
const commitTime = getCommitTime();
430-
431-
let phase = finishedWork.alternate === null ? 'mount' : 'update';
432-
if (enableProfilerNestedUpdatePhase) {
433-
if (isCurrentUpdateNested()) {
434-
phase = 'nested-update';
435-
}
436-
}
437-
438-
if (typeof onPostCommit === 'function') {
439-
if (enableSchedulerTracing) {
440-
onPostCommit(
441-
id,
442-
phase,
443-
passiveEffectDuration,
444-
commitTime,
445-
finishedRoot.memoizedInteractions,
446-
);
447-
} else {
448-
onPostCommit(id, phase, passiveEffectDuration, commitTime);
449-
}
450-
}
451-
452-
// Bubble times to the next nearest ancestor Profiler.
453-
// After we process that Profiler, we'll bubble further up.
454-
let parentFiber = finishedWork.return;
455-
while (parentFiber !== null) {
456-
if (parentFiber.tag === Profiler) {
457-
const parentStateNode = parentFiber.stateNode;
458-
parentStateNode.passiveEffectDuration += passiveEffectDuration;
459-
break;
460-
}
461-
parentFiber = parentFiber.return;
462-
}
463-
break;
464-
}
465-
default:
466-
break;
467-
}
468-
}
469-
}
470-
}
471-
472414
function commitLifeCycles(
473415
finishedRoot: FiberRoot,
474416
current: Fiber | null,
@@ -750,11 +692,6 @@ function commitLifeCycles(
750692
}
751693
}
752694

753-
// Schedule a passive effect for this Profiler to call onPostCommit hooks.
754-
// This effect should be scheduled even if there is no onPostCommit callback for this Profiler,
755-
// because the effect is also where times bubble to parent Profilers.
756-
enqueuePendingPassiveProfilerEffect(finishedWork);
757-
758695
// Propagate layout effect durations to the next nearest Profiler ancestor.
759696
// Do not reset these values until the next render so DevTools has a chance to read them first.
760697
let parentFiber = finishedWork.return;
@@ -1889,6 +1826,53 @@ function commitPassiveMountOnFiber(
18891826
}
18901827
break;
18911828
}
1829+
case Profiler: {
1830+
if (enableProfilerTimer && enableProfilerCommitHooks) {
1831+
// Only Profilers with work in their subtree will have an Update effect scheduled.
1832+
if ((finishedWork.flags & Update) !== NoFlags) {
1833+
const {passiveEffectDuration} = finishedWork.stateNode;
1834+
const {id, onPostCommit} = finishedWork.memoizedProps;
1835+
1836+
// This value will still reflect the previous commit phase.
1837+
// It does not get reset until the start of the next commit phase.
1838+
const commitTime = getCommitTime();
1839+
1840+
let phase = finishedWork.alternate === null ? 'mount' : 'update';
1841+
if (enableProfilerNestedUpdatePhase) {
1842+
if (isCurrentUpdateNested()) {
1843+
phase = 'nested-update';
1844+
}
1845+
}
1846+
1847+
if (typeof onPostCommit === 'function') {
1848+
if (enableSchedulerTracing) {
1849+
onPostCommit(
1850+
id,
1851+
phase,
1852+
passiveEffectDuration,
1853+
commitTime,
1854+
finishedRoot.memoizedInteractions,
1855+
);
1856+
} else {
1857+
onPostCommit(id, phase, passiveEffectDuration, commitTime);
1858+
}
1859+
}
1860+
1861+
// Bubble times to the next nearest ancestor Profiler.
1862+
// After we process that Profiler, we'll bubble further up.
1863+
let parentFiber = finishedWork.return;
1864+
while (parentFiber !== null) {
1865+
if (parentFiber.tag === Profiler) {
1866+
const parentStateNode = parentFiber.stateNode;
1867+
parentStateNode.passiveEffectDuration += passiveEffectDuration;
1868+
break;
1869+
}
1870+
parentFiber = parentFiber.return;
1871+
}
1872+
}
1873+
}
1874+
break;
1875+
}
18921876
}
18931877
}
18941878

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
enableSuspenseServerRenderer,
2222
replayFailedUnitOfWorkWithInvokeGuardedCallback,
2323
enableProfilerTimer,
24-
enableProfilerCommitHooks,
2524
enableProfilerNestedUpdatePhase,
2625
enableProfilerNestedUpdateScheduledHook,
2726
enableSchedulerTracing,
@@ -197,7 +196,6 @@ import {
197196
commitDeletion,
198197
commitDetachRef,
199198
commitAttachRef,
200-
commitPassiveEffectDurations,
201199
commitResetTextContent,
202200
isSuspenseBoundaryBeingHidden,
203201
commitPassiveMountEffects,
@@ -347,7 +345,6 @@ let rootDoesHavePassiveEffects: boolean = false;
347345
let rootWithPendingPassiveEffects: FiberRoot | null = null;
348346
let pendingPassiveEffectsRenderPriority: ReactPriorityLevel = NoSchedulerPriority;
349347
let pendingPassiveEffectsLanes: Lanes = NoLanes;
350-
let pendingPassiveProfilerEffects: Array<Fiber> = [];
351348

352349
let rootsWithPendingDiscreteUpdates: Set<FiberRoot> | null = null;
353350

@@ -2491,19 +2488,6 @@ export function flushPassiveEffects(): boolean {
24912488
return false;
24922489
}
24932490

2494-
export function enqueuePendingPassiveProfilerEffect(fiber: Fiber): void {
2495-
if (enableProfilerTimer && enableProfilerCommitHooks) {
2496-
pendingPassiveProfilerEffects.push(fiber);
2497-
if (!rootDoesHavePassiveEffects) {
2498-
rootDoesHavePassiveEffects = true;
2499-
scheduleCallback(NormalSchedulerPriority, () => {
2500-
flushPassiveEffects();
2501-
return null;
2502-
});
2503-
}
2504-
}
2505-
}
2506-
25072491
function flushPassiveEffectsImpl() {
25082492
if (rootWithPendingPassiveEffects === null) {
25092493
return false;
@@ -2540,16 +2524,6 @@ function flushPassiveEffectsImpl() {
25402524
commitPassiveUnmountEffects(root.current);
25412525
commitPassiveMountEffects(root, root.current);
25422526

2543-
// TODO: Move to commitPassiveMountEffects
2544-
if (enableProfilerTimer && enableProfilerCommitHooks) {
2545-
const profilerEffects = pendingPassiveProfilerEffects;
2546-
pendingPassiveProfilerEffects = [];
2547-
for (let i = 0; i < profilerEffects.length; i++) {
2548-
const fiber = ((profilerEffects[i]: any): Fiber);
2549-
commitPassiveEffectDurations(root, fiber);
2550-
}
2551-
}
2552-
25532527
if (enableSchedulerTracing) {
25542528
popInteractions(((prevInteractions: any): Set<Interaction>));
25552529
finishPendingInteractions(root, lanes);

0 commit comments

Comments
 (0)