Match-id-e0350c199ac3e8354d02617e9f692d807c56383a

This commit is contained in:
* 2023-06-07 10:24:02 +08:00
parent a72fb10ef3
commit 6175b4e308
12 changed files with 58 additions and 53 deletions

View File

@ -54,6 +54,7 @@ import {
import { getPathArr } from './utils/vNodePath';
import { injectUpdater } from '../external/devtools';
import { popCurrentRoot, pushCurrentRoot } from './RootStack';
import { isNull } from '../dom/utils/Common';
// 不可恢复错误
let unrecoverableErrorDuringBuild: any = null;
@ -139,7 +140,7 @@ function bubbleVNode(vNode: VNode): void {
node = parent;
// 更新processing抛出异常时可以使用
processing = node;
} while (node !== null);
} while (!isNull(node));
// 修改结果
if (getBuildResult() === BuildInComplete) {
@ -180,7 +181,7 @@ function getChildByIndex(vNode: VNode, idx: number) {
let node = vNode.child;
for (let i = 0; i < idx; i++) {
// 场景当组件被销毁业务若异步定时器调用setState修改状态可能出现路径错误此处进行保护。
if (node == null) {
if (isNull(node)) {
return null;
}
@ -225,7 +226,7 @@ export function calcStartUpdateVNode(treeRoot: VNode) {
const pathIndex = Number(startNodePath[i]);
node = getChildByIndex(node, pathIndex)!;
// 路径错误时,回退到从根更新
if (node == null) {
if (isNull(node)) {
return treeRoot;
}
}

View File

@ -52,7 +52,7 @@ export function createHook(state: any = null): Hook<any, any> {
return currentHook;
}
export function getNextHook(hook: Hook<any, any>, hooks: Array<Hook<any, any>>) {
export function getNextHook(hook: Hook<any, any>, hooks: Array<Hook<any, any>>): Hook<any, any> | null {
return hooks[hook.hIndex + 1] || null;
}

View File

@ -22,7 +22,7 @@ function isNotNull(object: any): boolean {
return object !== null && object !== undefined;
}
function effectFunc<R>(func: () => R, ref: Ref<R> | ((any) => any) | null): (() => void) | void {
function effectFunc<R>(func: () => R, ref: Ref<R> | ((any) => any) | null): (() => void) | null {
if (typeof ref === 'function') {
const value = func();
ref(value);
@ -37,6 +37,7 @@ function effectFunc<R>(func: () => R, ref: Ref<R> | ((any) => any) | null): (()
ref.current = null;
};
}
return null;
}
export function useImperativeHandleImpl<R>(

View File

@ -64,7 +64,7 @@ function handleContextChange(processing: VNode, context: ContextType<any>): void
node => {
const depContexts = node.depContexts;
if (depContexts && depContexts.length) {
isMatch = matchDependencies(depContexts, context, node) ?? isMatch;
isMatch = matchDependencies(depContexts, context, node) || isMatch;
}
},
node =>

View File

@ -23,6 +23,7 @@ import { markRef } from './BaseComponent';
import { DomComponent, DomPortal, DomText } from '../vnode/VNodeTags';
import { travelVNodeTree } from '../vnode/VNodeUtils';
import { createChildrenByDiff } from '../diff/nodeDiffComparator';
import { isNull } from '../../dom/utils/Common';
function updateDom(processing: VNode, type: any, newProps: Props) {
// 如果oldProps !== newProps意味着存在更新并且需要处理其相关的副作用
@ -54,7 +55,7 @@ export function bubbleRender(processing: VNode) {
const type = processing.type;
const newProps = processing.props;
if (!processing.isCreated && processing.realNode != null) {
if (!processing.isCreated && !isNull(processing.realNode)) {
// 更新dom属性
updateDom(processing, type, newProps);

View File

@ -18,6 +18,7 @@ import type { VNode } from '../Types';
import { throwIfTrue } from '../utils/throwIfTrue';
import { newTextDom } from '../../dom/DOMOperator';
import { FlagUtils } from '../vnode/VNodeFlags';
import { isNull } from '../../dom/utils/Common';
export function captureRender(): VNode | null {
return null;
@ -26,7 +27,7 @@ export function captureRender(): VNode | null {
export function bubbleRender(processing: VNode) {
const newText = processing.props;
if (!processing.isCreated && processing.realNode != null) {
if (!processing.isCreated && !isNull(processing.realNode)) {
// 更新
const oldText = processing.oldProps;
// 如果文本不同,将其标记为更新

View File

@ -165,7 +165,7 @@ function canCapturePromise(vNode: VNode | null): boolean {
// 处理Suspense子组件抛出的promise
export function handleSuspenseChildThrowError(parent: VNode, processing: VNode, promise: PromiseType<any>): boolean {
let vNode = parent;
let vNode: VNode | null = parent;
// 向上找到最近的不在fallback状态的Suspense并触发重新渲染
do {

View File

@ -35,7 +35,7 @@ export function callDerivedStateFromProps(
const newState = getDerivedStateFromProps(nextProps, oldState);
// 组件未返回state,需要返回旧的preState
processing.state = newState === null || newState === undefined ? oldState : { ...oldState, ...newState };
processing.state = newState ? { ...oldState, ...newState } : oldState;
}
}

View File

@ -35,7 +35,7 @@ function callRenderQueue() {
try {
let callback;
while ((callback = renderQueue.shift())) {
while (callback = renderQueue.shift()) {
callback();
}

View File

@ -63,12 +63,12 @@ export function add(node: Node): void {
export function first(): Node | null {
const val: Node | null | undefined = taskQueue[0];
return val !== undefined ? val : null;
return val ?? null;
}
export function shift(): Node | null {
const val = taskQueue.shift();
return val !== undefined ? val : null;
return val ?? null;
}
export function remove(node: Node) {

View File

@ -44,6 +44,7 @@ import {
import { VNode } from './VNode';
import { JSXElement, Source } from '../Types';
import { markVNodePath } from '../utils/vNodePath';
import { isNull } from '../../dom/utils/Common';
const typeLazyMap = {
[TYPE_FORWARD_REF]: ForwardRef,
@ -136,7 +137,7 @@ export function createUndeterminedVNode(type, key, props, source: Source | null)
vNodeTag = typeMap[type.vtype];
isLazy = type.vtype === TYPE_LAZY;
} else {
throw Error(`Component type is invalid, got: ${type == null ? type : componentType}`);
throw Error(`Component type is invalid, got: ${isNull(type) ? type : componentType}`);
}
const vNode = newVirtualNode(vNodeTag, key, props);
@ -183,7 +184,7 @@ export function createTreeRootVNode(container) {
return vNode;
}
// TODO: 暂时保留给测试用例使用,后续修改测试用例
// 暂时保留给测试用例使用,后续修改测试用例
export function createVNode(tag: VNodeTag | string, ...secondArg) {
let vNode = null;
switch (tag) {

View File

@ -36,71 +36,71 @@ export const ForceUpdate = /** */ 1 << 12; // For suspense
export const Clear = /** */ 1 << 13;
const LifecycleEffectArr = Update | Callback | Ref | Snapshot;
export class FlagUtils {
static removeFlag(node: VNode, flag: number) {
export const FlagUtils = {
removeFlag(node: VNode, flag: number) {
node.flags &= ~flag;
}
},
static removeLifecycleEffectFlags(node) {
removeLifecycleEffectFlags(node) {
node.flags &= ~LifecycleEffectArr;
}
},
static hasAnyFlag(node: VNode) {
hasAnyFlag(node: VNode) {
// 有标志位
return node.flags !== InitFlag;
}
},
static hasFlag(node: VNode, flag) {
hasFlag(node: VNode, flag) {
return (node.flags & flag) !== 0;
}
},
static setNoFlags(node: VNode) {
setNoFlags(node: VNode) {
node.flags = InitFlag;
}
},
static markAddition(node: VNode) {
markAddition(node: VNode) {
node.flags |= Addition;
}
},
static setAddition(node: VNode) {
setAddition(node: VNode) {
node.flags = Addition;
}
},
static markDirectAddition(node: VNode) {
markDirectAddition(node: VNode) {
node.flags |= DirectAddition;
}
static markUpdate(node: VNode) {
},
markUpdate(node: VNode) {
node.flags |= Update;
}
static setDeletion(node: VNode) {
},
setDeletion(node: VNode) {
node.flags = Deletion;
}
static markContentReset(node: VNode) {
},
markContentReset(node: VNode) {
node.flags |= ResetText;
}
static markCallback(node: VNode) {
},
markCallback(node: VNode) {
node.flags |= Callback;
}
static markDidCapture(node: VNode) {
},
markDidCapture(node: VNode) {
node.flags |= DidCapture;
}
static markShouldCapture(node: VNode) {
},
markShouldCapture(node: VNode) {
node.flags |= ShouldCapture;
}
static markRef(node: VNode) {
},
markRef(node: VNode) {
node.flags |= Ref;
}
static markSnapshot(node: VNode) {
},
markSnapshot(node: VNode) {
node.flags |= Snapshot;
}
static markInterrupted(node: VNode) {
},
markInterrupted(node: VNode) {
node.flags |= Interrupted;
}
static markForceUpdate(node: VNode) {
},
markForceUpdate(node: VNode) {
node.flags |= ForceUpdate;
}
},
static markClear(node: VNode) {
markClear(node: VNode) {
node.flags |= Clear;
}
}