@samitouri / QOS-React-2 / commits / c4c87e049b

Small cleanup in ReactFiberCompleteWork (#27681)

These are all functionally equivalent changes. - remove double negation and more explicit naming of `hadNoMutationsEffects` - use docblock syntax that's consumed by Flow - remove useless cast

Jan Kassens committed Nov 10, 2023 at 10:20 UTC c4c87e049b891ebc38a7a14b8c93285c877e90af
1 file changed +24 -23
packages/react-reconciler/src/ReactFiberCompleteWork.js
+24 -23
@@ -178,9 +178,11 @@ import {
178 } from './ReactFiberTracingMarkerComponent';
179 import {suspendCommit} from './ReactFiberThenable';
180
181 +/**
182 + * Tag the fiber with an update effect. This turns a Placement into
183 + * a PlacementAndUpdate.
184 + */
185 function markUpdate(workInProgress: Fiber) {
182 - // Tag the fiber with an update effect. This turns a Placement into
183 - // a PlacementAndUpdate.
186 workInProgress.flags |= Update;
187 }
188
@@ -188,17 +190,20 @@ function markRef(workInProgress: Fiber) {
190 workInProgress.flags |= Ref | RefStatic;
191 }
192
191 -function hadNoMutationsEffects(current: null | Fiber, completedWork: Fiber) {
193 +/**
194 + * In persistent mode, return whether this update needs to clone the subtree.
195 + */
196 +function doesRequireClone(current: null | Fiber, completedWork: Fiber) {
197 const didBailout = current !== null && current.child === completedWork.child;
198 if (didBailout) {
194 - return true;
199 + return false;
200 }
201
202 if ((completedWork.flags & ChildDeletion) !== NoFlags) {
198 - return false;
203 + return true;
204 }
205
201 - // TODO: If we move the `hadNoMutationsEffects` call after `bubbleProperties`
206 + // TODO: If we move the `doesRequireClone` call after `bubbleProperties`
207 // then we only have to check the `completedWork.subtreeFlags`.
208 let child = completedWork.child;
209 while (child !== null) {
@@ -206,11 +211,11 @@ function hadNoMutationsEffects(current: null | Fiber, completedWork: Fiber) {
211 (child.flags & MutationMask) !== NoFlags ||
212 (child.subtreeFlags & MutationMask) !== NoFlags
213 ) {
209 - return false;
214 + return true;
215 }
216 child = child.sibling;
217 }
213 - return true;
218 + return false;
219 }
220
221 function appendAllChildren(
@@ -303,7 +308,6 @@ function appendAllChildren(
308 node = node.child;
309 continue;
310 }
306 - node = (node: Fiber);
311 if (node === workInProgress) {
312 return;
313 }
@@ -400,15 +404,12 @@ function appendAllChildrenToContainer(
404
405 function updateHostContainer(current: null | Fiber, workInProgress: Fiber) {
406 if (supportsPersistence) {
403 - const portalOrRoot: {
404 - containerInfo: Container,
405 - pendingChildren: ChildSet,
406 - ...
407 - } = workInProgress.stateNode;
408 - const childrenUnchanged = hadNoMutationsEffects(current, workInProgress);
409 - if (childrenUnchanged) {
410 - // No changes, just reuse the existing instance.
411 - } else {
407 + if (doesRequireClone(current, workInProgress)) {
408 + const portalOrRoot: {
409 + containerInfo: Container,
410 + pendingChildren: ChildSet,
411 + ...
412 + } = workInProgress.stateNode;
413 const container = portalOrRoot.containerInfo;
414 const newChildSet = createContainerChildSet();
415 // If children might have changed, we have to add them all to the set.
@@ -449,8 +450,8 @@ function updateHostComponent(
450 const oldProps = current.memoizedProps;
451 // If there are no effects associated with this node, then none of our children had any updates.
452 // This guarantees that we can reuse all of them.
452 - const childrenUnchanged = hadNoMutationsEffects(current, workInProgress);
453 - if (childrenUnchanged && oldProps === newProps) {
453 + const requiresClone = doesRequireClone(current, workInProgress);
454 + if (!requiresClone && oldProps === newProps) {
455 // No changes, just reuse the existing instance.
456 // Note that this might release a previous clone.
457 workInProgress.stateNode = currentInstance;
@@ -459,7 +460,7 @@ function updateHostComponent(
460 const currentHostContext = getHostContext();
461
462 let newChildSet = null;
462 - if (!childrenUnchanged && passChildrenWhenCloningPersistedNodes) {
463 + if (requiresClone && passChildrenWhenCloningPersistedNodes) {
464 newChildSet = createContainerChildSet();
465 // If children might have changed, we have to add them all to the set.
466 appendAllChildrenToContainer(
@@ -475,7 +476,7 @@ function updateHostComponent(
476 type,
477 oldProps,
478 newProps,
478 - childrenUnchanged,
479 + !requiresClone,
480 newChildSet,
481 );
482 if (newInstance === currentInstance) {
@@ -494,7 +495,7 @@ function updateHostComponent(
495 markUpdate(workInProgress);
496 }
497 workInProgress.stateNode = newInstance;
497 - if (childrenUnchanged) {
498 + if (!requiresClone) {
499 // If there are no other effects in this tree, we need to flag this node as having one.
500 // Even though we're not going to use it for anything.
501 // Otherwise parents won't know that there are new children to propagate upwards.