Skip to content

Commit

Permalink
Merge pull request #8 from Matt-Esch/thunk-patch
Browse files Browse the repository at this point in the history
add thunk patch type
  • Loading branch information
Matt-Esch committed Jul 22, 2014
2 parents ac58375 + 5815772 commit fb73ceb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
34 changes: 28 additions & 6 deletions diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ function diff(a, b) {
}

function walk(a, b, patch, index) {
var nodes = handleThunk(a, b);
a = nodes.a
b = nodes.b

if (a === b) {
hooks(b, patch, index)
if (isThunk(a) || isThunk(b)) {
thunks(a, b, patch, index)
} else {
hooks(b, patch, index)
}
return
}

Expand All @@ -31,6 +31,8 @@ function walk(a, b, patch, index) {
if (b == null) {
apply = appendPatch(apply, new VPatch(VPatch.REMOVE, a, b))
destroyWidgets(a, patch, index)
} else if (isThunk(a) || isThunk(b)) {
thunks(a, b, patch, index)
} else if (isVNode(b)) {
if (isVNode(a)) {
if (a.tagName === b.tagName &&
Expand Down Expand Up @@ -141,7 +143,8 @@ function diffChildren(a, b, patch, apply, index) {
if (!leftNode) {
if (rightNode) {
// Excess nodes in b need to be added
apply = appendPatch(apply, new VPatch(VPatch.INSERT, null, rightNode))
apply = appendPatch(apply,
new VPatch(VPatch.INSERT, null, rightNode))
}
} else if (!rightNode) {
if (leftNode) {
Expand Down Expand Up @@ -189,6 +192,25 @@ function destroyWidgets(vNode, patch, index) {
}
}

// Create a sub-patch for thunks
function thunks(a, b, patch, index) {
var nodes = handleThunk(a, b);
var thunkPatch = diff(nodes.a, nodes.b)
if (hasPatches(thunkPatch)) {
patch[index] = new VPatch(VPatch.THUNK, null, thunkPatch)
}
}

function hasPatches(patch) {
for (var index in patch) {
if (index !== "a") {
return true;
}
}

return false;
}

// Execute hooks when two nodes are identical
function hooks(vNode, patch, index) {
if (isVNode(vNode)) {
Expand Down
1 change: 1 addition & 0 deletions vpatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ VirtualPatch.PROPS = 4
VirtualPatch.ORDER = 5
VirtualPatch.INSERT = 6
VirtualPatch.REMOVE = 7
VirtualPatch.THUNK = 8

module.exports = VirtualPatch

Expand Down

0 comments on commit fb73ceb

Please sign in to comment.