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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -35,7 +35,7 @@ export function callDerivedStateFromProps(
const newState = getDerivedStateFromProps(nextProps, oldState); const newState = getDerivedStateFromProps(nextProps, oldState);
// 组件未返回state,需要返回旧的preState // 组件未返回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 { try {
let callback; let callback;
while ((callback = renderQueue.shift())) { while (callback = renderQueue.shift()) {
callback(); callback();
} }

View File

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

View File

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

View File

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