Add postpatch hook

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

@ -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

@ -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;
}

@ -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 = [];

@ -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,
});
};

Loading…
Cancel
Save