1.把react elment转化成fiber结构
- 链表结构遍历
- 使用nextworkUnit保存当前fiber,使其能够异步的遍历组件树中的fiber节点,用
requestIdleCallback包裹workLoop
let wipRoot = null;
let nextUnitOfwork = null
function performUnitOfWork(wip) {
const { type } = wip;
if (isStr(type)) {
updateHostComponent(wip)
} else if (isFn(type)) {
updateFunctionComponent(wip)
} else {
updateFragmentComponent(wip)
}
if (wip.child) {
return wip.child
}
let next = wip;
while (next) {
if (next.sibling) {
return next.sibling
}
next = next.return;
}
return null
}
function workLoop(IdleDeadline) {
while (nextUnitOfwork && IdleDeadline.timeRemaining() > 0) {
nextUnitOfwork = performUnitOfWork(nextUnitOfwork)
}
if (!nextUnitOfwork && wipRoot) {
console.log(wipRoot)
commitRoot(wipRoot)
}
}
2.递归commit
function commitWorker(wip) {
if (!wip) return;
const { stateNode } = wip;
let parentNode = getParentNode(wip.return);
if (stateNode) {
parentNode.appendChild(stateNode);
}
commitWorker(wip.child);
commitWorker(wip.sibling);
}
3.fiber结构
export function createFiber(vnode, returnFiber) {
const newFiber = {
type: vnode.type,
key: vnode.key,
props: vnode.props,
stateNode: null,
child: null,
return: returnFiber,
sibling: null,
alternate: null,
flags: Placement,
}
return newFiber
}