From 26a62cab4d8d09a1c2a4c033ed732b566451b699 Mon Sep 17 00:00:00 2001 From: paldepind Date: Thu, 11 Jun 2015 11:17:42 +0200 Subject: [PATCH] Add postpatch hook --- README.md | 21 +++++++++++---------- snabbdom.js | 9 ++++++--- test/core.js | 34 +++++++++++++++++++++++++++++++--- thunk.js | 4 ++-- 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 27d3fde..7a591a3 100644 --- a/README.md +++ b/README.md @@ -123,16 +123,17 @@ var vnode = h('div', {style: {color: '#000'}}, [ #### Overview -| Name | Triggered when | Arguments to callback | -| --------- | -------------- | --------------------- | -| `pre` | the patch process begins. | none | -| `create` | a DOM element has been created based on a VNode. | `emptyVNode, vnode` | -| `insert` | an element has been inserted into the DOM. | `vnode` | -| `patch` | an element is about to be patched. | `oldVnode, vnode` | -| `update` | an element is being updated. | `oldVnode, vnode` | -| `remove` | an element is directly being removed from the DOM. | `vnode, removeCallback` | -| `destroy` | an element is being removed from the DOM or it's parent is. | `vnode` | -| `post` | the patch process is done. | none | +| Name | Triggered when | Arguments to callback | +| ----------- | -------------- | --------------------- | +| `pre` | the patch process begins. | none | +| `create` | a DOM element has been created based on a VNode. | `emptyVNode, vnode` | +| `insert` | an element has been inserted into the DOM. | `vnode` | +| `prepatch` | an element is about to be patched. | `oldVnode, vnode` | +| `update` | an element is being updated. | `oldVnode, vnode` | +| `postpatch` | an element has been patched. | `oldVnode, vnode` | +| `remove` | an element is directly being removed from the DOM. | `vnode, removeCallback` | +| `destroy` | an element is being removed from the DOM or it's parent is. | `vnode` | +| `post` | the patch process is done. | none | ## Modules documentation diff --git a/snabbdom.js b/snabbdom.js index 7f9f298..f7895d5 100644 --- a/snabbdom.js +++ b/snabbdom.js @@ -175,9 +175,9 @@ function init(modules) { } function patchVnode(oldVnode, vnode) { - var i; - if (!isUndef(i = vnode.data) && !isUndef(i = i.hook) && !isUndef(i = i.patch)) { - i = i(oldVnode, vnode); + var i, hook; + if (!isUndef(i = vnode.data) && !isUndef(hook = i.hook) && !isUndef(i = hook.prepatch)) { + i(oldVnode, vnode); } if (!isUndef(i = oldVnode.data) && !isUndef(i = i.vnode)) oldVnode = i; if (!isUndef(i = vnode.data) && !isUndef(i = i.vnode)) vnode = i; @@ -199,6 +199,9 @@ function init(modules) { } else if (oldVnode.text !== vnode.text) { elm.textContent = vnode.text; } + if (!isUndef(hook) && !isUndef(i = hook.postpatch)) { + i(oldVnode, vnode); + } return vnode; } diff --git a/test/core.js b/test/core.js index fab0384..7f99d61 100644 --- a/test/core.js +++ b/test/core.js @@ -505,7 +505,7 @@ describe('snabbdom', function() { patch(vnode0, vnode1); assert.equal(1, result.length); }); - it('calls `patch` listener', function() { + it('calls `prepatch` listener', function() { var result = []; function cb(oldVnode, vnode) { assert.strictEqual(oldVnode, vnode1.children[1]); @@ -514,14 +514,14 @@ describe('snabbdom', function() { } var vnode1 = h('div', [ h('span', 'First sibling'), - h('div', {hook: {patch: cb}}, [ + h('div', {hook: {prepatch: cb}}, [ h('span', 'Child 1'), h('span', 'Child 2'), ]), ]); var vnode2 = h('div', [ h('span', 'First sibling'), - h('div', {hook: {patch: cb}}, [ + h('div', {hook: {prepatch: cb}}, [ h('span', 'Child 1'), h('span', 'Child 2'), ]), @@ -530,6 +530,34 @@ describe('snabbdom', function() { patch(vnode1, vnode2); assert.equal(result.length, 1); }); + it('calls `postpatch` after `prepatch` listener', function() { + var pre = [], post = []; + function preCb(oldVnode, vnode) { + pre.push(pre); + } + function postCb(oldVnode, vnode) { + assert.equal(pre.length, post.length + 1); + post.push(post); + } + var vnode1 = h('div', [ + h('span', 'First sibling'), + h('div', {hook: {prepatch: preCb, postpatch: postCb}}, [ + h('span', 'Child 1'), + h('span', 'Child 2'), + ]), + ]); + var vnode2 = h('div', [ + h('span', 'First sibling'), + h('div', {hook: {prepatch: preCb, postpatch: postCb}}, [ + h('span', 'Child 1'), + h('span', 'Child 2'), + ]), + ]); + patch(vnode0, vnode1); + patch(vnode1, vnode2); + assert.equal(pre.length, 1); + assert.equal(post.length, 1); + }); it('calls `update` listener', function() { var result1 = []; var result2 = []; diff --git a/thunk.js b/thunk.js index d51a610..5372782 100644 --- a/thunk.js +++ b/thunk.js @@ -5,7 +5,7 @@ function init(thunk) { cur.vnode = cur.fn.apply(undefined, cur.args); } -function patch(oldThunk, thunk) { +function prepatch(oldThunk, thunk) { var i, old = oldThunk.data, cur = thunk.data; var oldArgs = old.args, args = cur.args; cur.vnode = old.vnode; @@ -27,7 +27,7 @@ module.exports = function(name, fn /* args */) { args[i - 2] = arguments[i]; } return h('thunk' + name, { - hook: {init: init, patch: patch}, + hook: {init: init, prepatch: prepatch}, fn: fn, args: args, }); };