From 2d797bea92ff1e92f594ef45240a16a01f1c4486 Mon Sep 17 00:00:00 2001 From: * <8> Date: Wed, 19 Jan 2022 20:15:02 +0800 Subject: [PATCH] Match-id-3d4f22ee5c412590b9561eba180766f92a003435 --- libs/horizon/src/renderer/Types.ts | 1 - libs/horizon/src/renderer/UpdateHandler.ts | 5 +---- .../src/renderer/diff/nodeDiffComparator.ts | 20 ++++++++++--------- .../src/renderer/submit/LifeCycleHandler.ts | 5 ----- libs/horizon/src/renderer/vnode/VNodeFlags.ts | 5 +++-- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/libs/horizon/src/renderer/Types.ts b/libs/horizon/src/renderer/Types.ts index b1732fd1..2f3fad4b 100644 --- a/libs/horizon/src/renderer/Types.ts +++ b/libs/horizon/src/renderer/Types.ts @@ -2,7 +2,6 @@ export { VNode } from './vnode/VNode'; type Trigger = (A) => void; -export type ReadContextHookType = { readContext(context: ContextType): T }; export type UseStateHookType = { useState( initialState: (() => S) | S diff --git a/libs/horizon/src/renderer/UpdateHandler.ts b/libs/horizon/src/renderer/UpdateHandler.ts index 473d8fe4..d00f3ce9 100644 --- a/libs/horizon/src/renderer/UpdateHandler.ts +++ b/libs/horizon/src/renderer/UpdateHandler.ts @@ -35,11 +35,8 @@ export function newUpdate(): Update { // 将update对象加入updates export function pushUpdate(vNode: VNode, update: Update) { const updates = vNode.updates; - if (updates === null) { - return; - } - updates.push(update); + updates?.push(update); } // 根据update获取新的state diff --git a/libs/horizon/src/renderer/diff/nodeDiffComparator.ts b/libs/horizon/src/renderer/diff/nodeDiffComparator.ts index 1adf32c8..606a97d9 100644 --- a/libs/horizon/src/renderer/diff/nodeDiffComparator.ts +++ b/libs/horizon/src/renderer/diff/nodeDiffComparator.ts @@ -354,9 +354,9 @@ function diffArrayNodes( // 把剩下的currentVNode转成Map const leftChildrenMap = transLeftChildrenToMap(oldNode, rightEndOldNode); // 通过贪心算法+二分法获取最长递增子序列 - const eIndexes = []; // 记录 eIndex 值 - const result = []; // 记录最长子序列在eIndexes中的 index 值 - const preIndex = []; // 贪心算法在替换的过程中会使得数组不正确,通过记录preIndex找到正确值 + const eIndexes: Array = []; // 记录 eIndex 值 + const result: Array = []; // 记录最长子序列在eIndexes中的 index 值 + const preIndex: Array = []; // 贪心算法在替换的过程中会使得数组不正确,通过记录preIndex找到正确值 const reuseNodes = []; // 记录复用的 VNode let i = 0; for (; leftIdx < rightIdx; leftIdx++) { @@ -367,6 +367,7 @@ function diffArrayNodes( // 从Map删除,后面不会deleteVNode leftChildrenMap.delete(newNode.key || leftIdx); } + if (oldNodeFromMap !== null) { let eIndex = newNode.eIndex; eIndexes.push(eIndex); @@ -380,7 +381,7 @@ function diffArrayNodes( let middle; // 二分法找到需要替换的值 while (start < end) { - middle = Math.floor((start + end) / 2) + middle = Math.floor((start + end) / 2); if (eIndexes[result[middle]] > eIndex) { end = middle; } else { @@ -403,6 +404,7 @@ function diffArrayNodes( appendNode(newNode); } } + if (isComparing) { // 向前回溯找到正确的结果 let length = result.length; @@ -411,9 +413,9 @@ function diffArrayNodes( result[length] = prev; prev = preIndex[result[length]]; } - result.forEach(i => { + result.forEach(idx => { // 把需要复用的节点从 restNodes 中清理掉,因为不需要打 add 标记,直接复用 dom 节点 - reuseNodes[i] = null; + reuseNodes[idx] = null; }); reuseNodes.forEach(node => { if (node !== null) { @@ -490,7 +492,7 @@ function diffStringNodeHandler( firstChildVNode: VNode, isComparing: boolean ) { - let newTextNode = null; + let newTextNode: VNode | null = null; // 第一个vNode是Text,则复用 if (firstChildVNode !== null && firstChildVNode.tag === DomText) { @@ -520,7 +522,7 @@ function diffObjectNodeHandler( firstChildVNode: VNode, isComparing: boolean ) { - let canReuseNode = null; + let canReuseNode: VNode | null = null; // 通过key比对是否有可以reuse const newKey = newChild.key; @@ -535,7 +537,7 @@ function diffObjectNodeHandler( } } - let resultNode = null; + let resultNode: VNode | null = null; let startDelVNode = firstChildVNode; if (newChild.vtype === TYPE_ELEMENT) { if (canReuseNode) { diff --git a/libs/horizon/src/renderer/submit/LifeCycleHandler.ts b/libs/horizon/src/renderer/submit/LifeCycleHandler.ts index b23beb93..779f3653 100644 --- a/libs/horizon/src/renderer/submit/LifeCycleHandler.ts +++ b/libs/horizon/src/renderer/submit/LifeCycleHandler.ts @@ -186,11 +186,6 @@ function detachRef(vNode: VNode, isOldRef?: boolean) { // 卸载一个vNode,不会递归 function unmountVNode(vNode: VNode): void { - // TODO 暂时用于规避error处理逻辑,后续删除 - if (vNode.flags.Addition) { - return; - } - switch (vNode.tag) { case FunctionComponent: case ForwardRef: diff --git a/libs/horizon/src/renderer/vnode/VNodeFlags.ts b/libs/horizon/src/renderer/vnode/VNodeFlags.ts index 7259beb7..15e25131 100644 --- a/libs/horizon/src/renderer/vnode/VNodeFlags.ts +++ b/libs/horizon/src/renderer/vnode/VNodeFlags.ts @@ -19,7 +19,7 @@ export const ShouldCapture = 'ShouldCapture'; // For suspense export const ForceUpdate = 'ForceUpdate'; -const flagArr = [Addition, Update, Deletion, ResetText, Callback, DidCapture, Ref, Snapshot, Interrupted, ShouldCapture, ForceUpdate]; +const FlagArr = [Addition, Update, Deletion, ResetText, Callback, DidCapture, Ref, Snapshot, Interrupted, ShouldCapture, ForceUpdate]; const LifecycleEffectArr = [Update, Callback, Ref, Snapshot]; @@ -38,9 +38,10 @@ export class FlagUtils { } static hasAnyFlag(node: VNode) { // 有标志位 let keyFlag = false; - flagArr.forEach(key => { + FlagArr.forEach(key => { if (node.flags[key]) { keyFlag = true; + return; } }); return keyFlag;