Skip to content

Commit

Permalink
Add try/catch to places used to be covered by commitDeletionEffects
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Sep 4, 2024
1 parent 6201d60 commit 7275edb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
30 changes: 29 additions & 1 deletion packages/react-reconciler/src/ReactFiberCommitHostEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {
unhideTextInstance,
commitHydratedContainer,
commitHydratedSuspenseInstance,
removeChildFromContainer,
removeChild,
} from './ReactFiberConfig';
import {captureCommitPhaseError} from './ReactFiberWorkLoop';

Expand Down Expand Up @@ -334,6 +336,32 @@ export function commitHostPlacement(finishedWork: Fiber) {
}
}

export function commitHostRemoveChildFromContainer(
deletedFiber: Fiber,
nearestMountedAncestor: Fiber,
parentContainer: Container,
hostInstance: Instance | TextInstance,
) {
try {
removeChildFromContainer(parentContainer, hostInstance);
} catch (error) {
captureCommitPhaseError(deletedFiber, nearestMountedAncestor, error);
}
}

export function commitHostRemoveChild(
deletedFiber: Fiber,
nearestMountedAncestor: Fiber,
parentInstance: Instance,
hostInstance: Instance | TextInstance,
) {
try {
removeChild(parentInstance, hostInstance);
} catch (error) {
captureCommitPhaseError(deletedFiber, nearestMountedAncestor, error);
}
}

export function commitHostRootContainerChildren(
root: FiberRoot,
finishedWork: Fiber,
Expand All @@ -354,9 +382,9 @@ export function commitHostPortalContainerChildren(
...
},
finishedWork: Fiber,
pendingChildren: ChildSet,
) {
const containerInfo = portal.containerInfo;
const pendingChildren = portal.pendingChildren;
try {
replaceContainerChildren(containerInfo, pendingChildren);
} catch (error) {
Expand Down
52 changes: 27 additions & 25 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
TextInstance,
SuspenseInstance,
Container,
ChildSet,
HoistableRoot,
FormInstance,
} from './ReactFiberConfig';
Expand Down Expand Up @@ -114,11 +113,8 @@ import {
supportsHydration,
supportsResources,
supportsSingletons,
removeChild,
removeChildFromContainer,
clearSuspenseBoundary,
clearSuspenseBoundaryFromContainer,
replaceContainerChildren,
createContainerChildSet,
clearContainer,
prepareScopeUpdate,
Expand Down Expand Up @@ -212,6 +208,8 @@ import {
commitHostPortalContainerChildren,
commitHostHydratedContainer,
commitHostHydratedSuspense,
commitHostRemoveChildFromContainer,
commitHostRemoveChild,
} from './ReactFiberCommitHostEffects';

// Used during the commit phase to track the state of the Offscreen component stack.
Expand Down Expand Up @@ -1064,21 +1062,6 @@ function detachFiberAfterEffects(fiber: Fiber) {
fiber.updateQueue = null;
}

function emptyPortalContainer(current: Fiber) {
if (!supportsPersistence) {
return;
}

const portal: {
containerInfo: Container,
pendingChildren: ChildSet,
...
} = current.stateNode;
const {containerInfo} = portal;
const emptyChildSet = createContainerChildSet();
replaceContainerChildren(containerInfo, emptyChildSet);
}

// These are tracked on the stack as we recursively traverse a
// deleted subtree.
// TODO: Update these during the whole mutation phase, not just during
Expand Down Expand Up @@ -1249,12 +1232,16 @@ function commitDeletionEffectsOnFiber(
// Now that all the child effects have unmounted, we can remove the
// node from the tree.
if (hostParentIsContainer) {
removeChildFromContainer(
commitHostRemoveChildFromContainer(
deletedFiber,
nearestMountedAncestor,
((hostParent: any): Container),
(deletedFiber.stateNode: Instance | TextInstance),
);
} else {
removeChild(
commitHostRemoveChild(
deletedFiber,
nearestMountedAncestor,
((hostParent: any): Instance),
(deletedFiber.stateNode: Instance | TextInstance),
);
Expand All @@ -1273,9 +1260,17 @@ function commitDeletionEffectsOnFiber(
if (enableSuspenseCallback) {
const hydrationCallbacks = finishedRoot.hydrationCallbacks;
if (hydrationCallbacks !== null) {
const onDeleted = hydrationCallbacks.onDeleted;
if (onDeleted) {
onDeleted((deletedFiber.stateNode: SuspenseInstance));
try {
const onDeleted = hydrationCallbacks.onDeleted;
if (onDeleted) {
onDeleted((deletedFiber.stateNode: SuspenseInstance));
}
} catch (error) {
captureCommitPhaseError(
deletedFiber,
nearestMountedAncestor,
error,
);
}
}
}
Expand Down Expand Up @@ -1315,7 +1310,13 @@ function commitDeletionEffectsOnFiber(
hostParent = prevHostParent;
hostParentIsContainer = prevHostParentIsContainer;
} else {
emptyPortalContainer(deletedFiber);
if (supportsPersistence) {
commitHostPortalContainerChildren(
deletedFiber.stateNode,
deletedFiber,
createContainerChildSet(),
);
}

recursivelyTraverseDeletionEffects(
finishedRoot,
Expand Down Expand Up @@ -2006,6 +2007,7 @@ function commitMutationEffectsOnFiber(
commitHostPortalContainerChildren(
finishedWork.stateNode,
finishedWork,
finishedWork.stateNode.pendingChildren,
);
}
}
Expand Down

0 comments on commit 7275edb

Please sign in to comment.