renderer.ts 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. import {
  2. Text,
  3. Fragment,
  4. Comment,
  5. cloneIfMounted,
  6. normalizeVNode,
  7. VNode,
  8. VNodeArrayChildren,
  9. createVNode,
  10. isSameVNodeType,
  11. Static,
  12. VNodeNormalizedRef,
  13. VNodeHook,
  14. VNodeNormalizedRefAtom
  15. } from './vnode'
  16. import {
  17. ComponentInternalInstance,
  18. createComponentInstance,
  19. Data,
  20. setupComponent
  21. } from './component'
  22. import {
  23. filterSingleRoot,
  24. renderComponentRoot,
  25. shouldUpdateComponent,
  26. updateHOCHostEl
  27. } from './componentRenderUtils'
  28. import {
  29. isString,
  30. EMPTY_OBJ,
  31. EMPTY_ARR,
  32. isReservedProp,
  33. isFunction,
  34. PatchFlags,
  35. ShapeFlags,
  36. NOOP,
  37. hasOwn,
  38. invokeArrayFns,
  39. isArray
  40. } from '@vue/shared'
  41. import {
  42. queueJob,
  43. queuePostFlushCb,
  44. flushPostFlushCbs,
  45. invalidateJob,
  46. flushPreFlushCbs,
  47. SchedulerCb
  48. } from './scheduler'
  49. import { effect, stop, ReactiveEffectOptions, isRef } from '@vue/reactivity'
  50. import { updateProps } from './componentProps'
  51. import { updateSlots } from './componentSlots'
  52. import { pushWarningContext, popWarningContext, warn } from './warning'
  53. import { createAppAPI, CreateAppFunction } from './apiCreateApp'
  54. import {
  55. SuspenseBoundary,
  56. queueEffectWithSuspense,
  57. SuspenseImpl
  58. } from './components/Suspense'
  59. import {
  60. isTeleportDisabled,
  61. TeleportImpl,
  62. TeleportVNode
  63. } from './components/Teleport'
  64. import { isKeepAlive, KeepAliveContext } from './components/KeepAlive'
  65. import { registerHMR, unregisterHMR, isHmrUpdating } from './hmr'
  66. import {
  67. ErrorCodes,
  68. callWithErrorHandling,
  69. callWithAsyncErrorHandling
  70. } from './errorHandling'
  71. import { createHydrationFunctions, RootHydrateFunction } from './hydration'
  72. import { invokeDirectiveHook } from './directives'
  73. import { startMeasure, endMeasure } from './profiling'
  74. import { ComponentPublicInstance } from './componentPublicInstance'
  75. import { devtoolsComponentRemoved, devtoolsComponentUpdated } from './devtools'
  76. import { initFeatureFlags } from './featureFlags'
  77. import { isAsyncWrapper } from './apiAsyncComponent'
  78. export interface Renderer<HostElement = RendererElement> {
  79. render: RootRenderFunction<HostElement>
  80. createApp: CreateAppFunction<HostElement>
  81. }
  82. export interface HydrationRenderer extends Renderer<Element> {
  83. hydrate: RootHydrateFunction
  84. }
  85. export type RootRenderFunction<HostElement = RendererElement> = (
  86. vnode: VNode | null,
  87. container: HostElement
  88. ) => void
  89. export interface RendererOptions<
  90. HostNode = RendererNode,
  91. HostElement = RendererElement
  92. > {
  93. patchProp(
  94. el: HostElement,
  95. key: string,
  96. prevValue: any,
  97. nextValue: any,
  98. isSVG?: boolean,
  99. prevChildren?: VNode<HostNode, HostElement>[],
  100. parentComponent?: ComponentInternalInstance | null,
  101. parentSuspense?: SuspenseBoundary | null,
  102. unmountChildren?: UnmountChildrenFn
  103. ): void
  104. forcePatchProp?(el: HostElement, key: string): boolean
  105. insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void
  106. remove(el: HostNode): void
  107. createElement(
  108. type: string,
  109. isSVG?: boolean,
  110. isCustomizedBuiltIn?: string
  111. ): HostElement
  112. createText(text: string): HostNode
  113. createComment(text: string): HostNode
  114. setText(node: HostNode, text: string): void
  115. setElementText(node: HostElement, text: string): void
  116. parentNode(node: HostNode): HostElement | null
  117. nextSibling(node: HostNode): HostNode | null
  118. querySelector?(selector: string): HostElement | null
  119. setScopeId?(el: HostElement, id: string): void
  120. cloneNode?(node: HostNode): HostNode
  121. insertStaticContent?(
  122. content: string,
  123. parent: HostElement,
  124. anchor: HostNode | null,
  125. isSVG: boolean
  126. ): HostElement[]
  127. }
  128. // Renderer Node can technically be any object in the context of core renderer
  129. // logic - they are never directly operated on and always passed to the node op
  130. // functions provided via options, so the internal constraint is really just
  131. // a generic object.
  132. export interface RendererNode {
  133. [key: string]: any
  134. }
  135. export interface RendererElement extends RendererNode {}
  136. // An object exposing the internals of a renderer, passed to tree-shakeable
  137. // features so that they can be decoupled from this file. Keys are shortened
  138. // to optimize bundle size.
  139. export interface RendererInternals<
  140. HostNode = RendererNode,
  141. HostElement = RendererElement
  142. > {
  143. p: PatchFn
  144. um: UnmountFn
  145. r: RemoveFn
  146. m: MoveFn
  147. mt: MountComponentFn
  148. mc: MountChildrenFn
  149. pc: PatchChildrenFn
  150. pbc: PatchBlockChildrenFn
  151. n: NextFn
  152. o: RendererOptions<HostNode, HostElement>
  153. }
  154. // These functions are created inside a closure and therefore their types cannot
  155. // be directly exported. In order to avoid maintaining function signatures in
  156. // two places, we declare them once here and use them inside the closure.
  157. type PatchFn = (
  158. n1: VNode | null, // null means this is a mount
  159. n2: VNode,
  160. container: RendererElement,
  161. anchor?: RendererNode | null,
  162. parentComponent?: ComponentInternalInstance | null,
  163. parentSuspense?: SuspenseBoundary | null,
  164. isSVG?: boolean,
  165. optimized?: boolean
  166. ) => void
  167. type MountChildrenFn = (
  168. children: VNodeArrayChildren,
  169. container: RendererElement,
  170. anchor: RendererNode | null,
  171. parentComponent: ComponentInternalInstance | null,
  172. parentSuspense: SuspenseBoundary | null,
  173. isSVG: boolean,
  174. optimized: boolean,
  175. start?: number
  176. ) => void
  177. type PatchChildrenFn = (
  178. n1: VNode | null,
  179. n2: VNode,
  180. container: RendererElement,
  181. anchor: RendererNode | null,
  182. parentComponent: ComponentInternalInstance | null,
  183. parentSuspense: SuspenseBoundary | null,
  184. isSVG: boolean,
  185. optimized?: boolean
  186. ) => void
  187. type PatchBlockChildrenFn = (
  188. oldChildren: VNode[],
  189. newChildren: VNode[],
  190. fallbackContainer: RendererElement,
  191. parentComponent: ComponentInternalInstance | null,
  192. parentSuspense: SuspenseBoundary | null,
  193. isSVG: boolean
  194. ) => void
  195. type MoveFn = (
  196. vnode: VNode,
  197. container: RendererElement,
  198. anchor: RendererNode | null,
  199. type: MoveType,
  200. parentSuspense?: SuspenseBoundary | null
  201. ) => void
  202. type NextFn = (vnode: VNode) => RendererNode | null
  203. type UnmountFn = (
  204. vnode: VNode,
  205. parentComponent: ComponentInternalInstance | null,
  206. parentSuspense: SuspenseBoundary | null,
  207. doRemove?: boolean,
  208. optimized?: boolean
  209. ) => void
  210. type RemoveFn = (vnode: VNode) => void
  211. type UnmountChildrenFn = (
  212. children: VNode[],
  213. parentComponent: ComponentInternalInstance | null,
  214. parentSuspense: SuspenseBoundary | null,
  215. doRemove?: boolean,
  216. optimized?: boolean,
  217. start?: number
  218. ) => void
  219. export type MountComponentFn = (
  220. initialVNode: VNode,
  221. container: RendererElement,
  222. anchor: RendererNode | null,
  223. parentComponent: ComponentInternalInstance | null,
  224. parentSuspense: SuspenseBoundary | null,
  225. isSVG: boolean,
  226. optimized: boolean
  227. ) => void
  228. type ProcessTextOrCommentFn = (
  229. n1: VNode | null,
  230. n2: VNode,
  231. container: RendererElement,
  232. anchor: RendererNode | null
  233. ) => void
  234. export type SetupRenderEffectFn = (
  235. instance: ComponentInternalInstance,
  236. initialVNode: VNode,
  237. container: RendererElement,
  238. anchor: RendererNode | null,
  239. parentSuspense: SuspenseBoundary | null,
  240. isSVG: boolean,
  241. optimized: boolean
  242. ) => void
  243. export const enum MoveType {
  244. ENTER,
  245. LEAVE,
  246. REORDER
  247. }
  248. const prodEffectOptions = {
  249. scheduler: queueJob,
  250. // #1801, #2043 component render effects should allow recursive updates
  251. allowRecurse: true
  252. }
  253. function createDevEffectOptions(
  254. instance: ComponentInternalInstance
  255. ): ReactiveEffectOptions {
  256. return {
  257. scheduler: queueJob,
  258. allowRecurse: true,
  259. onTrack: instance.rtc ? e => invokeArrayFns(instance.rtc!, e) : void 0,
  260. onTrigger: instance.rtg ? e => invokeArrayFns(instance.rtg!, e) : void 0
  261. }
  262. }
  263. export const queuePostRenderEffect = __FEATURE_SUSPENSE__
  264. ? queueEffectWithSuspense
  265. : queuePostFlushCb
  266. export const setRef = (
  267. rawRef: VNodeNormalizedRef,
  268. oldRawRef: VNodeNormalizedRef | null,
  269. parentSuspense: SuspenseBoundary | null,
  270. vnode: VNode | null
  271. ) => {
  272. if (isArray(rawRef)) {
  273. rawRef.forEach((r, i) =>
  274. setRef(
  275. r,
  276. oldRawRef && (isArray(oldRawRef) ? oldRawRef[i] : oldRawRef),
  277. parentSuspense,
  278. vnode
  279. )
  280. )
  281. return
  282. }
  283. let value: ComponentPublicInstance | RendererNode | Record<string, any> | null
  284. if (!vnode || isAsyncWrapper(vnode)) {
  285. value = null
  286. } else {
  287. if (vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
  288. value = vnode.component!.exposed || vnode.component!.proxy
  289. } else {
  290. value = vnode.el
  291. }
  292. }
  293. const { i: owner, r: ref } = rawRef
  294. if (__DEV__ && !owner) {
  295. warn(
  296. `Missing ref owner context. ref cannot be used on hoisted vnodes. ` +
  297. `A vnode with ref must be created inside the render function.`
  298. )
  299. return
  300. }
  301. const oldRef = oldRawRef && (oldRawRef as VNodeNormalizedRefAtom).r
  302. const refs = owner.refs === EMPTY_OBJ ? (owner.refs = {}) : owner.refs
  303. const setupState = owner.setupState
  304. // unset old ref
  305. if (oldRef != null && oldRef !== ref) {
  306. if (isString(oldRef)) {
  307. refs[oldRef] = null
  308. if (hasOwn(setupState, oldRef)) {
  309. setupState[oldRef] = null
  310. }
  311. } else if (isRef(oldRef)) {
  312. oldRef.value = null
  313. }
  314. }
  315. if (isString(ref)) {
  316. const doSet = () => {
  317. refs[ref] = value
  318. if (hasOwn(setupState, ref)) {
  319. setupState[ref] = value
  320. }
  321. }
  322. // #1789: for non-null values, set them after render
  323. // null values means this is unmount and it should not overwrite another
  324. // ref with the same key
  325. if (value) {
  326. ;(doSet as SchedulerCb).id = -1
  327. queuePostRenderEffect(doSet, parentSuspense)
  328. } else {
  329. doSet()
  330. }
  331. } else if (isRef(ref)) {
  332. const doSet = () => {
  333. ref.value = value
  334. }
  335. if (value) {
  336. ;(doSet as SchedulerCb).id = -1
  337. queuePostRenderEffect(doSet, parentSuspense)
  338. } else {
  339. doSet()
  340. }
  341. } else if (isFunction(ref)) {
  342. callWithErrorHandling(ref, owner, ErrorCodes.FUNCTION_REF, [value, refs])
  343. } else if (__DEV__) {
  344. warn('Invalid template ref type:', value, `(${typeof value})`)
  345. }
  346. }
  347. /**
  348. * The createRenderer function accepts two generic arguments:
  349. * HostNode and HostElement, corresponding to Node and Element types in the
  350. * host environment. For example, for runtime-dom, HostNode would be the DOM
  351. * `Node` interface and HostElement would be the DOM `Element` interface.
  352. *
  353. * Custom renderers can pass in the platform specific types like this:
  354. *
  355. * ``` js
  356. * const { render, createApp } = createRenderer<Node, Element>({
  357. * patchProp,
  358. * ...nodeOps
  359. * })
  360. * ```
  361. */
  362. export function createRenderer<
  363. HostNode = RendererNode,
  364. HostElement = RendererElement
  365. >(options: RendererOptions<HostNode, HostElement>) {
  366. return baseCreateRenderer<HostNode, HostElement>(options)
  367. }
  368. // Separate API for creating hydration-enabled renderer.
  369. // Hydration logic is only used when calling this function, making it
  370. // tree-shakable.
  371. export function createHydrationRenderer(
  372. options: RendererOptions<Node, Element>
  373. ) {
  374. return baseCreateRenderer(options, createHydrationFunctions)
  375. }
  376. // overload 1: no hydration
  377. function baseCreateRenderer<
  378. HostNode = RendererNode,
  379. HostElement = RendererElement
  380. >(options: RendererOptions<HostNode, HostElement>): Renderer<HostElement>
  381. // overload 2: with hydration
  382. function baseCreateRenderer(
  383. options: RendererOptions<Node, Element>,
  384. createHydrationFns: typeof createHydrationFunctions
  385. ): HydrationRenderer
  386. // implementation
  387. function baseCreateRenderer(
  388. options: RendererOptions,
  389. createHydrationFns?: typeof createHydrationFunctions
  390. ): any {
  391. // compile-time feature flags check
  392. if (__ESM_BUNDLER__ && !__TEST__) {
  393. initFeatureFlags()
  394. }
  395. const {
  396. insert: hostInsert,
  397. remove: hostRemove,
  398. patchProp: hostPatchProp,
  399. forcePatchProp: hostForcePatchProp,
  400. createElement: hostCreateElement,
  401. createText: hostCreateText,
  402. createComment: hostCreateComment,
  403. setText: hostSetText,
  404. setElementText: hostSetElementText,
  405. parentNode: hostParentNode,
  406. nextSibling: hostNextSibling,
  407. setScopeId: hostSetScopeId = NOOP,
  408. cloneNode: hostCloneNode,
  409. insertStaticContent: hostInsertStaticContent
  410. } = options
  411. // Note: functions inside this closure should use `const xxx = () => {}`
  412. // style in order to prevent being inlined by minifiers.
  413. const patch: PatchFn = (
  414. n1,
  415. n2,
  416. container,
  417. anchor = null,
  418. parentComponent = null,
  419. parentSuspense = null,
  420. isSVG = false,
  421. optimized = false
  422. ) => {
  423. // patching & not same type, unmount old tree
  424. if (n1 && !isSameVNodeType(n1, n2)) {
  425. anchor = getNextHostNode(n1)
  426. unmount(n1, parentComponent, parentSuspense, true)
  427. n1 = null
  428. }
  429. if (n2.patchFlag === PatchFlags.BAIL) {
  430. optimized = false
  431. n2.dynamicChildren = null
  432. }
  433. const { type, ref, shapeFlag } = n2
  434. switch (type) {
  435. case Text:
  436. processText(n1, n2, container, anchor)
  437. break
  438. case Comment:
  439. processCommentNode(n1, n2, container, anchor)
  440. break
  441. case Static:
  442. if (n1 == null) {
  443. mountStaticNode(n2, container, anchor, isSVG)
  444. } else if (__DEV__) {
  445. patchStaticNode(n1, n2, container, isSVG)
  446. }
  447. break
  448. case Fragment:
  449. processFragment(
  450. n1,
  451. n2,
  452. container,
  453. anchor,
  454. parentComponent,
  455. parentSuspense,
  456. isSVG,
  457. optimized
  458. )
  459. break
  460. default:
  461. if (shapeFlag & ShapeFlags.ELEMENT) {
  462. processElement(
  463. n1,
  464. n2,
  465. container,
  466. anchor,
  467. parentComponent,
  468. parentSuspense,
  469. isSVG,
  470. optimized
  471. )
  472. } else if (shapeFlag & ShapeFlags.COMPONENT) {
  473. processComponent(
  474. n1,
  475. n2,
  476. container,
  477. anchor,
  478. parentComponent,
  479. parentSuspense,
  480. isSVG,
  481. optimized
  482. )
  483. } else if (shapeFlag & ShapeFlags.TELEPORT) {
  484. ;(type as typeof TeleportImpl).process(
  485. n1 as TeleportVNode,
  486. n2 as TeleportVNode,
  487. container,
  488. anchor,
  489. parentComponent,
  490. parentSuspense,
  491. isSVG,
  492. optimized,
  493. internals
  494. )
  495. } else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
  496. ;(type as typeof SuspenseImpl).process(
  497. n1,
  498. n2,
  499. container,
  500. anchor,
  501. parentComponent,
  502. parentSuspense,
  503. isSVG,
  504. optimized,
  505. internals
  506. )
  507. } else if (__DEV__) {
  508. warn('Invalid VNode type:', type, `(${typeof type})`)
  509. }
  510. }
  511. // set ref
  512. if (ref != null && parentComponent) {
  513. setRef(ref, n1 && n1.ref, parentSuspense, n2)
  514. }
  515. }
  516. const processText: ProcessTextOrCommentFn = (n1, n2, container, anchor) => {
  517. if (n1 == null) {
  518. hostInsert(
  519. (n2.el = hostCreateText(n2.children as string)),
  520. container,
  521. anchor
  522. )
  523. } else {
  524. const el = (n2.el = n1.el!)
  525. if (n2.children !== n1.children) {
  526. hostSetText(el, n2.children as string)
  527. }
  528. }
  529. }
  530. const processCommentNode: ProcessTextOrCommentFn = (
  531. n1,
  532. n2,
  533. container,
  534. anchor
  535. ) => {
  536. if (n1 == null) {
  537. hostInsert(
  538. (n2.el = hostCreateComment((n2.children as string) || '')),
  539. container,
  540. anchor
  541. )
  542. } else {
  543. // there's no support for dynamic comments
  544. n2.el = n1.el
  545. }
  546. }
  547. const mountStaticNode = (
  548. n2: VNode,
  549. container: RendererElement,
  550. anchor: RendererNode | null,
  551. isSVG: boolean
  552. ) => {
  553. // static nodes are only present when used with compiler-dom/runtime-dom
  554. // which guarantees presence of hostInsertStaticContent.
  555. ;[n2.el, n2.anchor] = hostInsertStaticContent!(
  556. n2.children as string,
  557. container,
  558. anchor,
  559. isSVG
  560. )
  561. }
  562. /**
  563. * Dev / HMR only
  564. */
  565. const patchStaticNode = (
  566. n1: VNode,
  567. n2: VNode,
  568. container: RendererElement,
  569. isSVG: boolean
  570. ) => {
  571. // static nodes are only patched during dev for HMR
  572. if (n2.children !== n1.children) {
  573. const anchor = hostNextSibling(n1.anchor!)
  574. // remove existing
  575. removeStaticNode(n1)
  576. // insert new
  577. ;[n2.el, n2.anchor] = hostInsertStaticContent!(
  578. n2.children as string,
  579. container,
  580. anchor,
  581. isSVG
  582. )
  583. } else {
  584. n2.el = n1.el
  585. n2.anchor = n1.anchor
  586. }
  587. }
  588. const moveStaticNode = (
  589. { el, anchor }: VNode,
  590. container: RendererElement,
  591. nextSibling: RendererNode | null
  592. ) => {
  593. let next
  594. while (el && el !== anchor) {
  595. next = hostNextSibling(el)
  596. hostInsert(el, container, nextSibling)
  597. el = next
  598. }
  599. hostInsert(anchor!, container, nextSibling)
  600. }
  601. const removeStaticNode = ({ el, anchor }: VNode) => {
  602. let next
  603. while (el && el !== anchor) {
  604. next = hostNextSibling(el)
  605. hostRemove(el)
  606. el = next
  607. }
  608. hostRemove(anchor!)
  609. }
  610. const processElement = (
  611. n1: VNode | null,
  612. n2: VNode,
  613. container: RendererElement,
  614. anchor: RendererNode | null,
  615. parentComponent: ComponentInternalInstance | null,
  616. parentSuspense: SuspenseBoundary | null,
  617. isSVG: boolean,
  618. optimized: boolean
  619. ) => {
  620. isSVG = isSVG || (n2.type as string) === 'svg'
  621. if (n1 == null) {
  622. mountElement(
  623. n2,
  624. container,
  625. anchor,
  626. parentComponent,
  627. parentSuspense,
  628. isSVG,
  629. optimized
  630. )
  631. } else {
  632. patchElement(n1, n2, parentComponent, parentSuspense, isSVG, optimized)
  633. }
  634. }
  635. const mountElement = (
  636. vnode: VNode,
  637. container: RendererElement,
  638. anchor: RendererNode | null,
  639. parentComponent: ComponentInternalInstance | null,
  640. parentSuspense: SuspenseBoundary | null,
  641. isSVG: boolean,
  642. optimized: boolean
  643. ) => {
  644. let el: RendererElement
  645. let vnodeHook: VNodeHook | undefined | null
  646. const {
  647. type,
  648. props,
  649. shapeFlag,
  650. transition,
  651. scopeId,
  652. patchFlag,
  653. dirs
  654. } = vnode
  655. if (
  656. !__DEV__ &&
  657. vnode.el &&
  658. hostCloneNode !== undefined &&
  659. patchFlag === PatchFlags.HOISTED
  660. ) {
  661. // If a vnode has non-null el, it means it's being reused.
  662. // Only static vnodes can be reused, so its mounted DOM nodes should be
  663. // exactly the same, and we can simply do a clone here.
  664. // only do this in production since cloned trees cannot be HMR updated.
  665. el = vnode.el = hostCloneNode(vnode.el)
  666. } else {
  667. el = vnode.el = hostCreateElement(
  668. vnode.type as string,
  669. isSVG,
  670. props && props.is
  671. )
  672. // mount children first, since some props may rely on child content
  673. // being already rendered, e.g. `<select value>`
  674. if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
  675. hostSetElementText(el, vnode.children as string)
  676. } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  677. mountChildren(
  678. vnode.children as VNodeArrayChildren,
  679. el,
  680. null,
  681. parentComponent,
  682. parentSuspense,
  683. isSVG && type !== 'foreignObject',
  684. optimized || !!vnode.dynamicChildren
  685. )
  686. }
  687. if (dirs) {
  688. invokeDirectiveHook(vnode, null, parentComponent, 'created')
  689. }
  690. // props
  691. if (props) {
  692. for (const key in props) {
  693. if (!isReservedProp(key)) {
  694. hostPatchProp(
  695. el,
  696. key,
  697. null,
  698. props[key],
  699. isSVG,
  700. vnode.children as VNode[],
  701. parentComponent,
  702. parentSuspense,
  703. unmountChildren
  704. )
  705. }
  706. }
  707. if ((vnodeHook = props.onVnodeBeforeMount)) {
  708. invokeVNodeHook(vnodeHook, parentComponent, vnode)
  709. }
  710. }
  711. // scopeId
  712. setScopeId(el, scopeId, vnode, parentComponent)
  713. }
  714. if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
  715. Object.defineProperty(el, '__vnode', {
  716. value: vnode,
  717. enumerable: false
  718. })
  719. Object.defineProperty(el, '__vueParentComponent', {
  720. value: parentComponent,
  721. enumerable: false
  722. })
  723. }
  724. if (dirs) {
  725. invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount')
  726. }
  727. // #1583 For inside suspense + suspense not resolved case, enter hook should call when suspense resolved
  728. // #1689 For inside suspense + suspense resolved case, just call it
  729. const needCallTransitionHooks =
  730. (!parentSuspense || (parentSuspense && !parentSuspense.pendingBranch)) &&
  731. transition &&
  732. !transition.persisted
  733. if (needCallTransitionHooks) {
  734. transition!.beforeEnter(el)
  735. }
  736. hostInsert(el, container, anchor)
  737. if (
  738. (vnodeHook = props && props.onVnodeMounted) ||
  739. needCallTransitionHooks ||
  740. dirs
  741. ) {
  742. queuePostRenderEffect(() => {
  743. vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode)
  744. needCallTransitionHooks && transition!.enter(el)
  745. dirs && invokeDirectiveHook(vnode, null, parentComponent, 'mounted')
  746. }, parentSuspense)
  747. }
  748. }
  749. const setScopeId = (
  750. el: RendererElement,
  751. scopeId: string | false | null,
  752. vnode: VNode,
  753. parentComponent: ComponentInternalInstance | null
  754. ) => {
  755. if (scopeId) {
  756. hostSetScopeId(el, scopeId)
  757. }
  758. if (parentComponent) {
  759. const treeOwnerId = parentComponent.type.__scopeId
  760. // vnode's own scopeId and the current patched component's scopeId is
  761. // different - this is a slot content node.
  762. if (treeOwnerId && treeOwnerId !== scopeId) {
  763. hostSetScopeId(el, treeOwnerId + '-s')
  764. }
  765. let subTree = parentComponent.subTree
  766. if (__DEV__ && subTree.type === Fragment) {
  767. subTree =
  768. filterSingleRoot(subTree.children as VNodeArrayChildren) || subTree
  769. }
  770. if (vnode === subTree) {
  771. setScopeId(
  772. el,
  773. parentComponent.vnode.scopeId,
  774. parentComponent.vnode,
  775. parentComponent.parent
  776. )
  777. }
  778. }
  779. }
  780. const mountChildren: MountChildrenFn = (
  781. children,
  782. container,
  783. anchor,
  784. parentComponent,
  785. parentSuspense,
  786. isSVG,
  787. optimized,
  788. start = 0
  789. ) => {
  790. for (let i = start; i < children.length; i++) {
  791. const child = (children[i] = optimized
  792. ? cloneIfMounted(children[i] as VNode)
  793. : normalizeVNode(children[i]))
  794. patch(
  795. null,
  796. child,
  797. container,
  798. anchor,
  799. parentComponent,
  800. parentSuspense,
  801. isSVG,
  802. optimized
  803. )
  804. }
  805. }
  806. const patchElement = (
  807. n1: VNode,
  808. n2: VNode,
  809. parentComponent: ComponentInternalInstance | null,
  810. parentSuspense: SuspenseBoundary | null,
  811. isSVG: boolean,
  812. optimized: boolean
  813. ) => {
  814. const el = (n2.el = n1.el!)
  815. let { patchFlag, dynamicChildren, dirs } = n2
  816. // #1426 take the old vnode's patch flag into account since user may clone a
  817. // compiler-generated vnode, which de-opts to FULL_PROPS
  818. patchFlag |= n1.patchFlag & PatchFlags.FULL_PROPS
  819. const oldProps = n1.props || EMPTY_OBJ
  820. const newProps = n2.props || EMPTY_OBJ
  821. let vnodeHook: VNodeHook | undefined | null
  822. if ((vnodeHook = newProps.onVnodeBeforeUpdate)) {
  823. invokeVNodeHook(vnodeHook, parentComponent, n2, n1)
  824. }
  825. if (dirs) {
  826. invokeDirectiveHook(n2, n1, parentComponent, 'beforeUpdate')
  827. }
  828. if (__DEV__ && isHmrUpdating) {
  829. // HMR updated, force full diff
  830. patchFlag = 0
  831. optimized = false
  832. dynamicChildren = null
  833. }
  834. if (patchFlag > 0) {
  835. // the presence of a patchFlag means this element's render code was
  836. // generated by the compiler and can take the fast path.
  837. // in this path old node and new node are guaranteed to have the same shape
  838. // (i.e. at the exact same position in the source template)
  839. if (patchFlag & PatchFlags.FULL_PROPS) {
  840. // element props contain dynamic keys, full diff needed
  841. patchProps(
  842. el,
  843. n2,
  844. oldProps,
  845. newProps,
  846. parentComponent,
  847. parentSuspense,
  848. isSVG
  849. )
  850. } else {
  851. // class
  852. // this flag is matched when the element has dynamic class bindings.
  853. if (patchFlag & PatchFlags.CLASS) {
  854. if (oldProps.class !== newProps.class) {
  855. hostPatchProp(el, 'class', null, newProps.class, isSVG)
  856. }
  857. }
  858. // style
  859. // this flag is matched when the element has dynamic style bindings
  860. if (patchFlag & PatchFlags.STYLE) {
  861. hostPatchProp(el, 'style', oldProps.style, newProps.style, isSVG)
  862. }
  863. // props
  864. // This flag is matched when the element has dynamic prop/attr bindings
  865. // other than class and style. The keys of dynamic prop/attrs are saved for
  866. // faster iteration.
  867. // Note dynamic keys like :[foo]="bar" will cause this optimization to
  868. // bail out and go through a full diff because we need to unset the old key
  869. if (patchFlag & PatchFlags.PROPS) {
  870. // if the flag is present then dynamicProps must be non-null
  871. const propsToUpdate = n2.dynamicProps!
  872. for (let i = 0; i < propsToUpdate.length; i++) {
  873. const key = propsToUpdate[i]
  874. const prev = oldProps[key]
  875. const next = newProps[key]
  876. if (
  877. next !== prev ||
  878. (hostForcePatchProp && hostForcePatchProp(el, key))
  879. ) {
  880. hostPatchProp(
  881. el,
  882. key,
  883. prev,
  884. next,
  885. isSVG,
  886. n1.children as VNode[],
  887. parentComponent,
  888. parentSuspense,
  889. unmountChildren
  890. )
  891. }
  892. }
  893. }
  894. }
  895. // text
  896. // This flag is matched when the element has only dynamic text children.
  897. if (patchFlag & PatchFlags.TEXT) {
  898. if (n1.children !== n2.children) {
  899. hostSetElementText(el, n2.children as string)
  900. }
  901. }
  902. } else if (!optimized && dynamicChildren == null) {
  903. // unoptimized, full diff
  904. patchProps(
  905. el,
  906. n2,
  907. oldProps,
  908. newProps,
  909. parentComponent,
  910. parentSuspense,
  911. isSVG
  912. )
  913. }
  914. const areChildrenSVG = isSVG && n2.type !== 'foreignObject'
  915. if (dynamicChildren) {
  916. patchBlockChildren(
  917. n1.dynamicChildren!,
  918. dynamicChildren,
  919. el,
  920. parentComponent,
  921. parentSuspense,
  922. areChildrenSVG
  923. )
  924. if (__DEV__ && parentComponent && parentComponent.type.__hmrId) {
  925. traverseStaticChildren(n1, n2)
  926. }
  927. } else if (!optimized) {
  928. // full diff
  929. patchChildren(
  930. n1,
  931. n2,
  932. el,
  933. null,
  934. parentComponent,
  935. parentSuspense,
  936. areChildrenSVG
  937. )
  938. }
  939. if ((vnodeHook = newProps.onVnodeUpdated) || dirs) {
  940. queuePostRenderEffect(() => {
  941. vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, n2, n1)
  942. dirs && invokeDirectiveHook(n2, n1, parentComponent, 'updated')
  943. }, parentSuspense)
  944. }
  945. }
  946. // The fast path for blocks.
  947. const patchBlockChildren: PatchBlockChildrenFn = (
  948. oldChildren,
  949. newChildren,
  950. fallbackContainer,
  951. parentComponent,
  952. parentSuspense,
  953. isSVG
  954. ) => {
  955. for (let i = 0; i < newChildren.length; i++) {
  956. const oldVNode = oldChildren[i]
  957. const newVNode = newChildren[i]
  958. // Determine the container (parent element) for the patch.
  959. const container =
  960. // - In the case of a Fragment, we need to provide the actual parent
  961. // of the Fragment itself so it can move its children.
  962. oldVNode.type === Fragment ||
  963. // - In the case of different nodes, there is going to be a replacement
  964. // which also requires the correct parent container
  965. !isSameVNodeType(oldVNode, newVNode) ||
  966. // - In the case of a component, it could contain anything.
  967. oldVNode.shapeFlag & ShapeFlags.COMPONENT ||
  968. oldVNode.shapeFlag & ShapeFlags.TELEPORT
  969. ? hostParentNode(oldVNode.el!)!
  970. : // In other cases, the parent container is not actually used so we
  971. // just pass the block element here to avoid a DOM parentNode call.
  972. fallbackContainer
  973. patch(
  974. oldVNode,
  975. newVNode,
  976. container,
  977. null,
  978. parentComponent,
  979. parentSuspense,
  980. isSVG,
  981. true
  982. )
  983. }
  984. }
  985. const patchProps = (
  986. el: RendererElement,
  987. vnode: VNode,
  988. oldProps: Data,
  989. newProps: Data,
  990. parentComponent: ComponentInternalInstance | null,
  991. parentSuspense: SuspenseBoundary | null,
  992. isSVG: boolean
  993. ) => {
  994. if (oldProps !== newProps) {
  995. for (const key in newProps) {
  996. // empty string is not valid prop
  997. if (isReservedProp(key)) continue
  998. const next = newProps[key]
  999. const prev = oldProps[key]
  1000. if (
  1001. next !== prev ||
  1002. (hostForcePatchProp && hostForcePatchProp(el, key))
  1003. ) {
  1004. hostPatchProp(
  1005. el,
  1006. key,
  1007. prev,
  1008. next,
  1009. isSVG,
  1010. vnode.children as VNode[],
  1011. parentComponent,
  1012. parentSuspense,
  1013. unmountChildren
  1014. )
  1015. }
  1016. }
  1017. if (oldProps !== EMPTY_OBJ) {
  1018. for (const key in oldProps) {
  1019. if (!isReservedProp(key) && !(key in newProps)) {
  1020. hostPatchProp(
  1021. el,
  1022. key,
  1023. oldProps[key],
  1024. null,
  1025. isSVG,
  1026. vnode.children as VNode[],
  1027. parentComponent,
  1028. parentSuspense,
  1029. unmountChildren
  1030. )
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. const processFragment = (
  1037. n1: VNode | null,
  1038. n2: VNode,
  1039. container: RendererElement,
  1040. anchor: RendererNode | null,
  1041. parentComponent: ComponentInternalInstance | null,
  1042. parentSuspense: SuspenseBoundary | null,
  1043. isSVG: boolean,
  1044. optimized: boolean
  1045. ) => {
  1046. const fragmentStartAnchor = (n2.el = n1 ? n1.el : hostCreateText(''))!
  1047. const fragmentEndAnchor = (n2.anchor = n1 ? n1.anchor : hostCreateText(''))!
  1048. let { patchFlag, dynamicChildren } = n2
  1049. if (patchFlag > 0) {
  1050. optimized = true
  1051. }
  1052. if (__DEV__ && isHmrUpdating) {
  1053. // HMR updated, force full diff
  1054. patchFlag = 0
  1055. optimized = false
  1056. dynamicChildren = null
  1057. }
  1058. if (n1 == null) {
  1059. hostInsert(fragmentStartAnchor, container, anchor)
  1060. hostInsert(fragmentEndAnchor, container, anchor)
  1061. // a fragment can only have array children
  1062. // since they are either generated by the compiler, or implicitly created
  1063. // from arrays.
  1064. mountChildren(
  1065. n2.children as VNodeArrayChildren,
  1066. container,
  1067. fragmentEndAnchor,
  1068. parentComponent,
  1069. parentSuspense,
  1070. isSVG,
  1071. optimized
  1072. )
  1073. } else {
  1074. if (
  1075. patchFlag > 0 &&
  1076. patchFlag & PatchFlags.STABLE_FRAGMENT &&
  1077. dynamicChildren &&
  1078. // #2715 the previous fragment could've been a BAILed one as a result
  1079. // of renderSlot() with no valid children
  1080. n1.dynamicChildren
  1081. ) {
  1082. // a stable fragment (template root or <template v-for>) doesn't need to
  1083. // patch children order, but it may contain dynamicChildren.
  1084. patchBlockChildren(
  1085. n1.dynamicChildren,
  1086. dynamicChildren,
  1087. container,
  1088. parentComponent,
  1089. parentSuspense,
  1090. isSVG
  1091. )
  1092. if (__DEV__ && parentComponent && parentComponent.type.__hmrId) {
  1093. traverseStaticChildren(n1, n2)
  1094. } else if (
  1095. // #2080 if the stable fragment has a key, it's a <template v-for> that may
  1096. // get moved around. Make sure all root level vnodes inherit el.
  1097. // #2134 or if it's a component root, it may also get moved around
  1098. // as the component is being moved.
  1099. n2.key != null ||
  1100. (parentComponent && n2 === parentComponent.subTree)
  1101. ) {
  1102. traverseStaticChildren(n1, n2, true /* shallow */)
  1103. }
  1104. } else {
  1105. // keyed / unkeyed, or manual fragments.
  1106. // for keyed & unkeyed, since they are compiler generated from v-for,
  1107. // each child is guaranteed to be a block so the fragment will never
  1108. // have dynamicChildren.
  1109. patchChildren(
  1110. n1,
  1111. n2,
  1112. container,
  1113. fragmentEndAnchor,
  1114. parentComponent,
  1115. parentSuspense,
  1116. isSVG,
  1117. optimized
  1118. )
  1119. }
  1120. }
  1121. }
  1122. const processComponent = (
  1123. n1: VNode | null,
  1124. n2: VNode,
  1125. container: RendererElement,
  1126. anchor: RendererNode | null,
  1127. parentComponent: ComponentInternalInstance | null,
  1128. parentSuspense: SuspenseBoundary | null,
  1129. isSVG: boolean,
  1130. optimized: boolean
  1131. ) => {
  1132. if (n1 == null) {
  1133. if (n2.shapeFlag & ShapeFlags.COMPONENT_KEPT_ALIVE) {
  1134. ;(parentComponent!.ctx as KeepAliveContext).activate(
  1135. n2,
  1136. container,
  1137. anchor,
  1138. isSVG,
  1139. optimized
  1140. )
  1141. } else {
  1142. mountComponent(
  1143. n2,
  1144. container,
  1145. anchor,
  1146. parentComponent,
  1147. parentSuspense,
  1148. isSVG,
  1149. optimized
  1150. )
  1151. }
  1152. } else {
  1153. updateComponent(n1, n2, optimized)
  1154. }
  1155. }
  1156. const mountComponent: MountComponentFn = (
  1157. initialVNode,
  1158. container,
  1159. anchor,
  1160. parentComponent,
  1161. parentSuspense,
  1162. isSVG,
  1163. optimized
  1164. ) => {
  1165. const instance: ComponentInternalInstance = (initialVNode.component = createComponentInstance(
  1166. initialVNode,
  1167. parentComponent,
  1168. parentSuspense
  1169. ))
  1170. if (__DEV__ && instance.type.__hmrId) {
  1171. registerHMR(instance)
  1172. }
  1173. if (__DEV__) {
  1174. pushWarningContext(initialVNode)
  1175. startMeasure(instance, `mount`)
  1176. }
  1177. // inject renderer internals for keepAlive
  1178. if (isKeepAlive(initialVNode)) {
  1179. ;(instance.ctx as KeepAliveContext).renderer = internals
  1180. }
  1181. // resolve props and slots for setup context
  1182. if (__DEV__) {
  1183. startMeasure(instance, `init`)
  1184. }
  1185. setupComponent(instance)
  1186. if (__DEV__) {
  1187. endMeasure(instance, `init`)
  1188. }
  1189. // setup() is async. This component relies on async logic to be resolved
  1190. // before proceeding
  1191. if (__FEATURE_SUSPENSE__ && instance.asyncDep) {
  1192. parentSuspense && parentSuspense.registerDep(instance, setupRenderEffect)
  1193. // Give it a placeholder if this is not hydration
  1194. // TODO handle self-defined fallback
  1195. if (!initialVNode.el) {
  1196. const placeholder = (instance.subTree = createVNode(Comment))
  1197. processCommentNode(null, placeholder, container!, anchor)
  1198. }
  1199. return
  1200. }
  1201. setupRenderEffect(
  1202. instance,
  1203. initialVNode,
  1204. container,
  1205. anchor,
  1206. parentSuspense,
  1207. isSVG,
  1208. optimized
  1209. )
  1210. if (__DEV__) {
  1211. popWarningContext()
  1212. endMeasure(instance, `mount`)
  1213. }
  1214. }
  1215. const updateComponent = (n1: VNode, n2: VNode, optimized: boolean) => {
  1216. const instance = (n2.component = n1.component)!
  1217. if (shouldUpdateComponent(n1, n2, optimized)) {
  1218. if (
  1219. __FEATURE_SUSPENSE__ &&
  1220. instance.asyncDep &&
  1221. !instance.asyncResolved
  1222. ) {
  1223. // async & still pending - just update props and slots
  1224. // since the component's reactive effect for render isn't set-up yet
  1225. if (__DEV__) {
  1226. pushWarningContext(n2)
  1227. }
  1228. updateComponentPreRender(instance, n2, optimized)
  1229. if (__DEV__) {
  1230. popWarningContext()
  1231. }
  1232. return
  1233. } else {
  1234. // normal update
  1235. instance.next = n2
  1236. // in case the child component is also queued, remove it to avoid
  1237. // double updating the same child component in the same flush.
  1238. invalidateJob(instance.update)
  1239. // instance.update is the reactive effect runner.
  1240. instance.update()
  1241. }
  1242. } else {
  1243. // no update needed. just copy over properties
  1244. n2.component = n1.component
  1245. n2.el = n1.el
  1246. instance.vnode = n2
  1247. }
  1248. }
  1249. const setupRenderEffect: SetupRenderEffectFn = (
  1250. instance,
  1251. initialVNode,
  1252. container,
  1253. anchor,
  1254. parentSuspense,
  1255. isSVG,
  1256. optimized
  1257. ) => {
  1258. // create reactive effect for rendering
  1259. instance.update = effect(function componentEffect() {
  1260. if (!instance.isMounted) {
  1261. let vnodeHook: VNodeHook | null | undefined
  1262. const { el, props } = initialVNode
  1263. const { bm, m, parent } = instance
  1264. // beforeMount hook
  1265. if (bm) {
  1266. invokeArrayFns(bm)
  1267. }
  1268. // onVnodeBeforeMount
  1269. if ((vnodeHook = props && props.onVnodeBeforeMount)) {
  1270. invokeVNodeHook(vnodeHook, parent, initialVNode)
  1271. }
  1272. // render
  1273. if (__DEV__) {
  1274. startMeasure(instance, `render`)
  1275. }
  1276. const subTree = (instance.subTree = renderComponentRoot(instance))
  1277. if (__DEV__) {
  1278. endMeasure(instance, `render`)
  1279. }
  1280. if (el && hydrateNode) {
  1281. if (__DEV__) {
  1282. startMeasure(instance, `hydrate`)
  1283. }
  1284. // vnode has adopted host node - perform hydration instead of mount.
  1285. hydrateNode(
  1286. initialVNode.el as Node,
  1287. subTree,
  1288. instance,
  1289. parentSuspense
  1290. )
  1291. if (__DEV__) {
  1292. endMeasure(instance, `hydrate`)
  1293. }
  1294. } else {
  1295. if (__DEV__) {
  1296. startMeasure(instance, `patch`)
  1297. }
  1298. patch(
  1299. null,
  1300. subTree,
  1301. container,
  1302. anchor,
  1303. instance,
  1304. parentSuspense,
  1305. isSVG
  1306. )
  1307. if (__DEV__) {
  1308. endMeasure(instance, `patch`)
  1309. }
  1310. initialVNode.el = subTree.el
  1311. }
  1312. // mounted hook
  1313. if (m) {
  1314. queuePostRenderEffect(m, parentSuspense)
  1315. }
  1316. // onVnodeMounted
  1317. if ((vnodeHook = props && props.onVnodeMounted)) {
  1318. queuePostRenderEffect(() => {
  1319. invokeVNodeHook(vnodeHook!, parent, initialVNode)
  1320. }, parentSuspense)
  1321. }
  1322. // activated hook for keep-alive roots.
  1323. // #1742 activated hook must be accessed after first render
  1324. // since the hook may be injected by a child keep-alive
  1325. const { a } = instance
  1326. if (
  1327. a &&
  1328. initialVNode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
  1329. ) {
  1330. queuePostRenderEffect(a, parentSuspense)
  1331. }
  1332. instance.isMounted = true
  1333. } else {
  1334. // updateComponent
  1335. // This is triggered by mutation of component's own state (next: null)
  1336. // OR parent calling processComponent (next: VNode)
  1337. let { next, bu, u, parent, vnode } = instance
  1338. let originNext = next
  1339. let vnodeHook: VNodeHook | null | undefined
  1340. if (__DEV__) {
  1341. pushWarningContext(next || instance.vnode)
  1342. }
  1343. if (next) {
  1344. next.el = vnode.el
  1345. updateComponentPreRender(instance, next, optimized)
  1346. } else {
  1347. next = vnode
  1348. }
  1349. // beforeUpdate hook
  1350. if (bu) {
  1351. invokeArrayFns(bu)
  1352. }
  1353. // onVnodeBeforeUpdate
  1354. if ((vnodeHook = next.props && next.props.onVnodeBeforeUpdate)) {
  1355. invokeVNodeHook(vnodeHook, parent, next, vnode)
  1356. }
  1357. // render
  1358. if (__DEV__) {
  1359. startMeasure(instance, `render`)
  1360. }
  1361. const nextTree = renderComponentRoot(instance)
  1362. if (__DEV__) {
  1363. endMeasure(instance, `render`)
  1364. }
  1365. const prevTree = instance.subTree
  1366. instance.subTree = nextTree
  1367. if (__DEV__) {
  1368. startMeasure(instance, `patch`)
  1369. }
  1370. patch(
  1371. prevTree,
  1372. nextTree,
  1373. // parent may have changed if it's in a teleport
  1374. hostParentNode(prevTree.el!)!,
  1375. // anchor may have changed if it's in a fragment
  1376. getNextHostNode(prevTree),
  1377. instance,
  1378. parentSuspense,
  1379. isSVG
  1380. )
  1381. if (__DEV__) {
  1382. endMeasure(instance, `patch`)
  1383. }
  1384. next.el = nextTree.el
  1385. if (originNext === null) {
  1386. // self-triggered update. In case of HOC, update parent component
  1387. // vnode el. HOC is indicated by parent instance's subTree pointing
  1388. // to child component's vnode
  1389. updateHOCHostEl(instance, nextTree.el)
  1390. }
  1391. // updated hook
  1392. if (u) {
  1393. queuePostRenderEffect(u, parentSuspense)
  1394. }
  1395. // onVnodeUpdated
  1396. if ((vnodeHook = next.props && next.props.onVnodeUpdated)) {
  1397. queuePostRenderEffect(() => {
  1398. invokeVNodeHook(vnodeHook!, parent, next!, vnode)
  1399. }, parentSuspense)
  1400. }
  1401. if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
  1402. devtoolsComponentUpdated(instance)
  1403. }
  1404. if (__DEV__) {
  1405. popWarningContext()
  1406. }
  1407. }
  1408. }, __DEV__ ? createDevEffectOptions(instance) : prodEffectOptions)
  1409. }
  1410. const updateComponentPreRender = (
  1411. instance: ComponentInternalInstance,
  1412. nextVNode: VNode,
  1413. optimized: boolean
  1414. ) => {
  1415. nextVNode.component = instance
  1416. const prevProps = instance.vnode.props
  1417. instance.vnode = nextVNode
  1418. instance.next = null
  1419. updateProps(instance, nextVNode.props, prevProps, optimized)
  1420. updateSlots(instance, nextVNode.children)
  1421. // props update may have triggered pre-flush watchers.
  1422. // flush them before the render update.
  1423. flushPreFlushCbs(undefined, instance.update)
  1424. }
  1425. const patchChildren: PatchChildrenFn = (
  1426. n1,
  1427. n2,
  1428. container,
  1429. anchor,
  1430. parentComponent,
  1431. parentSuspense,
  1432. isSVG,
  1433. optimized = false
  1434. ) => {
  1435. const c1 = n1 && n1.children
  1436. const prevShapeFlag = n1 ? n1.shapeFlag : 0
  1437. const c2 = n2.children
  1438. const { patchFlag, shapeFlag } = n2
  1439. // fast path
  1440. if (patchFlag > 0) {
  1441. if (patchFlag & PatchFlags.KEYED_FRAGMENT) {
  1442. // this could be either fully-keyed or mixed (some keyed some not)
  1443. // presence of patchFlag means children are guaranteed to be arrays
  1444. patchKeyedChildren(
  1445. c1 as VNode[],
  1446. c2 as VNodeArrayChildren,
  1447. container,
  1448. anchor,
  1449. parentComponent,
  1450. parentSuspense,
  1451. isSVG,
  1452. optimized
  1453. )
  1454. return
  1455. } else if (patchFlag & PatchFlags.UNKEYED_FRAGMENT) {
  1456. // unkeyed
  1457. patchUnkeyedChildren(
  1458. c1 as VNode[],
  1459. c2 as VNodeArrayChildren,
  1460. container,
  1461. anchor,
  1462. parentComponent,
  1463. parentSuspense,
  1464. isSVG,
  1465. optimized
  1466. )
  1467. return
  1468. }
  1469. }
  1470. // children has 3 possibilities: text, array or no children.
  1471. if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
  1472. // text children fast path
  1473. if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1474. unmountChildren(c1 as VNode[], parentComponent, parentSuspense)
  1475. }
  1476. if (c2 !== c1) {
  1477. hostSetElementText(container, c2 as string)
  1478. }
  1479. } else {
  1480. if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1481. // prev children was array
  1482. if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1483. // two arrays, cannot assume anything, do full diff
  1484. patchKeyedChildren(
  1485. c1 as VNode[],
  1486. c2 as VNodeArrayChildren,
  1487. container,
  1488. anchor,
  1489. parentComponent,
  1490. parentSuspense,
  1491. isSVG,
  1492. optimized
  1493. )
  1494. } else {
  1495. // no new children, just unmount old
  1496. unmountChildren(c1 as VNode[], parentComponent, parentSuspense, true)
  1497. }
  1498. } else {
  1499. // prev children was text OR null
  1500. // new children is array OR null
  1501. if (prevShapeFlag & ShapeFlags.TEXT_CHILDREN) {
  1502. hostSetElementText(container, '')
  1503. }
  1504. // mount new if array
  1505. if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1506. mountChildren(
  1507. c2 as VNodeArrayChildren,
  1508. container,
  1509. anchor,
  1510. parentComponent,
  1511. parentSuspense,
  1512. isSVG,
  1513. optimized
  1514. )
  1515. }
  1516. }
  1517. }
  1518. }
  1519. const patchUnkeyedChildren = (
  1520. c1: VNode[],
  1521. c2: VNodeArrayChildren,
  1522. container: RendererElement,
  1523. anchor: RendererNode | null,
  1524. parentComponent: ComponentInternalInstance | null,
  1525. parentSuspense: SuspenseBoundary | null,
  1526. isSVG: boolean,
  1527. optimized: boolean
  1528. ) => {
  1529. c1 = c1 || EMPTY_ARR
  1530. c2 = c2 || EMPTY_ARR
  1531. const oldLength = c1.length
  1532. const newLength = c2.length
  1533. const commonLength = Math.min(oldLength, newLength)
  1534. let i
  1535. for (i = 0; i < commonLength; i++) {
  1536. const nextChild = (c2[i] = optimized
  1537. ? cloneIfMounted(c2[i] as VNode)
  1538. : normalizeVNode(c2[i]))
  1539. patch(
  1540. c1[i],
  1541. nextChild,
  1542. container,
  1543. null,
  1544. parentComponent,
  1545. parentSuspense,
  1546. isSVG,
  1547. optimized
  1548. )
  1549. }
  1550. if (oldLength > newLength) {
  1551. // remove old
  1552. unmountChildren(
  1553. c1,
  1554. parentComponent,
  1555. parentSuspense,
  1556. true,
  1557. false,
  1558. commonLength
  1559. )
  1560. } else {
  1561. // mount new
  1562. mountChildren(
  1563. c2,
  1564. container,
  1565. anchor,
  1566. parentComponent,
  1567. parentSuspense,
  1568. isSVG,
  1569. optimized,
  1570. commonLength
  1571. )
  1572. }
  1573. }
  1574. // can be all-keyed or mixed
  1575. const patchKeyedChildren = (
  1576. c1: VNode[],
  1577. c2: VNodeArrayChildren,
  1578. container: RendererElement,
  1579. parentAnchor: RendererNode | null,
  1580. parentComponent: ComponentInternalInstance | null,
  1581. parentSuspense: SuspenseBoundary | null,
  1582. isSVG: boolean,
  1583. optimized: boolean
  1584. ) => {
  1585. let i = 0
  1586. const l2 = c2.length
  1587. let e1 = c1.length - 1 // prev ending index
  1588. let e2 = l2 - 1 // next ending index
  1589. // 1. sync from start
  1590. // (a b) c
  1591. // (a b) d e
  1592. while (i <= e1 && i <= e2) {
  1593. const n1 = c1[i]
  1594. const n2 = (c2[i] = optimized
  1595. ? cloneIfMounted(c2[i] as VNode)
  1596. : normalizeVNode(c2[i]))
  1597. if (isSameVNodeType(n1, n2)) {
  1598. patch(
  1599. n1,
  1600. n2,
  1601. container,
  1602. null,
  1603. parentComponent,
  1604. parentSuspense,
  1605. isSVG,
  1606. optimized
  1607. )
  1608. } else {
  1609. break
  1610. }
  1611. i++
  1612. }
  1613. // 2. sync from end
  1614. // a (b c)
  1615. // d e (b c)
  1616. while (i <= e1 && i <= e2) {
  1617. const n1 = c1[e1]
  1618. const n2 = (c2[e2] = optimized
  1619. ? cloneIfMounted(c2[e2] as VNode)
  1620. : normalizeVNode(c2[e2]))
  1621. if (isSameVNodeType(n1, n2)) {
  1622. patch(
  1623. n1,
  1624. n2,
  1625. container,
  1626. null,
  1627. parentComponent,
  1628. parentSuspense,
  1629. isSVG,
  1630. optimized
  1631. )
  1632. } else {
  1633. break
  1634. }
  1635. e1--
  1636. e2--
  1637. }
  1638. // 3. common sequence + mount
  1639. // (a b)
  1640. // (a b) c
  1641. // i = 2, e1 = 1, e2 = 2
  1642. // (a b)
  1643. // c (a b)
  1644. // i = 0, e1 = -1, e2 = 0
  1645. if (i > e1) {
  1646. if (i <= e2) {
  1647. const nextPos = e2 + 1
  1648. const anchor = nextPos < l2 ? (c2[nextPos] as VNode).el : parentAnchor
  1649. while (i <= e2) {
  1650. patch(
  1651. null,
  1652. (c2[i] = optimized
  1653. ? cloneIfMounted(c2[i] as VNode)
  1654. : normalizeVNode(c2[i])),
  1655. container,
  1656. anchor,
  1657. parentComponent,
  1658. parentSuspense,
  1659. isSVG
  1660. )
  1661. i++
  1662. }
  1663. }
  1664. }
  1665. // 4. common sequence + unmount
  1666. // (a b) c
  1667. // (a b)
  1668. // i = 2, e1 = 2, e2 = 1
  1669. // a (b c)
  1670. // (b c)
  1671. // i = 0, e1 = 0, e2 = -1
  1672. else if (i > e2) {
  1673. while (i <= e1) {
  1674. unmount(c1[i], parentComponent, parentSuspense, true)
  1675. i++
  1676. }
  1677. }
  1678. // 5. unknown sequence
  1679. // [i ... e1 + 1]: a b [c d e] f g
  1680. // [i ... e2 + 1]: a b [e d c h] f g
  1681. // i = 2, e1 = 4, e2 = 5
  1682. else {
  1683. const s1 = i // prev starting index
  1684. const s2 = i // next starting index
  1685. // 5.1 build key:index map for newChildren
  1686. const keyToNewIndexMap: Map<string | number, number> = new Map()
  1687. for (i = s2; i <= e2; i++) {
  1688. const nextChild = (c2[i] = optimized
  1689. ? cloneIfMounted(c2[i] as VNode)
  1690. : normalizeVNode(c2[i]))
  1691. if (nextChild.key != null) {
  1692. if (__DEV__ && keyToNewIndexMap.has(nextChild.key)) {
  1693. warn(
  1694. `Duplicate keys found during update:`,
  1695. JSON.stringify(nextChild.key),
  1696. `Make sure keys are unique.`
  1697. )
  1698. }
  1699. keyToNewIndexMap.set(nextChild.key, i)
  1700. }
  1701. }
  1702. // 5.2 loop through old children left to be patched and try to patch
  1703. // matching nodes & remove nodes that are no longer present
  1704. let j
  1705. let patched = 0
  1706. const toBePatched = e2 - s2 + 1
  1707. let moved = false
  1708. // used to track whether any node has moved
  1709. let maxNewIndexSoFar = 0
  1710. // works as Map<newIndex, oldIndex>
  1711. // Note that oldIndex is offset by +1
  1712. // and oldIndex = 0 is a special value indicating the new node has
  1713. // no corresponding old node.
  1714. // used for determining longest stable subsequence
  1715. const newIndexToOldIndexMap = new Array(toBePatched)
  1716. for (i = 0; i < toBePatched; i++) newIndexToOldIndexMap[i] = 0
  1717. for (i = s1; i <= e1; i++) {
  1718. const prevChild = c1[i]
  1719. if (patched >= toBePatched) {
  1720. // all new children have been patched so this can only be a removal
  1721. unmount(prevChild, parentComponent, parentSuspense, true)
  1722. continue
  1723. }
  1724. let newIndex
  1725. if (prevChild.key != null) {
  1726. newIndex = keyToNewIndexMap.get(prevChild.key)
  1727. } else {
  1728. // key-less node, try to locate a key-less node of the same type
  1729. for (j = s2; j <= e2; j++) {
  1730. if (
  1731. newIndexToOldIndexMap[j - s2] === 0 &&
  1732. isSameVNodeType(prevChild, c2[j] as VNode)
  1733. ) {
  1734. newIndex = j
  1735. break
  1736. }
  1737. }
  1738. }
  1739. if (newIndex === undefined) {
  1740. unmount(prevChild, parentComponent, parentSuspense, true)
  1741. } else {
  1742. newIndexToOldIndexMap[newIndex - s2] = i + 1
  1743. if (newIndex >= maxNewIndexSoFar) {
  1744. maxNewIndexSoFar = newIndex
  1745. } else {
  1746. moved = true
  1747. }
  1748. patch(
  1749. prevChild,
  1750. c2[newIndex] as VNode,
  1751. container,
  1752. null,
  1753. parentComponent,
  1754. parentSuspense,
  1755. isSVG,
  1756. optimized
  1757. )
  1758. patched++
  1759. }
  1760. }
  1761. // 5.3 move and mount
  1762. // generate longest stable subsequence only when nodes have moved
  1763. const increasingNewIndexSequence = moved
  1764. ? getSequence(newIndexToOldIndexMap)
  1765. : EMPTY_ARR
  1766. j = increasingNewIndexSequence.length - 1
  1767. // looping backwards so that we can use last patched node as anchor
  1768. for (i = toBePatched - 1; i >= 0; i--) {
  1769. const nextIndex = s2 + i
  1770. const nextChild = c2[nextIndex] as VNode
  1771. const anchor =
  1772. nextIndex + 1 < l2 ? (c2[nextIndex + 1] as VNode).el : parentAnchor
  1773. if (newIndexToOldIndexMap[i] === 0) {
  1774. // mount new
  1775. patch(
  1776. null,
  1777. nextChild,
  1778. container,
  1779. anchor,
  1780. parentComponent,
  1781. parentSuspense,
  1782. isSVG
  1783. )
  1784. } else if (moved) {
  1785. // move if:
  1786. // There is no stable subsequence (e.g. a reverse)
  1787. // OR current node is not among the stable sequence
  1788. if (j < 0 || i !== increasingNewIndexSequence[j]) {
  1789. move(nextChild, container, anchor, MoveType.REORDER)
  1790. } else {
  1791. j--
  1792. }
  1793. }
  1794. }
  1795. }
  1796. }
  1797. const move: MoveFn = (
  1798. vnode,
  1799. container,
  1800. anchor,
  1801. moveType,
  1802. parentSuspense = null
  1803. ) => {
  1804. const { el, type, transition, children, shapeFlag } = vnode
  1805. if (shapeFlag & ShapeFlags.COMPONENT) {
  1806. move(vnode.component!.subTree, container, anchor, moveType)
  1807. return
  1808. }
  1809. if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
  1810. vnode.suspense!.move(container, anchor, moveType)
  1811. return
  1812. }
  1813. if (shapeFlag & ShapeFlags.TELEPORT) {
  1814. ;(type as typeof TeleportImpl).move(vnode, container, anchor, internals)
  1815. return
  1816. }
  1817. if (type === Fragment) {
  1818. hostInsert(el!, container, anchor)
  1819. for (let i = 0; i < (children as VNode[]).length; i++) {
  1820. move((children as VNode[])[i], container, anchor, moveType)
  1821. }
  1822. hostInsert(vnode.anchor!, container, anchor)
  1823. return
  1824. }
  1825. if (type === Static) {
  1826. moveStaticNode(vnode, container, anchor)
  1827. return
  1828. }
  1829. // single nodes
  1830. const needTransition =
  1831. moveType !== MoveType.REORDER &&
  1832. shapeFlag & ShapeFlags.ELEMENT &&
  1833. transition
  1834. if (needTransition) {
  1835. if (moveType === MoveType.ENTER) {
  1836. transition!.beforeEnter(el!)
  1837. hostInsert(el!, container, anchor)
  1838. queuePostRenderEffect(() => transition!.enter(el!), parentSuspense)
  1839. } else {
  1840. const { leave, delayLeave, afterLeave } = transition!
  1841. const remove = () => hostInsert(el!, container, anchor)
  1842. const performLeave = () => {
  1843. leave(el!, () => {
  1844. remove()
  1845. afterLeave && afterLeave()
  1846. })
  1847. }
  1848. if (delayLeave) {
  1849. delayLeave(el!, remove, performLeave)
  1850. } else {
  1851. performLeave()
  1852. }
  1853. }
  1854. } else {
  1855. hostInsert(el!, container, anchor)
  1856. }
  1857. }
  1858. const unmount: UnmountFn = (
  1859. vnode,
  1860. parentComponent,
  1861. parentSuspense,
  1862. doRemove = false,
  1863. optimized = false
  1864. ) => {
  1865. const {
  1866. type,
  1867. props,
  1868. ref,
  1869. children,
  1870. dynamicChildren,
  1871. shapeFlag,
  1872. patchFlag,
  1873. dirs
  1874. } = vnode
  1875. // unset ref
  1876. if (ref != null) {
  1877. setRef(ref, null, parentSuspense, null)
  1878. }
  1879. if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
  1880. ;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode)
  1881. return
  1882. }
  1883. const shouldInvokeDirs = shapeFlag & ShapeFlags.ELEMENT && dirs
  1884. let vnodeHook: VNodeHook | undefined | null
  1885. if ((vnodeHook = props && props.onVnodeBeforeUnmount)) {
  1886. invokeVNodeHook(vnodeHook, parentComponent, vnode)
  1887. }
  1888. if (shapeFlag & ShapeFlags.COMPONENT) {
  1889. unmountComponent(vnode.component!, parentSuspense, doRemove)
  1890. } else {
  1891. if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
  1892. vnode.suspense!.unmount(parentSuspense, doRemove)
  1893. return
  1894. }
  1895. if (shouldInvokeDirs) {
  1896. invokeDirectiveHook(vnode, null, parentComponent, 'beforeUnmount')
  1897. }
  1898. if (
  1899. dynamicChildren &&
  1900. // #1153: fast path should not be taken for non-stable (v-for) fragments
  1901. (type !== Fragment ||
  1902. (patchFlag > 0 && patchFlag & PatchFlags.STABLE_FRAGMENT))
  1903. ) {
  1904. // fast path for block nodes: only need to unmount dynamic children.
  1905. unmountChildren(
  1906. dynamicChildren,
  1907. parentComponent,
  1908. parentSuspense,
  1909. false,
  1910. true
  1911. )
  1912. } else if (
  1913. (type === Fragment &&
  1914. (patchFlag & PatchFlags.KEYED_FRAGMENT ||
  1915. patchFlag & PatchFlags.UNKEYED_FRAGMENT)) ||
  1916. (!optimized && shapeFlag & ShapeFlags.ARRAY_CHILDREN)
  1917. ) {
  1918. unmountChildren(children as VNode[], parentComponent, parentSuspense)
  1919. }
  1920. // an unmounted teleport should always remove its children if not disabled
  1921. if (
  1922. shapeFlag & ShapeFlags.TELEPORT &&
  1923. (doRemove || !isTeleportDisabled(vnode.props))
  1924. ) {
  1925. ;(vnode.type as typeof TeleportImpl).remove(vnode, internals)
  1926. }
  1927. if (doRemove) {
  1928. remove(vnode)
  1929. }
  1930. }
  1931. if ((vnodeHook = props && props.onVnodeUnmounted) || shouldInvokeDirs) {
  1932. queuePostRenderEffect(() => {
  1933. vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode)
  1934. shouldInvokeDirs &&
  1935. invokeDirectiveHook(vnode, null, parentComponent, 'unmounted')
  1936. }, parentSuspense)
  1937. }
  1938. }
  1939. const remove: RemoveFn = vnode => {
  1940. const { type, el, anchor, transition } = vnode
  1941. if (type === Fragment) {
  1942. removeFragment(el!, anchor!)
  1943. return
  1944. }
  1945. if (type === Static) {
  1946. removeStaticNode(vnode)
  1947. return
  1948. }
  1949. const performRemove = () => {
  1950. hostRemove(el!)
  1951. if (transition && !transition.persisted && transition.afterLeave) {
  1952. transition.afterLeave()
  1953. }
  1954. }
  1955. if (
  1956. vnode.shapeFlag & ShapeFlags.ELEMENT &&
  1957. transition &&
  1958. !transition.persisted
  1959. ) {
  1960. const { leave, delayLeave } = transition
  1961. const performLeave = () => leave(el!, performRemove)
  1962. if (delayLeave) {
  1963. delayLeave(vnode.el!, performRemove, performLeave)
  1964. } else {
  1965. performLeave()
  1966. }
  1967. } else {
  1968. performRemove()
  1969. }
  1970. }
  1971. const removeFragment = (cur: RendererNode, end: RendererNode) => {
  1972. // For fragments, directly remove all contained DOM nodes.
  1973. // (fragment child nodes cannot have transition)
  1974. let next
  1975. while (cur !== end) {
  1976. next = hostNextSibling(cur)!
  1977. hostRemove(cur)
  1978. cur = next
  1979. }
  1980. hostRemove(end)
  1981. }
  1982. const unmountComponent = (
  1983. instance: ComponentInternalInstance,
  1984. parentSuspense: SuspenseBoundary | null,
  1985. doRemove?: boolean
  1986. ) => {
  1987. if (__DEV__ && instance.type.__hmrId) {
  1988. unregisterHMR(instance)
  1989. }
  1990. const { bum, effects, update, subTree, um } = instance
  1991. // beforeUnmount hook
  1992. if (bum) {
  1993. invokeArrayFns(bum)
  1994. }
  1995. if (effects) {
  1996. for (let i = 0; i < effects.length; i++) {
  1997. stop(effects[i])
  1998. }
  1999. }
  2000. // update may be null if a component is unmounted before its async
  2001. // setup has resolved.
  2002. if (update) {
  2003. stop(update)
  2004. unmount(subTree, instance, parentSuspense, doRemove)
  2005. }
  2006. // unmounted hook
  2007. if (um) {
  2008. queuePostRenderEffect(um, parentSuspense)
  2009. }
  2010. queuePostRenderEffect(() => {
  2011. instance.isUnmounted = true
  2012. }, parentSuspense)
  2013. // A component with async dep inside a pending suspense is unmounted before
  2014. // its async dep resolves. This should remove the dep from the suspense, and
  2015. // cause the suspense to resolve immediately if that was the last dep.
  2016. if (
  2017. __FEATURE_SUSPENSE__ &&
  2018. parentSuspense &&
  2019. parentSuspense.pendingBranch &&
  2020. !parentSuspense.isUnmounted &&
  2021. instance.asyncDep &&
  2022. !instance.asyncResolved &&
  2023. instance.suspenseId === parentSuspense.pendingId
  2024. ) {
  2025. parentSuspense.deps--
  2026. if (parentSuspense.deps === 0) {
  2027. parentSuspense.resolve()
  2028. }
  2029. }
  2030. if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
  2031. devtoolsComponentRemoved(instance)
  2032. }
  2033. }
  2034. const unmountChildren: UnmountChildrenFn = (
  2035. children,
  2036. parentComponent,
  2037. parentSuspense,
  2038. doRemove = false,
  2039. optimized = false,
  2040. start = 0
  2041. ) => {
  2042. for (let i = start; i < children.length; i++) {
  2043. unmount(children[i], parentComponent, parentSuspense, doRemove, optimized)
  2044. }
  2045. }
  2046. const getNextHostNode: NextFn = vnode => {
  2047. if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
  2048. return getNextHostNode(vnode.component!.subTree)
  2049. }
  2050. if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
  2051. return vnode.suspense!.next()
  2052. }
  2053. return hostNextSibling((vnode.anchor || vnode.el)!)
  2054. }
  2055. const render: RootRenderFunction = (vnode, container) => {
  2056. if (vnode == null) {
  2057. if (container._vnode) {
  2058. unmount(container._vnode, null, null, true)
  2059. }
  2060. } else {
  2061. patch(container._vnode || null, vnode, container)
  2062. }
  2063. flushPostFlushCbs()
  2064. container._vnode = vnode
  2065. }
  2066. const internals: RendererInternals = {
  2067. p: patch,
  2068. um: unmount,
  2069. m: move,
  2070. r: remove,
  2071. mt: mountComponent,
  2072. mc: mountChildren,
  2073. pc: patchChildren,
  2074. pbc: patchBlockChildren,
  2075. n: getNextHostNode,
  2076. o: options
  2077. }
  2078. let hydrate: ReturnType<typeof createHydrationFunctions>[0] | undefined
  2079. let hydrateNode: ReturnType<typeof createHydrationFunctions>[1] | undefined
  2080. if (createHydrationFns) {
  2081. ;[hydrate, hydrateNode] = createHydrationFns(internals as RendererInternals<
  2082. Node,
  2083. Element
  2084. >)
  2085. }
  2086. return {
  2087. render,
  2088. hydrate,
  2089. createApp: createAppAPI(render, hydrate)
  2090. }
  2091. }
  2092. export function invokeVNodeHook(
  2093. hook: VNodeHook,
  2094. instance: ComponentInternalInstance | null,
  2095. vnode: VNode,
  2096. prevVNode: VNode | null = null
  2097. ) {
  2098. callWithAsyncErrorHandling(hook, instance, ErrorCodes.VNODE_HOOK, [
  2099. vnode,
  2100. prevVNode
  2101. ])
  2102. }
  2103. /**
  2104. * #1156
  2105. * When a component is HMR-enabled, we need to make sure that all static nodes
  2106. * inside a block also inherit the DOM element from the previous tree so that
  2107. * HMR updates (which are full updates) can retrieve the element for patching.
  2108. *
  2109. * #2080
  2110. * Inside keyed `template` fragment static children, if a fragment is moved,
  2111. * the children will always moved so that need inherit el form previous nodes
  2112. * to ensure correct moved position.
  2113. */
  2114. export function traverseStaticChildren(n1: VNode, n2: VNode, shallow = false) {
  2115. const ch1 = n1.children
  2116. const ch2 = n2.children
  2117. if (isArray(ch1) && isArray(ch2)) {
  2118. for (let i = 0; i < ch1.length; i++) {
  2119. // this is only called in the optimized path so array children are
  2120. // guaranteed to be vnodes
  2121. const c1 = ch1[i] as VNode
  2122. let c2 = ch2[i] as VNode
  2123. if (c2.shapeFlag & ShapeFlags.ELEMENT && !c2.dynamicChildren) {
  2124. if (c2.patchFlag <= 0 || c2.patchFlag === PatchFlags.HYDRATE_EVENTS) {
  2125. c2 = ch2[i] = cloneIfMounted(ch2[i] as VNode)
  2126. c2.el = c1.el
  2127. }
  2128. if (!shallow) traverseStaticChildren(c1, c2)
  2129. }
  2130. // also inherit for comment nodes, but not placeholders (e.g. v-if which
  2131. // would have received .el during block patch)
  2132. if (__DEV__ && c2.type === Comment && !c2.el) {
  2133. c2.el = c1.el
  2134. }
  2135. }
  2136. }
  2137. }
  2138. // https://en.wikipedia.org/wiki/Longest_increasing_subsequence
  2139. function getSequence(arr: number[]): number[] {
  2140. const p = arr.slice()
  2141. const result = [0]
  2142. let i, j, u, v, c
  2143. const len = arr.length
  2144. for (i = 0; i < len; i++) {
  2145. const arrI = arr[i]
  2146. if (arrI !== 0) {
  2147. j = result[result.length - 1]
  2148. if (arr[j] < arrI) {
  2149. p[i] = j
  2150. result.push(i)
  2151. continue
  2152. }
  2153. u = 0
  2154. v = result.length - 1
  2155. while (u < v) {
  2156. c = ((u + v) / 2) | 0
  2157. if (arr[result[c]] < arrI) {
  2158. u = c + 1
  2159. } else {
  2160. v = c
  2161. }
  2162. }
  2163. if (arrI < arr[result[u]]) {
  2164. if (u > 0) {
  2165. p[i] = result[u - 1]
  2166. }
  2167. result[u] = i
  2168. }
  2169. }
  2170. }
  2171. u = result.length
  2172. v = result[u - 1]
  2173. while (u-- > 0) {
  2174. result[u] = v
  2175. v = p[v]
  2176. }
  2177. return result
  2178. }