Created
July 8, 2018 15:11
-
-
Save ManasJayanth/29ab229b4cc23ad6601494cdfbd6ef00 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
import ReactReconciler from 'react-reconciler'; | |
const hostConfig = { | |
getRootHostContext(rootContainerInstance) { | |
return {} | |
}, | |
getChildHostContext(parentHostContext, type, rootContainerInstance) { | |
return {}; | |
}, | |
getPublicInstance(instance) { | |
console.log('getPublicInstance'); | |
}, | |
prepareForCommit(containerInfo) { | |
}, | |
resetAfterCommit(containerInfo) { | |
}, | |
createInstance( | |
type, | |
props, | |
rootContainerInstance, | |
hostContext, | |
internalInstanceHandle | |
) { | |
return document.createElement(type); | |
}, | |
appendInitialChild(parentInstance, child) { | |
parentInstance.appendChild(child) | |
}, | |
finalizeInitialChildren( | |
domElement, | |
type, | |
props, | |
rootContainerInstance, | |
hostContext | |
) { | |
const { children, ...otherProps } = props; | |
Object.keys(otherProps).forEach(attr => { | |
if (attr === 'className') { | |
domElement.class = otherProps[attr]; | |
} else if (attr === 'onClick') { | |
const listener = otherProps[attr]; | |
if (domElement.__ourVeryHackCacheOfEventListeners) { | |
domElement.__ourVeryHackCacheOfEventListeners.push(listener) | |
} else { | |
domElement.__ourVeryHackCacheOfEventListeners = [ listener ] | |
} | |
domElement.addEventListener('click', listener); | |
} else { | |
throw new Error('TODO: We haven\'t handled other properties/attributes') | |
} | |
}) | |
}, | |
prepareUpdate( | |
domElement, | |
type, | |
oldProps, | |
newProps, | |
rootContainerInstance, | |
hostContext | |
) { | |
console.log('prepareUpdate'); | |
return [ null ]; | |
}, | |
shouldSetTextContent(type, props) { | |
return false; | |
}, | |
shouldDeprioritizeSubtree(type, props) { | |
console.log('shouldDeprioritizeSubtree'); | |
}, | |
createTextInstance( | |
text, | |
rootContainerInstance, | |
hostContext, | |
internalInstanceHandle | |
) { | |
return document.createTextNode(text); | |
}, | |
now: Date.now, | |
isPrimaryRenderer: true, | |
scheduleDeferredCallback: "", | |
cancelDeferredCallback: "", | |
// ------------------- | |
// Mutation | |
// ------------------- | |
supportsMutation: true, | |
commitMount(domElement, type, newProps, internalInstanceHandle) { | |
console.log('commitMount'); | |
}, | |
commitUpdate( | |
domElement, | |
updatePayload, | |
type, | |
oldProps, | |
newProps, | |
internalInstanceHandle | |
) { | |
}, | |
resetTextContent(domElement) { | |
}, | |
commitTextUpdate(textInstance, oldText, newText) { | |
textInstance.nodeValue = newText; | |
}, | |
appendChild(parentInstance, child) { | |
}, | |
appendChildToContainer(container, child) { | |
container.appendChild(child) | |
}, | |
insertBefore(parentInstance, child, beforeChild) { | |
console.log('insertBefore'); | |
}, | |
insertInContainerBefore(container, child, beforeChild) { | |
console.log('insertInContainerBefore'); | |
}, | |
removeChild(parentInstance, child) { | |
console.log('removeChild'); | |
}, | |
removeChildFromContainer(container, child) { | |
console.log('removeChildFromContainer'); | |
} | |
}; | |
const DOMRenderer = ReactReconciler(hostConfig); | |
let internalContainerStructure; | |
export default { | |
render(elements, containerNode, callback) { | |
// We must do this only once | |
if (!internalContainerStructure) { | |
internalContainerStructure = DOMRenderer.createContainer( | |
containerNode, | |
false, | |
false | |
); | |
} | |
DOMRenderer.updateContainer(elements, internalContainerStructure, null, callback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment