-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhook.js
49 lines (45 loc) · 1.23 KB
/
hook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const { _snabbmitt } = require('../symbols');
const copyRefs = require('./copy-refs');
const noop = () => {};
const hooks = [
'pre',
'init',
'create',
'insert',
'prepatch',
'update',
'postpatch',
'destroy',
'remove',
'post'
];
module.exports = function applyHook(vnode) {
vnode.data.hook = vnode.data.hook || {};
const componentHooks = {
create(...args) {
const vnode = args[args.length - 1];
if (vnode.data[_snabbmitt]) {
vnode.data[_snabbmitt].cvnode.elm = vnode.elm;
}
},
postpatch(oldVnode, vnode) {
if (oldVnode.data[_snabbmitt]) {
const rvnode = oldVnode.data[_snabbmitt].rvnode;
copyRefs(rvnode, vnode);
vnode.data[_snabbmitt] = oldVnode.data[_snabbmitt];
}
}
};
for (const hook of hooks) {
if (!vnode.data.hook[hook] && !componentHooks[hook]) {
continue;
}
const cb = vnode.data.hook[hook] || noop;
const componentCb = componentHooks[hook] || noop;
vnode.data.hook[hook] = (...args) => {
componentCb(...args);
cb(...args);
};
}
return vnode;
};