Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify diffChildren's handling of excessDomChildren and oldDom #2941

Merged
merged 5 commits into from Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 1 addition & 29 deletions src/diff/children.js
Expand Up @@ -44,20 +44,6 @@ export function diffChildren(

let oldChildrenLength = oldChildren.length;

// Only in very specific places should this logic be invoked (top level `render` and `diffElementNodes`).
// I'm using `EMPTY_OBJ` to signal when `diffChildren` is invoked in these situations. I can't use `null`
// for this purpose, because `null` is a valid value for `oldDom` which can mean to skip to this logic
// (e.g. if mounting a new tree in which the old DOM should be ignored (usually for Fragments).
if (oldDom == EMPTY_OBJ) {
if (excessDomChildren != null) {
oldDom = excessDomChildren[0];
} else if (oldChildrenLength) {
oldDom = getDomSibling(oldParentVNode, 0);
} else {
oldDom = null;
}
}

newParentVNode._children = [];
for (i = 0; i < renderResult.length; i++) {
childVNode = renderResult[i];
Expand Down Expand Up @@ -185,7 +171,6 @@ export function diffChildren(
childVNode,
oldVNode,
oldChildren,
excessDomChildren,
newDom,
oldDom
);
Expand Down Expand Up @@ -228,13 +213,6 @@ export function diffChildren(

newParentVNode._dom = firstChildDom;

// Remove children that are not part of any vnode.
if (excessDomChildren != null && typeof newParentVNode.type != 'function') {
for (i = excessDomChildren.length; i--; ) {
if (excessDomChildren[i] != null) removeNode(excessDomChildren[i]);
}
}

// Remove remaining oldChildren if there are any.
for (i = oldChildrenLength; i--; ) {
if (oldChildren[i] != null) {
Expand Down Expand Up @@ -279,7 +257,6 @@ function reorderChildren(childVNode, oldDom, parentDom) {
vnode,
vnode,
childVNode._children,
null,
vnode._dom,
oldDom
);
Expand Down Expand Up @@ -314,7 +291,6 @@ function placeChild(
childVNode,
oldVNode,
oldChildren,
excessDomChildren,
newDom,
oldDom
) {
Expand All @@ -331,14 +307,10 @@ function placeChild(
// can clean up the property
childVNode._nextDom = undefined;
} else if (
excessDomChildren == oldVNode ||
oldVNode == null ||
newDom != oldDom ||
newDom.parentNode == null
) {
// NOTE: excessDomChildren==oldVNode above:
// This is a compression of excessDomChildren==null && oldVNode==null!
// The values only have the same type when `null`.

outer: if (oldDom == null || oldDom.parentNode !== parentDom) {
parentDom.appendChild(newDom);
nextDom = null;
Expand Down
9 changes: 8 additions & 1 deletion src/diff/index.js
Expand Up @@ -409,9 +409,16 @@ function diffElementNodes(
newVNode.type === 'foreignObject' ? false : isSvg,
excessDomChildren,
commitQueue,
EMPTY_OBJ,
dom.firstChild,
isHydrating
);

// Remove children that are not part of any vnode.
if (excessDomChildren != null) {
for (i = excessDomChildren.length; i--; ) {
if (excessDomChildren[i] != null) removeNode(excessDomChildren[i]);
}
}
}

// (as above, don't diff props during hydration)
Expand Down
8 changes: 6 additions & 2 deletions src/render.js
Expand Up @@ -45,11 +45,15 @@ export function render(vnode, parentDom, replaceNode) {
? [replaceNode]
: oldVNode
? null
: parentDom.childNodes.length
: parentDom.firstChild
? EMPTY_ARR.slice.call(parentDom.childNodes)
: null,
commitQueue,
replaceNode || EMPTY_OBJ,
!isHydrating && replaceNode
? replaceNode
: oldVNode
? oldVNode._dom
: parentDom.firstChild,
isHydrating
);

Expand Down