将新旧节点分成了两个数组并设置四个指针分别指向它们的头尾,分别是新前、新后、旧前、旧后。
只要新前<新后且旧前<旧后就开始比较
步骤:
- 若遇到空节点,前面的节点指针向后移动,后面的节点指针向前移动 若新前节点=旧前节点,新前、旧前指针向后移动
- 若旧前=旧后,旧前、旧后指针向前移动 若新前=旧后,先将旧后对应的节点移动到新前指针位置的前面,然后新前指针向后移动、旧后指针向前移动
- 若新后=旧前,先将旧前对应的节点移动到新后指针位置的后面,然后新后指针向前移动,旧前指针向后移动
- 若上述条件都不符合,则使用暴力解法,让当前的新节点逐个与旧数组中的节点比较,先将旧数组转换成 key-index 对应的map,则先判断当前新节点是否有key值,若存在则找map对应的index,若不存在则使用findIdxInold函数找到该节点对应的下标,若不存在则新增节点,若存在则继续比较,若相等则将下标对应的旧节点移动到旧前的前面,否则新增一个节点。
- 循环以上步骤直到新前>新后或旧前>旧后退出循环
- 如果新前>新后,则删除旧前到旧后之间的节点
- 如果旧前>旧后,则新建新前到旧后之间的节点
function updateChildren(parentElm, oldCh, newCh, insertedVnodeQueue, removeOnly) {
let oldStartIdx = 0
let newStartIdx = 0
let oldEndIdx = oldCh.length - 1
let oldStartVnode = oldCh[0]
let oldEndVnode = oldCh[oldEndIdx]
let newEndIdx = newCh.length - 1
let newStartVnode = newCh[0]
let newEndVnode = newCh[newEndIdx]
let oldKeyToIdx, idxInOld, vnodeToMove, refElm
// removeOnly is a special flag used only by <transition-group>
// to ensure removed elements stay in correct relative positions
// during leaving transitions
const canMove = !removeOnly
if (process.env.NODE_ENV !== 'production') {
checkDuplicateKeys(newCh)
}
while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
// 遇到空,指针移动,开始指针向后移动,结束指针向前移动
if (isUndef(oldStartVnode)) {
oldStartVnode = oldCh[++oldStartIdx] // Vnode has been moved left
} else if (isUndef(oldEndVnode)) {
oldEndVnode = oldCh[--oldEndIdx]
// 新前和旧前是相同节点,指针后移
} else if (sameVnode(oldStartVnode, newStartVnode)) {
// 继续深度patch
patchVnode(oldStartVnode, newStartVnode, insertedVnodeQueue, newCh, newStartIdx)
oldStartVnode = oldCh[++oldStartIdx]
newStartVnode = newCh[++newStartIdx]
// 新后和旧后是相同节点,指针前移
} else if (sameVnode(oldEndVnode, newEndVnode)) {
// 继续深度patch
patchVnode(oldEndVnode, newEndVnode, insertedVnodeQueue, newCh, newEndIdx)
oldEndVnode = oldCh[--oldEndIdx]
newEndVnode = newCh[--newEndIdx]
// 旧前和新后相等,将旧前指针对应的节点移动到新后指向节点的后面,且旧前指针向后移动,新后指针向前移动
} else if (sameVnode(oldStartVnode, newEndVnode)) { // Vnode moved right
patchVnode(oldStartVnode, newEndVnode, insertedVnodeQueue, newCh, newEndIdx)
canMove && nodeOps.insertBefore(parentElm, oldStartVnode.elm, nodeOps.nextSibling(oldEndVnode.elm))
oldStartVnode = oldCh[++oldStartIdx]
newEndVnode = newCh[--newEndIdx]
// 旧后和新前相等,将旧后指针对应的节点移到新前指针对应节点的前面,且旧后指针向前移动,新前指针向后移动
} else if (sameVnode(oldEndVnode, newStartVnode)) { // Vnode moved left
patchVnode(oldEndVnode, newStartVnode, insertedVnodeQueue, newCh, newStartIdx)
canMove && nodeOps.insertBefore(parentElm, oldEndVnode.elm, oldStartVnode.elm)
oldEndVnode = oldCh[--oldEndIdx]
newStartVnode = newCh[++newStartIdx]
// 若上述条件都不符合
} else {
// 首先将旧数组转换成 节点 key-下标 的map (key值是唯一的)
if (isUndef(oldKeyToIdx)) oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx)
// 判断当前新前指针指向节点是否存在key,若存在直接在旧数组对应的map中找到该key对应的下标,若不存在遍历旧数组中剩余的节点,找到新节点对应匹配的下标
idxInOld = isDef(newStartVnode.key)
? oldKeyToIdx[newStartVnode.key]
: findIdxInOld(newStartVnode, oldCh, oldStartIdx, oldEndIdx)
// 如果下标为空,代表该新节点是一个新节点,需要新增节点
if (isUndef(idxInOld)) { // New element
createElm(newStartVnode, insertedVnodeQueue, parentElm, oldStartVnode.elm, false, newCh, newStartIdx)
} else {
// 若不为空则把对应的旧节点和新节点比较,看两个节点是否是相同节点
vnodeToMove = oldCh[idxInOld]
if (sameVnode(vnodeToMove, newStartVnode)) {
// 若相同,将当前旧节点位置置空,并把旧节点移动到旧前的前一位
patchVnode(vnodeToMove, newStartVnode, insertedVnodeQueue, newCh, newStartIdx)
oldCh[idxInOld] = undefined
canMove && nodeOps.insertBefore(parentElm, vnodeToMove.elm, oldStartVnode.elm)
} else {
// 若不同,则新建一个节点
// same key but different element. treat as new element
createElm(newStartVnode, insertedVnodeQueue, parentElm, oldStartVnode.elm, false, newCh, newStartIdx)
}
}
// 向后移动新前指针
newStartVnode = newCh[++newStartIdx]
}
}
// 如果旧前>旧后,而新前和新后还没重叠,则新增新前到新后之间的节点
if (oldStartIdx > oldEndIdx) {
refElm = isUndef(newCh[newEndIdx + 1]) ? null : newCh[newEndIdx + 1].elm
addVnodes(parentElm, refElm, newCh, newStartIdx, newEndIdx, insertedVnodeQueue)
} else if (newStartIdx > newEndIdx) {
// 如果新前>新后,而旧前和旧后还没重叠,则删除旧前到旧后之间的节点
removeVnodes(oldCh, oldStartIdx, oldEndIdx)
}
}