Skip to content

Commit 7355bf5

Browse files
authored
Consolidate commit phase hook functions (#19864)
There were a few pairs of commit phase functions that were almost identical except for one detail. I've refactored them a bit to consolidate their implementations: - Lifted error handling logic when mounting a fiber's passive hook effects to surround the entire list, instead of surrounding each effect. - Lifted profiler duration tracking to surround the entire list. In both cases, this matches the corresponding code for the layout phase. The naming is still a bit of a mess but I'm not too concerned because my next step is to refactor each commit sub-phase (layout, mutation) so that we can store values on the JS stack. So the existing function boundaries are about to change, anyway.
1 parent c91c1c4 commit 7355bf5

File tree

2 files changed

+86
-141
lines changed

2 files changed

+86
-141
lines changed

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

+60-133
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ import type {FiberRoot} from './ReactInternalTypes';
2020
importtype{Lanes}from'./ReactFiberLane';
2121
importtype{SuspenseState}from'./ReactFiberSuspenseComponent.new';
2222
importtype{UpdateQueue}from'./ReactUpdateQueue.new';
23-
importtype{
24-
EffectasHookEffect,
25-
FunctionComponentUpdateQueue,
26-
}from'./ReactFiberHooks.new';
23+
importtype{FunctionComponentUpdateQueue}from'./ReactFiberHooks.new';
2724
importtype{Wakeable}from'shared/ReactTypes';
2825
importtype{ReactPriorityLevel}from'./ReactInternalTypes';
2926
importtype{OffscreenState}from'./ReactFiberOffscreenComponent';
@@ -343,42 +340,6 @@ function commitHookEffectListUnmount(
343340
}
344341
}
345342

346-
// TODO: Remove this duplication.
347-
functioncommitHookEffectListUnmount2(
348-
// Tags to check for when deciding whether to unmount. e.g. to skip over layout effects
349-
hookFlags: HookFlags,
350-
fiber: Fiber,
351-
nearestMountedAncestor: Fiber|null,
352-
): void{
353-
constupdateQueue: FunctionComponentUpdateQueue|null=(fiber.updateQueue: any);
354-
constlastEffect=updateQueue!==null ? updateQueue.lastEffect : null;
355-
if(lastEffect!==null){
356-
const firstEffect =lastEffect.next;
357-
leteffect=firstEffect;
358-
do{
359-
const{next, tag}=effect;
360-
if((tag&hookFlags)===hookFlags){
361-
const destroy =effect.destroy;
362-
if(destroy!==undefined){
363-
effect.destroy=undefined;
364-
if(
365-
enableProfilerTimer&&
366-
enableProfilerCommitHooks&&
367-
fiber.mode&ProfileMode
368-
){
369-
startPassiveEffectTimer();
370-
safelyCallDestroy(fiber,nearestMountedAncestor,destroy);
371-
recordPassiveEffectDuration(fiber);
372-
}else{
373-
safelyCallDestroy(fiber,nearestMountedAncestor,destroy);
374-
}
375-
}
376-
}
377-
effect=next;
378-
}while(effect!==firstEffect);
379-
}
380-
}
381-
382343
functioncommitHookEffectListMount(tag: HookFlags,finishedWork: Fiber){
383344
constupdateQueue: FunctionComponentUpdateQueue|null=(finishedWork.updateQueue: any);
384345
constlastEffect=updateQueue!==null ? updateQueue.lastEffect : null;
@@ -429,83 +390,6 @@ function commitHookEffectListMount(tag: HookFlags, finishedWork: Fiber) {
429390
}
430391
}
431392

432-
functioninvokePassiveEffectCreate(effect: HookEffect): void{
433-
const create =effect.create;
434-
effect.destroy=create();
435-
}
436-
437-
// TODO: Remove this duplication.
438-
functioncommitHookEffectListMount2(fiber: Fiber): void{
439-
constupdateQueue: FunctionComponentUpdateQueue|null=(fiber.updateQueue: any);
440-
constlastEffect=updateQueue!==null ? updateQueue.lastEffect : null;
441-
if(lastEffect!==null){
442-
constfirstEffect=lastEffect.next;
443-
leteffect=firstEffect;
444-
do{
445-
const{next, tag}=effect;
446-
447-
if(
448-
(tag&HookPassive)!==NoHookEffect&&
449-
(tag&HookHasEffect)!==NoHookEffect
450-
){
451-
if(__DEV__){
452-
if(
453-
enableProfilerTimer&&
454-
enableProfilerCommitHooks&&
455-
fiber.mode&ProfileMode
456-
){
457-
startPassiveEffectTimer();
458-
invokeGuardedCallback(
459-
null,
460-
invokePassiveEffectCreate,
461-
null,
462-
effect,
463-
);
464-
recordPassiveEffectDuration(fiber);
465-
}else{
466-
invokeGuardedCallback(
467-
null,
468-
invokePassiveEffectCreate,
469-
null,
470-
effect,
471-
);
472-
}
473-
if(hasCaughtError()){
474-
invariant(fiber!==null,'Should be working on an effect.');
475-
consterror=clearCaughtError();
476-
captureCommitPhaseError(fiber,fiber.return,error);
477-
}
478-
}else{
479-
try{
480-
constcreate=effect.create;
481-
if(
482-
enableProfilerTimer&&
483-
enableProfilerCommitHooks&&
484-
fiber.mode&ProfileMode
485-
){
486-
try{
487-
startPassiveEffectTimer();
488-
effect.destroy=create();
489-
}finally{
490-
recordPassiveEffectDuration(fiber);
491-
}
492-
}else{
493-
effect.destroy=create();
494-
}
495-
// TODO: This is missing the warning that exists in commitHookEffectListMount.
496-
// The warning refers to useEffect but only applies to useLayoutEffect.
497-
}catch(error){
498-
invariant(fiber!==null,'Should be working on an effect.');
499-
captureCommitPhaseError(fiber,fiber.return,error);
500-
}
501-
}
502-
}
503-
504-
effect=next;
505-
}while(effect!==firstEffect);
506-
}
507-
}
508-
509393
functioncommitProfilerPassiveEffect(
510394
finishedRoot: FiberRoot,
511395
finishedWork: Fiber,
@@ -1894,17 +1778,31 @@ function commitResetTextContent(current: Fiber): void {
18941778
resetTextContent(current.stateNode);
18951779
}
18961780

1897-
functioncommitPassiveWork(finishedWork: Fiber): void{
1781+
functioncommitPassiveUnmountInsideDeletedTree(finishedWork: Fiber): void{
18981782
switch(finishedWork.tag){
18991783
caseFunctionComponent:
19001784
caseForwardRef:
19011785
caseSimpleMemoComponent:
19021786
caseBlock: {
1903-
commitHookEffectListUnmount2(
1904-
HookPassive|HookHasEffect,
1905-
finishedWork,
1906-
finishedWork.return,
1907-
);
1787+
if(
1788+
enableProfilerTimer&&
1789+
enableProfilerCommitHooks&&
1790+
finishedWork.mode&ProfileMode
1791+
){
1792+
startPassiveEffectTimer();
1793+
commitHookEffectListUnmount(
1794+
HookPassive|HookHasEffect,
1795+
finishedWork,
1796+
finishedWork.return,
1797+
);
1798+
recordPassiveEffectDuration(finishedWork);
1799+
}else{
1800+
commitHookEffectListUnmount(
1801+
HookPassive|HookHasEffect,
1802+
finishedWork,
1803+
finishedWork.return,
1804+
);
1805+
}
19081806
break;
19091807
}
19101808
}
@@ -1918,16 +1816,32 @@ function commitPassiveUnmount(
19181816
caseFunctionComponent:
19191817
caseForwardRef:
19201818
caseSimpleMemoComponent:
1921-
caseBlock:
1922-
commitHookEffectListUnmount2(
1923-
HookPassive,
1924-
current,
1925-
nearestMountedAncestor,
1926-
);
1819+
caseBlock: {
1820+
if(
1821+
enableProfilerTimer&&
1822+
enableProfilerCommitHooks&&
1823+
current.mode&ProfileMode
1824+
){
1825+
startPassiveEffectTimer();
1826+
commitHookEffectListUnmount(
1827+
HookPassive,
1828+
current,
1829+
nearestMountedAncestor,
1830+
);
1831+
recordPassiveEffectDuration(current);
1832+
}else{
1833+
commitHookEffectListUnmount(
1834+
HookPassive,
1835+
current,
1836+
nearestMountedAncestor,
1837+
);
1838+
}
1839+
break;
1840+
}
19271841
}
19281842
}
19291843

1930-
functioncommitPassiveLifeCycles(
1844+
functioncommitPassiveMount(
19311845
finishedRoot: FiberRoot,
19321846
finishedWork: Fiber,
19331847
): void{
@@ -1936,7 +1850,20 @@ function commitPassiveLifeCycles(
19361850
caseForwardRef:
19371851
caseSimpleMemoComponent:
19381852
caseBlock: {
1939-
commitHookEffectListMount2(finishedWork);
1853+
if(
1854+
enableProfilerTimer&&
1855+
enableProfilerCommitHooks&&
1856+
finishedWork.mode&ProfileMode
1857+
){
1858+
startPassiveEffectTimer();
1859+
try{
1860+
commitHookEffectListMount(HookPassive|HookHasEffect,finishedWork);
1861+
}finally{
1862+
recordPassiveEffectDuration(finishedWork);
1863+
}
1864+
}else{
1865+
commitHookEffectListMount(HookPassive|HookHasEffect,finishedWork);
1866+
}
19401867
break;
19411868
}
19421869
caseProfiler: {
@@ -1956,6 +1883,6 @@ export {
19561883
commitAttachRef,
19571884
commitDetachRef,
19581885
commitPassiveUnmount,
1959-
commitPassiveWork,
1960-
commitPassiveLifeCycles,
1886+
commitPassiveUnmountInsideDeletedTree,
1887+
commitPassiveMount,
19611888
};

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

+26-8
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ import {
191191
commitPlacement,
192192
commitWork,
193193
commitDeletion,
194-
commitPassiveUnmount,
195-
commitPassiveWork,
196-
commitPassiveLifeCyclesascommitPassiveEffectOnFiber,
194+
commitPassiveUnmountascommitPassiveUnmountOnFiber,
195+
commitPassiveUnmountInsideDeletedTreeascommitPassiveUnmountInsideDeletedTreeOnFiber,
196+
commitPassiveMountascommitPassiveMountOnFiber,
197197
commitDetachRef,
198198
commitAttachRef,
199199
commitResetTextContent,
@@ -2458,9 +2458,27 @@ function flushPassiveMountEffects(root, firstChild: Fiber): void {
24582458
}
24592459

24602460
if((fiber.flags&Passive)!==NoFlags){
2461-
setCurrentDebugFiberInDEV(fiber);
2462-
commitPassiveEffectOnFiber(root,fiber);
2463-
resetCurrentDebugFiberInDEV();
2461+
if(__DEV__){
2462+
setCurrentDebugFiberInDEV(fiber);
2463+
invokeGuardedCallback(
2464+
null,
2465+
commitPassiveMountOnFiber,
2466+
null,
2467+
root,
2468+
fiber,
2469+
);
2470+
if(hasCaughtError()){
2471+
consterror=clearCaughtError();
2472+
captureCommitPhaseError(fiber,fiber.return,error);
2473+
}
2474+
resetCurrentDebugFiberInDEV();
2475+
}else{
2476+
try{
2477+
commitPassiveMountOnFiber(root,fiber);
2478+
}catch(error){
2479+
captureCommitPhaseError(fiber,fiber.return,error);
2480+
}
2481+
}
24642482
}
24652483

24662484
fiber=fiber.sibling;
@@ -2496,7 +2514,7 @@ function flushPassiveUnmountEffects(firstChild: Fiber): void {
24962514
constprimaryFlags=fiber.flags&Passive;
24972515
if(primaryFlags!==NoFlags){
24982516
setCurrentDebugFiberInDEV(fiber);
2499-
commitPassiveWork(fiber);
2517+
commitPassiveUnmountInsideDeletedTreeOnFiber(fiber);
25002518
resetCurrentDebugFiberInDEV();
25012519
}
25022520

@@ -2525,7 +2543,7 @@ function flushPassiveUnmountEffectsInsideOfDeletedTree(
25252543

25262544
if((fiberToDelete.flags&PassiveStatic)!==NoFlags){
25272545
setCurrentDebugFiberInDEV(fiberToDelete);
2528-
commitPassiveUnmount(fiberToDelete,nearestMountedAncestor);
2546+
commitPassiveUnmountOnFiber(fiberToDelete,nearestMountedAncestor);
25292547
resetCurrentDebugFiberInDEV();
25302548
}
25312549
}

0 commit comments

Comments
 (0)
close