Skip to content

Commit f5fc642

Browse files
author
Brian Vaughn
committed
DRYed things up in the new fork
1 parent 7467f51 commit f5fc642

File tree

2 files changed

+38
-139
lines changed

2 files changed

+38
-139
lines changed

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

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ import {
112112
} from './ReactFiberHostConfig';
113113
import {
114114
captureCommitPhaseError,
115-
captureCommitPhaseErrorForUnmountedFiber,
116115
resolveRetryWakeable,
117116
markCommitTimeOfFallback,
118117
enqueuePendingPassiveProfilerEffect,
@@ -167,13 +166,13 @@ function safelyCallComponentWillUnmount(current, instance) {
167166
);
168167
if (hasCaughtError()) {
169168
const unmountError = clearCaughtError();
170-
captureCommitPhaseError(current, unmountError);
169+
captureCommitPhaseError(current, current.return, unmountError);
171170
}
172171
} else {
173172
try {
174173
callComponentWillUnmountWithTimer(current, instance);
175174
} catch (unmountError) {
176-
captureCommitPhaseError(current, unmountError);
175+
captureCommitPhaseError(current, current.return, unmountError);
177176
}
178177
}
179178
}
@@ -186,13 +185,13 @@ function safelyDetachRef(current: Fiber) {
186185
invokeGuardedCallback(null, ref, null, null);
187186
if (hasCaughtError()) {
188187
const refError = clearCaughtError();
189-
captureCommitPhaseError(current, refError);
188+
captureCommitPhaseError(current, current.return, refError);
190189
}
191190
} else {
192191
try {
193192
ref(null);
194193
} catch (refError) {
195-
captureCommitPhaseError(current, refError);
194+
captureCommitPhaseError(current, current.return, refError);
196195
}
197196
}
198197
} else {
@@ -201,46 +200,22 @@ function safelyDetachRef(current: Fiber) {
201200
}
202201
}
203202

204-
export function safelyCallDestroy(current: Fiber, destroy: () => void) {
205-
if (__DEV__) {
206-
invokeGuardedCallback(null, destroy, null);
207-
if (hasCaughtError()) {
208-
const error = clearCaughtError();
209-
captureCommitPhaseError(current, error);
210-
}
211-
} else {
212-
try {
213-
destroy();
214-
} catch (error) {
215-
captureCommitPhaseError(current, error);
216-
}
217-
}
218-
}
219-
220-
export function safelyCallDestroyForUnmountedFiber(
203+
export function safelyCallDestroy(
221204
current: Fiber,
222-
nearestMountedAncestor: Fiber,
205+
nearestMountedAncestor: Fiber | null,
223206
destroy: () => void,
224207
) {
225208
if (__DEV__) {
226209
invokeGuardedCallback(null, destroy, null);
227210
if (hasCaughtError()) {
228211
const error = clearCaughtError();
229-
captureCommitPhaseErrorForUnmountedFiber(
230-
current,
231-
nearestMountedAncestor,
232-
error,
233-
);
212+
captureCommitPhaseError(current, nearestMountedAncestor, error);
234213
}
235214
} else {
236215
try {
237216
destroy();
238217
} catch (error) {
239-
captureCommitPhaseErrorForUnmountedFiber(
240-
current,
241-
nearestMountedAncestor,
242-
error,
243-
);
218+
captureCommitPhaseError(current, nearestMountedAncestor, error);
244219
}
245220
}
246221
}
@@ -906,10 +881,10 @@ function commitUnmount(
906881
current.mode & ProfileMode
907882
) {
908883
startLayoutEffectTimer();
909-
safelyCallDestroy(current, destroy);
884+
safelyCallDestroy(current, current.return, destroy);
910885
recordLayoutEffectDuration(current);
911886
} else {
912-
safelyCallDestroy(current, destroy);
887+
safelyCallDestroy(current, current.return, destroy);
913888
}
914889
}
915890
}

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

Lines changed: 28 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ import {
212212
commitResetTextContent,
213213
isSuspenseBoundaryBeingHidden,
214214
safelyCallDestroy,
215-
safelyCallDestroyForUnmountedFiber,
216215
} from './ReactFiberCommitWork.new';
217216
import {enqueueUpdate} from './ReactUpdateQueue.new';
218217
import {resetContextDependencies} from './ReactFiberNewContext.new';
@@ -2410,14 +2409,14 @@ function commitBeforeMutationEffects(firstChild: Fiber) {
24102409
invokeGuardedCallback(null, commitBeforeMutationEffectsImpl, null, fiber);
24112410
if (hasCaughtError()) {
24122411
const error = clearCaughtError();
2413-
captureCommitPhaseError(fiber, error);
2412+
captureCommitPhaseError(fiber, fiber.return, error);
24142413
}
24152414
resetCurrentDebugFiberInDEV();
24162415
} else {
24172416
try {
24182417
commitBeforeMutationEffectsImpl(fiber);
24192418
} catch (error) {
2420-
captureCommitPhaseError(fiber, error);
2419+
captureCommitPhaseError(fiber, fiber.return, error);
24212420
}
24222421
}
24232422
fiber = fiber.sibling;
@@ -2507,14 +2506,14 @@ function commitMutationEffects(
25072506
);
25082507
if (hasCaughtError()) {
25092508
const error = clearCaughtError();
2510-
captureCommitPhaseError(fiber, error);
2509+
captureCommitPhaseError(fiber, fiber.return, error);
25112510
}
25122511
resetCurrentDebugFiberInDEV();
25132512
} else {
25142513
try {
25152514
commitMutationEffectsImpl(fiber, root, renderPriorityLevel);
25162515
} catch (error) {
2517-
captureCommitPhaseError(fiber, error);
2516+
captureCommitPhaseError(fiber, fiber.return, error);
25182517
}
25192518
}
25202519
fiber = fiber.sibling;
@@ -2610,13 +2609,13 @@ function commitMutationEffectsDeletions(
26102609
);
26112610
if (hasCaughtError()) {
26122611
const error = clearCaughtError();
2613-
captureCommitPhaseError(childToDelete, error);
2612+
captureCommitPhaseError(childToDelete, childToDelete.return, error);
26142613
}
26152614
} else {
26162615
try {
26172616
commitDeletion(root, childToDelete, renderPriorityLevel);
26182617
} catch (error) {
2619-
captureCommitPhaseError(childToDelete, error);
2618+
captureCommitPhaseError(childToDelete, childToDelete.return, error);
26202619
}
26212620
}
26222621
}
@@ -2658,14 +2657,14 @@ function commitLayoutEffects(
26582657
);
26592658
if (hasCaughtError()) {
26602659
const error = clearCaughtError();
2661-
captureCommitPhaseError(fiber, error);
2660+
captureCommitPhaseError(fiber, fiber.return, error);
26622661
}
26632662
resetCurrentDebugFiberInDEV();
26642663
} else {
26652664
try {
26662665
commitLayoutEffectsImpl(fiber, root, committedLanes);
26672666
} catch (error) {
2668-
captureCommitPhaseError(fiber, error);
2667+
captureCommitPhaseError(fiber, fiber.return, error);
26692668
}
26702669
}
26712670
fiber = fiber.sibling;
@@ -2808,7 +2807,7 @@ function flushPassiveMountEffectsImpl(fiber: Fiber): void {
28082807
if (hasCaughtError()) {
28092808
invariant(fiber !== null, 'Should be working on an effect.');
28102809
const error = clearCaughtError();
2811-
captureCommitPhaseError(fiber, error);
2810+
captureCommitPhaseError(fiber, fiber.return, error);
28122811
}
28132812
} else {
28142813
try {
@@ -2829,7 +2828,7 @@ function flushPassiveMountEffectsImpl(fiber: Fiber): void {
28292828
}
28302829
} catch (error) {
28312830
invariant(fiber !== null, 'Should be working on an effect.');
2832-
captureCommitPhaseError(fiber, error);
2831+
captureCommitPhaseError(fiber, fiber.return, error);
28332832
}
28342833
}
28352834
}
@@ -2848,7 +2847,7 @@ function flushPassiveUnmountEffects(firstChild: Fiber): void {
28482847
if (deletions !== null) {
28492848
for (let i = 0; i < deletions.length; i++) {
28502849
const fiberToDelete = deletions[i];
2851-
flushPassiveUnmountEffectsForUnmountedFiber(fiberToDelete, fiber);
2850+
flushPassiveUnmountEffectsInsideOfDeletedTree(fiberToDelete, fiber);
28522851

28532852
// Now that passive effects have been processed, it's safe to detach lingering pointers.
28542853
detachFiberAfterEffects(fiberToDelete);
@@ -2874,7 +2873,11 @@ function flushPassiveUnmountEffects(firstChild: Fiber): void {
28742873
case Block: {
28752874
const primaryEffectTag = fiber.effectTag & Passive;
28762875
if (primaryEffectTag !== NoEffect) {
2877-
flushPassiveUnmountEffectsImpl(fiber, HookPassive | HookHasEffect);
2876+
flushPassiveUnmountEffectsImpl(
2877+
fiber,
2878+
fiber.return,
2879+
HookPassive | HookHasEffect,
2880+
);
28782881
}
28792882
}
28802883
}
@@ -2883,7 +2886,7 @@ function flushPassiveUnmountEffects(firstChild: Fiber): void {
28832886
}
28842887
}
28852888

2886-
function flushPassiveUnmountEffectsForUnmountedFiber(
2889+
function flushPassiveUnmountEffectsInsideOfDeletedTree(
28872890
fiberToDelete: Fiber,
28882891
nearestMountedAncestor: Fiber,
28892892
): void {
@@ -2894,7 +2897,7 @@ function flushPassiveUnmountEffectsForUnmountedFiber(
28942897
// since that would not cover passive effects in siblings.
28952898
let child = fiberToDelete.child;
28962899
while (child !== null) {
2897-
flushPassiveUnmountEffectsForUnmountedFiber(
2900+
flushPassiveUnmountEffectsInsideOfDeletedTree(
28982901
child,
28992902
nearestMountedAncestor,
29002903
);
@@ -2908,63 +2911,19 @@ function flushPassiveUnmountEffectsForUnmountedFiber(
29082911
case ForwardRef:
29092912
case SimpleMemoComponent:
29102913
case Block: {
2911-
flushPassiveUnmountEffectsForUnmountedFiberImpl(
2914+
flushPassiveUnmountEffectsImpl(
29122915
fiberToDelete,
29132916
nearestMountedAncestor,
2917+
HookPassive,
29142918
);
29152919
}
29162920
}
29172921
}
29182922
}
29192923

2920-
function flushPassiveUnmountEffectsForUnmountedFiberImpl(
2921-
fiber: Fiber,
2922-
nearestMountedAncestor: Fiber,
2923-
): void {
2924-
const updateQueue: FunctionComponentUpdateQueue | null = (fiber.updateQueue: any);
2925-
const lastEffect = updateQueue !== null ? updateQueue.lastEffect : null;
2926-
if (lastEffect !== null) {
2927-
setCurrentDebugFiberInDEV(fiber);
2928-
2929-
const firstEffect = lastEffect.next;
2930-
let effect = firstEffect;
2931-
do {
2932-
const {next, tag} = effect;
2933-
if ((tag & HookPassive) === HookPassive) {
2934-
const destroy = effect.destroy;
2935-
if (destroy !== undefined) {
2936-
effect.destroy = undefined;
2937-
2938-
if (
2939-
enableProfilerTimer &&
2940-
enableProfilerCommitHooks &&
2941-
fiber.mode & ProfileMode
2942-
) {
2943-
startPassiveEffectTimer();
2944-
safelyCallDestroyForUnmountedFiber(
2945-
fiber,
2946-
nearestMountedAncestor,
2947-
destroy,
2948-
);
2949-
recordPassiveEffectDuration(fiber);
2950-
} else {
2951-
safelyCallDestroyForUnmountedFiber(
2952-
fiber,
2953-
nearestMountedAncestor,
2954-
destroy,
2955-
);
2956-
}
2957-
}
2958-
}
2959-
effect = next;
2960-
} while (effect !== firstEffect);
2961-
2962-
resetCurrentDebugFiberInDEV();
2963-
}
2964-
}
2965-
29662924
function flushPassiveUnmountEffectsImpl(
29672925
fiber: Fiber,
2926+
nearestMountedAncestor: Fiber | null,
29682927
// Tags to check for when deciding whether to unmount. e.g. to skip over layout effects
29692928
hookEffectTag: HookEffectTag,
29702929
): void {
@@ -2987,10 +2946,10 @@ function flushPassiveUnmountEffectsImpl(
29872946
fiber.mode & ProfileMode
29882947
) {
29892948
startPassiveEffectTimer();
2990-
safelyCallDestroy(fiber, destroy);
2949+
safelyCallDestroy(fiber, nearestMountedAncestor, destroy);
29912950
recordPassiveEffectDuration(fiber);
29922951
} else {
2993-
safelyCallDestroy(fiber, destroy);
2952+
safelyCallDestroy(fiber, nearestMountedAncestor, destroy);
29942953
}
29952954
}
29962955
}
@@ -3123,53 +3082,18 @@ function captureCommitPhaseErrorOnRoot(
31233082
}
31243083
}
31253084

3126-
export function captureCommitPhaseError(sourceFiber: Fiber, error: mixed) {
3085+
export function captureCommitPhaseError(
3086+
sourceFiber: Fiber,
3087+
nearestMountedAncestor: Fiber | null,
3088+
error: mixed,
3089+
) {
31273090
if (sourceFiber.tag === HostRoot) {
31283091
// Error was thrown at the root. There is no parent, so the root
31293092
// itself should capture it.
31303093
captureCommitPhaseErrorOnRoot(sourceFiber, sourceFiber, error);
31313094
return;
31323095
}
31333096

3134-
let fiber = sourceFiber.return;
3135-
while (fiber !== null) {
3136-
if (fiber.tag === HostRoot) {
3137-
captureCommitPhaseErrorOnRoot(fiber, sourceFiber, error);
3138-
return;
3139-
} else if (fiber.tag === ClassComponent) {
3140-
const ctor = fiber.type;
3141-
const instance = fiber.stateNode;
3142-
if (
3143-
typeof ctor.getDerivedStateFromError === 'function' ||
3144-
(typeof instance.componentDidCatch === 'function' &&
3145-
!isAlreadyFailedLegacyErrorBoundary(instance))
3146-
) {
3147-
const errorInfo = createCapturedValue(error, sourceFiber);
3148-
const update = createClassErrorUpdate(
3149-
fiber,
3150-
errorInfo,
3151-
(SyncLane: Lane),
3152-
);
3153-
enqueueUpdate(fiber, update);
3154-
const eventTime = requestEventTime();
3155-
const root = markUpdateLaneFromFiberToRoot(fiber, (SyncLane: Lane));
3156-
if (root !== null) {
3157-
markRootUpdated(root, SyncLane, eventTime);
3158-
ensureRootIsScheduled(root, eventTime);
3159-
schedulePendingInteractions(root, SyncLane);
3160-
}
3161-
return;
3162-
}
3163-
}
3164-
fiber = fiber.return;
3165-
}
3166-
}
3167-
3168-
export function captureCommitPhaseErrorForUnmountedFiber(
3169-
sourceFiber: Fiber,
3170-
nearestMountedAncestor: Fiber,
3171-
error: mixed,
3172-
) {
31733097
let fiber = nearestMountedAncestor;
31743098
while (fiber !== null) {
31753099
if (fiber.tag === HostRoot) {

0 commit comments

Comments
 (0)