patchFlags.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Patch flags are optimization hints generated by the compiler.
  3. * when a block with dynamicChildren is encountered during diff, the algorithm
  4. * enters "optimized mode". In this mode, we know that the vdom is produced by
  5. * a render function generated by the compiler, so the algorithm only needs to
  6. * handle updates explicitly marked by these patch flags.
  7. *
  8. * Patch flags can be combined using the | bitwise operator and can be checked
  9. * using the & operator, e.g.
  10. *
  11. * ```js
  12. * const flag = TEXT | CLASS
  13. * if (flag & TEXT) { ... }
  14. * ```
  15. *
  16. * Check the `patchElement` function in '../../runtime-core/src/renderer.ts' to see how the
  17. * flags are handled during diff.
  18. */
  19. export enum PatchFlags {
  20. /**
  21. * Indicates an element with dynamic textContent (children fast path)
  22. */
  23. TEXT = 1,
  24. /**
  25. * Indicates an element with dynamic class binding.
  26. */
  27. CLASS = 1 << 1,
  28. /**
  29. * Indicates an element with dynamic style
  30. * The compiler pre-compiles static string styles into static objects
  31. * + detects and hoists inline static objects
  32. * e.g. `style="color: red"` and `:style="{ color: 'red' }"` both get hoisted
  33. * as:
  34. * ```js
  35. * const style = { color: 'red' }
  36. * render() { return e('div', { style }) }
  37. * ```
  38. */
  39. STYLE = 1 << 2,
  40. /**
  41. * Indicates an element that has non-class/style dynamic props.
  42. * Can also be on a component that has any dynamic props (includes
  43. * class/style). when this flag is present, the vnode also has a dynamicProps
  44. * array that contains the keys of the props that may change so the runtime
  45. * can diff them faster (without having to worry about removed props)
  46. */
  47. PROPS = 1 << 3,
  48. /**
  49. * Indicates an element with props with dynamic keys. When keys change, a full
  50. * diff is always needed to remove the old key. This flag is mutually
  51. * exclusive with CLASS, STYLE and PROPS.
  52. */
  53. FULL_PROPS = 1 << 4,
  54. /**
  55. * Indicates an element that requires props hydration
  56. * (but not necessarily patching)
  57. * e.g. event listeners & v-bind with prop modifier
  58. */
  59. NEED_HYDRATION = 1 << 5,
  60. /**
  61. * Indicates a fragment whose children order doesn't change.
  62. */
  63. STABLE_FRAGMENT = 1 << 6,
  64. /**
  65. * Indicates a fragment with keyed or partially keyed children
  66. */
  67. KEYED_FRAGMENT = 1 << 7,
  68. /**
  69. * Indicates a fragment with unkeyed children.
  70. */
  71. UNKEYED_FRAGMENT = 1 << 8,
  72. /**
  73. * Indicates an element that only needs non-props patching, e.g. ref or
  74. * directives (onVnodeXXX hooks). since every patched vnode checks for refs
  75. * and onVnodeXXX hooks, it simply marks the vnode so that a parent block
  76. * will track it.
  77. */
  78. NEED_PATCH = 1 << 9,
  79. /**
  80. * Indicates a component with dynamic slots (e.g. slot that references a v-for
  81. * iterated value, or dynamic slot names).
  82. * Components with this flag are always force updated.
  83. */
  84. DYNAMIC_SLOTS = 1 << 10,
  85. /**
  86. * Indicates a fragment that was created only because the user has placed
  87. * comments at the root level of a template. This is a dev-only flag since
  88. * comments are stripped in production.
  89. */
  90. DEV_ROOT_FRAGMENT = 1 << 11,
  91. /**
  92. * SPECIAL FLAGS -------------------------------------------------------------
  93. * Special flags are negative integers. They are never matched against using
  94. * bitwise operators (bitwise matching should only happen in branches where
  95. * patchFlag > 0), and are mutually exclusive. When checking for a special
  96. * flag, simply check patchFlag === FLAG.
  97. */
  98. /**
  99. * Indicates a hoisted static vnode. This is a hint for hydration to skip
  100. * the entire sub tree since static content never needs to be updated.
  101. */
  102. HOISTED = -1,
  103. /**
  104. * A special flag that indicates that the diffing algorithm should bail out
  105. * of optimized mode. For example, on block fragments created by renderSlot()
  106. * when encountering non-compiler generated slots (i.e. manually written
  107. * render functions, which should always be fully diffed)
  108. * OR manually cloneVNodes
  109. */
  110. BAIL = -2,
  111. }
  112. /**
  113. * dev only flag -> name mapping
  114. */
  115. export const PatchFlagNames: Record<PatchFlags, string> = {
  116. [PatchFlags.TEXT]: `TEXT`,
  117. [PatchFlags.CLASS]: `CLASS`,
  118. [PatchFlags.STYLE]: `STYLE`,
  119. [PatchFlags.PROPS]: `PROPS`,
  120. [PatchFlags.FULL_PROPS]: `FULL_PROPS`,
  121. [PatchFlags.NEED_HYDRATION]: `NEED_HYDRATION`,
  122. [PatchFlags.STABLE_FRAGMENT]: `STABLE_FRAGMENT`,
  123. [PatchFlags.KEYED_FRAGMENT]: `KEYED_FRAGMENT`,
  124. [PatchFlags.UNKEYED_FRAGMENT]: `UNKEYED_FRAGMENT`,
  125. [PatchFlags.NEED_PATCH]: `NEED_PATCH`,
  126. [PatchFlags.DYNAMIC_SLOTS]: `DYNAMIC_SLOTS`,
  127. [PatchFlags.DEV_ROOT_FRAGMENT]: `DEV_ROOT_FRAGMENT`,
  128. [PatchFlags.HOISTED]: `HOISTED`,
  129. [PatchFlags.BAIL]: `BAIL`,
  130. }