feat(compiler): generate patchFlags for runtime
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
export * from './patchFlags'
|
||||
|
||||
export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
|
||||
? Object.freeze({})
|
||||
: {}
|
||||
|
||||
73
packages/shared/src/patchFlags.ts
Normal file
73
packages/shared/src/patchFlags.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
// Patch flags are optimization hints generated by the compiler.
|
||||
// when a block with dynamicChildren is encountered during diff, the algorithm
|
||||
// enters "optimized mode". In this mode, we know that the vdom is produced by
|
||||
// a render function generated by the compiler, so the algorithm only needs to
|
||||
// handle updates explicitly marked by these patch flags.
|
||||
|
||||
// Patch flags can be combined using the | bitwise operator and can be checked
|
||||
// using the & operator, e.g.
|
||||
//
|
||||
// const flag = TEXT | CLASS
|
||||
// if (flag & TEXT) { ... }
|
||||
//
|
||||
// Check the `patchElement` function in './createRednerer.ts' to see how the
|
||||
// flags are handled during diff.
|
||||
|
||||
export const enum PatchFlags {
|
||||
// Indicates an element with dynamic textContent (children fast path)
|
||||
TEXT = 1,
|
||||
|
||||
// Indicates an element with dynamic class binding.
|
||||
CLASS = 1 << 1,
|
||||
|
||||
// Indicates an element with dynamic style
|
||||
// The compiler pre-compiles static string styles into static objects
|
||||
// + detects and hoists inline static objects
|
||||
// e.g. style="color: red" and :style="{ color: 'red' }" both get hoisted as
|
||||
// const style = { color: 'red' }
|
||||
// render() { return e('div', { style }) }
|
||||
STYLE = 1 << 2,
|
||||
|
||||
// Indicates an element that has non-class/style dynamic props.
|
||||
// Can also be on a component that has any dynamic props (includes
|
||||
// class/style). when this flag is present, the vnode also has a dynamicProps
|
||||
// array that contains the keys of the props that may change so the runtime
|
||||
// can diff them faster (without having to worry about removed props)
|
||||
PROPS = 1 << 3,
|
||||
|
||||
// Indicates an element with props with dynamic keys. When keys change, a full
|
||||
// diff is always needed to remove the old key. This flag is mutually
|
||||
// exclusive with CLASS, STYLE and PROPS.
|
||||
FULL_PROPS = 1 << 4,
|
||||
|
||||
// Indicates an element that only needs non-props patching, e.g. ref or
|
||||
// directives (vnodeXXX hooks). It simply marks the vnode as "need patch",
|
||||
// since every pathced vnode checks for refs and vnodeXXX hooks.
|
||||
// This flag is never directly matched against, it simply serves as a non-zero
|
||||
// value.
|
||||
NEED_PATCH = 1 << 5,
|
||||
|
||||
// Indicates a fragment or element with keyed or partially-keyed v-for
|
||||
// children
|
||||
KEYED = 1 << 6,
|
||||
|
||||
// Indicates a fragment or element that contains unkeyed v-for children
|
||||
UNKEYED = 1 << 7,
|
||||
|
||||
// Indicates a component with dynamic slots (e.g. slot that references a v-for
|
||||
// iterated value, or dynamic slot names).
|
||||
// Components with this flag are always force updated.
|
||||
DYNAMIC_SLOTS = 1 << 8
|
||||
}
|
||||
|
||||
// runtime object for public consumption
|
||||
export const PublicPatchFlags = {
|
||||
TEXT: PatchFlags.TEXT,
|
||||
CLASS: PatchFlags.CLASS,
|
||||
STYLE: PatchFlags.STYLE,
|
||||
PROPS: PatchFlags.PROPS,
|
||||
NEED_PATCH: PatchFlags.NEED_PATCH,
|
||||
FULL_PROPS: PatchFlags.FULL_PROPS,
|
||||
KEYED: PatchFlags.KEYED,
|
||||
UNKEYED: PatchFlags.UNKEYED
|
||||
}
|
||||
Reference in New Issue
Block a user