Skip to content

Commit 46d7d69

Browse files
author
guimeisang
committed
add: 愿不负韶华
1 parent 08209b6 commit 46d7d69

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ React 内部实现的一套状态更新机制,支持任务不同的优先级
3636
和遍历树一样
3737

3838
- beginWork
39+
具体看src/Core/beginWork文件,并且是区别mount和update的
40+
更具不同的类型,创建fiber,并且遍历树,并且节点上可能会存在effectTag
3941

4042
- completeWork
43+
给有effectTag的Fiber节点执行对应操作,根据fiber不同类型处理
44+
分为update和mount:mount会将fiber实例化成dom,并且两个过程都会处理属性,事件等
45+
最后,将有effectTag的fiber挂载在父级的fiber的effectList末尾,并返回一个workInProgress
46+
4147

4248
第四:commit阶段
4349

src/Core/beginWork.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function beginWork(
2+
current: Fiber | null,
3+
workInProgress: Fiber,
4+
renderLanes: Lanes
5+
) {
6+
// update时:如果current存在可能存在优化路径,可以复用current(即上一次更新的Fiber节点)
7+
if (current !== null) {
8+
const oldProps = current.memoizedProps;
9+
const newProps = workInProgress.pengingProps;
10+
11+
if(oldProps !=== newProps || hasLegacyContextChanged() || (__DEV__ ? workInProgress.type !== current.type : false)) {
12+
didReceiveUpdate = true;
13+
}else if(!includesSomeLane(renderLanes, updateLanes) {
14+
didReceiveUpdate = false;
15+
switch (workInProgress.tag) {
16+
// 省略处理
17+
}
18+
return bailoutOnAlreadyFinishedWork(
19+
current,
20+
workInProgress,
21+
renderLanes,
22+
);
23+
}
24+
} else {
25+
didReceiveUpdate = false
26+
}
27+
28+
// mount 时:根据tag不同,创建不同的子Fiber节点
29+
switch (workInProgress.tag) {
30+
case IndeterminateComponent:
31+
// ...省略
32+
case LazyComponent:
33+
// ...省略
34+
case FunctionComponent:
35+
// ...省略
36+
case HostRoot:
37+
// ...省略
38+
case HostComponent:
39+
// ...省略
40+
case HostText:
41+
// ...省略
42+
// ...省略其他类型
43+
}
44+
}

src/Core/completeWork.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function completeWork(
2+
current: Fiber | null,
3+
workInProgress: Fiber,
4+
renderLanes: Lanes,
5+
): Fiber | null {
6+
const newProps = workInProgress.pendingProps;
7+
switch (workInProgress.tag) {
8+
case IndeterminateComponent:
9+
case LazyComponent:
10+
case SimpleMemoComponent:
11+
case FunctionComponent:
12+
case ForwardRef:
13+
case Fragment:
14+
case Mode:
15+
case Profiler:
16+
case ContextConsumer:
17+
case MemoComponent:
18+
return null;
19+
case ClassComponent: {
20+
// ...省略
21+
return null;
22+
}
23+
case HostRoot: {
24+
// ...省略
25+
updateHostContainer(workInProgress);
26+
return null;
27+
}
28+
case HostComponent: {
29+
popHostContext(workInProgress);
30+
const rootContainerInstance = getRootHostContainer();
31+
const type = workInProgress.type;
32+
33+
if (current !== null && workInProgress.stateNode != null) {
34+
// update的情况
35+
updateHostComponent(
36+
current,
37+
workInProgress,
38+
type,
39+
newProps,
40+
rootContainerInstance,
41+
);
42+
} else {
43+
// mount的情况
44+
// ...省略服务端渲染相关逻辑
45+
46+
const currentHostContext = getHostContext();
47+
// 为fiber创建对应DOM节点
48+
const instance = createInstance(
49+
type,
50+
newProps,
51+
rootContainerInstance,
52+
currentHostContext,
53+
workInProgress,
54+
);
55+
// 将子孙DOM节点插入刚生成的DOM节点中
56+
appendAllChildren(instance, workInProgress, false, false);
57+
// DOM节点赋值给fiber.stateNode
58+
workInProgress.stateNode = instance;
59+
// 与update逻辑中的updateHostComponent类似的处理props的过程
60+
if (
61+
finalizeInitialChildren(
62+
instance,
63+
type,
64+
newProps,
65+
rootContainerInstance,
66+
currentHostContext,
67+
)
68+
) {
69+
markUpdate(workInProgress);
70+
}
71+
}
72+
return null;
73+
}
74+
// ...省略
75+
}

src/Core/reconcileChildren.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export function reconcileChildren(
2+
current: Fiber | null,
3+
workInProgress: Fiber,
4+
nextChildren: any,
5+
renderLanes: Lanes
6+
) {
7+
if (current === null) {
8+
// 对于mount的组件
9+
workInProgress.child = mountChildFibers(
10+
workInProgress,
11+
null,
12+
nextChildren,
13+
renderLanes
14+
)
15+
} else {
16+
// 对于update的组件
17+
workInProgress.child = reconcileChildFibers(
18+
workInProgress,
19+
current.child,
20+
nextChildren,
21+
renderLanes
22+
)
23+
}
24+
}

0 commit comments

Comments
 (0)