Last active
February 17, 2019 23:04
-
-
Save pomber/056fa1523a26a9763be6f6a4f65062c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Effect tags | |
const PLACEMENT = 1; | |
const DELETION = 2; | |
const UPDATE = 3; | |
function arrify(val) { | |
return val == null ? [] : Array.isArray(val) ? val : [val]; | |
} | |
function reconcileChildrenArray(wipFiber, newChildElements) { | |
const elements = arrify(newChildElements); | |
let index = 0; | |
let oldFiber = wipFiber.alternate ? wipFiber.alternate.child : null; | |
let newFiber = null; | |
while (index < elements.length || oldFiber != null) { | |
const prevFiber = newFiber; | |
const element = index < elements.length && elements[index]; | |
const sameType = oldFiber && element && element.type == oldFiber.type; | |
if (sameType) { | |
newFiber = { | |
type: oldFiber.type, | |
tag: oldFiber.tag, | |
stateNode: oldFiber.stateNode, | |
props: element.props, | |
parent: wipFiber, | |
alternate: oldFiber, | |
partialState: oldFiber.partialState, | |
effectTag: UPDATE | |
}; | |
} | |
if (element && !sameType) { | |
newFiber = { | |
type: element.type, | |
tag: | |
typeof element.type === "string" ? HOST_COMPONENT : CLASS_COMPONENT, | |
props: element.props, | |
parent: wipFiber, | |
effectTag: PLACEMENT | |
}; | |
} | |
if (oldFiber && !sameType) { | |
oldFiber.effectTag = DELETION; | |
wipFiber.effects = wipFiber.effects || []; | |
wipFiber.effects.push(oldFiber); | |
} | |
if (oldFiber) { | |
oldFiber = oldFiber.sibling; | |
} | |
if (index == 0) { | |
wipFiber.child = newFiber; | |
} else if (prevFiber && element) { | |
prevFiber.sibling = newFiber; | |
} | |
index++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment