1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| function reconcile(parentDom, instance, element) { if (instance === null) { const newInstance = instantiate(element); newInstance.publicInstance && newInstance.publicInstance.componentWillMount && newInstance.publicInstance.componentWillMount(); parentDom.appendChild(newInstance.dom); newInstance.publicInstance && newInstance.publicInstance.componentDidMount && newInstance.publicInstance.componentDidMount(); return newInstance; } else if (element === null) { instance.publicInstance && instance.publicInstance.componentWillUnmount && instance.publicInstance.componentWillUnmount(); parentDom.removeChild(instance.dom); return null; } else if (instance.element.type !== element.type) { const newInstance = instantiate(element); newInstance.publicInstance && newInstance.publicInstance.componentDidMount && newInstance.publicInstance.componentDidMount(); parentDom.replaceChild(newInstance.dom, instance.dom); return newInstance; } else if (typeof element.type === 'string') { updateDomProperties(instance.dom, instance.element.props, element.props); instance.childInstances = reconcileChildren(instance, element); instance.element = element; return instance; } else { if (instance.publicInstance && instance.publicInstance.shouldcomponentUpdate) { if (!instance.publicInstance.shouldcomponentUpdate()) { return; } } instance.publicInstance && instance.publicInstance.componentWillUpdate && instance.publicInstance.componentWillUpdate(); instance.publicInstance.props = element.props; const newChildElement = instance.publicInstance.render(); const oldChildInstance = instance.childInstance; const newChildInstance = reconcile(parentDom, oldChildInstance, newChildElement); instance.publicInstance && instance.publicInstance.componentDidUpdate && instance.publicInstance.componentDidUpdate(); instance.dom = newChildInstance.dom; instance.childInstance = newChildInstance; instance.element = element; return instance; } }
function reconcileChildren(instance, element) { const { dom, childInstances } = instance; const newChildElements = element.props.children || []; const count = Math.max(childInstances.length, newChildElements.length); const newChildInstances = []; for (let i = 0; i < count; i++) { newChildInstances[i] = reconcile(dom, childInstances[i], newChildElements[i]); } return newChildInstances.filter(instance => instance !== null); }
|