Skip to content

Commit

Permalink
Wrap remaining Commit Effects in runWithFiberInDEV
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Sep 4, 2024
1 parent b614e89 commit a66c56d
Showing 1 changed file with 118 additions and 42 deletions.
160 changes: 118 additions & 42 deletions packages/react-reconciler/src/ReactFiberCommitEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,11 @@ export function commitClassCallbacks(finishedWork: Fiber) {
// but instead we rely on them being set during last render.
// TODO: revisit this when we implement resuming.
try {
commitCallbacks(updateQueue, instance);
if (__DEV__) {
runWithFiberInDEV(finishedWork, commitCallbacks, updateQueue, instance);
} else {
commitCallbacks(updateQueue, instance);
}
} catch (error) {
captureCommitPhaseError(finishedWork, finishedWork.return, error);
}
Expand All @@ -521,7 +525,16 @@ export function commitClassHiddenCallbacks(finishedWork: Fiber) {
if (updateQueue !== null) {
const instance = finishedWork.stateNode;
try {
commitHiddenCallbacks(updateQueue, instance);
if (__DEV__) {
runWithFiberInDEV(
finishedWork,
commitHiddenCallbacks,
updateQueue,
instance,
);
} else {
commitHiddenCallbacks(updateQueue, instance);
}
} catch (error) {
captureCommitPhaseError(finishedWork, finishedWork.return, error);
}
Expand All @@ -547,7 +560,11 @@ export function commitRootCallbacks(finishedWork: Fiber) {
}
}
try {
commitCallbacks(updateQueue, instance);
if (__DEV__) {
runWithFiberInDEV(finishedWork, commitCallbacks, updateQueue, instance);
} else {
commitCallbacks(updateQueue, instance);
}
} catch (error) {
captureCommitPhaseError(finishedWork, finishedWork.return, error);
}
Expand All @@ -559,6 +576,14 @@ if (__DEV__) {
didWarnAboutUndefinedSnapshotBeforeUpdate = new Set();
}

function callGetSnapshotBeforeUpdates(
instance: any,
prevProps: any,
prevState: any,
) {
return instance.getSnapshotBeforeUpdate(prevProps, prevState);
}

export function commitClassSnapshot(finishedWork: Fiber, current: Fiber) {
const prevProps = current.memoizedProps;
const prevState = current.memoizedState;
Expand Down Expand Up @@ -595,15 +620,20 @@ export function commitClassSnapshot(finishedWork: Fiber, current: Fiber) {
}
}
try {
const snapshot = instance.getSnapshotBeforeUpdate(
resolveClassComponentProps(
finishedWork.type,
prevProps,
finishedWork.elementType === finishedWork.type,
),
prevState,
const resolvedPrevProps = resolveClassComponentProps(
finishedWork.type,
prevProps,
finishedWork.elementType === finishedWork.type,
);
let snapshot;
if (__DEV__) {
snapshot = runWithFiberInDEV(
finishedWork,
callGetSnapshotBeforeUpdates,
instance,
resolvedPrevProps,
prevState,
);
const didWarnSet =
((didWarnAboutUndefinedSnapshotBeforeUpdate: any): Set<mixed>);
if (snapshot === undefined && !didWarnSet.has(finishedWork.type)) {
Expand All @@ -614,6 +644,12 @@ export function commitClassSnapshot(finishedWork: Fiber, current: Fiber) {
getComponentNameFromFiber(finishedWork),
);
}
} else {
snapshot = callGetSnapshotBeforeUpdates(
instance,
resolvedPrevProps,
prevState,
);
}
instance.__reactInternalSnapshotBeforeUpdate = snapshot;
} catch (error) {
Expand Down Expand Up @@ -726,7 +762,11 @@ export function safelyAttachRef(
nearestMountedAncestor: Fiber | null,
) {
try {
commitAttachRef(current);
if (__DEV__) {
runWithFiberInDEV(current, commitAttachRef, current);
} else {
commitAttachRef(current);
}
} catch (error) {
captureCommitPhaseError(current, nearestMountedAncestor, error);
}
Expand All @@ -745,12 +785,20 @@ export function safelyDetachRef(
if (shouldProfile(current)) {
try {
startLayoutEffectTimer();
refCleanup();
if (__DEV__) {
runWithFiberInDEV(current, refCleanup);
} else {
refCleanup();
}
} finally {
recordLayoutEffectDuration(current);
}
} else {
refCleanup();
if (__DEV__) {
runWithFiberInDEV(current, refCleanup);
} else {
refCleanup();
}
}
} catch (error) {
captureCommitPhaseError(current, nearestMountedAncestor, error);
Expand All @@ -767,12 +815,20 @@ export function safelyDetachRef(
if (shouldProfile(current)) {
try {
startLayoutEffectTimer();
ref(null);
if (__DEV__) {
(runWithFiberInDEV(current, ref, null): void);
} else {
ref(null);
}
} finally {
recordLayoutEffectDuration(current);
}
} else {
ref(null);
if (__DEV__) {
(runWithFiberInDEV(current, ref, null): void);
} else {
ref(null);
}
}
} catch (error) {
captureCommitPhaseError(current, nearestMountedAncestor, error);
Expand Down Expand Up @@ -806,6 +862,44 @@ export function safelyCallDestroy(
}
}

function commitProfiler(
finishedWork: Fiber,
current: Fiber | null,
commitTime: number,
effectDuration: number,
) {
const {onCommit, onRender} = finishedWork.memoizedProps;

let phase = current === null ? 'mount' : 'update';
if (enableProfilerNestedUpdatePhase) {
if (isCurrentUpdateNested()) {
phase = 'nested-update';
}
}

if (typeof onRender === 'function') {
onRender(
finishedWork.memoizedProps.id,
phase,
finishedWork.actualDuration,
finishedWork.treeBaseDuration,
finishedWork.actualStartTime,
commitTime,
);
}

if (enableProfilerCommitHooks) {
if (typeof onCommit === 'function') {
onCommit(
finishedWork.memoizedProps.id,
phase,
effectDuration,
commitTime,
);
}
}
}

export function commitProfilerUpdate(
finishedWork: Fiber,
current: Fiber | null,
Expand All @@ -814,35 +908,17 @@ export function commitProfilerUpdate(
) {
if (enableProfilerTimer && getExecutionContext() & CommitContext) {
try {
const {onCommit, onRender} = finishedWork.memoizedProps;

let phase = current === null ? 'mount' : 'update';
if (enableProfilerNestedUpdatePhase) {
if (isCurrentUpdateNested()) {
phase = 'nested-update';
}
}

if (typeof onRender === 'function') {
onRender(
finishedWork.memoizedProps.id,
phase,
finishedWork.actualDuration,
finishedWork.treeBaseDuration,
finishedWork.actualStartTime,
if (__DEV__) {
runWithFiberInDEV(
finishedWork,
commitProfiler,
finishedWork,
current,
commitTime,
effectDuration,
);
}

if (enableProfilerCommitHooks) {
if (typeof onCommit === 'function') {
onCommit(
finishedWork.memoizedProps.id,
phase,
effectDuration,
commitTime,
);
}
} else {
commitProfiler(finishedWork, current, commitTime, effectDuration);
}
} catch (error) {
captureCommitPhaseError(finishedWork, finishedWork.return, error);
Expand Down

0 comments on commit a66c56d

Please sign in to comment.