thunk.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { h, addNS } from "./h.js";
  2. function copyToThunk(vnode, thunk) {
  3. var _a;
  4. const ns = (_a = thunk.data) === null || _a === void 0 ? void 0 : _a.ns;
  5. vnode.data.fn = thunk.data.fn;
  6. vnode.data.args = thunk.data.args;
  7. thunk.data = vnode.data;
  8. thunk.children = vnode.children;
  9. thunk.text = vnode.text;
  10. thunk.elm = vnode.elm;
  11. if (ns)
  12. addNS(thunk.data, thunk.children, thunk.sel);
  13. }
  14. function init(thunk) {
  15. const cur = thunk.data;
  16. const vnode = cur.fn(...cur.args);
  17. copyToThunk(vnode, thunk);
  18. }
  19. function prepatch(oldVnode, thunk) {
  20. let i;
  21. const old = oldVnode.data;
  22. const cur = thunk.data;
  23. const oldArgs = old.args;
  24. const args = cur.args;
  25. if (old.fn !== cur.fn || oldArgs.length !== args.length) {
  26. copyToThunk(cur.fn(...args), thunk);
  27. return;
  28. }
  29. for (i = 0; i < args.length; ++i) {
  30. if (oldArgs[i] !== args[i]) {
  31. copyToThunk(cur.fn(...args), thunk);
  32. return;
  33. }
  34. }
  35. copyToThunk(oldVnode, thunk);
  36. }
  37. export const thunk = function thunk(sel, key, fn, args) {
  38. if (args === undefined) {
  39. args = fn;
  40. fn = key;
  41. key = undefined;
  42. }
  43. return h(sel, {
  44. key: key,
  45. hook: { init, prepatch },
  46. fn: fn,
  47. args: args
  48. });
  49. };
  50. //# sourceMappingURL=thunk.js.map