Add postpatch hook

pull/8/head
paldepind 10 years ago
parent 7344b0aca5
commit 26a62cab4d

@ -124,12 +124,13 @@ var vnode = h('div', {style: {color: '#000'}}, [
#### Overview #### Overview
| Name | Triggered when | Arguments to callback | | Name | Triggered when | Arguments to callback |
| --------- | -------------- | --------------------- | | ----------- | -------------- | --------------------- |
| `pre` | the patch process begins. | none | | `pre` | the patch process begins. | none |
| `create` | a DOM element has been created based on a VNode. | `emptyVNode, vnode` | | `create` | a DOM element has been created based on a VNode. | `emptyVNode, vnode` |
| `insert` | an element has been inserted into the DOM. | `vnode` | | `insert` | an element has been inserted into the DOM. | `vnode` |
| `patch` | an element is about to be patched. | `oldVnode, vnode` | | `prepatch` | an element is about to be patched. | `oldVnode, vnode` |
| `update` | an element is being updated. | `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` | | `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` | | `destroy` | an element is being removed from the DOM or it's parent is. | `vnode` |
| `post` | the patch process is done. | none | | `post` | the patch process is done. | none |

@ -175,9 +175,9 @@ function init(modules) {
} }
function patchVnode(oldVnode, vnode) { function patchVnode(oldVnode, vnode) {
var i; var i, hook;
if (!isUndef(i = vnode.data) && !isUndef(i = i.hook) && !isUndef(i = i.patch)) { if (!isUndef(i = vnode.data) && !isUndef(hook = i.hook) && !isUndef(i = hook.prepatch)) {
i = i(oldVnode, vnode); i(oldVnode, vnode);
} }
if (!isUndef(i = oldVnode.data) && !isUndef(i = i.vnode)) oldVnode = i; if (!isUndef(i = oldVnode.data) && !isUndef(i = i.vnode)) oldVnode = i;
if (!isUndef(i = vnode.data) && !isUndef(i = i.vnode)) vnode = 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) { } else if (oldVnode.text !== vnode.text) {
elm.textContent = vnode.text; elm.textContent = vnode.text;
} }
if (!isUndef(hook) && !isUndef(i = hook.postpatch)) {
i(oldVnode, vnode);
}
return vnode; return vnode;
} }

@ -505,7 +505,7 @@ describe('snabbdom', function() {
patch(vnode0, vnode1); patch(vnode0, vnode1);
assert.equal(1, result.length); assert.equal(1, result.length);
}); });
it('calls `patch` listener', function() { it('calls `prepatch` listener', function() {
var result = []; var result = [];
function cb(oldVnode, vnode) { function cb(oldVnode, vnode) {
assert.strictEqual(oldVnode, vnode1.children[1]); assert.strictEqual(oldVnode, vnode1.children[1]);
@ -514,14 +514,14 @@ describe('snabbdom', function() {
} }
var vnode1 = h('div', [ var vnode1 = h('div', [
h('span', 'First sibling'), h('span', 'First sibling'),
h('div', {hook: {patch: cb}}, [ h('div', {hook: {prepatch: cb}}, [
h('span', 'Child 1'), h('span', 'Child 1'),
h('span', 'Child 2'), h('span', 'Child 2'),
]), ]),
]); ]);
var vnode2 = h('div', [ var vnode2 = h('div', [
h('span', 'First sibling'), h('span', 'First sibling'),
h('div', {hook: {patch: cb}}, [ h('div', {hook: {prepatch: cb}}, [
h('span', 'Child 1'), h('span', 'Child 1'),
h('span', 'Child 2'), h('span', 'Child 2'),
]), ]),
@ -530,6 +530,34 @@ describe('snabbdom', function() {
patch(vnode1, vnode2); patch(vnode1, vnode2);
assert.equal(result.length, 1); 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() { it('calls `update` listener', function() {
var result1 = []; var result1 = [];
var result2 = []; var result2 = [];

@ -5,7 +5,7 @@ function init(thunk) {
cur.vnode = cur.fn.apply(undefined, cur.args); 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 i, old = oldThunk.data, cur = thunk.data;
var oldArgs = old.args, args = cur.args; var oldArgs = old.args, args = cur.args;
cur.vnode = old.vnode; cur.vnode = old.vnode;
@ -27,7 +27,7 @@ module.exports = function(name, fn /* args */) {
args[i - 2] = arguments[i]; args[i - 2] = arguments[i];
} }
return h('thunk' + name, { return h('thunk' + name, {
hook: {init: init, patch: patch}, hook: {init: init, prepatch: prepatch},
fn: fn, args: args, fn: fn, args: args,
}); });
}; };

Loading…
Cancel
Save