patchFlags.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 (onVnodeXXX hooks). It simply marks the vnode as "need patch",
  38. // since every patched vnode checks for refs and onVnodeXXX 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 whose children order doesn't change.
  43. STABLE_FRAGMENT = 1 << 6,
  44. // Indicates a fragment with keyed or partially keyed children
  45. KEYED_FRAGMENT = 1 << 7,
  46. // Indicates a fragment with unkeyed children.
  47. UNKEYED_FRAGMENT = 1 << 8,
  48. // Indicates a component with dynamic slots (e.g. slot that references a v-for
  49. // iterated value, or dynamic slot names).
  50. // Components with this flag are always force updated.
  51. DYNAMIC_SLOTS = 1 << 9,
  52. // A special flag that indicates that the diffing algorithm should bail out
  53. // of optimized mode. This is only on block fragments created by renderSlot()
  54. // when encountering non-compiler generated slots (i.e. manually written
  55. // render functions, which should always be fully diffed)
  56. BAIL = -1
  57. }
  58. // runtime object for public consumption
  59. export const PublicPatchFlags = {
  60. TEXT: PatchFlags.TEXT,
  61. CLASS: PatchFlags.CLASS,
  62. STYLE: PatchFlags.STYLE,
  63. PROPS: PatchFlags.PROPS,
  64. NEED_PATCH: PatchFlags.NEED_PATCH,
  65. FULL_PROPS: PatchFlags.FULL_PROPS,
  66. KEYED_FRAGMENT: PatchFlags.KEYED_FRAGMENT,
  67. UNKEYED_FRAGMENT: PatchFlags.UNKEYED_FRAGMENT,
  68. DYNAMIC_SLOTS: PatchFlags.DYNAMIC_SLOTS,
  69. BAIL: PatchFlags.BAIL
  70. }
  71. // dev only flag -> name mapping
  72. export const PatchFlagNames = {
  73. [PatchFlags.TEXT]: `TEXT`,
  74. [PatchFlags.CLASS]: `CLASS`,
  75. [PatchFlags.STYLE]: `STYLE`,
  76. [PatchFlags.PROPS]: `PROPS`,
  77. [PatchFlags.NEED_PATCH]: `NEED_PATCH`,
  78. [PatchFlags.FULL_PROPS]: `FULL_PROPS`,
  79. [PatchFlags.STABLE_FRAGMENT]: `STABLE_FRAGMENT`,
  80. [PatchFlags.KEYED_FRAGMENT]: `KEYED_FRAGMENT`,
  81. [PatchFlags.UNKEYED_FRAGMENT]: `UNKEYED_FRAGMENT`,
  82. [PatchFlags.DYNAMIC_SLOTS]: `DYNAMIC_SLOTS`,
  83. [PatchFlags.BAIL]: `BAIL`
  84. }