1 | import { h } from 'snabbdom';
|
2 | function copyToThunk(vnode, thunkVNode) {
|
3 | thunkVNode.elm = vnode.elm;
|
4 | vnode.data.fn = thunkVNode.data.fn;
|
5 | vnode.data.args = thunkVNode.data.args;
|
6 | vnode.data.isolate = thunkVNode.data.isolate;
|
7 | thunkVNode.data = vnode.data;
|
8 | thunkVNode.children = vnode.children;
|
9 | thunkVNode.text = vnode.text;
|
10 | thunkVNode.elm = vnode.elm;
|
11 | }
|
12 | function init(thunkVNode) {
|
13 | var cur = thunkVNode.data;
|
14 | var vnode = cur.fn.apply(undefined, cur.args);
|
15 | copyToThunk(vnode, thunkVNode);
|
16 | }
|
17 | function prepatch(oldVnode, thunkVNode) {
|
18 | var old = oldVnode.data, cur = thunkVNode.data;
|
19 | var i;
|
20 | var oldArgs = old.args, args = cur.args;
|
21 | if (old.fn !== cur.fn || oldArgs.length !== args.length) {
|
22 | copyToThunk(cur.fn.apply(undefined, args), thunkVNode);
|
23 | }
|
24 | for (i = 0; i < args.length; ++i) {
|
25 | if (oldArgs[i] !== args[i]) {
|
26 | copyToThunk(cur.fn.apply(undefined, args), thunkVNode);
|
27 | return;
|
28 | }
|
29 | }
|
30 | copyToThunk(oldVnode, thunkVNode);
|
31 | }
|
32 | export function thunk(sel, key, fn, args) {
|
33 | if (args === undefined) {
|
34 | args = fn;
|
35 | fn = key;
|
36 | key = undefined;
|
37 | }
|
38 | return h(sel, {
|
39 | key: key,
|
40 | hook: { init: init, prepatch: prepatch },
|
41 | fn: fn,
|
42 | args: args,
|
43 | });
|
44 | }
|
45 | export default thunk;
|
46 |
|
\ | No newline at end of file |