Skip to content

Commit

Permalink
specific cases with utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 4, 2017
1 parent c05a0bc commit d6e568d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 27 deletions.
66 changes: 53 additions & 13 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,46 @@
}
};

/**
* Checks if Something is not null
* @param {Any} item
*/
var isNotNull = function (item) {
return item !== null;
};

/**
* Checks if Something is not undefined
* @param {Any} item
*/
var isDefined = function (item) {
return item !== undefined;
};

/**
* Checks if Something is null
* @param {Any} item
*/
var isNull = function (item) {
return item === null;
};

/**
* Checks if Something is undefined
* @param {Any} item
*/
var isUndefined = function (item) {
return item === undefined;
};

/**
* Checks if Something is null OR undefined
* @param {Any} item
*/
var isNullOrUndefined = function (item) {
return item == null;
};

/**
* Does No Operation
*/
Expand Down Expand Up @@ -609,13 +649,13 @@
var hydrate = function (node, vnode, parent, instance) {
var nodeName = node ? node.nodeName.toLowerCase() : null;

if (node === null) {
if (isNull(node)) {
// No node, create one
var newNode = createNodeFromVNode(vnode, instance);
appendChild(newNode, vnode, parent);

return newNode;
} else if (vnode === undefined) {
} else if (isUndefined(vnode)) {
removeChild(node, parent);

return null;
Expand All @@ -624,7 +664,7 @@
replaceChild(node, newNode, vnode, parent);
return newNode;
} else if (vnode.type === "#text") {
if (node !== null && nodeName === "#text") {
if (isNotNull(node) && nodeName === "#text") {
// Both are textnodes, update the node
if (node.textContent !== vnode.val) {
node.textContent = vnode.val;
Expand All @@ -643,7 +683,7 @@
vnode.meta.el = node;

// Check for Component
if (vnode.meta.component !== undefined) {
if (isDefined(vnode.meta.component)) {
// Diff the Component
diffComponent(node, vnode);

Expand All @@ -658,15 +698,15 @@
addEventListeners(node, vnode, instance);

// Check if innerHTML was changed, and don't diff children if so
if (vnode.props.dom !== undefined && vnode.props.dom.innerHTML !== undefined) {
if (isDefined(vnode.props.dom) && isDefined(vnode.props.dom.innerHTML)) {
return node;
}

// Hydrate Children
var i = 0;
var currentChildNode = node.firstChild;
var vchild = vnode.children[i];
while (vchild !== undefined || currentChildNode !== null) {
while (isDefined(vchild) || isNotNull(currentChildNode)) {
hydrate(currentChildNode, vchild, node, instance);
vchild = vnode.children[++i];
currentChildNode = currentChildNode ? currentChildNode.nextSibling : null;
Expand All @@ -688,12 +728,12 @@
if (oldVNode === vnode) {
// Both have the same reference, exit early
return PATCH.SKIP;
} else if (!oldVNode) {
} else if (isNullOrUndefined(oldVNode)) {
// No Node, append a node
appendChild(createNodeFromVNode(vnode, instance), vnode, parent);

return PATCH.APPEND;
} else if (!vnode) {
} else if (isNullOrUndefined(vnode)) {
// No New VNode, remove Node
removeChild(oldVNode.meta.el, parent);

Expand All @@ -703,7 +743,7 @@
replaceChild(oldVNode.meta.el, createNodeFromVNode(vnode, instance), vnode, parent);

return PATCH.REPLACE;
} else if (vnode.meta.shouldRender && vnode.type === "#text") {
} else if (vnode.meta.shouldRender === true && vnode.type === "#text") {
var node = oldVNode.meta.el;

if (oldVNode.type === "#text") {
Expand All @@ -718,11 +758,11 @@
replaceChild(node, createNodeFromVNode(vnode, instance), vnode, parent);
return PATCH.REPLACE;
}
} else if (vnode.meta.shouldRender) {
} else if (vnode.meta.shouldRender === true) {
var _node = oldVNode.meta.el;

// Check for Component
if (vnode.meta.component) {
if (isDefined(vnode.meta.component)) {
// Diff Component
diffComponent(_node, vnode);

Expand All @@ -735,7 +775,7 @@
oldVNode.props.attrs = vnode.props.attrs;

// Check if innerHTML was changed, don't diff children
if (vnode.props.dom && vnode.props.dom.innerHTML) {
if (isDefined(vnode.props.dom) && isDefined(vnode.props.dom.innerHTML)) {
// Skip Children
return PATCH.SKIP;
}
Expand All @@ -748,7 +788,7 @@
// No Children, Remove all Children if not Already Removed
if (oldLength !== 0) {
var firstChild = null;
while (firstChild = _node.firstChild) {
while (isNotNull(firstChild = _node.firstChild)) {
removeChild(firstChild, _node);
}
}
Expand Down
Loading

0 comments on commit d6e568d

Please sign in to comment.