dispose.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {
  2. recyclables
  3. } from '../util';
  4. export function disposeVnode(vnode) {
  5. if (!vnode) {
  6. return;
  7. }
  8. if (vnode.refType === 1) {
  9. vnode.ref(null);
  10. vnode.ref = null;
  11. }
  12. if (vnode.mayInfo.instance) {
  13. disposeComponent(vnode, vnode.mayInfo.instance);
  14. } else if (vnode.mtype === 1) {
  15. disposeDomVnode(vnode);
  16. }
  17. vnode.mayInfo = null;
  18. }
  19. function disposeDomVnode(vnode) {
  20. var children = vnode.mayInfo.vChildren;
  21. if (children) {
  22. for (var c in children) {
  23. children[c].forEach(function (child) {
  24. disposeVnode(child);
  25. })
  26. }
  27. vnode.mayInfo.vChildren = null;
  28. }
  29. if (vnode.mayInfo.refOwner) {
  30. vnode.mayInfo.refOwner = null;
  31. }
  32. vnode.mayInfo = null;
  33. }
  34. export function disposeComponent(vnode, instance) {
  35. if (instance.setState) {
  36. instance.setState = noop;
  37. instance.forceUpdate = noop;
  38. }
  39. if (instance.componentWillUnmount) {
  40. instance.componentWillUnmount();
  41. instance.componentWillUnmount = noop;
  42. }
  43. if (instance.refs) {
  44. instance.refs = null;
  45. }
  46. if (instance.mayInst.rendered) {
  47. // vnode.mayInfo.rendered = null;
  48. disposeVnode(instance.mayInst.rendered);
  49. }
  50. instance.mayInst.forceUpdate = instance.mayInst.dirty = vnode.mayInfo.instance = instance.mayInst = null;
  51. }
  52. var isStandard = 'textContent' in document;
  53. var fragment = document.createDocumentFragment();
  54. export function disposeDom(dom) {
  55. if (dom._listener) {
  56. dom._listener = null;
  57. }
  58. if (dom.nodeType === 1) {
  59. if (isStandard) {
  60. dom.textContent = '';
  61. } else {
  62. emptyElement(dom);
  63. }
  64. } else if (dom.nodeType === 3) {
  65. if (recyclables['#text'].length < 100) {
  66. recyclables['#text'].push(dom);
  67. }
  68. }
  69. fragment.appendChild(dom);
  70. fragment.removeChild(dom);
  71. }
  72. export function emptyElement(dom) {
  73. var c;
  74. while (c = dom.firstChild) {
  75. emptyElement(c);
  76. dom.removeChild(c);
  77. }
  78. }
  79. function noop() { };