Skip to content

Commit

Permalink
Merge pull request #2941 from preactjs/simplify-diffChildren-oldDom
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Jan 19, 2021
2 parents 139a862 + 759b1eb commit f6c44cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 33 deletions.
31 changes: 1 addition & 30 deletions src/diff/children.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { diff, unmount, applyRef } from './index';
import { createVNode, Fragment } from '../create-element';
import { EMPTY_OBJ, EMPTY_ARR } from '../constants';
import { removeNode } from '../util';
import { getDomSibling } from '../component';

/**
Expand Down Expand Up @@ -44,20 +43,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 +170,6 @@ export function diffChildren(
childVNode,
oldVNode,
oldChildren,
excessDomChildren,
newDom,
oldDom
);
Expand Down Expand Up @@ -228,13 +212,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 +256,6 @@ function reorderChildren(childVNode, oldDom, parentDom) {
vnode,
vnode,
childVNode._children,
null,
vnode._dom,
oldDom
);
Expand Down Expand Up @@ -314,7 +290,6 @@ function placeChild(
childVNode,
oldVNode,
oldChildren,
excessDomChildren,
newDom,
oldDom
) {
Expand All @@ -331,14 +306,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ export function render(vnode, parentDom, replaceNode) {
? [replaceNode]
: oldVNode
? null
: parentDom.childNodes.length
: parentDom.firstChild
? EMPTY_ARR.slice.call(parentDom.childNodes)
: null,
commitQueue,
(!isHydrating && replaceNode) || EMPTY_OBJ,
!isHydrating && replaceNode
? replaceNode
: oldVNode
? oldVNode._dom
: parentDom.firstChild,
isHydrating
);

Expand Down

0 comments on commit f6c44cd

Please sign in to comment.