From 0d9a51fe377e75683c35fad300b77605ab0c9387 Mon Sep 17 00:00:00 2001 From: Leo Date: Thu, 9 Feb 2017 09:54:48 -0500 Subject: [PATCH] fix lifecycle in unkeyed child of recycled keyed --- mithril.js | 3 +-- render/render.js | 4 ++-- render/tests/test-render.js | 30 +++++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/mithril.js b/mithril.js index 0d8b4417..808d5b6e 100644 --- a/mithril.js +++ b/mithril.js @@ -475,14 +475,13 @@ var coreRenderer = function($window) { if (old[i] === vnodes[i]) continue else if (old[i] == null && vnodes[i] != null) createNode(parent, vnodes[i], hooks, ns, getNextSibling(old, i + 1, nextSibling)) else if (vnodes[i] == null) removeNodes(old, i, i + 1, vnodes) - else updateNode(parent, old[i], vnodes[i], hooks, getNextSibling(old, i + 1, nextSibling), false, ns) + else updateNode(parent, old[i], vnodes[i], hooks, getNextSibling(old, i + 1, nextSibling), recycling, ns) } return } } recycling = recycling || isRecyclable(old, vnodes) if (recycling) old = old.concat(old.pool) - var oldStart = 0, start = 0, oldEnd = old.length - 1, end = vnodes.length - 1, map while (oldEnd >= oldStart && end >= start) { var o = old[oldStart], v = vnodes[start] diff --git a/render/render.js b/render/render.js index 0a01dc14..22af7a32 100644 --- a/render/render.js +++ b/render/render.js @@ -141,14 +141,14 @@ module.exports = function($window) { if (old[i] === vnodes[i]) continue else if (old[i] == null && vnodes[i] != null) createNode(parent, vnodes[i], hooks, ns, getNextSibling(old, i + 1, nextSibling)) else if (vnodes[i] == null) removeNodes(old, i, i + 1, vnodes) - else updateNode(parent, old[i], vnodes[i], hooks, getNextSibling(old, i + 1, nextSibling), false, ns) + else updateNode(parent, old[i], vnodes[i], hooks, getNextSibling(old, i + 1, nextSibling), recycling, ns) } return } } recycling = recycling || isRecyclable(old, vnodes) if (recycling) old = old.concat(old.pool) - + var oldStart = 0, start = 0, oldEnd = old.length - 1, end = vnodes.length - 1, map while (oldEnd >= oldStart && end >= start) { var o = old[oldStart], v = vnodes[start] diff --git a/render/tests/test-render.js b/render/tests/test-render.js index d190cb85..21f6dc3d 100644 --- a/render/tests/test-render.js +++ b/render/tests/test-render.js @@ -55,7 +55,7 @@ o.spec("render", function() { o(threwOuter).equals(true) }) - o("lifecycle methods work in children of recycled", function() { + o("lifecycle methods work in keyed children of recycled keyed", function() { var createA = o.spy() var updateA = o.spy() var removeA = o.spy() @@ -78,6 +78,34 @@ o.spec("render", function() { render(root, b()) render(root, a()) + o(createA.callCount).equals(2) + o(updateA.callCount).equals(0) + o(removeA.callCount).equals(1) + o(createB.callCount).equals(1) + o(updateB.callCount).equals(0) + o(removeB.callCount).equals(1) + }) + o("lifecycle methods work in unkeyed children of recycled keyed", function() { + var createA = o.spy() + var updateA = o.spy() + var removeA = o.spy() + var createB = o.spy() + var updateB = o.spy() + var removeB = o.spy() + var a = function() { + return {tag: "div", key: 1, children: [ + {tag: "div", attrs: {oncreate: createA, onupdate: updateA, onremove: removeA}}, + ]} + } + var b = function() { + return {tag: "div", key: 2, children: [ + {tag: "div", attrs: {oncreate: createB, onupdate: updateB, onremove: removeB}}, + ]} + } + render(root, a()) + render(root, b()) + render(root, a()) + o(createA.callCount).equals(2) o(updateA.callCount).equals(0) o(removeA.callCount).equals(1)