moy-dom
Version:
A flexiable Virtual DOM library for building modern web interface.
37 lines (36 loc) • 1.16 kB
JavaScript
import setProps from './setProps'
import renderChildren from './renderChildren'
/**
* [applyPatches apply the patches to the node]
* @param {[DOMElement]} node [the node]
* @param {[patches]} currentPatches [the patches]
* @return {[undefined]} [undefined]
*/
export default function applyPatches(node, currentPatches){
for(let currentPatch of currentPatches){
switch(currentPatch.type){
case 0 : { // replace
let newNode = Object.prototype.toString.call(currentPatch.node) === '[object Element]' ?
currentPatch.node.render() :
document.createTextNode(currentPatch.node)
node.parentNode.replaceChild(newNode, node)
break
}
case 1 : { // remove, move, insert, append node
renderChildren(node, currentPatch.changes)
break
}
case 2 : { //props
setProps(node, currentPatch.props)
break
}
case 3 : { // text node
node.textContent = currentPatch.content
break
}
default : {
throw new Error('Unknown patch type ' + currentPatch.type)
}
}
}
}