Match-id-816ac7b4af214e4dae368f74f23df0b478d2c62f
This commit is contained in:
parent
0ff6a0bddb
commit
b55ab599b2
|
@ -215,11 +215,11 @@ function setCIndex(vNode: VNode, idx: number) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// diff数组类型的节点,核心算法
|
// diff数组类型的节点,核心算法
|
||||||
function diffArrayNodes(
|
function diffArrayNodesHandler(
|
||||||
parentNode: VNode,
|
parentNode: VNode,
|
||||||
firstChild: VNode | null,
|
firstChild: VNode | null,
|
||||||
newChildren: Array<any>,
|
newChildren: Array<any>,
|
||||||
isComparing: boolean = true
|
isComparing: boolean
|
||||||
): VNode | null {
|
): VNode | null {
|
||||||
let resultingFirstChild: VNode | null = null;
|
let resultingFirstChild: VNode | null = null;
|
||||||
|
|
||||||
|
@ -241,6 +241,8 @@ function diffArrayNodes(
|
||||||
prevNewNode = newNode;
|
prevNewNode = newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let canBeReuse;
|
||||||
|
let newNode;
|
||||||
// 1. 从左侧开始比对currentVNode和newChildren,若不能复用则跳出循环
|
// 1. 从左侧开始比对currentVNode和newChildren,若不能复用则跳出循环
|
||||||
for (; oldNode !== null && leftIdx < newChildren.length; leftIdx++) {
|
for (; oldNode !== null && leftIdx < newChildren.length; leftIdx++) {
|
||||||
if (oldNode.eIndex > leftIdx) {
|
if (oldNode.eIndex > leftIdx) {
|
||||||
|
@ -251,14 +253,14 @@ function diffArrayNodes(
|
||||||
nextOldNode = oldNode.next;
|
nextOldNode = oldNode.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
const canBeReuse = checkCanReuseNode(oldNode, newChildren[leftIdx]);
|
canBeReuse = checkCanReuseNode(oldNode, newChildren[leftIdx]);
|
||||||
// 不能复用,break
|
// 不能复用,break
|
||||||
if (!canBeReuse) {
|
if (!canBeReuse) {
|
||||||
oldNode = oldNode ?? nextOldNode;
|
oldNode = oldNode ?? nextOldNode;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newNode = getNewNode(parentNode, newChildren[leftIdx], oldNode);
|
newNode = getNewNode(parentNode, newChildren[leftIdx], oldNode);
|
||||||
// 没有生成新节点,break
|
// 没有生成新节点,break
|
||||||
if (!newNode) {
|
if (!newNode) {
|
||||||
oldNode = oldNode ?? nextOldNode;
|
oldNode = oldNode ?? nextOldNode;
|
||||||
|
@ -286,19 +288,20 @@ function diffArrayNodes(
|
||||||
let rightOldIndex: number | null = rightRemainingOldChildren.length - 1;
|
let rightOldIndex: number | null = rightRemainingOldChildren.length - 1;
|
||||||
|
|
||||||
// 2. 从右侧开始比对currentVNode和newChildren,若不能复用则跳出循环
|
// 2. 从右侧开始比对currentVNode和newChildren,若不能复用则跳出循环
|
||||||
|
let rightOldNode;
|
||||||
for (; rightIdx > leftIdx; rightIdx--) {
|
for (; rightIdx > leftIdx; rightIdx--) {
|
||||||
const rightOldNode = rightRemainingOldChildren[rightOldIndex];
|
rightOldNode = rightRemainingOldChildren[rightOldIndex];
|
||||||
if (rightOldIndex < 0 || rightOldNode === null) {
|
if (rightOldIndex < 0 || rightOldNode === null) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const canBeReuse = checkCanReuseNode(rightOldNode, newChildren[rightIdx - 1]);
|
canBeReuse = checkCanReuseNode(rightOldNode, newChildren[rightIdx - 1]);
|
||||||
// 不能复用,break
|
// 不能复用,break
|
||||||
if (!canBeReuse) {
|
if (!canBeReuse) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newNode = getNewNode(parentNode, newChildren[rightIdx - 1], rightOldNode);
|
newNode = getNewNode(parentNode, newChildren[rightIdx - 1], rightOldNode);
|
||||||
// 没有生成新节点,break
|
// 没有生成新节点,break
|
||||||
if (newNode === null) {
|
if (newNode === null) {
|
||||||
break;
|
break;
|
||||||
|
@ -345,7 +348,7 @@ function diffArrayNodes(
|
||||||
// 4. 新节点还有一部分,但是老节点已经没有了
|
// 4. 新节点还有一部分,但是老节点已经没有了
|
||||||
if (oldNode === null) {
|
if (oldNode === null) {
|
||||||
for (; leftIdx < rightIdx; leftIdx++) {
|
for (; leftIdx < rightIdx; leftIdx++) {
|
||||||
const newNode = getNewNode(parentNode, newChildren[leftIdx], null);
|
newNode = getNewNode(parentNode, newChildren[leftIdx], null);
|
||||||
|
|
||||||
if (newNode !== null) {
|
if (newNode !== null) {
|
||||||
theLastPosition = setVNodeAdditionFlag(newNode, theLastPosition, isComparing);
|
theLastPosition = setVNodeAdditionFlag(newNode, theLastPosition, isComparing);
|
||||||
|
@ -371,9 +374,11 @@ function diffArrayNodes(
|
||||||
const preIndex: Array<number> = []; // 贪心算法在替换的过程中会使得数组不正确,通过记录preIndex找到正确值
|
const preIndex: Array<number> = []; // 贪心算法在替换的过程中会使得数组不正确,通过记录preIndex找到正确值
|
||||||
const reuseNodes = []; // 记录复用的 VNode
|
const reuseNodes = []; // 记录复用的 VNode
|
||||||
let i = 0;
|
let i = 0;
|
||||||
|
let oldNodeFromMap;
|
||||||
|
let last;
|
||||||
for (; leftIdx < rightIdx; leftIdx++) {
|
for (; leftIdx < rightIdx; leftIdx++) {
|
||||||
const oldNodeFromMap = getOldNodeFromMap(leftChildrenMap, leftIdx, newChildren[leftIdx]);
|
oldNodeFromMap = getOldNodeFromMap(leftChildrenMap, leftIdx, newChildren[leftIdx]);
|
||||||
const newNode = getNewNode(parentNode, newChildren[leftIdx], oldNodeFromMap);
|
newNode = getNewNode(parentNode, newChildren[leftIdx], oldNodeFromMap);
|
||||||
if (newNode !== null) {
|
if (newNode !== null) {
|
||||||
if (isComparing && !newNode.isCreated) {
|
if (isComparing && !newNode.isCreated) {
|
||||||
// 从Map删除,后面不会deleteVNode
|
// 从Map删除,后面不会deleteVNode
|
||||||
|
@ -383,7 +388,7 @@ function diffArrayNodes(
|
||||||
if (oldNodeFromMap !== null) {
|
if (oldNodeFromMap !== null) {
|
||||||
let eIndex = newNode.eIndex;
|
let eIndex = newNode.eIndex;
|
||||||
eIndexes.push(eIndex);
|
eIndexes.push(eIndex);
|
||||||
const last: number | undefined = eIndexes[result[result.length - 1]];
|
last = eIndexes[result[result.length - 1]];
|
||||||
if (eIndex > last || last === undefined) { // 大的 eIndex直接放在最后
|
if (eIndex > last || last === undefined) { // 大的 eIndex直接放在最后
|
||||||
preIndex[i] = result[result.length - 1];
|
preIndex[i] = result[result.length - 1];
|
||||||
result.push(i);
|
result.push(i);
|
||||||
|
@ -461,16 +466,6 @@ function setVNodesCIndex(startChild: VNode, startIdx: number) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 新节点是数组类型
|
|
||||||
function diffArrayNodesHandler(
|
|
||||||
parentNode: VNode,
|
|
||||||
firstChild: VNode | null,
|
|
||||||
newChildren: Array<any>,
|
|
||||||
isComparing: boolean = true
|
|
||||||
): VNode | null {
|
|
||||||
return diffArrayNodes(parentNode, firstChild, newChildren, isComparing);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 新节点是迭代器类型
|
// 新节点是迭代器类型
|
||||||
function diffIteratorNodesHandler(
|
function diffIteratorNodesHandler(
|
||||||
parentNode: VNode,
|
parentNode: VNode,
|
||||||
|
@ -489,7 +484,7 @@ function diffIteratorNodesHandler(
|
||||||
result = iteratorObj.next();
|
result = iteratorObj.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
return diffArrayNodes(parentNode, firstChild, childrenArray, isComparing);
|
return diffArrayNodesHandler(parentNode, firstChild, childrenArray, isComparing);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 新节点是字符串类型
|
// 新节点是字符串类型
|
||||||
|
|
Loading…
Reference in New Issue