@@ -18,24 +18,29 @@ const createElement = (type, props, ...children) => {
18
18
}
19
19
}
20
20
const render = ( el , container ) => {
21
- wipRoot = ( nextWorkOfUnit = {
21
+ wipRoot = {
22
22
dom : container ,
23
23
props : {
24
24
children : [ el ]
25
25
}
26
- } )
26
+ }
27
+ nextWorkOfUnit = wipRoot
27
28
}
28
29
29
30
let wipRoot = null
30
31
let currentRoot = null
31
32
// 下一个任务(节点)
32
33
let nextWorkOfUnit = null
33
34
let deletions = [ ]
35
+ let wipFiber = null
34
36
function workLoop ( deadline ) {
35
37
let shouldYield = false
36
38
while ( ! shouldYield && nextWorkOfUnit ) {
37
39
// 返回下一个节点
38
40
nextWorkOfUnit = performWorkOfUnit ( nextWorkOfUnit )
41
+ if ( wipRoot ?. sibling ?. type === nextWorkOfUnit ?. type ) {
42
+ nextWorkOfUnit = null
43
+ }
39
44
// 这里为什么是小于 1
40
45
shouldYield = deadline . timeRemaining ( ) < 1
41
46
}
@@ -72,7 +77,6 @@ const commitWork = (fiber) => {
72
77
filberParent = filberParent . parent
73
78
}
74
79
if ( fiber . effectTag === 'update' ) {
75
- console . log ( 3333 )
76
80
updateProps ( fiber . dom , fiber . props , fiber . alternate ?. props )
77
81
} else if ( fiber . effectTag === 'placement' ) {
78
82
if ( fiber . dom ) {
@@ -87,18 +91,6 @@ const createDom = (type) => {
87
91
return type === 'TEXT_ELEMENT' ? document . createTextNode ( '' ) : document . createElement ( type )
88
92
}
89
93
const updateProps = ( dom , nextProps , prevProps ) => {
90
- console . log ( nextProps , 'nnnnnnn' )
91
- // Object.keys(props).forEach(attr => {
92
- // const isEvent = attr.startsWith('on')
93
- // if (isEvent) {
94
- // const eventType = attr.slice(2).toLocaleLowerCase()
95
- // dom.addEventListener(eventType, props[attr])
96
- // } else {
97
- // if (attr !== 'children') {
98
- // dom[attr] = props[attr]
99
- // }
100
- // }
101
- // })
102
94
// 1. old 有 new 没有 -> 删除
103
95
Object . keys ( prevProps ) . forEach ( key => {
104
96
if ( key !== 'children' ) {
@@ -110,10 +102,8 @@ const updateProps = (dom, nextProps, prevProps) => {
110
102
// 2. old 没有 new 有 -> 添加
111
103
// 3. new 有 old 也有 -> 修改
112
104
Object . keys ( nextProps ) . forEach ( key => {
113
- console . log ( 1111111 )
114
105
if ( key !== 'children' ) {
115
106
if ( nextProps [ key ] !== prevProps [ key ] ) {
116
- console . log ( nextProps [ key ] , 'hhhhhhhh' )
117
107
// 不相等进行更新赋值
118
108
const isEvent = key . startsWith ( 'on' )
119
109
if ( isEvent ) {
@@ -149,14 +139,16 @@ const initChildren = (fiber, children) => {
149
139
}
150
140
} else {
151
141
// 添加
152
- newFiber = {
153
- type : child . type ,
154
- props : child . props ,
155
- child : null , // child 和 sibling 初始化我们不知道
156
- sibling : null ,
157
- parent : fiber ,
158
- dom : null ,
159
- effectTag : 'placement'
142
+ if ( child ) {
143
+ newFiber = {
144
+ type : child . type ,
145
+ props : child . props ,
146
+ child : null , // child 和 sibling 初始化我们不知道
147
+ sibling : null ,
148
+ parent : fiber ,
149
+ dom : null ,
150
+ effectTag : 'placement'
151
+ }
160
152
}
161
153
if ( oldFiber ) {
162
154
deletions . push ( oldFiber )
@@ -174,14 +166,17 @@ const initChildren = (fiber, children) => {
174
166
}
175
167
// 考虑到我们还需要设置 parent.sibling,因为我们是从上往下获取的,所以work肯定是顶层也就是 parent,我们只能给 child 设置,
176
168
// 但是如果直接在child 上加就会破坏原有结构,所以我们单独维护一个newWork 对象,
177
- prevChild = newFiber
169
+ if ( newFiber ) {
170
+ prevChild = newFiber
171
+ }
178
172
} )
179
173
while ( oldFiber ) {
180
174
deletions . push ( oldFiber )
181
175
oldFiber = oldFiber . sibling
182
176
}
183
177
}
184
178
const updateFunctionComponent = ( fiber ) => {
179
+ wipFiber = fiber
185
180
const children = [ fiber . type ( fiber . props ) ]
186
181
initChildren ( fiber , children )
187
182
}
@@ -190,7 +185,6 @@ const updateHostComponent = (fiber) => {
190
185
// 1. 创建dom
191
186
const dom = ( fiber . dom = createDom ( fiber . type ) )
192
187
// 3. 设置 dom 的 props
193
- console . log ( fiber , 'xxxxxxxxx' )
194
188
updateProps ( dom , fiber . props , { } )
195
189
}
196
190
const children = fiber . props . children
@@ -218,14 +212,15 @@ const performWorkOfUnit = (fiber) => {
218
212
}
219
213
requestIdleCallback ( workLoop )
220
214
221
- // 这里的节点不是新的节点吗,那么新节点的 alternate 指向旧节点为啥也是 currentRoot 难道currentRoot 即是新节点也是旧节点?
222
215
const update = ( ) => {
223
- nextWorkOfUnit = {
224
- dom : currentRoot . dom ,
225
- props : currentRoot . props ,
226
- alternate : currentRoot
216
+ let currentFiber = wipFiber
217
+ return ( ) => {
218
+ wipRoot = {
219
+ ...currentFiber ,
220
+ alternate : currentFiber
221
+ }
222
+ nextWorkOfUnit = wipRoot
227
223
}
228
- wipRoot = nextWorkOfUnit
229
224
}
230
225
const React = {
231
226
render,
0 commit comments