From 622e00981138601d8e9a309013e0f251a0e3d44b Mon Sep 17 00:00:00 2001 From: valtron Date: Mon, 16 Oct 2017 00:38:44 -0600 Subject: [PATCH] recycling => shouldRecycle, Fix #1992 (#1993) * Fix #1992 * doc in changelog * add test for #1992 --- docs/change-log.md | 1 + render/render.js | 2 +- render/tests/test-oninit.js | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/change-log.md b/docs/change-log.md index 6c5adc35..a2f8259d 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -40,6 +40,7 @@ - API: Event handlers, when set to literally `undefined` (or any non-function), are now correctly removed. - core: `xlink:href` attributes are now correctly removed - core: render() function can no longer prevent from changing `document.activeElement` in lifecycle hooks +- render: fixed an ommission that caused `oninit` to be called unnecessarily in some cases [#1992](https://github.com/MithrilJS/mithril.js/issues/1992) --- diff --git a/render/render.js b/render/render.js index 8674925b..37855447 100644 --- a/render/render.js +++ b/render/render.js @@ -235,7 +235,7 @@ module.exports = function($window) { if (oldIndex != null) { var movable = old[oldIndex] var shouldRecycle = (pool != null && oldIndex >= old.length - pool.length) || ((pool == null) && recycling) - updateNode(parent, movable, v, hooks, getNextSibling(old, oldEnd + 1, nextSibling), recycling, ns) + updateNode(parent, movable, v, hooks, getNextSibling(old, oldEnd + 1, nextSibling), shouldRecycle, ns) insertNode(parent, toFragment(movable), nextSibling) old[oldIndex].skip = true if (movable.dom != null) nextSibling = movable.dom diff --git a/render/tests/test-oninit.js b/render/tests/test-oninit.js index 4d94cae4..f6ffb873 100644 --- a/render/tests/test-oninit.js +++ b/render/tests/test-oninit.js @@ -199,4 +199,27 @@ o.spec("oninit", function() { o(vnode.dom.oninit).equals(undefined) o(vnode.dom.attributes["oninit"]).equals(undefined) }) + + o("No spurious oninit calls in mapped keyed diff when the pool is involved (#1992)", function () { + var oninit1 = o.spy() + var oninit2 = o.spy() + var oninit3 = o.spy() + + render(root, [ + {tag: "p", key: 1, attrs: {oninit: oninit1}}, + {tag: "p", key: 2, attrs: {oninit: oninit2}}, + {tag: "p", key: 3, attrs: {oninit: oninit3}}, + ]) + render(root, [ + {tag: "p", key: 1, attrs: {oninit: oninit1}}, + {tag: "p", key: 3, attrs: {oninit: oninit3}}, + ]) + render(root, [ + {tag: "p", key: 3, attrs: {oninit: oninit3}}, + ]) + + o(oninit1.callCount).equals(1) + o(oninit2.callCount).equals(1) + o(oninit3.callCount).equals(1) + }) })