Skip to content

Commit

Permalink
correct destroy ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-Esch committed Dec 21, 2014
1 parent 6bad652 commit 87b9ad3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
30 changes: 30 additions & 0 deletions test/main.js
Expand Up @@ -619,6 +619,36 @@ test("Patch nested widgets", function (assert) {
assert.end()
})

test("Can replace stateful widget with vnode", function (assert) {
var statefulWidget = {
init: function () {
return render(h("div.widget"))
},
update: function () {},
destroy: function () {},
type: "Widget"
}

var leftNode = h("div", statefulWidget)
var rightNode = h("div", h("div.vnode"))

var rootNode = render(leftNode)

assert.equal(rootNode.childNodes.length, 1)
assert.equal(rootNode.childNodes[0].className, 'widget')

var patches = diff(leftNode, rightNode)

var newRoot = patch(rootNode, patches)

assert.equal(newRoot, rootNode)

assert.equal(newRoot.childNodes.length, 1)
assert.equal(newRoot.childNodes[0].className, 'vnode')

assert.end()
})

test("Ensure children are not rendered more than once", function (assert) {
var initCount = 0
var updateCount = 0
Expand Down
18 changes: 9 additions & 9 deletions vtree/diff.js
Expand Up @@ -23,6 +23,7 @@ function walk(a, b, patch, index) {
}

var apply = patch[index]
var applyClear = false

if (isThunk(a) || isThunk(b)) {
thunks(a, b, patch, index)
Expand All @@ -49,27 +50,22 @@ function walk(a, b, patch, index) {
}
apply = diffChildren(a, b, patch, apply, index)
} else {
clearState(a, patch, index)
apply = patch[index]
apply = appendPatch(apply, new VPatch(VPatch.VNODE, a, b))
applyClear = true
}
} else {
clearState(a, patch, index)
apply = patch[index]
apply = appendPatch(apply, new VPatch(VPatch.VNODE, a, b))
applyClear = true
}
} else if (isVText(b)) {
if (!isVText(a)) {
clearState(a, patch, index)
apply = patch[index]
apply = appendPatch(apply, new VPatch(VPatch.VTEXT, a, b))

This comment has been minimized.

Copy link
@Raynos

Raynos Dec 22, 2014

Collaborator

Looks like you accidentally removed the VPatch appendpatch call

This comment has been minimized.

Copy link
@Raynos

Raynos Dec 22, 2014

Collaborator

This introduces a regression in mercury v11.1.0

This comment has been minimized.

Copy link
@mmckegg

mmckegg Dec 22, 2014

Contributor

Bug caused by this commit:

an element's child elements are not removed when patched with a node with no child elements

Putting this line back in fixed the problem 👍

applyClear = true
} else if (a.text !== b.text) {
apply = appendPatch(apply, new VPatch(VPatch.VTEXT, a, b))
}
} else if (isWidget(b)) {
if (!isWidget(a)) {
clearState(a, patch, index)
apply = patch[index]
applyClear = true;
}

apply = appendPatch(apply, new VPatch(VPatch.WIDGET, a, b))
Expand All @@ -78,6 +74,10 @@ function walk(a, b, patch, index) {
if (apply) {
patch[index] = apply
}

if (applyClear) {
clearState(a, patch, index)
}
}

function diffProps(a, b) {
Expand Down

0 comments on commit 87b9ad3

Please sign in to comment.