From 87b9ad36fd72548bf60f62eaa0db130fd6f869d5 Mon Sep 17 00:00:00 2001 From: Matt Esch Date: Sat, 20 Dec 2014 19:03:05 -0800 Subject: [PATCH] correct destroy ordering --- test/main.js | 30 ++++++++++++++++++++++++++++++ vtree/diff.js | 18 +++++++++--------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/test/main.js b/test/main.js index 4cc8d95a..30d666d6 100644 --- a/test/main.js +++ b/test/main.js @@ -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 diff --git a/vtree/diff.js b/vtree/diff.js index f5dc9f44..a860b104 100644 --- a/vtree/diff.js +++ b/vtree/diff.js @@ -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) @@ -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)) + 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)) @@ -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) {