From 337b72940207dea1ba6a11d191c5c0b1145c3dfe Mon Sep 17 00:00:00 2001 From: * <8> Date: Fri, 18 Mar 2022 10:35:40 +0800 Subject: [PATCH] Match-id-0ec66063b933d6f41b5ae51bd1357e88e68b67ae --- .../src/renderer/diff/nodeDiffComparator.ts | 6 +++--- .../src/renderer/vnode/VNodeCreator.ts | 21 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/libs/horizon/src/renderer/diff/nodeDiffComparator.ts b/libs/horizon/src/renderer/diff/nodeDiffComparator.ts index 59d4829f..db920f79 100644 --- a/libs/horizon/src/renderer/diff/nodeDiffComparator.ts +++ b/libs/horizon/src/renderer/diff/nodeDiffComparator.ts @@ -376,7 +376,7 @@ function diffArrayNodesHandler( if (rightNewNode) { appendNode(rightNewNode); - setVNodesCIndex(rightNewNode.next, prevNewNode.cIndex + 1); + setVNodesCIndex(rightNewNode.next, rightNewNode.cIndex + 1); } return resultingFirstChild; @@ -464,14 +464,14 @@ function diffArrayNodesHandler( if (rightNewNode) { appendNode(rightNewNode); - setVNodesCIndex(rightNewNode.next, prevNewNode.cIndex + 1); + setVNodesCIndex(rightNewNode.next, rightNewNode.cIndex + 1); } return resultingFirstChild; } // 设置vNode中的cIndex属性,cIndex是节点在children中的位置 -function setVNodesCIndex(startChild: VNode, startIdx: number) { +function setVNodesCIndex(startChild: VNode | null, startIdx: number) { let node: VNode | null = startChild; let idx = startIdx; diff --git a/libs/horizon/src/renderer/vnode/VNodeCreator.ts b/libs/horizon/src/renderer/vnode/VNodeCreator.ts index 5428aa4a..865f791e 100644 --- a/libs/horizon/src/renderer/vnode/VNodeCreator.ts +++ b/libs/horizon/src/renderer/vnode/VNodeCreator.ts @@ -187,15 +187,12 @@ export function onlyUpdateChildVNodes(processing: VNode): VNode | null { return processing.child; } - // 当跳过子树更新时,需要更新子树path + // 当跳过子树更新时,父节点path更新时,需要更新所有子树path if (processing.child && processing.path !== processing.child.path.slice(0, processing.path.length)) { - // 更新子树path - const queue: VNode[] = [processing]; - while (queue.length) { - const vNode = queue.shift()!; + // bfs更新子树path + const queue: VNode[] = []; - // 忽略processing path重新计算 - vNode.path = vNode.parent.path + vNode.cIndex; + const putChildrenIntoQueue = (vNode: VNode) => { const child = vNode.child; if (child) { queue.push(child); @@ -206,6 +203,16 @@ export function onlyUpdateChildVNodes(processing: VNode): VNode | null { } } } + + putChildrenIntoQueue(processing.child); + + while (queue.length) { + const vNode = queue.shift()!; + + vNode.path = vNode.parent.path + vNode.cIndex; + + putChildrenIntoQueue(vNode) + } } // 子树无需工作 return null;