From 25c3a84991bd95fdd406026bf921a3212371dfcb Mon Sep 17 00:00:00 2001 From: * <8> Date: Fri, 21 Jan 2022 10:00:29 +0800 Subject: [PATCH] Match-id-dda58446000d7d98ad097c8e72465ec8e13cfe58 --- libs/horizon/src/renderer/Renderer.ts | 2 +- .../src/renderer/render/ContextProvider.ts | 52 +++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/libs/horizon/src/renderer/Renderer.ts b/libs/horizon/src/renderer/Renderer.ts index 32c3cc0f..d5cf89da 100644 --- a/libs/horizon/src/renderer/Renderer.ts +++ b/libs/horizon/src/renderer/Renderer.ts @@ -37,7 +37,7 @@ export function startUpdate( launchUpdateFromVNode(treeRoot); } -export function getFirstCustomDom(treeRoot: VNode | undefined | null): Element | Text | null { +export function getFirstCustomDom(treeRoot?: VNode | null): Element | Text | null { if (treeRoot?.child) { return treeRoot.child.realNode; } diff --git a/libs/horizon/src/renderer/render/ContextProvider.ts b/libs/horizon/src/renderer/render/ContextProvider.ts index c60156c7..b980273c 100644 --- a/libs/horizon/src/renderer/render/ContextProvider.ts +++ b/libs/horizon/src/renderer/render/ContextProvider.ts @@ -14,6 +14,32 @@ import {launchUpdateFromVNode} from '../TreeBuilder'; import {onlyUpdateChildVNodes} from '../vnode/VNodeCreator'; import {setParentsChildShouldUpdate} from '../vnode/VNodeShouldUpdate'; +// 从当前子节点开始向下遍历,找到消费此context的组件,并更新 +function handleContextChange(processing: VNode, context: ContextType): void { + const vNode = processing.child; + if (vNode === null) { + return; + } + + let isMatch = false; + + // 从vNode开始遍历 + travelVNodeTree(vNode, (node) => { + const depContexts = node.depContexts; + if (depContexts.length) { + isMatch = matchDependencies(depContexts, context, node) ?? isMatch; + } + }, (node) => { + // 如果这是匹配的provider,则不要更深入地扫描 + return node.tag === ContextProvider && node.type === processing.type; + }, processing); + + // 找到了依赖context的子节点,触发一次更新 + if (isMatch) { + launchUpdateFromVNode(processing); + } +} + function captureContextProvider(processing: VNode): VNode | null { const providerType: ProviderType = processing.type; const contextType: ContextType = providerType._context; @@ -77,29 +103,3 @@ function matchDependencies(depContexts, context, vNode): boolean { return false; } - -// 从当前子节点开始向下遍历,找到消费此context的组件,并更新 -function handleContextChange(processing: VNode, context: ContextType): void { - const vNode = processing.child; - if (vNode === null) { - return; - } - - let isMatch = false; - - // 从vNode开始遍历 - travelVNodeTree(vNode, (node) => { - const depContexts = node.depContexts; - if (depContexts.length) { - isMatch = matchDependencies(depContexts, context, node) ?? isMatch; - } - }, (node) => { - // 如果这是匹配的provider,则不要更深入地扫描 - return node.tag === ContextProvider && node.type === processing.type; - }, processing); - - // 找到了依赖context的子节点,触发一次更新 - if (isMatch) { - launchUpdateFromVNode(processing); - } -}