Match-id-2a7374e4246145afb0681f473ee89cd06e492eb7
This commit is contained in:
parent
a4f376b84b
commit
867cb48365
|
@ -3,11 +3,11 @@
|
||||||
* 在深度遍历过程中,begin阶段会修改一些全局的值,在complete阶段会恢复。
|
* 在深度遍历过程中,begin阶段会修改一些全局的值,在complete阶段会恢复。
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type {VNode, ContextType} from './Types';
|
import type { VNode, ContextType } from './Types';
|
||||||
import type {Container} from '../dom/DOMOperator';
|
import type { Container } from '../dom/DOMOperator';
|
||||||
|
|
||||||
import {getNSCtx} from '../dom/DOMOperator';
|
import { getNSCtx } from '../dom/DOMOperator';
|
||||||
import {ContextProvider} from './vnode/VNodeTags';
|
import { ContextProvider } from './vnode/VNodeTags';
|
||||||
|
|
||||||
// 保存的是“http://www.w3.org/1999/xhtml”或“http://www.w3.org/2000/svg”,
|
// 保存的是“http://www.w3.org/1999/xhtml”或“http://www.w3.org/2000/svg”,
|
||||||
// 用于识别是使用document.createElement()还是使用document.createElementNS()创建DOM
|
// 用于识别是使用document.createElement()还是使用document.createElementNS()创建DOM
|
||||||
|
@ -18,16 +18,8 @@ const CTX_CONTEXT = 'CTX_CONTEXT';
|
||||||
|
|
||||||
// 旧版context API,是否更改。
|
// 旧版context API,是否更改。
|
||||||
const CTX_OLD_CHANGE = 'CTX_OLD_CHANGE';
|
const CTX_OLD_CHANGE = 'CTX_OLD_CHANGE';
|
||||||
// 旧版context API,保存的是的当前组件提供给子组件使用的context。
|
let ctxOldChange = false;
|
||||||
const CTX_OLD_CONTEXT = 'CTX_OLD_CONTEXT';
|
let ctxNamespace = '';
|
||||||
// 旧版context API,保存的是的上一个提供者提供给后代组件使用的context。
|
|
||||||
const CTX_OLD_PREVIOUS_CONTEXT = 'CTX_OLD_PREVIOUS_CONTEXT';
|
|
||||||
|
|
||||||
let ctxNamespace: string = '';
|
|
||||||
|
|
||||||
let ctxOldContext: Object = {};
|
|
||||||
let ctxOldChange: Boolean = false;
|
|
||||||
let ctxOldPreviousContext: Object = {};
|
|
||||||
|
|
||||||
function setContext(vNode: VNode, contextName, value) {
|
function setContext(vNode: VNode, contextName, value) {
|
||||||
if (vNode.contexts === null) {
|
if (vNode.contexts === null) {
|
||||||
|
@ -38,6 +30,7 @@ function setContext(vNode: VNode, contextName, value) {
|
||||||
vNode.contexts[contextName] = value;
|
vNode.contexts[contextName] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getContext(vNode: VNode, contextName) {
|
function getContext(vNode: VNode, contextName) {
|
||||||
if (vNode.contexts !== null) {
|
if (vNode.contexts !== null) {
|
||||||
return vNode.contexts[contextName];
|
return vNode.contexts[contextName];
|
||||||
|
@ -87,44 +80,6 @@ function recoverParentsContextCtx(vNode: VNode) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ctxOldContext是 旧context提供者的context
|
|
||||||
function setVNodeOldContext(providerVNode: VNode, context: Object) {
|
|
||||||
setContext(providerVNode, CTX_OLD_CONTEXT, context);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getVNodeOldContext(vNode: VNode) {
|
|
||||||
return getContext(vNode, CTX_OLD_CONTEXT);
|
|
||||||
}
|
|
||||||
|
|
||||||
function setOldContextCtx(providerVNode: VNode, context: Object) {
|
|
||||||
setVNodeOldContext(providerVNode, context);
|
|
||||||
ctxOldContext = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getOldContextCtx() {
|
|
||||||
return ctxOldContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resetOldContextCtx(vNode: VNode) {
|
|
||||||
ctxOldContext = getVNodeOldContext(vNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
function setVNodeOldPreviousContext(providerVNode: VNode, context: Object) {
|
|
||||||
setContext(providerVNode, CTX_OLD_PREVIOUS_CONTEXT, context);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getVNodeOldPreviousContext(vNode: VNode) {
|
|
||||||
return getContext(vNode, CTX_OLD_PREVIOUS_CONTEXT);
|
|
||||||
}
|
|
||||||
|
|
||||||
function setOldPreviousContextCtx(context: Object) {
|
|
||||||
ctxOldPreviousContext = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getOldPreviousContextCtx() {
|
|
||||||
return ctxOldPreviousContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setContextChangeCtx(providerVNode: VNode, didChange: boolean) {
|
function setContextChangeCtx(providerVNode: VNode, didChange: boolean) {
|
||||||
setContext(providerVNode, CTX_OLD_CHANGE, didChange);
|
setContext(providerVNode, CTX_OLD_CHANGE, didChange);
|
||||||
ctxOldChange = didChange;
|
ctxOldChange = didChange;
|
||||||
|
@ -145,18 +100,9 @@ export {
|
||||||
setContextCtx,
|
setContextCtx,
|
||||||
resetContextCtx,
|
resetContextCtx,
|
||||||
recoverParentsContextCtx,
|
recoverParentsContextCtx,
|
||||||
setOldContextCtx,
|
|
||||||
getOldContextCtx,
|
|
||||||
resetOldContextCtx,
|
|
||||||
setContextChangeCtx,
|
setContextChangeCtx,
|
||||||
getContextChangeCtx,
|
getContextChangeCtx,
|
||||||
resetContextChangeCtx,
|
resetContextChangeCtx,
|
||||||
setOldPreviousContextCtx,
|
|
||||||
getOldPreviousContextCtx,
|
|
||||||
setVNodeOldContext,
|
|
||||||
getVNodeOldContext,
|
|
||||||
setVNodeOldPreviousContext,
|
|
||||||
getVNodeOldPreviousContext,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,101 +0,0 @@
|
||||||
import type {VNode} from '../../Types';
|
|
||||||
|
|
||||||
import {
|
|
||||||
setOldContextCtx,
|
|
||||||
setContextChangeCtx,
|
|
||||||
getOldContextCtx,
|
|
||||||
resetOldContextCtx,
|
|
||||||
resetContextChangeCtx,
|
|
||||||
setOldPreviousContextCtx,
|
|
||||||
getOldPreviousContextCtx,
|
|
||||||
setVNodeOldContext,
|
|
||||||
getVNodeOldContext,
|
|
||||||
setVNodeOldPreviousContext,
|
|
||||||
getVNodeOldPreviousContext,
|
|
||||||
} from '../../ContextSaver';
|
|
||||||
|
|
||||||
const emptyObject = {};
|
|
||||||
|
|
||||||
// 判断是否是过时的context的提供者
|
|
||||||
export function isOldProvider(comp: Function): boolean {
|
|
||||||
// @ts-ignore
|
|
||||||
const childContextTypes = comp.childContextTypes;
|
|
||||||
return childContextTypes !== null && childContextTypes !== undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 判断是否是过时的context的消费者
|
|
||||||
export function isOldConsumer(comp: Function): boolean {
|
|
||||||
// @ts-ignore
|
|
||||||
const contextTypes = comp.contextTypes;
|
|
||||||
return contextTypes !== null && contextTypes !== undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是旧版context提供者,则缓存两个全局变量,上一个提供者提供的context和当前提供者提供的context
|
|
||||||
export function cacheOldCtx(processing: VNode, hasOldContext: any): void {
|
|
||||||
// 每一个context提供者都会更新ctxOldContext
|
|
||||||
if (hasOldContext) {
|
|
||||||
setOldPreviousContextCtx(getOldContextCtx());
|
|
||||||
|
|
||||||
const vNodeContext = getVNodeOldContext(processing) || emptyObject;
|
|
||||||
setOldContextCtx(processing, vNodeContext);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取当前组件可以消费的context
|
|
||||||
export function getOldContext(processing: VNode, clazz: Function, ifProvider: boolean) {
|
|
||||||
const type = processing.type;
|
|
||||||
// 不是context消费者, 则直接返回空对象
|
|
||||||
if (!isOldConsumer(type)) {
|
|
||||||
return emptyObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 当组件既是提供者,也是消费者时,取上一个context,不能直接取最新context,因为已经被更新为当前组件的context;
|
|
||||||
// 当组件只是消费者时,则取最新context
|
|
||||||
const parentContext = (ifProvider && isOldProvider(clazz)) ?
|
|
||||||
getOldPreviousContextCtx() :
|
|
||||||
getOldContextCtx();
|
|
||||||
|
|
||||||
// 除非父级context更改,否则不需要重新创建子context,直接取对应节点上存的。
|
|
||||||
if (getVNodeOldPreviousContext(processing) === parentContext) {
|
|
||||||
return getVNodeOldContext(processing);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 从父的context中取出子定义的context
|
|
||||||
const context = {};
|
|
||||||
for (const key in type.contextTypes) {
|
|
||||||
context[key] = parentContext[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
// 缓存当前组件的context,最近祖先传递下来context,当前可消费的context
|
|
||||||
setVNodeOldPreviousContext(processing, parentContext);
|
|
||||||
setVNodeOldContext(processing, context);
|
|
||||||
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 重置context
|
|
||||||
export function resetOldCtx(vNode: VNode): void {
|
|
||||||
resetOldContextCtx(vNode);
|
|
||||||
resetContextChangeCtx(vNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 当前组件是提供者,则需要合并祖先context和当前组件提供的context
|
|
||||||
function handleContext(vNode: VNode, parentContext: Object): Object {
|
|
||||||
const instance = vNode.realNode;
|
|
||||||
|
|
||||||
if (typeof instance.getChildContext !== 'function') {
|
|
||||||
return parentContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 合并祖先提供的context和当前组件提供的context
|
|
||||||
return {...parentContext, ...instance.getChildContext()};
|
|
||||||
}
|
|
||||||
|
|
||||||
// 当前组件是context提供者,更新时,需要合并祖先context和当前组件提供的context
|
|
||||||
export function updateOldContext(vNode: VNode): void {
|
|
||||||
const ctx = handleContext(vNode, getOldPreviousContextCtx());
|
|
||||||
// 更新context,给子组件用的context
|
|
||||||
setOldContextCtx(vNode, ctx);
|
|
||||||
// 标记更改
|
|
||||||
setContextChangeCtx(vNode, true);
|
|
||||||
}
|
|
|
@ -1,8 +1,6 @@
|
||||||
import type { VNode } from '../Types';
|
import type { VNode } from '../Types';
|
||||||
|
|
||||||
import {cacheOldCtx, isOldProvider} from '../components/context/CompatibleContext';
|
|
||||||
import {
|
import {
|
||||||
ClassComponent,
|
|
||||||
ContextProvider,
|
ContextProvider,
|
||||||
DomComponent,
|
DomComponent,
|
||||||
DomPortal,
|
DomPortal,
|
||||||
|
@ -23,11 +21,6 @@ function handlerContext(processing: VNode) {
|
||||||
case DomComponent:
|
case DomComponent:
|
||||||
setNamespaceCtx(processing);
|
setNamespaceCtx(processing);
|
||||||
break;
|
break;
|
||||||
case ClassComponent: {
|
|
||||||
const isOldCxtExist = isOldProvider(processing.type);
|
|
||||||
cacheOldCtx(processing, isOldCxtExist);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case DomPortal:
|
case DomPortal:
|
||||||
setNamespaceCtx(processing, processing.realNode);
|
setNamespaceCtx(processing, processing.realNode);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -2,13 +2,6 @@ import type { VNode } from '../Types';
|
||||||
|
|
||||||
import { mergeDefaultProps } from './LazyComponent';
|
import { mergeDefaultProps } from './LazyComponent';
|
||||||
import { getNewContext, resetDepContexts } from '../components/context/Context';
|
import { getNewContext, resetDepContexts } from '../components/context/Context';
|
||||||
import {
|
|
||||||
cacheOldCtx,
|
|
||||||
getOldContext,
|
|
||||||
isOldProvider,
|
|
||||||
resetOldCtx,
|
|
||||||
updateOldContext,
|
|
||||||
} from '../components/context/CompatibleContext';
|
|
||||||
import {
|
import {
|
||||||
callComponentWillMount,
|
callComponentWillMount,
|
||||||
callComponentWillReceiveProps,
|
callComponentWillReceiveProps,
|
||||||
|
@ -25,17 +18,18 @@ import { markRef } from './BaseComponent';
|
||||||
import {
|
import {
|
||||||
processUpdates,
|
processUpdates,
|
||||||
} from '../UpdateHandler';
|
} from '../UpdateHandler';
|
||||||
import { getContextChangeCtx, setContextChangeCtx } from '../ContextSaver';
|
import { getContextChangeCtx } from '../ContextSaver';
|
||||||
import { setProcessingClassVNode } from '../GlobalVar';
|
import { setProcessingClassVNode } from '../GlobalVar';
|
||||||
import { onlyUpdateChildVNodes } from '../vnode/VNodeCreator';
|
import { onlyUpdateChildVNodes } from '../vnode/VNodeCreator';
|
||||||
import { createChildrenByDiff } from '../diff/nodeDiffComparator';
|
import { createChildrenByDiff } from '../diff/nodeDiffComparator';
|
||||||
|
|
||||||
|
const emptyContextObj = {};
|
||||||
// 获取当前节点的context
|
// 获取当前节点的context
|
||||||
export function getCurrentContext(clazz, processing: VNode) {
|
export function getCurrentContext(clazz, processing: VNode) {
|
||||||
const context = clazz.contextType;
|
const context = clazz.contextType;
|
||||||
return typeof context === 'object' && context !== null
|
return typeof context === 'object' && context !== null
|
||||||
? getNewContext(processing, context)
|
? getNewContext(processing, context)
|
||||||
: getOldContext(processing, clazz, true);
|
: emptyContextObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 挂载实例
|
// 挂载实例
|
||||||
|
@ -112,8 +106,6 @@ export function captureRender(processing: VNode): VNode | null {
|
||||||
clazz = clazz._load(clazz._content);
|
clazz = clazz._load(clazz._content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const isOldCxtExist = isOldProvider(clazz);
|
|
||||||
cacheOldCtx(processing, isOldCxtExist);
|
|
||||||
|
|
||||||
resetDepContexts(processing);
|
resetDepContexts(processing);
|
||||||
|
|
||||||
|
@ -170,24 +162,13 @@ export function captureRender(processing: VNode): VNode | null {
|
||||||
|
|
||||||
// 不复用
|
// 不复用
|
||||||
if (shouldUpdate) {
|
if (shouldUpdate) {
|
||||||
// 更新context
|
|
||||||
if (isOldCxtExist) {
|
|
||||||
updateOldContext(processing);
|
|
||||||
}
|
|
||||||
return createChildren(clazz, processing);
|
return createChildren(clazz, processing);
|
||||||
} else {
|
} else {
|
||||||
if (isOldCxtExist) {
|
|
||||||
setContextChangeCtx(processing, false);
|
|
||||||
}
|
|
||||||
return onlyUpdateChildVNodes(processing);
|
return onlyUpdateChildVNodes(processing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function bubbleRender(processing: VNode) {
|
export function bubbleRender(processing: VNode) {}
|
||||||
if (isOldProvider(processing.type)) {
|
|
||||||
resetOldCtx(processing);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 用于未完成的类组件
|
// 用于未完成的类组件
|
||||||
export function getIncompleteClassComponent(clazz, processing: VNode, nextProps: object): VNode | null {
|
export function getIncompleteClassComponent(clazz, processing: VNode, nextProps: object): VNode | null {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import type {VNode} from '../Types';
|
import type {VNode} from '../Types';
|
||||||
|
|
||||||
import {mergeDefaultProps} from './LazyComponent';
|
import {mergeDefaultProps} from './LazyComponent';
|
||||||
import {getOldContext} from '../components/context/CompatibleContext';
|
|
||||||
import {resetDepContexts} from '../components/context/Context';
|
import {resetDepContexts} from '../components/context/Context';
|
||||||
import {exeFunctionHook} from '../hooks/HookMain';
|
import {exeFunctionHook} from '../hooks/HookMain';
|
||||||
import {ForwardRef} from '../vnode/VNodeTags';
|
import {ForwardRef} from '../vnode/VNodeTags';
|
||||||
|
@ -54,11 +53,6 @@ export function captureFunctionComponent(
|
||||||
nextProps: any,
|
nextProps: any,
|
||||||
shouldUpdate?: boolean
|
shouldUpdate?: boolean
|
||||||
) {
|
) {
|
||||||
let context;
|
|
||||||
if (processing.tag !== ForwardRef) {
|
|
||||||
context = getOldContext(processing, funcComp, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
resetDepContexts(processing);
|
resetDepContexts(processing);
|
||||||
|
|
||||||
const isCanReuse = checkIfCanReuseChildren(processing, shouldUpdate);
|
const isCanReuse = checkIfCanReuseChildren(processing, shouldUpdate);
|
||||||
|
@ -68,7 +62,7 @@ export function captureFunctionComponent(
|
||||||
const newElements = exeFunctionHook(
|
const newElements = exeFunctionHook(
|
||||||
processing.tag === ForwardRef ? funcComp.render : funcComp,
|
processing.tag === ForwardRef ? funcComp.render : funcComp,
|
||||||
nextProps,
|
nextProps,
|
||||||
processing.tag === ForwardRef ? processing.ref : context,
|
processing.tag === ForwardRef ? processing.ref : undefined,
|
||||||
processing,
|
processing,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -4,18 +4,10 @@ import {mergeDefaultProps} from './LazyComponent';
|
||||||
import {ClassComponent} from '../vnode/VNodeTags';
|
import {ClassComponent} from '../vnode/VNodeTags';
|
||||||
import {resetDepContexts} from '../components/context/Context';
|
import {resetDepContexts} from '../components/context/Context';
|
||||||
import {getIncompleteClassComponent} from './ClassComponent';
|
import {getIncompleteClassComponent} from './ClassComponent';
|
||||||
import {
|
|
||||||
isOldProvider,
|
|
||||||
resetOldCtx,
|
|
||||||
cacheOldCtx,
|
|
||||||
} from '../components/context/CompatibleContext';
|
|
||||||
|
|
||||||
function captureIncompleteClassComponent(processing, Component, nextProps) {
|
function captureIncompleteClassComponent(processing, Component, nextProps) {
|
||||||
processing.tag = ClassComponent;
|
processing.tag = ClassComponent;
|
||||||
|
|
||||||
const hasOldContext = isOldProvider(Component);
|
|
||||||
cacheOldCtx(processing, hasOldContext);
|
|
||||||
|
|
||||||
resetDepContexts(processing);
|
resetDepContexts(processing);
|
||||||
|
|
||||||
return getIncompleteClassComponent(Component, processing, nextProps);
|
return getIncompleteClassComponent(Component, processing, nextProps);
|
||||||
|
@ -32,10 +24,4 @@ export function captureRender(processing: VNode): VNode | null {
|
||||||
return captureIncompleteClassComponent(processing, Component, resolvedProps);
|
return captureIncompleteClassComponent(processing, Component, resolvedProps);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function bubbleRender(processing: VNode) {
|
export function bubbleRender(processing: VNode) {}
|
||||||
// 处理与类组件相同。
|
|
||||||
const Component = processing.type;
|
|
||||||
if (isOldProvider(Component)) {
|
|
||||||
resetOldCtx(processing);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -92,9 +92,11 @@ export function captureSuspenseComponent(processing: VNode) {
|
||||||
if (showFallback) {
|
if (showFallback) {
|
||||||
processing.suspenseDidCapture = false;
|
processing.suspenseDidCapture = false;
|
||||||
const nextFallbackChildren = nextProps.fallback;
|
const nextFallbackChildren = nextProps.fallback;
|
||||||
|
debugger
|
||||||
return createFallback(processing, nextFallbackChildren);
|
return createFallback(processing, nextFallbackChildren);
|
||||||
} else {
|
} else {
|
||||||
const newChildren = nextProps.children;
|
const newChildren = nextProps.children;
|
||||||
|
debugger
|
||||||
return createSuspenseChildren(processing, newChildren);
|
return createSuspenseChildren(processing, newChildren);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,11 @@ import type {VNode} from '../Types';
|
||||||
import {throwIfTrue} from '../utils/throwIfTrue';
|
import {throwIfTrue} from '../utils/throwIfTrue';
|
||||||
import {processUpdates} from '../UpdateHandler';
|
import {processUpdates} from '../UpdateHandler';
|
||||||
import {resetNamespaceCtx, setNamespaceCtx} from '../ContextSaver';
|
import {resetNamespaceCtx, setNamespaceCtx} from '../ContextSaver';
|
||||||
import {resetOldCtx} from '../components/context/CompatibleContext';
|
|
||||||
import {onlyUpdateChildVNodes} from '../vnode/VNodeCreator';
|
import {onlyUpdateChildVNodes} from '../vnode/VNodeCreator';
|
||||||
import { createChildrenByDiff } from '../diff/nodeDiffComparator';
|
import { createChildrenByDiff } from '../diff/nodeDiffComparator';
|
||||||
|
|
||||||
export function bubbleRender(processing: VNode) {
|
export function bubbleRender(processing: VNode) {
|
||||||
resetNamespaceCtx(processing);
|
resetNamespaceCtx(processing);
|
||||||
resetOldCtx(processing);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateTreeRoot(processing) {
|
function updateTreeRoot(processing) {
|
||||||
|
|
|
@ -41,6 +41,7 @@ export class FlagUtils {
|
||||||
node.flags |= Addition;
|
node.flags |= Addition;
|
||||||
}
|
}
|
||||||
static setAddition(node: VNode) {
|
static setAddition(node: VNode) {
|
||||||
|
console.log('set addition', node.flags);
|
||||||
node.flags = Addition;
|
node.flags = Addition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue