| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010 |
- import {
- Text,
- Fragment,
- Comment,
- cloneIfMounted,
- normalizeVNode,
- VNode,
- VNodeArrayChildren,
- createVNode,
- isSameVNodeType,
- Static,
- VNodeNormalizedRef,
- VNodeHook
- } from './vnode'
- import {
- ComponentInternalInstance,
- createComponentInstance,
- Data,
- setupComponent
- } from './component'
- import {
- renderComponentRoot,
- shouldUpdateComponent,
- updateHOCHostEl
- } from './componentRenderUtils'
- import {
- isString,
- EMPTY_OBJ,
- EMPTY_ARR,
- isReservedProp,
- isFunction,
- PatchFlags,
- ShapeFlags,
- NOOP,
- hasOwn,
- invokeArrayFns
- } from '@vue/shared'
- import {
- queueJob,
- queuePostFlushCb,
- flushPostFlushCbs,
- invalidateJob
- } from './scheduler'
- import { effect, stop, ReactiveEffectOptions, isRef } from '@vue/reactivity'
- import { resolveProps } from './componentProps'
- import { resolveSlots } from './componentSlots'
- import { pushWarningContext, popWarningContext, warn } from './warning'
- import { ComponentPublicInstance } from './componentProxy'
- import { createAppAPI, CreateAppFunction } from './apiCreateApp'
- import {
- SuspenseBoundary,
- queueEffectWithSuspense,
- SuspenseImpl
- } from './components/Suspense'
- import { TeleportImpl } from './components/Teleport'
- import { KeepAliveSink, isKeepAlive } from './components/KeepAlive'
- import { registerHMR, unregisterHMR } from './hmr'
- import {
- ErrorCodes,
- callWithErrorHandling,
- callWithAsyncErrorHandling
- } from './errorHandling'
- import { createHydrationFunctions, RootHydrateFunction } from './hydration'
- import { invokeDirectiveHook } from './directives'
- import { startMeasure, endMeasure } from './profiling'
- const __HMR__ = __BUNDLER__ && __DEV__
- export interface Renderer<HostElement = any> {
- render: RootRenderFunction<HostElement>
- createApp: CreateAppFunction<HostElement>
- }
- export interface HydrationRenderer extends Renderer<Element> {
- hydrate: RootHydrateFunction
- }
- export type RootRenderFunction<HostElement = RendererElement> = (
- vnode: VNode | null,
- container: HostElement
- ) => void
- export interface RendererOptions<
- HostNode = RendererNode,
- HostElement = RendererElement
- > {
- patchProp(
- el: HostElement,
- key: string,
- prevValue: any,
- nextValue: any,
- isSVG?: boolean,
- prevChildren?: VNode<HostNode, HostElement>[],
- parentComponent?: ComponentInternalInstance | null,
- parentSuspense?: SuspenseBoundary | null,
- unmountChildren?: UnmountChildrenFn
- ): void
- insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void
- remove(el: HostNode): void
- createElement(
- type: string,
- isSVG?: boolean,
- isCustomizedBuiltIn?: string
- ): HostElement
- createText(text: string): HostNode
- createComment(text: string): HostNode
- setText(node: HostNode, text: string): void
- setElementText(node: HostElement, text: string): void
- parentNode(node: HostNode): HostElement | null
- nextSibling(node: HostNode): HostNode | null
- querySelector?(selector: string): HostElement | null
- setScopeId?(el: HostElement, id: string): void
- cloneNode?(node: HostNode): HostNode
- insertStaticContent?(
- content: string,
- parent: HostElement,
- anchor: HostNode | null,
- isSVG: boolean
- ): HostElement
- }
- // Renderer Node can technically be any object in the context of core renderer
- // logic - they are never directly operated on and always passed to the node op
- // functions provided via options, so the internal constraint is really just
- // a generic object.
- export interface RendererNode {
- [key: string]: any
- }
- export interface RendererElement extends RendererNode {}
- // An object exposing the internals of a renderer, passed to tree-shakeable
- // features so that they can be decoupled from this file. Keys are shortened
- // to optimize bundle size.
- export interface RendererInternals<
- HostNode = RendererNode,
- HostElement = RendererElement
- > {
- p: PatchFn
- um: UnmountFn
- r: RemoveFn
- m: MoveFn
- mt: MountComponentFn
- mc: MountChildrenFn
- pc: PatchChildrenFn
- pbc: PatchBlockChildrenFn
- n: NextFn
- o: RendererOptions<HostNode, HostElement>
- }
- // These functions are created inside a closure and therefore their types cannot
- // be directly exported. In order to avoid maintaining function signatures in
- // two places, we declare them once here and use them inside the closure.
- type PatchFn = (
- n1: VNode | null, // null means this is a mount
- n2: VNode,
- container: RendererElement,
- anchor?: RendererNode | null,
- parentComponent?: ComponentInternalInstance | null,
- parentSuspense?: SuspenseBoundary | null,
- isSVG?: boolean,
- optimized?: boolean
- ) => void
- type MountChildrenFn = (
- children: VNodeArrayChildren,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean,
- start?: number
- ) => void
- type PatchChildrenFn = (
- n1: VNode | null,
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean,
- optimized?: boolean
- ) => void
- type PatchBlockChildrenFn = (
- oldChildren: VNode[],
- newChildren: VNode[],
- fallbackContainer: RendererElement,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean
- ) => void
- type MoveFn = (
- vnode: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- type: MoveType,
- parentSuspense?: SuspenseBoundary | null
- ) => void
- type NextFn = (vnode: VNode) => RendererNode | null
- type UnmountFn = (
- vnode: VNode,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- doRemove?: boolean
- ) => void
- type RemoveFn = (vnode: VNode) => void
- type UnmountChildrenFn = (
- children: VNode[],
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- doRemove?: boolean,
- start?: number
- ) => void
- export type MountComponentFn = (
- initialVNode: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean
- ) => void
- type ProcessTextOrCommentFn = (
- n1: VNode | null,
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null
- ) => void
- export type SetupRenderEffectFn = (
- instance: ComponentInternalInstance,
- initialVNode: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean
- ) => void
- export const enum MoveType {
- ENTER,
- LEAVE,
- REORDER
- }
- const prodEffectOptions = {
- scheduler: queueJob
- }
- function createDevEffectOptions(
- instance: ComponentInternalInstance
- ): ReactiveEffectOptions {
- return {
- scheduler: queueJob,
- onTrack: instance.rtc ? e => invokeArrayFns(instance.rtc!, e) : void 0,
- onTrigger: instance.rtg ? e => invokeArrayFns(instance.rtg!, e) : void 0
- }
- }
- export const queuePostRenderEffect = __FEATURE_SUSPENSE__
- ? queueEffectWithSuspense
- : queuePostFlushCb
- /**
- * The createRenderer function accepts two generic arguments:
- * HostNode and HostElement, corresponding to Node and Element types in the
- * host environment. For example, for runtime-dom, HostNode would be the DOM
- * `Node` interface and HostElement would be the DOM `Element` interface.
- *
- * Custom renderers can pass in the platform specific types like this:
- *
- * ``` js
- * const { render, createApp } = createRenderer<Node, Element>({
- * patchProp,
- * ...nodeOps
- * })
- * ```
- */
- export function createRenderer<
- HostNode = RendererNode,
- HostElement = RendererElement
- >(options: RendererOptions<HostNode, HostElement>) {
- return baseCreateRenderer<HostNode, HostElement>(options)
- }
- // Separate API for creating hydration-enabled renderer.
- // Hydration logic is only used when calling this function, making it
- // tree-shakable.
- export function createHydrationRenderer(
- options: RendererOptions<Node, Element>
- ) {
- return baseCreateRenderer(options, createHydrationFunctions)
- }
- // overload 1: no hydration
- function baseCreateRenderer<
- HostNode = RendererNode,
- HostElement = RendererElement
- >(options: RendererOptions<HostNode, HostElement>): Renderer<HostElement>
- // overload 2: with hydration
- function baseCreateRenderer(
- options: RendererOptions<Node, Element>,
- createHydrationFns: typeof createHydrationFunctions
- ): HydrationRenderer
- // implementation
- function baseCreateRenderer(
- options: RendererOptions,
- createHydrationFns?: typeof createHydrationFunctions
- ): any {
- const {
- insert: hostInsert,
- remove: hostRemove,
- patchProp: hostPatchProp,
- createElement: hostCreateElement,
- createText: hostCreateText,
- createComment: hostCreateComment,
- setText: hostSetText,
- setElementText: hostSetElementText,
- parentNode: hostParentNode,
- nextSibling: hostNextSibling,
- setScopeId: hostSetScopeId = NOOP,
- cloneNode: hostCloneNode,
- insertStaticContent: hostInsertStaticContent
- } = options
- // Note: functions inside this closure should use `const xxx = () => {}`
- // style in order to prevent being inlined by minifiers.
- const patch: PatchFn = (
- n1,
- n2,
- container,
- anchor = null,
- parentComponent = null,
- parentSuspense = null,
- isSVG = false,
- optimized = false
- ) => {
- // patching & not same type, unmount old tree
- if (n1 && !isSameVNodeType(n1, n2)) {
- anchor = getNextHostNode(n1)
- unmount(n1, parentComponent, parentSuspense, true)
- n1 = null
- }
- const { type, ref, shapeFlag } = n2
- switch (type) {
- case Text:
- processText(n1, n2, container, anchor)
- break
- case Comment:
- processCommentNode(n1, n2, container, anchor)
- break
- case Static:
- if (n1 == null) {
- mountStaticNode(n2, container, anchor, isSVG)
- } // static nodes are noop on patch
- break
- case Fragment:
- processFragment(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- break
- default:
- if (shapeFlag & ShapeFlags.ELEMENT) {
- processElement(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else if (shapeFlag & ShapeFlags.COMPONENT) {
- processComponent(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else if (shapeFlag & ShapeFlags.TELEPORT) {
- ;(type as typeof TeleportImpl).process(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized,
- internals
- )
- } else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
- ;(type as typeof SuspenseImpl).process(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized,
- internals
- )
- } else if (__DEV__) {
- warn('Invalid VNode type:', type, `(${typeof type})`)
- }
- }
- // set ref
- if (ref != null && parentComponent) {
- const refValue =
- shapeFlag & ShapeFlags.STATEFUL_COMPONENT ? n2.component!.proxy : n2.el
- setRef(ref, n1 && n1.ref, parentComponent, refValue)
- }
- }
- const processText: ProcessTextOrCommentFn = (n1, n2, container, anchor) => {
- if (n1 == null) {
- hostInsert(
- (n2.el = hostCreateText(n2.children as string)),
- container,
- anchor
- )
- } else {
- const el = (n2.el = n1.el!)
- if (n2.children !== n1.children) {
- hostSetText(el, n2.children as string)
- }
- }
- }
- const processCommentNode: ProcessTextOrCommentFn = (
- n1,
- n2,
- container,
- anchor
- ) => {
- if (n1 == null) {
- hostInsert(
- (n2.el = hostCreateComment((n2.children as string) || '')),
- container,
- anchor
- )
- } else {
- // there's no support for dynamic comments
- n2.el = n1.el
- }
- }
- const mountStaticNode = (
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- isSVG: boolean
- ) => {
- if (n2.el && hostCloneNode !== undefined) {
- hostInsert(hostCloneNode(n2.el), container, anchor)
- } else {
- // static nodes are only present when used with compiler-dom/runtime-dom
- // which guarantees presence of hostInsertStaticContent.
- n2.el = hostInsertStaticContent!(
- n2.children as string,
- container,
- anchor,
- isSVG
- )
- }
- }
- const processElement = (
- n1: VNode | null,
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) => {
- isSVG = isSVG || (n2.type as string) === 'svg'
- if (n1 == null) {
- mountElement(
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else {
- patchElement(n1, n2, parentComponent, parentSuspense, isSVG, optimized)
- }
- }
- const mountElement = (
- vnode: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) => {
- let el: RendererElement
- let vnodeHook: VNodeHook | undefined | null
- const {
- type,
- props,
- shapeFlag,
- transition,
- scopeId,
- patchFlag,
- dirs
- } = vnode
- if (
- vnode.el &&
- hostCloneNode !== undefined &&
- patchFlag === PatchFlags.HOISTED
- ) {
- // If a vnode has non-null el, it means it's being reused.
- // Only static vnodes can be reused, so its mounted DOM nodes should be
- // exactly the same, and we can simply do a clone here.
- el = vnode.el = hostCloneNode(vnode.el)
- } else {
- el = vnode.el = hostCreateElement(
- vnode.type as string,
- isSVG,
- props && props.is
- )
- // props
- if (props) {
- for (const key in props) {
- if (!isReservedProp(key)) {
- hostPatchProp(el, key, null, props[key], isSVG)
- }
- }
- if ((vnodeHook = props.onVnodeBeforeMount)) {
- invokeVNodeHook(vnodeHook, parentComponent, vnode)
- }
- }
- if (dirs) {
- invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount')
- }
- // scopeId
- if (scopeId) {
- hostSetScopeId(el, scopeId)
- }
- const treeOwnerId = parentComponent && parentComponent.type.__scopeId
- // vnode's own scopeId and the current patched component's scopeId is
- // different - this is a slot content node.
- if (treeOwnerId && treeOwnerId !== scopeId) {
- hostSetScopeId(el, treeOwnerId + '-s')
- }
- // children
- if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
- hostSetElementText(el, vnode.children as string)
- } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- mountChildren(
- vnode.children as VNodeArrayChildren,
- el,
- null,
- parentComponent,
- parentSuspense,
- isSVG && type !== 'foreignObject',
- optimized || !!vnode.dynamicChildren
- )
- }
- if (transition && !transition.persisted) {
- transition.beforeEnter(el)
- }
- }
- hostInsert(el, container, anchor)
- if (
- (vnodeHook = props && props.onVnodeMounted) ||
- (transition && !transition.persisted) ||
- dirs
- ) {
- queuePostRenderEffect(() => {
- vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode)
- transition && !transition.persisted && transition.enter(el)
- dirs && invokeDirectiveHook(vnode, null, parentComponent, 'mounted')
- }, parentSuspense)
- }
- }
- const mountChildren: MountChildrenFn = (
- children,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized,
- start = 0
- ) => {
- for (let i = start; i < children.length; i++) {
- const child = (children[i] = optimized
- ? cloneIfMounted(children[i] as VNode)
- : normalizeVNode(children[i]))
- patch(
- null,
- child,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- }
- }
- const patchElement = (
- n1: VNode,
- n2: VNode,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) => {
- const el = (n2.el = n1.el!)
- let { patchFlag, dynamicChildren, dirs } = n2
- const oldProps = (n1 && n1.props) || EMPTY_OBJ
- const newProps = n2.props || EMPTY_OBJ
- let vnodeHook: VNodeHook | undefined | null
- if ((vnodeHook = newProps.onVnodeBeforeUpdate)) {
- invokeVNodeHook(vnodeHook, parentComponent, n2, n1)
- }
- if (dirs) {
- invokeDirectiveHook(n2, n1, parentComponent, 'beforeUpdate')
- }
- if (__HMR__ && parentComponent && parentComponent.renderUpdated) {
- // HMR updated, force full diff
- patchFlag = 0
- optimized = false
- dynamicChildren = null
- }
- if (patchFlag > 0) {
- // the presence of a patchFlag means this element's render code was
- // generated by the compiler and can take the fast path.
- // in this path old node and new node are guaranteed to have the same shape
- // (i.e. at the exact same position in the source template)
- if (patchFlag & PatchFlags.FULL_PROPS) {
- // element props contain dynamic keys, full diff needed
- patchProps(
- el,
- n2,
- oldProps,
- newProps,
- parentComponent,
- parentSuspense,
- isSVG
- )
- } else {
- // class
- // this flag is matched when the element has dynamic class bindings.
- if (patchFlag & PatchFlags.CLASS) {
- if (oldProps.class !== newProps.class) {
- hostPatchProp(el, 'class', null, newProps.class, isSVG)
- }
- }
- // style
- // this flag is matched when the element has dynamic style bindings
- if (patchFlag & PatchFlags.STYLE) {
- hostPatchProp(el, 'style', oldProps.style, newProps.style, isSVG)
- }
- // props
- // This flag is matched when the element has dynamic prop/attr bindings
- // other than class and style. The keys of dynamic prop/attrs are saved for
- // faster iteration.
- // Note dynamic keys like :[foo]="bar" will cause this optimization to
- // bail out and go through a full diff because we need to unset the old key
- if (patchFlag & PatchFlags.PROPS) {
- // if the flag is present then dynamicProps must be non-null
- const propsToUpdate = n2.dynamicProps!
- for (let i = 0; i < propsToUpdate.length; i++) {
- const key = propsToUpdate[i]
- const prev = oldProps[key]
- const next = newProps[key]
- if (prev !== next) {
- hostPatchProp(
- el,
- key,
- prev,
- next,
- isSVG,
- n1.children as VNode[],
- parentComponent,
- parentSuspense,
- unmountChildren
- )
- }
- }
- }
- }
- // text
- // This flag is matched when the element has only dynamic text children.
- if (patchFlag & PatchFlags.TEXT) {
- if (n1.children !== n2.children) {
- hostSetElementText(el, n2.children as string)
- }
- }
- } else if (!optimized && dynamicChildren == null) {
- // unoptimized, full diff
- patchProps(
- el,
- n2,
- oldProps,
- newProps,
- parentComponent,
- parentSuspense,
- isSVG
- )
- }
- const areChildrenSVG = isSVG && n2.type !== 'foreignObject'
- if (dynamicChildren) {
- patchBlockChildren(
- n1.dynamicChildren!,
- dynamicChildren,
- el,
- parentComponent,
- parentSuspense,
- areChildrenSVG
- )
- } else if (!optimized) {
- // full diff
- patchChildren(
- n1,
- n2,
- el,
- null,
- parentComponent,
- parentSuspense,
- areChildrenSVG
- )
- }
- if ((vnodeHook = newProps.onVnodeUpdated) || dirs) {
- queuePostRenderEffect(() => {
- vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, n2, n1)
- dirs && invokeDirectiveHook(n2, n1, parentComponent, 'updated')
- }, parentSuspense)
- }
- }
- // The fast path for blocks.
- const patchBlockChildren: PatchBlockChildrenFn = (
- oldChildren,
- newChildren,
- fallbackContainer,
- parentComponent,
- parentSuspense,
- isSVG
- ) => {
- for (let i = 0; i < newChildren.length; i++) {
- const oldVNode = oldChildren[i]
- const newVNode = newChildren[i]
- // Determine the container (parent element) for the patch.
- const container =
- // - In the case of a Fragment, we need to provide the actual parent
- // of the Fragment itself so it can move its children.
- oldVNode.type === Fragment ||
- // - In the case of different nodes, there is going to be a replacement
- // which also requires the correct parent container
- !isSameVNodeType(oldVNode, newVNode) ||
- // - In the case of a component, it could contain anything.
- oldVNode.shapeFlag & ShapeFlags.COMPONENT
- ? hostParentNode(oldVNode.el!)!
- : // In other cases, the parent container is not actually used so we
- // just pass the block element here to avoid a DOM parentNode call.
- fallbackContainer
- patch(
- oldVNode,
- newVNode,
- container,
- null,
- parentComponent,
- parentSuspense,
- isSVG,
- true
- )
- }
- }
- const patchProps = (
- el: RendererElement,
- vnode: VNode,
- oldProps: Data,
- newProps: Data,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean
- ) => {
- if (oldProps !== newProps) {
- for (const key in newProps) {
- if (isReservedProp(key)) continue
- const next = newProps[key]
- const prev = oldProps[key]
- if (next !== prev) {
- hostPatchProp(
- el,
- key,
- prev,
- next,
- isSVG,
- vnode.children as VNode[],
- parentComponent,
- parentSuspense,
- unmountChildren
- )
- }
- }
- if (oldProps !== EMPTY_OBJ) {
- for (const key in oldProps) {
- if (!isReservedProp(key) && !(key in newProps)) {
- hostPatchProp(
- el,
- key,
- oldProps[key],
- null,
- isSVG,
- vnode.children as VNode[],
- parentComponent,
- parentSuspense,
- unmountChildren
- )
- }
- }
- }
- }
- }
- const processFragment = (
- n1: VNode | null,
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) => {
- const fragmentStartAnchor = (n2.el = n1 ? n1.el : hostCreateText(''))!
- const fragmentEndAnchor = (n2.anchor = n1 ? n1.anchor : hostCreateText(''))!
- let { patchFlag, dynamicChildren } = n2
- if (patchFlag > 0) {
- optimized = true
- }
- if (__HMR__ && parentComponent && parentComponent.renderUpdated) {
- // HMR updated, force full diff
- patchFlag = 0
- optimized = false
- dynamicChildren = null
- }
- if (n1 == null) {
- hostInsert(fragmentStartAnchor, container, anchor)
- hostInsert(fragmentEndAnchor, container, anchor)
- // a fragment can only have array children
- // since they are either generated by the compiler, or implicitly created
- // from arrays.
- mountChildren(
- n2.children as VNodeArrayChildren,
- container,
- fragmentEndAnchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else {
- if (patchFlag & PatchFlags.STABLE_FRAGMENT && dynamicChildren) {
- // a stable fragment (template root or <template v-for>) doesn't need to
- // patch children order, but it may contain dynamicChildren.
- patchBlockChildren(
- n1.dynamicChildren!,
- dynamicChildren,
- container,
- parentComponent,
- parentSuspense,
- isSVG
- )
- } else {
- // keyed / unkeyed, or manual fragments.
- // for keyed & unkeyed, since they are compiler generated from v-for,
- // each child is guaranteed to be a block so the fragment will never
- // have dynamicChildren.
- patchChildren(
- n1,
- n2,
- container,
- fragmentEndAnchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- }
- }
- }
- const processComponent = (
- n1: VNode | null,
- n2: VNode,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) => {
- if (n1 == null) {
- if (n2.shapeFlag & ShapeFlags.COMPONENT_KEPT_ALIVE) {
- ;(parentComponent!.sink as KeepAliveSink).activate(
- n2,
- container,
- anchor,
- isSVG,
- optimized
- )
- } else {
- mountComponent(
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG
- )
- }
- } else {
- const instance = (n2.component = n1.component)!
- if (shouldUpdateComponent(n1, n2, parentComponent, optimized)) {
- if (
- __FEATURE_SUSPENSE__ &&
- instance.asyncDep &&
- !instance.asyncResolved
- ) {
- // async & still pending - just update props and slots
- // since the component's reactive effect for render isn't set-up yet
- if (__DEV__) {
- pushWarningContext(n2)
- }
- updateComponentPreRender(instance, n2)
- if (__DEV__) {
- popWarningContext()
- }
- return
- } else {
- // normal update
- instance.next = n2
- // in case the child component is also queued, remove it to avoid
- // double updating the same child component in the same flush.
- invalidateJob(instance.update)
- // instance.update is the reactive effect runner.
- instance.update()
- }
- } else {
- // no update needed. just copy over properties
- n2.component = n1.component
- n2.el = n1.el
- }
- }
- }
- const mountComponent: MountComponentFn = (
- initialVNode,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG
- ) => {
- const instance: ComponentInternalInstance = (initialVNode.component = createComponentInstance(
- initialVNode,
- parentComponent,
- parentSuspense
- ))
- if (__HMR__ && instance.type.__hmrId) {
- registerHMR(instance)
- }
- if (__DEV__) {
- pushWarningContext(initialVNode)
- startMeasure(instance, `mount`)
- }
- // inject renderer internals for keepAlive
- if (isKeepAlive(initialVNode)) {
- const sink = instance.sink as KeepAliveSink
- sink.renderer = internals
- sink.parentSuspense = parentSuspense
- }
- // resolve props and slots for setup context
- if (__DEV__) {
- startMeasure(instance, `init`)
- }
- setupComponent(instance, parentSuspense)
- if (__DEV__) {
- endMeasure(instance, `init`)
- }
- // setup() is async. This component relies on async logic to be resolved
- // before proceeding
- if (__FEATURE_SUSPENSE__ && instance.asyncDep) {
- if (!parentSuspense) {
- if (__DEV__) warn('async setup() is used without a suspense boundary!')
- return
- }
- parentSuspense.registerDep(instance, setupRenderEffect)
- // Give it a placeholder if this is not hydration
- if (!initialVNode.el) {
- const placeholder = (instance.subTree = createVNode(Comment))
- processCommentNode(null, placeholder, container!, anchor)
- }
- return
- }
- setupRenderEffect(
- instance,
- initialVNode,
- container,
- anchor,
- parentSuspense,
- isSVG
- )
- if (__DEV__) {
- popWarningContext()
- endMeasure(instance, `mount`)
- }
- }
- const setupRenderEffect: SetupRenderEffectFn = (
- instance,
- initialVNode,
- container,
- anchor,
- parentSuspense,
- isSVG
- ) => {
- // create reactive effect for rendering
- instance.update = effect(function componentEffect() {
- if (!instance.isMounted) {
- let vnodeHook: VNodeHook | null | undefined
- const { el, props } = initialVNode
- const { bm, m, a, parent } = instance
- if (__DEV__) {
- startMeasure(instance, `render`)
- }
- const subTree = (instance.subTree = renderComponentRoot(instance))
- if (__DEV__) {
- endMeasure(instance, `render`)
- }
- // beforeMount hook
- if (bm) {
- invokeArrayFns(bm)
- }
- // onVnodeBeforeMount
- if ((vnodeHook = props && props.onVnodeBeforeMount)) {
- invokeVNodeHook(vnodeHook, parent, initialVNode)
- }
- if (el && hydrateNode) {
- if (__DEV__) {
- startMeasure(instance, `hydrate`)
- }
- // vnode has adopted host node - perform hydration instead of mount.
- hydrateNode(
- initialVNode.el as Node,
- subTree,
- instance,
- parentSuspense
- )
- if (__DEV__) {
- endMeasure(instance, `hydrate`)
- }
- } else {
- if (__DEV__) {
- startMeasure(instance, `patch`)
- }
- patch(
- null,
- subTree,
- container,
- anchor,
- instance,
- parentSuspense,
- isSVG
- )
- if (__DEV__) {
- endMeasure(instance, `patch`)
- }
- initialVNode.el = subTree.el
- }
- // mounted hook
- if (m) {
- queuePostRenderEffect(m, parentSuspense)
- }
- // onVnodeMounted
- if ((vnodeHook = props && props.onVnodeMounted)) {
- queuePostRenderEffect(() => {
- invokeVNodeHook(vnodeHook!, parent, initialVNode)
- }, parentSuspense)
- }
- // activated hook for keep-alive roots.
- if (
- a &&
- initialVNode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
- ) {
- queuePostRenderEffect(a, parentSuspense)
- }
- instance.isMounted = true
- } else {
- // updateComponent
- // This is triggered by mutation of component's own state (next: null)
- // OR parent calling processComponent (next: VNode)
- let { next, bu, u, parent, vnode } = instance
- let vnodeHook: VNodeHook | null | undefined
- if (__DEV__) {
- pushWarningContext(next || instance.vnode)
- }
- if (next) {
- updateComponentPreRender(instance, next)
- } else {
- next = vnode
- }
- if (__DEV__) {
- startMeasure(instance, `render`)
- }
- const nextTree = renderComponentRoot(instance)
- if (__DEV__) {
- endMeasure(instance, `render`)
- }
- const prevTree = instance.subTree
- instance.subTree = nextTree
- next.el = vnode.el
- // beforeUpdate hook
- if (bu) {
- invokeArrayFns(bu)
- }
- // onVnodeBeforeUpdate
- if ((vnodeHook = next.props && next.props.onVnodeBeforeUpdate)) {
- invokeVNodeHook(vnodeHook, parent, next, vnode)
- }
- // reset refs
- // only needed if previous patch had refs
- if (instance.refs !== EMPTY_OBJ) {
- instance.refs = {}
- }
- if (__DEV__) {
- startMeasure(instance, `patch`)
- }
- patch(
- prevTree,
- nextTree,
- // parent may have changed if it's in a teleport
- hostParentNode(prevTree.el!)!,
- // anchor may have changed if it's in a fragment
- getNextHostNode(prevTree),
- instance,
- parentSuspense,
- isSVG
- )
- if (__DEV__) {
- endMeasure(instance, `patch`)
- }
- next.el = nextTree.el
- if (next === null) {
- // self-triggered update. In case of HOC, update parent component
- // vnode el. HOC is indicated by parent instance's subTree pointing
- // to child component's vnode
- updateHOCHostEl(instance, nextTree.el)
- }
- // updated hook
- if (u) {
- queuePostRenderEffect(u, parentSuspense)
- }
- // onVnodeUpdated
- if ((vnodeHook = next.props && next.props.onVnodeUpdated)) {
- queuePostRenderEffect(() => {
- invokeVNodeHook(vnodeHook!, parent, next!, vnode)
- }, parentSuspense)
- }
- if (__DEV__) {
- popWarningContext()
- }
- }
- }, __DEV__ ? createDevEffectOptions(instance) : prodEffectOptions)
- }
- const updateComponentPreRender = (
- instance: ComponentInternalInstance,
- nextVNode: VNode
- ) => {
- nextVNode.component = instance
- instance.vnode = nextVNode
- instance.next = null
- resolveProps(instance, nextVNode.props)
- resolveSlots(instance, nextVNode.children)
- }
- const patchChildren: PatchChildrenFn = (
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized = false
- ) => {
- const c1 = n1 && n1.children
- const prevShapeFlag = n1 ? n1.shapeFlag : 0
- const c2 = n2.children
- const { patchFlag, shapeFlag } = n2
- if (patchFlag === PatchFlags.BAIL) {
- optimized = false
- }
- // fast path
- if (patchFlag > 0) {
- if (patchFlag & PatchFlags.KEYED_FRAGMENT) {
- // this could be either fully-keyed or mixed (some keyed some not)
- // presence of patchFlag means children are guaranteed to be arrays
- patchKeyedChildren(
- c1 as VNode[],
- c2 as VNodeArrayChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- return
- } else if (patchFlag & PatchFlags.UNKEYED_FRAGMENT) {
- // unkeyed
- patchUnkeyedChildren(
- c1 as VNode[],
- c2 as VNodeArrayChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- return
- }
- }
- // children has 3 possibilities: text, array or no children.
- if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
- // text children fast path
- if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- unmountChildren(c1 as VNode[], parentComponent, parentSuspense)
- }
- if (c2 !== c1) {
- hostSetElementText(container, c2 as string)
- }
- } else {
- if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- // prev children was array
- if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- // two arrays, cannot assume anything, do full diff
- patchKeyedChildren(
- c1 as VNode[],
- c2 as VNodeArrayChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else {
- // no new children, just unmount old
- unmountChildren(c1 as VNode[], parentComponent, parentSuspense, true)
- }
- } else {
- // prev children was text OR null
- // new children is array OR null
- if (prevShapeFlag & ShapeFlags.TEXT_CHILDREN) {
- hostSetElementText(container, '')
- }
- // mount new if array
- if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- mountChildren(
- c2 as VNodeArrayChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- }
- }
- }
- }
- const patchUnkeyedChildren = (
- c1: VNode[],
- c2: VNodeArrayChildren,
- container: RendererElement,
- anchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) => {
- c1 = c1 || EMPTY_ARR
- c2 = c2 || EMPTY_ARR
- const oldLength = c1.length
- const newLength = c2.length
- const commonLength = Math.min(oldLength, newLength)
- let i
- for (i = 0; i < commonLength; i++) {
- const nextChild = (c2[i] = optimized
- ? cloneIfMounted(c2[i] as VNode)
- : normalizeVNode(c2[i]))
- patch(
- c1[i],
- nextChild,
- container,
- null,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- }
- if (oldLength > newLength) {
- // remove old
- unmountChildren(c1, parentComponent, parentSuspense, true, commonLength)
- } else {
- // mount new
- mountChildren(
- c2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized,
- commonLength
- )
- }
- }
- // can be all-keyed or mixed
- const patchKeyedChildren = (
- c1: VNode[],
- c2: VNodeArrayChildren,
- container: RendererElement,
- parentAnchor: RendererNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) => {
- let i = 0
- const l2 = c2.length
- let e1 = c1.length - 1 // prev ending index
- let e2 = l2 - 1 // next ending index
- // 1. sync from start
- // (a b) c
- // (a b) d e
- while (i <= e1 && i <= e2) {
- const n1 = c1[i]
- const n2 = (c2[i] = optimized
- ? cloneIfMounted(c2[i] as VNode)
- : normalizeVNode(c2[i]))
- if (isSameVNodeType(n1, n2)) {
- patch(
- n1,
- n2,
- container,
- parentAnchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else {
- break
- }
- i++
- }
- // 2. sync from end
- // a (b c)
- // d e (b c)
- while (i <= e1 && i <= e2) {
- const n1 = c1[e1]
- const n2 = (c2[e2] = optimized
- ? cloneIfMounted(c2[e2] as VNode)
- : normalizeVNode(c2[e2]))
- if (isSameVNodeType(n1, n2)) {
- patch(
- n1,
- n2,
- container,
- parentAnchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else {
- break
- }
- e1--
- e2--
- }
- // 3. common sequence + mount
- // (a b)
- // (a b) c
- // i = 2, e1 = 1, e2 = 2
- // (a b)
- // c (a b)
- // i = 0, e1 = -1, e2 = 0
- if (i > e1) {
- if (i <= e2) {
- const nextPos = e2 + 1
- const anchor = nextPos < l2 ? (c2[nextPos] as VNode).el : parentAnchor
- while (i <= e2) {
- patch(
- null,
- (c2[i] = optimized
- ? cloneIfMounted(c2[i] as VNode)
- : normalizeVNode(c2[i])),
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG
- )
- i++
- }
- }
- }
- // 4. common sequence + unmount
- // (a b) c
- // (a b)
- // i = 2, e1 = 2, e2 = 1
- // a (b c)
- // (b c)
- // i = 0, e1 = 0, e2 = -1
- else if (i > e2) {
- while (i <= e1) {
- unmount(c1[i], parentComponent, parentSuspense, true)
- i++
- }
- }
- // 5. unknown sequence
- // [i ... e1 + 1]: a b [c d e] f g
- // [i ... e2 + 1]: a b [e d c h] f g
- // i = 2, e1 = 4, e2 = 5
- else {
- const s1 = i // prev starting index
- const s2 = i // next starting index
- // 5.1 build key:index map for newChildren
- const keyToNewIndexMap: Map<string | number, number> = new Map()
- for (i = s2; i <= e2; i++) {
- const nextChild = (c2[i] = optimized
- ? cloneIfMounted(c2[i] as VNode)
- : normalizeVNode(c2[i]))
- if (nextChild.key != null) {
- if (__DEV__ && keyToNewIndexMap.has(nextChild.key)) {
- warn(
- `Duplicate keys found during update:`,
- JSON.stringify(nextChild.key),
- `Make sure keys are unique.`
- )
- }
- keyToNewIndexMap.set(nextChild.key, i)
- }
- }
- // 5.2 loop through old children left to be patched and try to patch
- // matching nodes & remove nodes that are no longer present
- let j
- let patched = 0
- const toBePatched = e2 - s2 + 1
- let moved = false
- // used to track whether any node has moved
- let maxNewIndexSoFar = 0
- // works as Map<newIndex, oldIndex>
- // Note that oldIndex is offset by +1
- // and oldIndex = 0 is a special value indicating the new node has
- // no corresponding old node.
- // used for determining longest stable subsequence
- const newIndexToOldIndexMap = new Array(toBePatched)
- for (i = 0; i < toBePatched; i++) newIndexToOldIndexMap[i] = 0
- for (i = s1; i <= e1; i++) {
- const prevChild = c1[i]
- if (patched >= toBePatched) {
- // all new children have been patched so this can only be a removal
- unmount(prevChild, parentComponent, parentSuspense, true)
- continue
- }
- let newIndex
- if (prevChild.key != null) {
- newIndex = keyToNewIndexMap.get(prevChild.key)
- } else {
- // key-less node, try to locate a key-less node of the same type
- for (j = s2; j <= e2; j++) {
- if (
- newIndexToOldIndexMap[j - s2] === 0 &&
- isSameVNodeType(prevChild, c2[j] as VNode)
- ) {
- newIndex = j
- break
- }
- }
- }
- if (newIndex === undefined) {
- unmount(prevChild, parentComponent, parentSuspense, true)
- } else {
- newIndexToOldIndexMap[newIndex - s2] = i + 1
- if (newIndex >= maxNewIndexSoFar) {
- maxNewIndexSoFar = newIndex
- } else {
- moved = true
- }
- patch(
- prevChild,
- c2[newIndex] as VNode,
- container,
- null,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- patched++
- }
- }
- // 5.3 move and mount
- // generate longest stable subsequence only when nodes have moved
- const increasingNewIndexSequence = moved
- ? getSequence(newIndexToOldIndexMap)
- : EMPTY_ARR
- j = increasingNewIndexSequence.length - 1
- // looping backwards so that we can use last patched node as anchor
- for (i = toBePatched - 1; i >= 0; i--) {
- const nextIndex = s2 + i
- const nextChild = c2[nextIndex] as VNode
- const anchor =
- nextIndex + 1 < l2 ? (c2[nextIndex + 1] as VNode).el : parentAnchor
- if (newIndexToOldIndexMap[i] === 0) {
- // mount new
- patch(
- null,
- nextChild,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG
- )
- } else if (moved) {
- // move if:
- // There is no stable subsequence (e.g. a reverse)
- // OR current node is not among the stable sequence
- if (j < 0 || i !== increasingNewIndexSequence[j]) {
- move(nextChild, container, anchor, MoveType.REORDER)
- } else {
- j--
- }
- }
- }
- }
- }
- const move: MoveFn = (
- vnode,
- container,
- anchor,
- moveType,
- parentSuspense = null
- ) => {
- const { el, type, transition, children, shapeFlag } = vnode
- if (shapeFlag & ShapeFlags.COMPONENT) {
- move(vnode.component!.subTree, container, anchor, moveType)
- return
- }
- if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
- vnode.suspense!.move(container, anchor, moveType)
- return
- }
- if (shapeFlag & ShapeFlags.TELEPORT) {
- ;(type as typeof TeleportImpl).move(vnode, container, anchor, internals)
- return
- }
- if (type === Fragment) {
- hostInsert(el!, container, anchor)
- for (let i = 0; i < (children as VNode[]).length; i++) {
- move((children as VNode[])[i], container, anchor, moveType)
- }
- hostInsert(vnode.anchor!, container, anchor)
- return
- }
- // single nodes
- const needTransition =
- moveType !== MoveType.REORDER &&
- shapeFlag & ShapeFlags.ELEMENT &&
- transition
- if (needTransition) {
- if (moveType === MoveType.ENTER) {
- transition!.beforeEnter(el!)
- hostInsert(el!, container, anchor)
- queuePostRenderEffect(() => transition!.enter(el!), parentSuspense)
- } else {
- const { leave, delayLeave, afterLeave } = transition!
- const remove = () => hostInsert(el!, container, anchor)
- const performLeave = () => {
- leave(el!, () => {
- remove()
- afterLeave && afterLeave()
- })
- }
- if (delayLeave) {
- delayLeave(el!, remove, performLeave)
- } else {
- performLeave()
- }
- }
- } else {
- hostInsert(el!, container, anchor)
- }
- }
- const unmount: UnmountFn = (
- vnode,
- parentComponent,
- parentSuspense,
- doRemove = false
- ) => {
- const { props, ref, children, dynamicChildren, shapeFlag, dirs } = vnode
- const shouldInvokeDirs = shapeFlag & ShapeFlags.ELEMENT && dirs
- let vnodeHook: VNodeHook | undefined | null
- // unset ref
- if (ref != null && parentComponent) {
- setRef(ref, null, parentComponent, null)
- }
- if ((vnodeHook = props && props.onVnodeBeforeUnmount)) {
- invokeVNodeHook(vnodeHook, parentComponent, vnode)
- }
- if (shapeFlag & ShapeFlags.COMPONENT) {
- if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
- ;(parentComponent!.sink as KeepAliveSink).deactivate(vnode)
- } else {
- unmountComponent(vnode.component!, parentSuspense, doRemove)
- }
- } else {
- if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
- vnode.suspense!.unmount(parentSuspense, doRemove)
- return
- }
- if (shouldInvokeDirs) {
- invokeDirectiveHook(vnode, null, parentComponent, 'beforeUnmount')
- }
- if (dynamicChildren) {
- // fast path for block nodes: only need to unmount dynamic children.
- unmountChildren(dynamicChildren, parentComponent, parentSuspense)
- } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- unmountChildren(children as VNode[], parentComponent, parentSuspense)
- }
- // an unmounted teleport should always remove its children
- if (shapeFlag & ShapeFlags.TELEPORT) {
- ;(vnode.type as typeof TeleportImpl).remove(vnode, internals)
- }
- if (doRemove) {
- remove(vnode)
- }
- }
- if ((vnodeHook = props && props.onVnodeUnmounted) || shouldInvokeDirs) {
- queuePostRenderEffect(() => {
- vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode)
- shouldInvokeDirs &&
- invokeDirectiveHook(vnode, null, parentComponent, 'unmounted')
- }, parentSuspense)
- }
- }
- const remove: RemoveFn = vnode => {
- const { type, el, anchor, transition } = vnode
- if (type === Fragment) {
- removeFragment(el!, anchor!)
- return
- }
- const performRemove = () => {
- hostRemove(el!)
- if (transition && !transition.persisted && transition.afterLeave) {
- transition.afterLeave()
- }
- }
- if (
- vnode.shapeFlag & ShapeFlags.ELEMENT &&
- transition &&
- !transition.persisted
- ) {
- const { leave, delayLeave } = transition
- const performLeave = () => leave(el!, performRemove)
- if (delayLeave) {
- delayLeave(vnode.el!, performRemove, performLeave)
- } else {
- performLeave()
- }
- } else {
- performRemove()
- }
- }
- const removeFragment = (cur: RendererNode, end: RendererNode) => {
- // For fragments, directly remove all contained DOM nodes.
- // (fragment child nodes cannot have transition)
- let next
- while (cur !== end) {
- next = hostNextSibling(cur)!
- hostRemove(cur)
- cur = next
- }
- hostRemove(end)
- }
- const unmountComponent = (
- instance: ComponentInternalInstance,
- parentSuspense: SuspenseBoundary | null,
- doRemove?: boolean
- ) => {
- if (__HMR__ && instance.type.__hmrId) {
- unregisterHMR(instance)
- }
- const { bum, effects, update, subTree, um, da, isDeactivated } = instance
- // beforeUnmount hook
- if (bum) {
- invokeArrayFns(bum)
- }
- if (effects) {
- for (let i = 0; i < effects.length; i++) {
- stop(effects[i])
- }
- }
- // update may be null if a component is unmounted before its async
- // setup has resolved.
- if (update) {
- stop(update)
- unmount(subTree, instance, parentSuspense, doRemove)
- }
- // unmounted hook
- if (um) {
- queuePostRenderEffect(um, parentSuspense)
- }
- // deactivated hook
- if (
- da &&
- !isDeactivated &&
- instance.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
- ) {
- queuePostRenderEffect(da, parentSuspense)
- }
- queuePostFlushCb(() => {
- instance.isUnmounted = true
- })
- // A component with async dep inside a pending suspense is unmounted before
- // its async dep resolves. This should remove the dep from the suspense, and
- // cause the suspense to resolve immediately if that was the last dep.
- if (
- __FEATURE_SUSPENSE__ &&
- parentSuspense &&
- !parentSuspense.isResolved &&
- !parentSuspense.isUnmounted &&
- instance.asyncDep &&
- !instance.asyncResolved
- ) {
- parentSuspense.deps--
- if (parentSuspense.deps === 0) {
- parentSuspense.resolve()
- }
- }
- }
- const unmountChildren: UnmountChildrenFn = (
- children,
- parentComponent,
- parentSuspense,
- doRemove = false,
- start = 0
- ) => {
- for (let i = start; i < children.length; i++) {
- unmount(children[i], parentComponent, parentSuspense, doRemove)
- }
- }
- const getNextHostNode: NextFn = vnode => {
- if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
- return getNextHostNode(vnode.component!.subTree)
- }
- if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
- return vnode.suspense!.next()
- }
- return hostNextSibling((vnode.anchor || vnode.el)!)
- }
- const setRef = (
- rawRef: VNodeNormalizedRef,
- oldRawRef: VNodeNormalizedRef | null,
- parent: ComponentInternalInstance,
- value: RendererNode | ComponentPublicInstance | null
- ) => {
- const [owner, ref] = rawRef
- if (__DEV__ && !owner) {
- warn(
- `Missing ref owner context. ref cannot be used on hoisted vnodes. ` +
- `A vnode with ref must be created inside the render function.`
- )
- return
- }
- const oldRef = oldRawRef && oldRawRef[1]
- const refs = owner.refs === EMPTY_OBJ ? (owner.refs = {}) : owner.refs
- const renderContext = owner.renderContext
- // unset old ref
- if (oldRef != null && oldRef !== ref) {
- if (isString(oldRef)) {
- refs[oldRef] = null
- if (hasOwn(renderContext, oldRef)) {
- renderContext[oldRef] = null
- }
- } else if (isRef(oldRef)) {
- oldRef.value = null
- }
- }
- if (isString(ref)) {
- refs[ref] = value
- if (hasOwn(renderContext, ref)) {
- renderContext[ref] = value
- }
- } else if (isRef(ref)) {
- ref.value = value
- } else if (isFunction(ref)) {
- callWithErrorHandling(ref, parent, ErrorCodes.FUNCTION_REF, [value, refs])
- } else if (__DEV__) {
- warn('Invalid template ref type:', value, `(${typeof value})`)
- }
- }
- const render: RootRenderFunction = (vnode, container) => {
- if (vnode == null) {
- if (container._vnode) {
- unmount(container._vnode, null, null, true)
- }
- } else {
- patch(container._vnode || null, vnode, container)
- }
- flushPostFlushCbs()
- container._vnode = vnode
- }
- const internals: RendererInternals = {
- p: patch,
- um: unmount,
- m: move,
- r: remove,
- mt: mountComponent,
- mc: mountChildren,
- pc: patchChildren,
- pbc: patchBlockChildren,
- n: getNextHostNode,
- o: options
- }
- let hydrate: ReturnType<typeof createHydrationFunctions>[0] | undefined
- let hydrateNode: ReturnType<typeof createHydrationFunctions>[1] | undefined
- if (createHydrationFns) {
- ;[hydrate, hydrateNode] = createHydrationFns(internals as RendererInternals<
- Node,
- Element
- >)
- }
- return {
- render,
- hydrate,
- createApp: createAppAPI(render, hydrate)
- }
- }
- export function invokeVNodeHook(
- hook: VNodeHook,
- instance: ComponentInternalInstance | null,
- vnode: VNode,
- prevVNode: VNode | null = null
- ) {
- callWithAsyncErrorHandling(hook, instance, ErrorCodes.VNODE_HOOK, [
- vnode,
- prevVNode
- ])
- }
- // https://en.wikipedia.org/wiki/Longest_increasing_subsequence
- function getSequence(arr: number[]): number[] {
- const p = arr.slice()
- const result = [0]
- let i, j, u, v, c
- const len = arr.length
- for (i = 0; i < len; i++) {
- const arrI = arr[i]
- if (arrI !== 0) {
- j = result[result.length - 1]
- if (arr[j] < arrI) {
- p[i] = j
- result.push(i)
- continue
- }
- u = 0
- v = result.length - 1
- while (u < v) {
- c = ((u + v) / 2) | 0
- if (arr[result[c]] < arrI) {
- u = c + 1
- } else {
- v = c
- }
- }
- if (arrI < arr[result[u]]) {
- if (u > 0) {
- p[i] = result[u - 1]
- }
- result[u] = i
- }
- }
- }
- u = result.length
- v = result[u - 1]
- while (u-- > 0) {
- result[u] = v
- v = p[v]
- }
- return result
- }
|