renderer.ts 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  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. const scopedInitialVNode = initialVNode
  1319. queuePostRenderEffect(() => {
  1320. invokeVNodeHook(vnodeHook!, parent, scopedInitialVNode)
  1321. }, parentSuspense)
  1322. }
  1323. // activated hook for keep-alive roots.
  1324. // #1742 activated hook must be accessed after first render
  1325. // since the hook may be injected by a child keep-alive
  1326. const { a } = instance
  1327. if (
  1328. a &&
  1329. initialVNode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
  1330. ) {
  1331. queuePostRenderEffect(a, parentSuspense)
  1332. }
  1333. instance.isMounted = true
  1334. // #2458: deference mount-only object parameters to prevent memleaks
  1335. initialVNode = container = anchor = null as any
  1336. } else {
  1337. // updateComponent
  1338. // This is triggered by mutation of component's own state (next: null)
  1339. // OR parent calling processComponent (next: VNode)
  1340. let { next, bu, u, parent, vnode } = instance
  1341. let originNext = next
  1342. let vnodeHook: VNodeHook | null | undefined
  1343. if (__DEV__) {
  1344. pushWarningContext(next || instance.vnode)
  1345. }
  1346. if (next) {
  1347. next.el = vnode.el
  1348. updateComponentPreRender(instance, next, optimized)
  1349. } else {
  1350. next = vnode
  1351. }
  1352. // beforeUpdate hook
  1353. if (bu) {
  1354. invokeArrayFns(bu)
  1355. }
  1356. // onVnodeBeforeUpdate
  1357. if ((vnodeHook = next.props && next.props.onVnodeBeforeUpdate)) {
  1358. invokeVNodeHook(vnodeHook, parent, next, vnode)
  1359. }
  1360. // render
  1361. if (__DEV__) {
  1362. startMeasure(instance, `render`)
  1363. }
  1364. const nextTree = renderComponentRoot(instance)
  1365. if (__DEV__) {
  1366. endMeasure(instance, `render`)
  1367. }
  1368. const prevTree = instance.subTree
  1369. instance.subTree = nextTree
  1370. if (__DEV__) {
  1371. startMeasure(instance, `patch`)
  1372. }
  1373. patch(
  1374. prevTree,
  1375. nextTree,
  1376. // parent may have changed if it's in a teleport
  1377. hostParentNode(prevTree.el!)!,
  1378. // anchor may have changed if it's in a fragment
  1379. getNextHostNode(prevTree),
  1380. instance,
  1381. parentSuspense,
  1382. isSVG
  1383. )
  1384. if (__DEV__) {
  1385. endMeasure(instance, `patch`)
  1386. }
  1387. next.el = nextTree.el
  1388. if (originNext === null) {
  1389. // self-triggered update. In case of HOC, update parent component
  1390. // vnode el. HOC is indicated by parent instance's subTree pointing
  1391. // to child component's vnode
  1392. updateHOCHostEl(instance, nextTree.el)
  1393. }
  1394. // updated hook
  1395. if (u) {
  1396. queuePostRenderEffect(u, parentSuspense)
  1397. }
  1398. // onVnodeUpdated
  1399. if ((vnodeHook = next.props && next.props.onVnodeUpdated)) {
  1400. queuePostRenderEffect(() => {
  1401. invokeVNodeHook(vnodeHook!, parent, next!, vnode)
  1402. }, parentSuspense)
  1403. }
  1404. if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
  1405. devtoolsComponentUpdated(instance)
  1406. }
  1407. if (__DEV__) {
  1408. popWarningContext()
  1409. }
  1410. }
  1411. }, __DEV__ ? createDevEffectOptions(instance) : prodEffectOptions)
  1412. }
  1413. const updateComponentPreRender = (
  1414. instance: ComponentInternalInstance,
  1415. nextVNode: VNode,
  1416. optimized: boolean
  1417. ) => {
  1418. nextVNode.component = instance
  1419. const prevProps = instance.vnode.props
  1420. instance.vnode = nextVNode
  1421. instance.next = null
  1422. updateProps(instance, nextVNode.props, prevProps, optimized)
  1423. updateSlots(instance, nextVNode.children)
  1424. // props update may have triggered pre-flush watchers.
  1425. // flush them before the render update.
  1426. flushPreFlushCbs(undefined, instance.update)
  1427. }
  1428. const patchChildren: PatchChildrenFn = (
  1429. n1,
  1430. n2,
  1431. container,
  1432. anchor,
  1433. parentComponent,
  1434. parentSuspense,
  1435. isSVG,
  1436. optimized = false
  1437. ) => {
  1438. const c1 = n1 && n1.children
  1439. const prevShapeFlag = n1 ? n1.shapeFlag : 0
  1440. const c2 = n2.children
  1441. const { patchFlag, shapeFlag } = n2
  1442. // fast path
  1443. if (patchFlag > 0) {
  1444. if (patchFlag & PatchFlags.KEYED_FRAGMENT) {
  1445. // this could be either fully-keyed or mixed (some keyed some not)
  1446. // presence of patchFlag means children are guaranteed to be arrays
  1447. patchKeyedChildren(
  1448. c1 as VNode[],
  1449. c2 as VNodeArrayChildren,
  1450. container,
  1451. anchor,
  1452. parentComponent,
  1453. parentSuspense,
  1454. isSVG,
  1455. optimized
  1456. )
  1457. return
  1458. } else if (patchFlag & PatchFlags.UNKEYED_FRAGMENT) {
  1459. // unkeyed
  1460. patchUnkeyedChildren(
  1461. c1 as VNode[],
  1462. c2 as VNodeArrayChildren,
  1463. container,
  1464. anchor,
  1465. parentComponent,
  1466. parentSuspense,
  1467. isSVG,
  1468. optimized
  1469. )
  1470. return
  1471. }
  1472. }
  1473. // children has 3 possibilities: text, array or no children.
  1474. if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
  1475. // text children fast path
  1476. if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1477. unmountChildren(c1 as VNode[], parentComponent, parentSuspense)
  1478. }
  1479. if (c2 !== c1) {
  1480. hostSetElementText(container, c2 as string)
  1481. }
  1482. } else {
  1483. if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1484. // prev children was array
  1485. if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1486. // two arrays, cannot assume anything, do full diff
  1487. patchKeyedChildren(
  1488. c1 as VNode[],
  1489. c2 as VNodeArrayChildren,
  1490. container,
  1491. anchor,
  1492. parentComponent,
  1493. parentSuspense,
  1494. isSVG,
  1495. optimized
  1496. )
  1497. } else {
  1498. // no new children, just unmount old
  1499. unmountChildren(c1 as VNode[], parentComponent, parentSuspense, true)
  1500. }
  1501. } else {
  1502. // prev children was text OR null
  1503. // new children is array OR null
  1504. if (prevShapeFlag & ShapeFlags.TEXT_CHILDREN) {
  1505. hostSetElementText(container, '')
  1506. }
  1507. // mount new if array
  1508. if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1509. mountChildren(
  1510. c2 as VNodeArrayChildren,
  1511. container,
  1512. anchor,
  1513. parentComponent,
  1514. parentSuspense,
  1515. isSVG,
  1516. optimized
  1517. )
  1518. }
  1519. }
  1520. }
  1521. }
  1522. const patchUnkeyedChildren = (
  1523. c1: VNode[],
  1524. c2: VNodeArrayChildren,
  1525. container: RendererElement,
  1526. anchor: RendererNode | null,
  1527. parentComponent: ComponentInternalInstance | null,
  1528. parentSuspense: SuspenseBoundary | null,
  1529. isSVG: boolean,
  1530. optimized: boolean
  1531. ) => {
  1532. c1 = c1 || EMPTY_ARR
  1533. c2 = c2 || EMPTY_ARR
  1534. const oldLength = c1.length
  1535. const newLength = c2.length
  1536. const commonLength = Math.min(oldLength, newLength)
  1537. let i
  1538. for (i = 0; i < commonLength; i++) {
  1539. const nextChild = (c2[i] = optimized
  1540. ? cloneIfMounted(c2[i] as VNode)
  1541. : normalizeVNode(c2[i]))
  1542. patch(
  1543. c1[i],
  1544. nextChild,
  1545. container,
  1546. null,
  1547. parentComponent,
  1548. parentSuspense,
  1549. isSVG,
  1550. optimized
  1551. )
  1552. }
  1553. if (oldLength > newLength) {
  1554. // remove old
  1555. unmountChildren(
  1556. c1,
  1557. parentComponent,
  1558. parentSuspense,
  1559. true,
  1560. false,
  1561. commonLength
  1562. )
  1563. } else {
  1564. // mount new
  1565. mountChildren(
  1566. c2,
  1567. container,
  1568. anchor,
  1569. parentComponent,
  1570. parentSuspense,
  1571. isSVG,
  1572. optimized,
  1573. commonLength
  1574. )
  1575. }
  1576. }
  1577. // can be all-keyed or mixed
  1578. const patchKeyedChildren = (
  1579. c1: VNode[],
  1580. c2: VNodeArrayChildren,
  1581. container: RendererElement,
  1582. parentAnchor: RendererNode | null,
  1583. parentComponent: ComponentInternalInstance | null,
  1584. parentSuspense: SuspenseBoundary | null,
  1585. isSVG: boolean,
  1586. optimized: boolean
  1587. ) => {
  1588. let i = 0
  1589. const l2 = c2.length
  1590. let e1 = c1.length - 1 // prev ending index
  1591. let e2 = l2 - 1 // next ending index
  1592. // 1. sync from start
  1593. // (a b) c
  1594. // (a b) d e
  1595. while (i <= e1 && i <= e2) {
  1596. const n1 = c1[i]
  1597. const n2 = (c2[i] = optimized
  1598. ? cloneIfMounted(c2[i] as VNode)
  1599. : normalizeVNode(c2[i]))
  1600. if (isSameVNodeType(n1, n2)) {
  1601. patch(
  1602. n1,
  1603. n2,
  1604. container,
  1605. null,
  1606. parentComponent,
  1607. parentSuspense,
  1608. isSVG,
  1609. optimized
  1610. )
  1611. } else {
  1612. break
  1613. }
  1614. i++
  1615. }
  1616. // 2. sync from end
  1617. // a (b c)
  1618. // d e (b c)
  1619. while (i <= e1 && i <= e2) {
  1620. const n1 = c1[e1]
  1621. const n2 = (c2[e2] = optimized
  1622. ? cloneIfMounted(c2[e2] as VNode)
  1623. : normalizeVNode(c2[e2]))
  1624. if (isSameVNodeType(n1, n2)) {
  1625. patch(
  1626. n1,
  1627. n2,
  1628. container,
  1629. null,
  1630. parentComponent,
  1631. parentSuspense,
  1632. isSVG,
  1633. optimized
  1634. )
  1635. } else {
  1636. break
  1637. }
  1638. e1--
  1639. e2--
  1640. }
  1641. // 3. common sequence + mount
  1642. // (a b)
  1643. // (a b) c
  1644. // i = 2, e1 = 1, e2 = 2
  1645. // (a b)
  1646. // c (a b)
  1647. // i = 0, e1 = -1, e2 = 0
  1648. if (i > e1) {
  1649. if (i <= e2) {
  1650. const nextPos = e2 + 1
  1651. const anchor = nextPos < l2 ? (c2[nextPos] as VNode).el : parentAnchor
  1652. while (i <= e2) {
  1653. patch(
  1654. null,
  1655. (c2[i] = optimized
  1656. ? cloneIfMounted(c2[i] as VNode)
  1657. : normalizeVNode(c2[i])),
  1658. container,
  1659. anchor,
  1660. parentComponent,
  1661. parentSuspense,
  1662. isSVG
  1663. )
  1664. i++
  1665. }
  1666. }
  1667. }
  1668. // 4. common sequence + unmount
  1669. // (a b) c
  1670. // (a b)
  1671. // i = 2, e1 = 2, e2 = 1
  1672. // a (b c)
  1673. // (b c)
  1674. // i = 0, e1 = 0, e2 = -1
  1675. else if (i > e2) {
  1676. while (i <= e1) {
  1677. unmount(c1[i], parentComponent, parentSuspense, true)
  1678. i++
  1679. }
  1680. }
  1681. // 5. unknown sequence
  1682. // [i ... e1 + 1]: a b [c d e] f g
  1683. // [i ... e2 + 1]: a b [e d c h] f g
  1684. // i = 2, e1 = 4, e2 = 5
  1685. else {
  1686. const s1 = i // prev starting index
  1687. const s2 = i // next starting index
  1688. // 5.1 build key:index map for newChildren
  1689. const keyToNewIndexMap: Map<string | number, number> = new Map()
  1690. for (i = s2; i <= e2; i++) {
  1691. const nextChild = (c2[i] = optimized
  1692. ? cloneIfMounted(c2[i] as VNode)
  1693. : normalizeVNode(c2[i]))
  1694. if (nextChild.key != null) {
  1695. if (__DEV__ && keyToNewIndexMap.has(nextChild.key)) {
  1696. warn(
  1697. `Duplicate keys found during update:`,
  1698. JSON.stringify(nextChild.key),
  1699. `Make sure keys are unique.`
  1700. )
  1701. }
  1702. keyToNewIndexMap.set(nextChild.key, i)
  1703. }
  1704. }
  1705. // 5.2 loop through old children left to be patched and try to patch
  1706. // matching nodes & remove nodes that are no longer present
  1707. let j
  1708. let patched = 0
  1709. const toBePatched = e2 - s2 + 1
  1710. let moved = false
  1711. // used to track whether any node has moved
  1712. let maxNewIndexSoFar = 0
  1713. // works as Map<newIndex, oldIndex>
  1714. // Note that oldIndex is offset by +1
  1715. // and oldIndex = 0 is a special value indicating the new node has
  1716. // no corresponding old node.
  1717. // used for determining longest stable subsequence
  1718. const newIndexToOldIndexMap = new Array(toBePatched)
  1719. for (i = 0; i < toBePatched; i++) newIndexToOldIndexMap[i] = 0
  1720. for (i = s1; i <= e1; i++) {
  1721. const prevChild = c1[i]
  1722. if (patched >= toBePatched) {
  1723. // all new children have been patched so this can only be a removal
  1724. unmount(prevChild, parentComponent, parentSuspense, true)
  1725. continue
  1726. }
  1727. let newIndex
  1728. if (prevChild.key != null) {
  1729. newIndex = keyToNewIndexMap.get(prevChild.key)
  1730. } else {
  1731. // key-less node, try to locate a key-less node of the same type
  1732. for (j = s2; j <= e2; j++) {
  1733. if (
  1734. newIndexToOldIndexMap[j - s2] === 0 &&
  1735. isSameVNodeType(prevChild, c2[j] as VNode)
  1736. ) {
  1737. newIndex = j
  1738. break
  1739. }
  1740. }
  1741. }
  1742. if (newIndex === undefined) {
  1743. unmount(prevChild, parentComponent, parentSuspense, true)
  1744. } else {
  1745. newIndexToOldIndexMap[newIndex - s2] = i + 1
  1746. if (newIndex >= maxNewIndexSoFar) {
  1747. maxNewIndexSoFar = newIndex
  1748. } else {
  1749. moved = true
  1750. }
  1751. patch(
  1752. prevChild,
  1753. c2[newIndex] as VNode,
  1754. container,
  1755. null,
  1756. parentComponent,
  1757. parentSuspense,
  1758. isSVG,
  1759. optimized
  1760. )
  1761. patched++
  1762. }
  1763. }
  1764. // 5.3 move and mount
  1765. // generate longest stable subsequence only when nodes have moved
  1766. const increasingNewIndexSequence = moved
  1767. ? getSequence(newIndexToOldIndexMap)
  1768. : EMPTY_ARR
  1769. j = increasingNewIndexSequence.length - 1
  1770. // looping backwards so that we can use last patched node as anchor
  1771. for (i = toBePatched - 1; i >= 0; i--) {
  1772. const nextIndex = s2 + i
  1773. const nextChild = c2[nextIndex] as VNode
  1774. const anchor =
  1775. nextIndex + 1 < l2 ? (c2[nextIndex + 1] as VNode).el : parentAnchor
  1776. if (newIndexToOldIndexMap[i] === 0) {
  1777. // mount new
  1778. patch(
  1779. null,
  1780. nextChild,
  1781. container,
  1782. anchor,
  1783. parentComponent,
  1784. parentSuspense,
  1785. isSVG
  1786. )
  1787. } else if (moved) {
  1788. // move if:
  1789. // There is no stable subsequence (e.g. a reverse)
  1790. // OR current node is not among the stable sequence
  1791. if (j < 0 || i !== increasingNewIndexSequence[j]) {
  1792. move(nextChild, container, anchor, MoveType.REORDER)
  1793. } else {
  1794. j--
  1795. }
  1796. }
  1797. }
  1798. }
  1799. }
  1800. const move: MoveFn = (
  1801. vnode,
  1802. container,
  1803. anchor,
  1804. moveType,
  1805. parentSuspense = null
  1806. ) => {
  1807. const { el, type, transition, children, shapeFlag } = vnode
  1808. if (shapeFlag & ShapeFlags.COMPONENT) {
  1809. move(vnode.component!.subTree, container, anchor, moveType)
  1810. return
  1811. }
  1812. if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
  1813. vnode.suspense!.move(container, anchor, moveType)
  1814. return
  1815. }
  1816. if (shapeFlag & ShapeFlags.TELEPORT) {
  1817. ;(type as typeof TeleportImpl).move(vnode, container, anchor, internals)
  1818. return
  1819. }
  1820. if (type === Fragment) {
  1821. hostInsert(el!, container, anchor)
  1822. for (let i = 0; i < (children as VNode[]).length; i++) {
  1823. move((children as VNode[])[i], container, anchor, moveType)
  1824. }
  1825. hostInsert(vnode.anchor!, container, anchor)
  1826. return
  1827. }
  1828. if (type === Static) {
  1829. moveStaticNode(vnode, container, anchor)
  1830. return
  1831. }
  1832. // single nodes
  1833. const needTransition =
  1834. moveType !== MoveType.REORDER &&
  1835. shapeFlag & ShapeFlags.ELEMENT &&
  1836. transition
  1837. if (needTransition) {
  1838. if (moveType === MoveType.ENTER) {
  1839. transition!.beforeEnter(el!)
  1840. hostInsert(el!, container, anchor)
  1841. queuePostRenderEffect(() => transition!.enter(el!), parentSuspense)
  1842. } else {
  1843. const { leave, delayLeave, afterLeave } = transition!
  1844. const remove = () => hostInsert(el!, container, anchor)
  1845. const performLeave = () => {
  1846. leave(el!, () => {
  1847. remove()
  1848. afterLeave && afterLeave()
  1849. })
  1850. }
  1851. if (delayLeave) {
  1852. delayLeave(el!, remove, performLeave)
  1853. } else {
  1854. performLeave()
  1855. }
  1856. }
  1857. } else {
  1858. hostInsert(el!, container, anchor)
  1859. }
  1860. }
  1861. const unmount: UnmountFn = (
  1862. vnode,
  1863. parentComponent,
  1864. parentSuspense,
  1865. doRemove = false,
  1866. optimized = false
  1867. ) => {
  1868. const {
  1869. type,
  1870. props,
  1871. ref,
  1872. children,
  1873. dynamicChildren,
  1874. shapeFlag,
  1875. patchFlag,
  1876. dirs
  1877. } = vnode
  1878. // unset ref
  1879. if (ref != null) {
  1880. setRef(ref, null, parentSuspense, null)
  1881. }
  1882. if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
  1883. ;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode)
  1884. return
  1885. }
  1886. const shouldInvokeDirs = shapeFlag & ShapeFlags.ELEMENT && dirs
  1887. let vnodeHook: VNodeHook | undefined | null
  1888. if ((vnodeHook = props && props.onVnodeBeforeUnmount)) {
  1889. invokeVNodeHook(vnodeHook, parentComponent, vnode)
  1890. }
  1891. if (shapeFlag & ShapeFlags.COMPONENT) {
  1892. unmountComponent(vnode.component!, parentSuspense, doRemove)
  1893. } else {
  1894. if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
  1895. vnode.suspense!.unmount(parentSuspense, doRemove)
  1896. return
  1897. }
  1898. if (shouldInvokeDirs) {
  1899. invokeDirectiveHook(vnode, null, parentComponent, 'beforeUnmount')
  1900. }
  1901. if (
  1902. dynamicChildren &&
  1903. // #1153: fast path should not be taken for non-stable (v-for) fragments
  1904. (type !== Fragment ||
  1905. (patchFlag > 0 && patchFlag & PatchFlags.STABLE_FRAGMENT))
  1906. ) {
  1907. // fast path for block nodes: only need to unmount dynamic children.
  1908. unmountChildren(
  1909. dynamicChildren,
  1910. parentComponent,
  1911. parentSuspense,
  1912. false,
  1913. true
  1914. )
  1915. } else if (
  1916. (type === Fragment &&
  1917. (patchFlag & PatchFlags.KEYED_FRAGMENT ||
  1918. patchFlag & PatchFlags.UNKEYED_FRAGMENT)) ||
  1919. (!optimized && shapeFlag & ShapeFlags.ARRAY_CHILDREN)
  1920. ) {
  1921. unmountChildren(children as VNode[], parentComponent, parentSuspense)
  1922. }
  1923. // an unmounted teleport should always remove its children if not disabled
  1924. if (
  1925. shapeFlag & ShapeFlags.TELEPORT &&
  1926. (doRemove || !isTeleportDisabled(vnode.props))
  1927. ) {
  1928. ;(vnode.type as typeof TeleportImpl).remove(vnode, internals)
  1929. }
  1930. if (doRemove) {
  1931. remove(vnode)
  1932. }
  1933. }
  1934. if ((vnodeHook = props && props.onVnodeUnmounted) || shouldInvokeDirs) {
  1935. queuePostRenderEffect(() => {
  1936. vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode)
  1937. shouldInvokeDirs &&
  1938. invokeDirectiveHook(vnode, null, parentComponent, 'unmounted')
  1939. }, parentSuspense)
  1940. }
  1941. }
  1942. const remove: RemoveFn = vnode => {
  1943. const { type, el, anchor, transition } = vnode
  1944. if (type === Fragment) {
  1945. removeFragment(el!, anchor!)
  1946. return
  1947. }
  1948. if (type === Static) {
  1949. removeStaticNode(vnode)
  1950. return
  1951. }
  1952. const performRemove = () => {
  1953. hostRemove(el!)
  1954. if (transition && !transition.persisted && transition.afterLeave) {
  1955. transition.afterLeave()
  1956. }
  1957. }
  1958. if (
  1959. vnode.shapeFlag & ShapeFlags.ELEMENT &&
  1960. transition &&
  1961. !transition.persisted
  1962. ) {
  1963. const { leave, delayLeave } = transition
  1964. const performLeave = () => leave(el!, performRemove)
  1965. if (delayLeave) {
  1966. delayLeave(vnode.el!, performRemove, performLeave)
  1967. } else {
  1968. performLeave()
  1969. }
  1970. } else {
  1971. performRemove()
  1972. }
  1973. }
  1974. const removeFragment = (cur: RendererNode, end: RendererNode) => {
  1975. // For fragments, directly remove all contained DOM nodes.
  1976. // (fragment child nodes cannot have transition)
  1977. let next
  1978. while (cur !== end) {
  1979. next = hostNextSibling(cur)!
  1980. hostRemove(cur)
  1981. cur = next
  1982. }
  1983. hostRemove(end)
  1984. }
  1985. const unmountComponent = (
  1986. instance: ComponentInternalInstance,
  1987. parentSuspense: SuspenseBoundary | null,
  1988. doRemove?: boolean
  1989. ) => {
  1990. if (__DEV__ && instance.type.__hmrId) {
  1991. unregisterHMR(instance)
  1992. }
  1993. const { bum, effects, update, subTree, um } = instance
  1994. // beforeUnmount hook
  1995. if (bum) {
  1996. invokeArrayFns(bum)
  1997. }
  1998. if (effects) {
  1999. for (let i = 0; i < effects.length; i++) {
  2000. stop(effects[i])
  2001. }
  2002. }
  2003. // update may be null if a component is unmounted before its async
  2004. // setup has resolved.
  2005. if (update) {
  2006. stop(update)
  2007. unmount(subTree, instance, parentSuspense, doRemove)
  2008. }
  2009. // unmounted hook
  2010. if (um) {
  2011. queuePostRenderEffect(um, parentSuspense)
  2012. }
  2013. queuePostRenderEffect(() => {
  2014. instance.isUnmounted = true
  2015. }, parentSuspense)
  2016. // A component with async dep inside a pending suspense is unmounted before
  2017. // its async dep resolves. This should remove the dep from the suspense, and
  2018. // cause the suspense to resolve immediately if that was the last dep.
  2019. if (
  2020. __FEATURE_SUSPENSE__ &&
  2021. parentSuspense &&
  2022. parentSuspense.pendingBranch &&
  2023. !parentSuspense.isUnmounted &&
  2024. instance.asyncDep &&
  2025. !instance.asyncResolved &&
  2026. instance.suspenseId === parentSuspense.pendingId
  2027. ) {
  2028. parentSuspense.deps--
  2029. if (parentSuspense.deps === 0) {
  2030. parentSuspense.resolve()
  2031. }
  2032. }
  2033. if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
  2034. devtoolsComponentRemoved(instance)
  2035. }
  2036. }
  2037. const unmountChildren: UnmountChildrenFn = (
  2038. children,
  2039. parentComponent,
  2040. parentSuspense,
  2041. doRemove = false,
  2042. optimized = false,
  2043. start = 0
  2044. ) => {
  2045. for (let i = start; i < children.length; i++) {
  2046. unmount(children[i], parentComponent, parentSuspense, doRemove, optimized)
  2047. }
  2048. }
  2049. const getNextHostNode: NextFn = vnode => {
  2050. if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
  2051. return getNextHostNode(vnode.component!.subTree)
  2052. }
  2053. if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
  2054. return vnode.suspense!.next()
  2055. }
  2056. return hostNextSibling((vnode.anchor || vnode.el)!)
  2057. }
  2058. const render: RootRenderFunction = (vnode, container) => {
  2059. if (vnode == null) {
  2060. if (container._vnode) {
  2061. unmount(container._vnode, null, null, true)
  2062. }
  2063. } else {
  2064. patch(container._vnode || null, vnode, container)
  2065. }
  2066. flushPostFlushCbs()
  2067. container._vnode = vnode
  2068. }
  2069. const internals: RendererInternals = {
  2070. p: patch,
  2071. um: unmount,
  2072. m: move,
  2073. r: remove,
  2074. mt: mountComponent,
  2075. mc: mountChildren,
  2076. pc: patchChildren,
  2077. pbc: patchBlockChildren,
  2078. n: getNextHostNode,
  2079. o: options
  2080. }
  2081. let hydrate: ReturnType<typeof createHydrationFunctions>[0] | undefined
  2082. let hydrateNode: ReturnType<typeof createHydrationFunctions>[1] | undefined
  2083. if (createHydrationFns) {
  2084. ;[hydrate, hydrateNode] = createHydrationFns(internals as RendererInternals<
  2085. Node,
  2086. Element
  2087. >)
  2088. }
  2089. return {
  2090. render,
  2091. hydrate,
  2092. createApp: createAppAPI(render, hydrate)
  2093. }
  2094. }
  2095. export function invokeVNodeHook(
  2096. hook: VNodeHook,
  2097. instance: ComponentInternalInstance | null,
  2098. vnode: VNode,
  2099. prevVNode: VNode | null = null
  2100. ) {
  2101. callWithAsyncErrorHandling(hook, instance, ErrorCodes.VNODE_HOOK, [
  2102. vnode,
  2103. prevVNode
  2104. ])
  2105. }
  2106. /**
  2107. * #1156
  2108. * When a component is HMR-enabled, we need to make sure that all static nodes
  2109. * inside a block also inherit the DOM element from the previous tree so that
  2110. * HMR updates (which are full updates) can retrieve the element for patching.
  2111. *
  2112. * #2080
  2113. * Inside keyed `template` fragment static children, if a fragment is moved,
  2114. * the children will always moved so that need inherit el form previous nodes
  2115. * to ensure correct moved position.
  2116. */
  2117. export function traverseStaticChildren(n1: VNode, n2: VNode, shallow = false) {
  2118. const ch1 = n1.children
  2119. const ch2 = n2.children
  2120. if (isArray(ch1) && isArray(ch2)) {
  2121. for (let i = 0; i < ch1.length; i++) {
  2122. // this is only called in the optimized path so array children are
  2123. // guaranteed to be vnodes
  2124. const c1 = ch1[i] as VNode
  2125. let c2 = ch2[i] as VNode
  2126. if (c2.shapeFlag & ShapeFlags.ELEMENT && !c2.dynamicChildren) {
  2127. if (c2.patchFlag <= 0 || c2.patchFlag === PatchFlags.HYDRATE_EVENTS) {
  2128. c2 = ch2[i] = cloneIfMounted(ch2[i] as VNode)
  2129. c2.el = c1.el
  2130. }
  2131. if (!shallow) traverseStaticChildren(c1, c2)
  2132. }
  2133. // also inherit for comment nodes, but not placeholders (e.g. v-if which
  2134. // would have received .el during block patch)
  2135. if (__DEV__ && c2.type === Comment && !c2.el) {
  2136. c2.el = c1.el
  2137. }
  2138. }
  2139. }
  2140. }
  2141. // https://en.wikipedia.org/wiki/Longest_increasing_subsequence
  2142. function getSequence(arr: number[]): number[] {
  2143. const p = arr.slice()
  2144. const result = [0]
  2145. let i, j, u, v, c
  2146. const len = arr.length
  2147. for (i = 0; i < len; i++) {
  2148. const arrI = arr[i]
  2149. if (arrI !== 0) {
  2150. j = result[result.length - 1]
  2151. if (arr[j] < arrI) {
  2152. p[i] = j
  2153. result.push(i)
  2154. continue
  2155. }
  2156. u = 0
  2157. v = result.length - 1
  2158. while (u < v) {
  2159. c = ((u + v) / 2) | 0
  2160. if (arr[result[c]] < arrI) {
  2161. u = c + 1
  2162. } else {
  2163. v = c
  2164. }
  2165. }
  2166. if (arrI < arr[result[u]]) {
  2167. if (u > 0) {
  2168. p[i] = result[u - 1]
  2169. }
  2170. result[u] = i
  2171. }
  2172. }
  2173. }
  2174. u = result.length
  2175. v = result[u - 1]
  2176. while (u-- > 0) {
  2177. result[u] = v
  2178. v = p[v]
  2179. }
  2180. return result
  2181. }