patchFlags.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Patch flags are optimization hints generated by the compiler.
  2. // when a block with dynamicChildren is encountered during diff, the algorithm
  3. // enters "optimized mode". In this mode, we know that the vdom is produced by
  4. // a render function generated by the compiler, so the algorithm only needs to
  5. // handle updates explicitly marked by these patch flags.
  6. // Patch flags can be combined using the | bitwise operator and can be checked
  7. // using the & operator, e.g.
  8. //
  9. // const flag = TEXT | CLASS
  10. // if (flag & TEXT) { ... }
  11. //
  12. // Check the `patchElement` function in './createRenderer.ts' to see how the
  13. // flags are handled during diff.
  14. export const enum PatchFlags {
  15. // Indicates an element with dynamic textContent (children fast path)
  16. TEXT = 1,
  17. // Indicates an element with dynamic class binding.
  18. CLASS = 1 << 1,
  19. // Indicates an element with dynamic style
  20. // The compiler pre-compiles static string styles into static objects
  21. // + detects and hoists inline static objects
  22. // e.g. style="color: red" and :style="{ color: 'red' }" both get hoisted as
  23. // const style = { color: 'red' }
  24. // render() { return e('div', { style }) }
  25. STYLE = 1 << 2,
  26. // Indicates an element that has non-class/style dynamic props.
  27. // Can also be on a component that has any dynamic props (includes
  28. // class/style). when this flag is present, the vnode also has a dynamicProps
  29. // array that contains the keys of the props that may change so the runtime
  30. // can diff them faster (without having to worry about removed props)
  31. PROPS = 1 << 3,
  32. // Indicates an element with props with dynamic keys. When keys change, a full
  33. // diff is always needed to remove the old key. This flag is mutually
  34. // exclusive with CLASS, STYLE and PROPS.
  35. FULL_PROPS = 1 << 4,
  36. // Indicates an element that only needs non-props patching, e.g. ref or
  37. // directives (vnodeXXX hooks). It simply marks the vnode as "need patch",
  38. // since every patched vnode checks for refs and vnodeXXX hooks.
  39. // This flag is never directly matched against, it simply serves as a non-zero
  40. // value.
  41. NEED_PATCH = 1 << 5,
  42. // Indicates a fragment with keyed or partially keyed children
  43. KEYED_FRAGMENT = 1 << 6,
  44. // Indicates a fragment with unkeyed children.
  45. UNKEYED_FRAGMENT = 1 << 7,
  46. // Indicates a component with dynamic slots (e.g. slot that references a v-for
  47. // iterated value, or dynamic slot names).
  48. // Components with this flag are always force updated.
  49. DYNAMIC_SLOTS = 1 << 8,
  50. // A special flag that indicates that the diffing algorithm should bail out
  51. // of optimized mode. This is only on block fragments created by renderSlot()
  52. // when encountering non-compiler generated slots (i.e. manually written
  53. // render functions, which should always be fully diffed)
  54. BAIL = -1
  55. }
  56. // runtime object for public consumption
  57. export const PublicPatchFlags = {
  58. TEXT: PatchFlags.TEXT,
  59. CLASS: PatchFlags.CLASS,
  60. STYLE: PatchFlags.STYLE,
  61. PROPS: PatchFlags.PROPS,
  62. NEED_PATCH: PatchFlags.NEED_PATCH,
  63. FULL_PROPS: PatchFlags.FULL_PROPS,
  64. KEYED_FRAGMENT: PatchFlags.KEYED_FRAGMENT,
  65. UNKEYED_FRAGMENT: PatchFlags.UNKEYED_FRAGMENT,
  66. DYNAMIC_SLOTS: PatchFlags.DYNAMIC_SLOTS,
  67. BAIL: PatchFlags.BAIL
  68. }
  69. // dev only flag -> name mapping
  70. export const PatchFlagNames = {
  71. [PatchFlags.TEXT]: `TEXT`,
  72. [PatchFlags.CLASS]: `CLASS`,
  73. [PatchFlags.STYLE]: `STYLE`,
  74. [PatchFlags.PROPS]: `PROPS`,
  75. [PatchFlags.NEED_PATCH]: `NEED_PATCH`,
  76. [PatchFlags.FULL_PROPS]: `FULL_PROPS`,
  77. [PatchFlags.KEYED_FRAGMENT]: `KEYED_FRAGMENT`,
  78. [PatchFlags.UNKEYED_FRAGMENT]: `UNKEYED_FRAGMENT`,
  79. [PatchFlags.DYNAMIC_SLOTS]: `DYNAMIC_SLOTS`,
  80. [PatchFlags.BAIL]: `BAIL`
  81. }