renderer.ts 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. import {
  2. Text,
  3. Fragment,
  4. Comment,
  5. Portal,
  6. normalizeVNode,
  7. VNode,
  8. VNodeChildren,
  9. createVNode,
  10. isSameVNodeType
  11. } from './vnode'
  12. import {
  13. ComponentInternalInstance,
  14. createComponentInstance,
  15. setupStatefulComponent,
  16. Component,
  17. Data
  18. } from './component'
  19. import {
  20. renderComponentRoot,
  21. shouldUpdateComponent,
  22. updateHOCHostEl
  23. } from './componentRenderUtils'
  24. import {
  25. isString,
  26. EMPTY_OBJ,
  27. EMPTY_ARR,
  28. isReservedProp,
  29. isFunction,
  30. PatchFlags
  31. } from '@vue/shared'
  32. import { queueJob, queuePostFlushCb, flushPostFlushCbs } from './scheduler'
  33. import {
  34. effect,
  35. stop,
  36. ReactiveEffectOptions,
  37. isRef,
  38. Ref,
  39. toRaw,
  40. DebuggerEvent
  41. } from '@vue/reactivity'
  42. import { resolveProps } from './componentProps'
  43. import { resolveSlots } from './componentSlots'
  44. import { ShapeFlags } from './shapeFlags'
  45. import { pushWarningContext, popWarningContext, warn } from './warning'
  46. import { invokeDirectiveHook } from './directives'
  47. import { ComponentPublicInstance } from './componentProxy'
  48. import { App, createAppAPI } from './apiApp'
  49. import {
  50. SuspenseBoundary,
  51. queueEffectWithSuspense,
  52. SuspenseImpl
  53. } from './components/Suspense'
  54. import { ErrorCodes, callWithErrorHandling } from './errorHandling'
  55. import { KeepAliveSink, isKeepAlive } from './components/KeepAlive'
  56. import { registerHMR, unregisterHMR } from './hmr'
  57. export interface RendererOptions<HostNode = any, HostElement = any> {
  58. patchProp(
  59. el: HostElement,
  60. key: string,
  61. value: any,
  62. oldValue: any,
  63. isSVG: boolean,
  64. prevChildren?: VNode<HostNode, HostElement>[],
  65. parentComponent?: ComponentInternalInstance | null,
  66. parentSuspense?: SuspenseBoundary<HostNode, HostElement> | null,
  67. unmountChildren?: (
  68. children: VNode<HostNode, HostElement>[],
  69. parentComponent: ComponentInternalInstance | null,
  70. parentSuspense: SuspenseBoundary<HostNode, HostElement> | null
  71. ) => void
  72. ): void
  73. insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void
  74. remove(el: HostNode): void
  75. createElement(type: string, isSVG?: boolean): HostElement
  76. createText(text: string): HostNode
  77. createComment(text: string): HostNode
  78. setText(node: HostNode, text: string): void
  79. setElementText(node: HostElement, text: string): void
  80. parentNode(node: HostNode): HostElement | null
  81. nextSibling(node: HostNode): HostNode | null
  82. querySelector(selector: string): HostElement | null
  83. }
  84. export type RootRenderFunction<HostNode, HostElement> = (
  85. vnode: VNode<HostNode, HostElement> | null,
  86. dom: HostElement
  87. ) => void
  88. // An object exposing the internals of a renderer, passed to tree-shakeable
  89. // features so that they can be decoupled from this file.
  90. export interface RendererInternals<HostNode = any, HostElement = any> {
  91. patch: (
  92. n1: VNode<HostNode, HostElement> | null, // null means this is a mount
  93. n2: VNode<HostNode, HostElement>,
  94. container: HostElement,
  95. anchor?: HostNode | null,
  96. parentComponent?: ComponentInternalInstance | null,
  97. parentSuspense?: SuspenseBoundary<HostNode, HostElement> | null,
  98. isSVG?: boolean,
  99. optimized?: boolean
  100. ) => void
  101. unmount: (
  102. vnode: VNode<HostNode, HostElement>,
  103. parentComponent: ComponentInternalInstance | null,
  104. parentSuspense: SuspenseBoundary<HostNode, HostElement> | null,
  105. doRemove?: boolean
  106. ) => void
  107. move: (
  108. vnode: VNode<HostNode, HostElement>,
  109. container: HostElement,
  110. anchor: HostNode | null,
  111. type: MoveType,
  112. parentSuspense?: SuspenseBoundary<HostNode, HostElement> | null
  113. ) => void
  114. next: (vnode: VNode<HostNode, HostElement>) => HostNode | null
  115. options: RendererOptions<HostNode, HostElement>
  116. }
  117. export const enum MoveType {
  118. ENTER,
  119. LEAVE,
  120. REORDER
  121. }
  122. const prodEffectOptions = {
  123. scheduler: queueJob
  124. }
  125. function createDevEffectOptions(
  126. instance: ComponentInternalInstance
  127. ): ReactiveEffectOptions {
  128. return {
  129. scheduler: queueJob,
  130. onTrack: instance.rtc ? e => invokeHooks(instance.rtc!, e) : void 0,
  131. onTrigger: instance.rtg ? e => invokeHooks(instance.rtg!, e) : void 0
  132. }
  133. }
  134. export function invokeHooks(hooks: Function[], arg?: DebuggerEvent) {
  135. for (let i = 0; i < hooks.length; i++) {
  136. hooks[i](arg)
  137. }
  138. }
  139. export const queuePostRenderEffect = __FEATURE_SUSPENSE__
  140. ? queueEffectWithSuspense
  141. : queuePostFlushCb
  142. /**
  143. * The createRenderer function accepts two generic arguments:
  144. * HostNode and HostElement, corresponding to Node and Element types in the
  145. * host environment. For example, for runtime-dom, HostNode would be the DOM
  146. * `Node` interface and HostElement would be the DOM `Element` interface.
  147. *
  148. * Custom renderers can pass in the platform specific types like this:
  149. *
  150. * ``` js
  151. * const { render, createApp } = createRenderer<Node, Element>({
  152. * patchProp,
  153. * ...nodeOps
  154. * })
  155. * ```
  156. */
  157. export function createRenderer<
  158. HostNode extends object = any,
  159. HostElement extends HostNode = any
  160. >(
  161. options: RendererOptions<HostNode, HostElement>
  162. ): {
  163. render: RootRenderFunction<HostNode, HostElement>
  164. createApp: () => App<HostElement>
  165. } {
  166. type HostVNode = VNode<HostNode, HostElement>
  167. type HostVNodeChildren = VNodeChildren<HostNode, HostElement>
  168. type HostSuspenseBoundary = SuspenseBoundary<HostNode, HostElement>
  169. const {
  170. insert: hostInsert,
  171. remove: hostRemove,
  172. patchProp: hostPatchProp,
  173. createElement: hostCreateElement,
  174. createText: hostCreateText,
  175. createComment: hostCreateComment,
  176. setText: hostSetText,
  177. setElementText: hostSetElementText,
  178. parentNode: hostParentNode,
  179. nextSibling: hostNextSibling,
  180. querySelector: hostQuerySelector
  181. } = options
  182. const internals: RendererInternals<HostNode, HostElement> = {
  183. patch,
  184. unmount,
  185. move,
  186. next: getNextHostNode,
  187. options
  188. }
  189. function patch(
  190. n1: HostVNode | null, // null means this is a mount
  191. n2: HostVNode,
  192. container: HostElement,
  193. anchor: HostNode | null = null,
  194. parentComponent: ComponentInternalInstance | null = null,
  195. parentSuspense: HostSuspenseBoundary | null = null,
  196. isSVG: boolean = false,
  197. optimized: boolean = false
  198. ) {
  199. // patching & not same type, unmount old tree
  200. if (n1 != null && !isSameVNodeType(n1, n2)) {
  201. anchor = getNextHostNode(n1)
  202. unmount(n1, parentComponent, parentSuspense, true)
  203. n1 = null
  204. }
  205. const { type, shapeFlag } = n2
  206. switch (type) {
  207. case Text:
  208. processText(n1, n2, container, anchor)
  209. break
  210. case Comment:
  211. processCommentNode(n1, n2, container, anchor)
  212. break
  213. case Fragment:
  214. processFragment(
  215. n1,
  216. n2,
  217. container,
  218. anchor,
  219. parentComponent,
  220. parentSuspense,
  221. isSVG,
  222. optimized
  223. )
  224. break
  225. case Portal:
  226. processPortal(
  227. n1,
  228. n2,
  229. container,
  230. anchor,
  231. parentComponent,
  232. parentSuspense,
  233. isSVG,
  234. optimized
  235. )
  236. break
  237. default:
  238. if (shapeFlag & ShapeFlags.ELEMENT) {
  239. processElement(
  240. n1,
  241. n2,
  242. container,
  243. anchor,
  244. parentComponent,
  245. parentSuspense,
  246. isSVG,
  247. optimized
  248. )
  249. } else if (shapeFlag & ShapeFlags.COMPONENT) {
  250. processComponent(
  251. n1,
  252. n2,
  253. container,
  254. anchor,
  255. parentComponent,
  256. parentSuspense,
  257. isSVG,
  258. optimized
  259. )
  260. } else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
  261. ;(type as typeof SuspenseImpl).process(
  262. n1,
  263. n2,
  264. container,
  265. anchor,
  266. parentComponent,
  267. parentSuspense,
  268. isSVG,
  269. optimized,
  270. internals
  271. )
  272. } else if (__DEV__) {
  273. warn('Invalid HostVNode type:', n2.type, `(${typeof n2.type})`)
  274. }
  275. }
  276. }
  277. function processText(
  278. n1: HostVNode | null,
  279. n2: HostVNode,
  280. container: HostElement,
  281. anchor: HostNode | null
  282. ) {
  283. if (n1 == null) {
  284. hostInsert(
  285. (n2.el = hostCreateText(n2.children as string)),
  286. container,
  287. anchor
  288. )
  289. } else {
  290. const el = (n2.el = n1.el) as HostNode
  291. if (n2.children !== n1.children) {
  292. hostSetText(el, n2.children as string)
  293. }
  294. }
  295. }
  296. function processCommentNode(
  297. n1: HostVNode | null,
  298. n2: HostVNode,
  299. container: HostElement,
  300. anchor: HostNode | null
  301. ) {
  302. if (n1 == null) {
  303. hostInsert(
  304. (n2.el = hostCreateComment((n2.children as string) || '')),
  305. container,
  306. anchor
  307. )
  308. } else {
  309. // there's no support for dynamic comments
  310. n2.el = n1.el
  311. }
  312. }
  313. function processElement(
  314. n1: HostVNode | null,
  315. n2: HostVNode,
  316. container: HostElement,
  317. anchor: HostNode | null,
  318. parentComponent: ComponentInternalInstance | null,
  319. parentSuspense: HostSuspenseBoundary | null,
  320. isSVG: boolean,
  321. optimized: boolean
  322. ) {
  323. if (n1 == null) {
  324. mountElement(
  325. n2,
  326. container,
  327. anchor,
  328. parentComponent,
  329. parentSuspense,
  330. isSVG,
  331. optimized
  332. )
  333. } else {
  334. patchElement(n1, n2, parentComponent, parentSuspense, isSVG, optimized)
  335. }
  336. if (n2.ref !== null && parentComponent !== null) {
  337. setRef(n2.ref, n1 && n1.ref, parentComponent, n2.el)
  338. }
  339. }
  340. function mountElement(
  341. vnode: HostVNode,
  342. container: HostElement,
  343. anchor: HostNode | null,
  344. parentComponent: ComponentInternalInstance | null,
  345. parentSuspense: HostSuspenseBoundary | null,
  346. isSVG: boolean,
  347. optimized: boolean
  348. ) {
  349. const tag = vnode.type as string
  350. isSVG = isSVG || tag === 'svg'
  351. const el = (vnode.el = hostCreateElement(tag, isSVG))
  352. const { props, shapeFlag, transition } = vnode
  353. if (props != null) {
  354. for (const key in props) {
  355. if (isReservedProp(key)) continue
  356. hostPatchProp(el, key, props[key], null, isSVG)
  357. }
  358. if (props.onVnodeBeforeMount != null) {
  359. invokeDirectiveHook(props.onVnodeBeforeMount, parentComponent, vnode)
  360. }
  361. }
  362. if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
  363. hostSetElementText(el, vnode.children as string)
  364. } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  365. mountChildren(
  366. vnode.children as HostVNodeChildren,
  367. el,
  368. null,
  369. parentComponent,
  370. parentSuspense,
  371. isSVG,
  372. optimized || vnode.dynamicChildren !== null
  373. )
  374. }
  375. if (transition != null && !transition.persisted) {
  376. transition.beforeEnter(el)
  377. }
  378. hostInsert(el, container, anchor)
  379. const vnodeMountedHook = props && props.onVnodeMounted
  380. if (
  381. vnodeMountedHook != null ||
  382. (transition != null && !transition.persisted)
  383. ) {
  384. queuePostRenderEffect(() => {
  385. vnodeMountedHook &&
  386. invokeDirectiveHook(vnodeMountedHook, parentComponent, vnode)
  387. transition && !transition.persisted && transition.enter(el)
  388. }, parentSuspense)
  389. }
  390. }
  391. function mountChildren(
  392. children: HostVNodeChildren,
  393. container: HostElement,
  394. anchor: HostNode | null,
  395. parentComponent: ComponentInternalInstance | null,
  396. parentSuspense: HostSuspenseBoundary | null,
  397. isSVG: boolean,
  398. optimized: boolean,
  399. start: number = 0
  400. ) {
  401. for (let i = start; i < children.length; i++) {
  402. const child = optimized
  403. ? (children[i] as HostVNode)
  404. : (children[i] = normalizeVNode(children[i]))
  405. patch(
  406. null,
  407. child,
  408. container,
  409. anchor,
  410. parentComponent,
  411. parentSuspense,
  412. isSVG,
  413. optimized
  414. )
  415. }
  416. }
  417. function patchElement(
  418. n1: HostVNode,
  419. n2: HostVNode,
  420. parentComponent: ComponentInternalInstance | null,
  421. parentSuspense: HostSuspenseBoundary | null,
  422. isSVG: boolean,
  423. optimized: boolean
  424. ) {
  425. const el = (n2.el = n1.el) as HostElement
  426. const { patchFlag, dynamicChildren } = n2
  427. const oldProps = (n1 && n1.props) || EMPTY_OBJ
  428. const newProps = n2.props || EMPTY_OBJ
  429. if (newProps.onVnodeBeforeUpdate != null) {
  430. invokeDirectiveHook(newProps.onVnodeBeforeUpdate, parentComponent, n2, n1)
  431. }
  432. if (patchFlag > 0) {
  433. // the presence of a patchFlag means this element's render code was
  434. // generated by the compiler and can take the fast path.
  435. // in this path old node and new node are guaranteed to have the same shape
  436. // (i.e. at the exact same position in the source template)
  437. if (patchFlag & PatchFlags.FULL_PROPS) {
  438. // element props contain dynamic keys, full diff needed
  439. patchProps(
  440. el,
  441. n2,
  442. oldProps,
  443. newProps,
  444. parentComponent,
  445. parentSuspense,
  446. isSVG
  447. )
  448. } else {
  449. // class
  450. // this flag is matched when the element has dynamic class bindings.
  451. if (patchFlag & PatchFlags.CLASS) {
  452. if (oldProps.class !== newProps.class) {
  453. hostPatchProp(el, 'class', newProps.class, null, isSVG)
  454. }
  455. }
  456. // style
  457. // this flag is matched when the element has dynamic style bindings
  458. if (patchFlag & PatchFlags.STYLE) {
  459. hostPatchProp(el, 'style', newProps.style, oldProps.style, isSVG)
  460. }
  461. // props
  462. // This flag is matched when the element has dynamic prop/attr bindings
  463. // other than class and style. The keys of dynamic prop/attrs are saved for
  464. // faster iteration.
  465. // Note dynamic keys like :[foo]="bar" will cause this optimization to
  466. // bail out and go through a full diff because we need to unset the old key
  467. if (patchFlag & PatchFlags.PROPS) {
  468. // if the flag is present then dynamicProps must be non-null
  469. const propsToUpdate = n2.dynamicProps!
  470. for (let i = 0; i < propsToUpdate.length; i++) {
  471. const key = propsToUpdate[i]
  472. const prev = oldProps[key]
  473. const next = newProps[key]
  474. if (prev !== next) {
  475. hostPatchProp(
  476. el,
  477. key,
  478. next,
  479. prev,
  480. isSVG,
  481. n1.children as HostVNode[],
  482. parentComponent,
  483. parentSuspense,
  484. unmountChildren
  485. )
  486. }
  487. }
  488. }
  489. }
  490. // text
  491. // This flag is matched when the element has only dynamic text children.
  492. // this flag is terminal (i.e. skips children diffing).
  493. if (patchFlag & PatchFlags.TEXT) {
  494. if (n1.children !== n2.children) {
  495. hostSetElementText(el, n2.children as string)
  496. }
  497. return // terminal
  498. }
  499. } else if (!optimized && dynamicChildren == null) {
  500. // unoptimized, full diff
  501. patchProps(
  502. el,
  503. n2,
  504. oldProps,
  505. newProps,
  506. parentComponent,
  507. parentSuspense,
  508. isSVG
  509. )
  510. }
  511. if (dynamicChildren != null) {
  512. patchBlockChildren(
  513. n1.dynamicChildren!,
  514. dynamicChildren,
  515. el,
  516. parentComponent,
  517. parentSuspense,
  518. isSVG
  519. )
  520. } else if (!optimized) {
  521. // full diff
  522. patchChildren(n1, n2, el, null, parentComponent, parentSuspense, isSVG)
  523. }
  524. if (newProps.onVnodeUpdated != null) {
  525. queuePostRenderEffect(() => {
  526. invokeDirectiveHook(newProps.onVnodeUpdated, parentComponent, n2, n1)
  527. }, parentSuspense)
  528. }
  529. }
  530. // The fast path for blocks.
  531. function patchBlockChildren(
  532. oldChildren: HostVNode[],
  533. newChildren: HostVNode[],
  534. fallbackContainer: HostElement,
  535. parentComponent: ComponentInternalInstance | null,
  536. parentSuspense: HostSuspenseBoundary | null,
  537. isSVG: boolean
  538. ) {
  539. for (let i = 0; i < newChildren.length; i++) {
  540. const oldVNode = oldChildren[i]
  541. patch(
  542. oldVNode,
  543. newChildren[i],
  544. // - In the case of a Fragment, we need to provide the actual parent
  545. // of the Fragment itself so it can move its children.
  546. // - In the case of a Comment, this is likely a v-if toggle, which also
  547. // needs the correct parent container.
  548. // In other cases, the parent container is not actually used so we just
  549. // pass the block element here to avoid a DOM parentNode call.
  550. oldVNode.type === Fragment || oldVNode.type === Comment
  551. ? hostParentNode(oldVNode.el!)!
  552. : fallbackContainer,
  553. null,
  554. parentComponent,
  555. parentSuspense,
  556. isSVG,
  557. true
  558. )
  559. }
  560. }
  561. function patchProps(
  562. el: HostElement,
  563. vnode: HostVNode,
  564. oldProps: Data,
  565. newProps: Data,
  566. parentComponent: ComponentInternalInstance | null,
  567. parentSuspense: HostSuspenseBoundary | null,
  568. isSVG: boolean
  569. ) {
  570. if (oldProps !== newProps) {
  571. for (const key in newProps) {
  572. if (isReservedProp(key)) continue
  573. const next = newProps[key]
  574. const prev = oldProps[key]
  575. if (next !== prev) {
  576. hostPatchProp(
  577. el,
  578. key,
  579. next,
  580. prev,
  581. isSVG,
  582. vnode.children as HostVNode[],
  583. parentComponent,
  584. parentSuspense,
  585. unmountChildren
  586. )
  587. }
  588. }
  589. if (oldProps !== EMPTY_OBJ) {
  590. for (const key in oldProps) {
  591. if (!isReservedProp(key) && !(key in newProps)) {
  592. hostPatchProp(
  593. el,
  594. key,
  595. null,
  596. null,
  597. isSVG,
  598. vnode.children as HostVNode[],
  599. parentComponent,
  600. parentSuspense,
  601. unmountChildren
  602. )
  603. }
  604. }
  605. }
  606. }
  607. }
  608. let devFragmentID = 0
  609. function processFragment(
  610. n1: HostVNode | null,
  611. n2: HostVNode,
  612. container: HostElement,
  613. anchor: HostNode | null,
  614. parentComponent: ComponentInternalInstance | null,
  615. parentSuspense: HostSuspenseBoundary | null,
  616. isSVG: boolean,
  617. optimized: boolean
  618. ) {
  619. const showID = __DEV__ && !__TEST__
  620. const fragmentStartAnchor = (n2.el = n1
  621. ? n1.el
  622. : hostCreateComment(showID ? `fragment-${devFragmentID}-start` : ''))!
  623. const fragmentEndAnchor = (n2.anchor = n1
  624. ? n1.anchor
  625. : hostCreateComment(showID ? `fragment-${devFragmentID}-end` : ''))!
  626. if (showID) {
  627. devFragmentID++
  628. }
  629. if (n1 == null) {
  630. hostInsert(fragmentStartAnchor, container, anchor)
  631. hostInsert(fragmentEndAnchor, container, anchor)
  632. // a fragment can only have array children
  633. // since they are either generated by the compiler, or implicitly created
  634. // from arrays.
  635. mountChildren(
  636. n2.children as HostVNodeChildren,
  637. container,
  638. fragmentEndAnchor,
  639. parentComponent,
  640. parentSuspense,
  641. isSVG,
  642. optimized
  643. )
  644. } else {
  645. patchChildren(
  646. n1,
  647. n2,
  648. container,
  649. fragmentEndAnchor,
  650. parentComponent,
  651. parentSuspense,
  652. isSVG,
  653. optimized
  654. )
  655. }
  656. }
  657. function processPortal(
  658. n1: HostVNode | null,
  659. n2: HostVNode,
  660. container: HostElement,
  661. anchor: HostNode | null,
  662. parentComponent: ComponentInternalInstance | null,
  663. parentSuspense: HostSuspenseBoundary | null,
  664. isSVG: boolean,
  665. optimized: boolean
  666. ) {
  667. const targetSelector = n2.props && n2.props.target
  668. const { patchFlag, shapeFlag, children } = n2
  669. if (n1 == null) {
  670. const target = (n2.target = isString(targetSelector)
  671. ? hostQuerySelector(targetSelector)
  672. : targetSelector)
  673. if (target != null) {
  674. if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
  675. hostSetElementText(target, children as string)
  676. } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  677. mountChildren(
  678. children as HostVNodeChildren,
  679. target,
  680. null,
  681. parentComponent,
  682. parentSuspense,
  683. isSVG,
  684. optimized
  685. )
  686. }
  687. } else if (__DEV__) {
  688. warn('Invalid Portal target on mount:', target, `(${typeof target})`)
  689. }
  690. } else {
  691. // update content
  692. const target = (n2.target = n1.target)!
  693. if (patchFlag === PatchFlags.TEXT) {
  694. hostSetElementText(target, children as string)
  695. } else if (n2.dynamicChildren) {
  696. // fast path when the portal happens to be a block root
  697. patchBlockChildren(
  698. n1.dynamicChildren!,
  699. n2.dynamicChildren,
  700. container,
  701. parentComponent,
  702. parentSuspense,
  703. isSVG
  704. )
  705. } else if (!optimized) {
  706. patchChildren(
  707. n1,
  708. n2,
  709. target,
  710. null,
  711. parentComponent,
  712. parentSuspense,
  713. isSVG
  714. )
  715. }
  716. // target changed
  717. if (targetSelector !== (n1.props && n1.props.target)) {
  718. const nextTarget = (n2.target = isString(targetSelector)
  719. ? hostQuerySelector(targetSelector)
  720. : targetSelector)
  721. if (nextTarget != null) {
  722. // move content
  723. if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
  724. hostSetElementText(target, '')
  725. hostSetElementText(nextTarget, children as string)
  726. } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  727. for (let i = 0; i < (children as HostVNode[]).length; i++) {
  728. move(
  729. (children as HostVNode[])[i],
  730. nextTarget,
  731. null,
  732. MoveType.REORDER
  733. )
  734. }
  735. }
  736. } else if (__DEV__) {
  737. warn('Invalid Portal target on update:', target, `(${typeof target})`)
  738. }
  739. }
  740. }
  741. // insert an empty node as the placeholder for the portal
  742. processCommentNode(n1, n2, container, anchor)
  743. }
  744. function processComponent(
  745. n1: HostVNode | null,
  746. n2: HostVNode,
  747. container: HostElement,
  748. anchor: HostNode | null,
  749. parentComponent: ComponentInternalInstance | null,
  750. parentSuspense: HostSuspenseBoundary | null,
  751. isSVG: boolean,
  752. optimized: boolean
  753. ) {
  754. if (n1 == null) {
  755. if (n2.shapeFlag & ShapeFlags.COMPONENT_KEPT_ALIVE) {
  756. ;(parentComponent!.sink as KeepAliveSink).activate(
  757. n2,
  758. container,
  759. anchor
  760. )
  761. } else {
  762. mountComponent(
  763. n2,
  764. container,
  765. anchor,
  766. parentComponent,
  767. parentSuspense,
  768. isSVG
  769. )
  770. }
  771. } else {
  772. const instance = (n2.component = n1.component)!
  773. if (shouldUpdateComponent(n1, n2, parentComponent, optimized)) {
  774. if (
  775. __FEATURE_SUSPENSE__ &&
  776. instance.asyncDep &&
  777. !instance.asyncResolved
  778. ) {
  779. // async & still pending - just update props and slots
  780. // since the component's reactive effect for render isn't set-up yet
  781. if (__DEV__) {
  782. pushWarningContext(n2)
  783. }
  784. updateComponentPreRender(instance, n2)
  785. if (__DEV__) {
  786. popWarningContext()
  787. }
  788. return
  789. } else {
  790. // normal update
  791. instance.next = n2
  792. // instance.update is the reactive effect runner.
  793. instance.update()
  794. }
  795. } else {
  796. // no update needed. just copy over properties
  797. n2.component = n1.component
  798. n2.el = n1.el
  799. }
  800. }
  801. if (n2.ref !== null && parentComponent !== null) {
  802. if (__DEV__ && !(n2.shapeFlag & ShapeFlags.STATEFUL_COMPONENT)) {
  803. pushWarningContext(n2)
  804. warn(
  805. `Functional components do not support "ref" because they do not ` +
  806. `have instances.`
  807. )
  808. popWarningContext()
  809. }
  810. setRef(n2.ref, n1 && n1.ref, parentComponent, n2.component!.proxy)
  811. }
  812. }
  813. function mountComponent(
  814. initialVNode: HostVNode,
  815. container: HostElement,
  816. anchor: HostNode | null,
  817. parentComponent: ComponentInternalInstance | null,
  818. parentSuspense: HostSuspenseBoundary | null,
  819. isSVG: boolean
  820. ) {
  821. const instance: ComponentInternalInstance = (initialVNode.component = createComponentInstance(
  822. initialVNode,
  823. parentComponent
  824. ))
  825. // HMR
  826. if (__BUNDLER__ && __DEV__ && instance.type.__hmrId != null) {
  827. registerHMR(instance)
  828. }
  829. if (__DEV__) {
  830. pushWarningContext(initialVNode)
  831. }
  832. const Comp = initialVNode.type as Component
  833. // inject renderer internals for keepAlive
  834. if (isKeepAlive(initialVNode)) {
  835. const sink = instance.sink as KeepAliveSink
  836. sink.renderer = internals
  837. sink.parentSuspense = parentSuspense
  838. }
  839. // resolve props and slots for setup context
  840. const propsOptions = Comp.props
  841. resolveProps(instance, initialVNode.props, propsOptions)
  842. resolveSlots(instance, initialVNode.children)
  843. // setup stateful logic
  844. if (initialVNode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
  845. setupStatefulComponent(instance, parentSuspense)
  846. }
  847. // setup() is async. This component relies on async logic to be resolved
  848. // before proceeding
  849. if (__FEATURE_SUSPENSE__ && instance.asyncDep) {
  850. if (!parentSuspense) {
  851. if (__DEV__) warn('async setup() is used without a suspense boundary!')
  852. return
  853. }
  854. parentSuspense.registerDep(instance, setupRenderEffect)
  855. // give it a placeholder
  856. const placeholder = (instance.subTree = createVNode(Comment))
  857. processCommentNode(null, placeholder, container, anchor)
  858. initialVNode.el = placeholder.el
  859. return
  860. }
  861. setupRenderEffect(
  862. instance,
  863. parentSuspense,
  864. initialVNode,
  865. container,
  866. anchor,
  867. isSVG
  868. )
  869. if (__DEV__) {
  870. popWarningContext()
  871. }
  872. }
  873. function setupRenderEffect(
  874. instance: ComponentInternalInstance,
  875. parentSuspense: HostSuspenseBoundary | null,
  876. initialVNode: HostVNode,
  877. container: HostElement,
  878. anchor: HostNode | null,
  879. isSVG: boolean
  880. ) {
  881. // create reactive effect for rendering
  882. instance.update = effect(function componentEffect() {
  883. if (!instance.isMounted) {
  884. const subTree = (instance.subTree = renderComponentRoot(instance))
  885. // beforeMount hook
  886. if (instance.bm !== null) {
  887. invokeHooks(instance.bm)
  888. }
  889. patch(null, subTree, container, anchor, instance, parentSuspense, isSVG)
  890. initialVNode.el = subTree.el
  891. // mounted hook
  892. if (instance.m !== null) {
  893. queuePostRenderEffect(instance.m, parentSuspense)
  894. }
  895. // activated hook for keep-alive roots.
  896. if (
  897. instance.a !== null &&
  898. instance.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
  899. ) {
  900. queuePostRenderEffect(instance.a, parentSuspense)
  901. }
  902. instance.isMounted = true
  903. } else {
  904. // updateComponent
  905. // This is triggered by mutation of component's own state (next: null)
  906. // OR parent calling processComponent (next: HostVNode)
  907. const { next } = instance
  908. if (__DEV__) {
  909. pushWarningContext(next || instance.vnode)
  910. }
  911. if (next !== null) {
  912. updateComponentPreRender(instance, next)
  913. }
  914. const nextTree = renderComponentRoot(instance)
  915. const prevTree = instance.subTree
  916. instance.subTree = nextTree
  917. // beforeUpdate hook
  918. if (instance.bu !== null) {
  919. invokeHooks(instance.bu)
  920. }
  921. // reset refs
  922. // only needed if previous patch had refs
  923. if (instance.refs !== EMPTY_OBJ) {
  924. instance.refs = {}
  925. }
  926. patch(
  927. prevTree,
  928. nextTree,
  929. // parent may have changed if it's in a portal
  930. hostParentNode(prevTree.el as HostNode) as HostElement,
  931. // anchor may have changed if it's in a fragment
  932. getNextHostNode(prevTree),
  933. instance,
  934. parentSuspense,
  935. isSVG
  936. )
  937. instance.vnode.el = nextTree.el
  938. if (next === null) {
  939. // self-triggered update. In case of HOC, update parent component
  940. // vnode el. HOC is indicated by parent instance's subTree pointing
  941. // to child component's vnode
  942. updateHOCHostEl(instance, nextTree.el)
  943. }
  944. // updated hook
  945. if (instance.u !== null) {
  946. queuePostRenderEffect(instance.u, parentSuspense)
  947. }
  948. if (__DEV__) {
  949. popWarningContext()
  950. }
  951. }
  952. }, __DEV__ ? createDevEffectOptions(instance) : prodEffectOptions)
  953. }
  954. function updateComponentPreRender(
  955. instance: ComponentInternalInstance,
  956. nextVNode: HostVNode
  957. ) {
  958. nextVNode.component = instance
  959. instance.vnode = nextVNode
  960. instance.next = null
  961. resolveProps(instance, nextVNode.props, (nextVNode.type as Component).props)
  962. resolveSlots(instance, nextVNode.children)
  963. }
  964. function patchChildren(
  965. n1: HostVNode | null,
  966. n2: HostVNode,
  967. container: HostElement,
  968. anchor: HostNode | null,
  969. parentComponent: ComponentInternalInstance | null,
  970. parentSuspense: HostSuspenseBoundary | null,
  971. isSVG: boolean,
  972. optimized: boolean = false
  973. ) {
  974. const c1 = n1 && n1.children
  975. const prevShapeFlag = n1 ? n1.shapeFlag : 0
  976. const c2 = n2.children
  977. const { patchFlag, shapeFlag } = n2
  978. if (patchFlag === PatchFlags.BAIL) {
  979. optimized = false
  980. }
  981. // fast path
  982. if (patchFlag > 0) {
  983. optimized = true
  984. if (patchFlag & PatchFlags.KEYED_FRAGMENT) {
  985. // this could be either fully-keyed or mixed (some keyed some not)
  986. // presence of patchFlag means children are guaranteed to be arrays
  987. patchKeyedChildren(
  988. c1 as HostVNode[],
  989. c2 as HostVNodeChildren,
  990. container,
  991. anchor,
  992. parentComponent,
  993. parentSuspense,
  994. isSVG,
  995. optimized
  996. )
  997. return
  998. } else if (patchFlag & PatchFlags.UNKEYED_FRAGMENT) {
  999. // unkeyed
  1000. patchUnkeyedChildren(
  1001. c1 as HostVNode[],
  1002. c2 as HostVNodeChildren,
  1003. container,
  1004. anchor,
  1005. parentComponent,
  1006. parentSuspense,
  1007. isSVG,
  1008. optimized
  1009. )
  1010. return
  1011. }
  1012. }
  1013. // children has 3 possibilities: text, array or no children.
  1014. if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
  1015. // text children fast path
  1016. if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1017. unmountChildren(c1 as HostVNode[], parentComponent, parentSuspense)
  1018. }
  1019. if (c2 !== c1) {
  1020. hostSetElementText(container, c2 as string)
  1021. }
  1022. } else {
  1023. if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1024. // prev children was array
  1025. if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1026. // two arrays, cannot assume anything, do full diff
  1027. patchKeyedChildren(
  1028. c1 as HostVNode[],
  1029. c2 as HostVNodeChildren,
  1030. container,
  1031. anchor,
  1032. parentComponent,
  1033. parentSuspense,
  1034. isSVG,
  1035. optimized
  1036. )
  1037. } else {
  1038. // no new children, just unmount old
  1039. unmountChildren(
  1040. c1 as HostVNode[],
  1041. parentComponent,
  1042. parentSuspense,
  1043. true
  1044. )
  1045. }
  1046. } else {
  1047. // prev children was text OR null
  1048. // new children is array OR null
  1049. if (prevShapeFlag & ShapeFlags.TEXT_CHILDREN) {
  1050. hostSetElementText(container, '')
  1051. }
  1052. // mount new if array
  1053. if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1054. mountChildren(
  1055. c2 as HostVNodeChildren,
  1056. container,
  1057. anchor,
  1058. parentComponent,
  1059. parentSuspense,
  1060. isSVG,
  1061. optimized
  1062. )
  1063. }
  1064. }
  1065. }
  1066. }
  1067. function patchUnkeyedChildren(
  1068. c1: HostVNode[],
  1069. c2: HostVNodeChildren,
  1070. container: HostElement,
  1071. anchor: HostNode | null,
  1072. parentComponent: ComponentInternalInstance | null,
  1073. parentSuspense: HostSuspenseBoundary | null,
  1074. isSVG: boolean,
  1075. optimized: boolean
  1076. ) {
  1077. c1 = c1 || EMPTY_ARR
  1078. c2 = c2 || EMPTY_ARR
  1079. const oldLength = c1.length
  1080. const newLength = c2.length
  1081. const commonLength = Math.min(oldLength, newLength)
  1082. let i
  1083. for (i = 0; i < commonLength; i++) {
  1084. const nextChild = optimized
  1085. ? (c2[i] as HostVNode)
  1086. : (c2[i] = normalizeVNode(c2[i]))
  1087. patch(
  1088. c1[i],
  1089. nextChild,
  1090. container,
  1091. null,
  1092. parentComponent,
  1093. parentSuspense,
  1094. isSVG,
  1095. optimized
  1096. )
  1097. }
  1098. if (oldLength > newLength) {
  1099. // remove old
  1100. unmountChildren(c1, parentComponent, parentSuspense, true, commonLength)
  1101. } else {
  1102. // mount new
  1103. mountChildren(
  1104. c2,
  1105. container,
  1106. anchor,
  1107. parentComponent,
  1108. parentSuspense,
  1109. isSVG,
  1110. optimized,
  1111. commonLength
  1112. )
  1113. }
  1114. }
  1115. // can be all-keyed or mixed
  1116. function patchKeyedChildren(
  1117. c1: HostVNode[],
  1118. c2: HostVNodeChildren,
  1119. container: HostElement,
  1120. parentAnchor: HostNode | null,
  1121. parentComponent: ComponentInternalInstance | null,
  1122. parentSuspense: HostSuspenseBoundary | null,
  1123. isSVG: boolean,
  1124. optimized: boolean
  1125. ) {
  1126. let i = 0
  1127. const l2 = c2.length
  1128. let e1 = c1.length - 1 // prev ending index
  1129. let e2 = l2 - 1 // next ending index
  1130. // 1. sync from start
  1131. // (a b) c
  1132. // (a b) d e
  1133. while (i <= e1 && i <= e2) {
  1134. const n1 = c1[i]
  1135. const n2 = optimized
  1136. ? (c2[i] as HostVNode)
  1137. : (c2[i] = normalizeVNode(c2[i]))
  1138. if (isSameVNodeType(n1, n2)) {
  1139. patch(
  1140. n1,
  1141. n2,
  1142. container,
  1143. parentAnchor,
  1144. parentComponent,
  1145. parentSuspense,
  1146. isSVG,
  1147. optimized
  1148. )
  1149. } else {
  1150. break
  1151. }
  1152. i++
  1153. }
  1154. // 2. sync from end
  1155. // a (b c)
  1156. // d e (b c)
  1157. while (i <= e1 && i <= e2) {
  1158. const n1 = c1[e1]
  1159. const n2 = optimized
  1160. ? (c2[i] as HostVNode)
  1161. : (c2[e2] = normalizeVNode(c2[e2]))
  1162. if (isSameVNodeType(n1, n2)) {
  1163. patch(
  1164. n1,
  1165. n2,
  1166. container,
  1167. parentAnchor,
  1168. parentComponent,
  1169. parentSuspense,
  1170. isSVG,
  1171. optimized
  1172. )
  1173. } else {
  1174. break
  1175. }
  1176. e1--
  1177. e2--
  1178. }
  1179. // 3. common sequence + mount
  1180. // (a b)
  1181. // (a b) c
  1182. // i = 2, e1 = 1, e2 = 2
  1183. // (a b)
  1184. // c (a b)
  1185. // i = 0, e1 = -1, e2 = 0
  1186. if (i > e1) {
  1187. if (i <= e2) {
  1188. const nextPos = e2 + 1
  1189. const anchor =
  1190. nextPos < l2 ? (c2[nextPos] as HostVNode).el : parentAnchor
  1191. while (i <= e2) {
  1192. patch(
  1193. null,
  1194. optimized ? (c2[i] as HostVNode) : (c2[i] = normalizeVNode(c2[i])),
  1195. container,
  1196. anchor,
  1197. parentComponent,
  1198. parentSuspense,
  1199. isSVG
  1200. )
  1201. i++
  1202. }
  1203. }
  1204. }
  1205. // 4. common sequence + unmount
  1206. // (a b) c
  1207. // (a b)
  1208. // i = 2, e1 = 2, e2 = 1
  1209. // a (b c)
  1210. // (b c)
  1211. // i = 0, e1 = 0, e2 = -1
  1212. else if (i > e2) {
  1213. while (i <= e1) {
  1214. unmount(c1[i], parentComponent, parentSuspense, true)
  1215. i++
  1216. }
  1217. }
  1218. // 5. unknown sequence
  1219. // [i ... e1 + 1]: a b [c d e] f g
  1220. // [i ... e2 + 1]: a b [e d c h] f g
  1221. // i = 2, e1 = 4, e2 = 5
  1222. else {
  1223. const s1 = i // prev starting index
  1224. const s2 = i // next starting index
  1225. // 5.1 build key:index map for newChildren
  1226. const keyToNewIndexMap: Map<string | number, number> = new Map()
  1227. for (i = s2; i <= e2; i++) {
  1228. const nextChild = optimized
  1229. ? (c2[i] as HostVNode)
  1230. : (c2[i] = normalizeVNode(c2[i]))
  1231. if (nextChild.key != null) {
  1232. if (__DEV__ && keyToNewIndexMap.has(nextChild.key)) {
  1233. warn(
  1234. `Duplicate keys found during update:`,
  1235. JSON.stringify(nextChild.key),
  1236. `Make sure keys are unique.`
  1237. )
  1238. }
  1239. keyToNewIndexMap.set(nextChild.key, i)
  1240. }
  1241. }
  1242. // 5.2 loop through old children left to be patched and try to patch
  1243. // matching nodes & remove nodes that are no longer present
  1244. let j
  1245. let patched = 0
  1246. const toBePatched = e2 - s2 + 1
  1247. let moved = false
  1248. // used to track whether any node has moved
  1249. let maxNewIndexSoFar = 0
  1250. // works as Map<newIndex, oldIndex>
  1251. // Note that oldIndex is offset by +1
  1252. // and oldIndex = 0 is a special value indicating the new node has
  1253. // no corresponding old node.
  1254. // used for determining longest stable subsequence
  1255. const newIndexToOldIndexMap = new Array(toBePatched)
  1256. for (i = 0; i < toBePatched; i++) newIndexToOldIndexMap[i] = 0
  1257. for (i = s1; i <= e1; i++) {
  1258. const prevChild = c1[i]
  1259. if (patched >= toBePatched) {
  1260. // all new children have been patched so this can only be a removal
  1261. unmount(prevChild, parentComponent, parentSuspense, true)
  1262. continue
  1263. }
  1264. let newIndex
  1265. if (prevChild.key != null) {
  1266. newIndex = keyToNewIndexMap.get(prevChild.key)
  1267. } else {
  1268. // key-less node, try to locate a key-less node of the same type
  1269. for (j = s2; j <= e2; j++) {
  1270. if (
  1271. newIndexToOldIndexMap[j - s2] === 0 &&
  1272. isSameVNodeType(prevChild, c2[j] as HostVNode)
  1273. ) {
  1274. newIndex = j
  1275. break
  1276. }
  1277. }
  1278. }
  1279. if (newIndex === undefined) {
  1280. unmount(prevChild, parentComponent, parentSuspense, true)
  1281. } else {
  1282. newIndexToOldIndexMap[newIndex - s2] = i + 1
  1283. if (newIndex >= maxNewIndexSoFar) {
  1284. maxNewIndexSoFar = newIndex
  1285. } else {
  1286. moved = true
  1287. }
  1288. patch(
  1289. prevChild,
  1290. c2[newIndex] as HostVNode,
  1291. container,
  1292. null,
  1293. parentComponent,
  1294. parentSuspense,
  1295. isSVG,
  1296. optimized
  1297. )
  1298. patched++
  1299. }
  1300. }
  1301. // 5.3 move and mount
  1302. // generate longest stable subsequence only when nodes have moved
  1303. const increasingNewIndexSequence = moved
  1304. ? getSequence(newIndexToOldIndexMap)
  1305. : EMPTY_ARR
  1306. j = increasingNewIndexSequence.length - 1
  1307. // looping backwards so that we can use last patched node as anchor
  1308. for (i = toBePatched - 1; i >= 0; i--) {
  1309. const nextIndex = s2 + i
  1310. const nextChild = c2[nextIndex] as HostVNode
  1311. const anchor =
  1312. nextIndex + 1 < l2
  1313. ? (c2[nextIndex + 1] as HostVNode).el
  1314. : parentAnchor
  1315. if (newIndexToOldIndexMap[i] === 0) {
  1316. // mount new
  1317. patch(
  1318. null,
  1319. nextChild,
  1320. container,
  1321. anchor,
  1322. parentComponent,
  1323. parentSuspense,
  1324. isSVG
  1325. )
  1326. } else if (moved) {
  1327. // move if:
  1328. // There is no stable subsequence (e.g. a reverse)
  1329. // OR current node is not among the stable sequence
  1330. if (j < 0 || i !== increasingNewIndexSequence[j]) {
  1331. move(nextChild, container, anchor, MoveType.REORDER)
  1332. } else {
  1333. j--
  1334. }
  1335. }
  1336. }
  1337. }
  1338. }
  1339. function move(
  1340. vnode: HostVNode,
  1341. container: HostElement,
  1342. anchor: HostNode | null,
  1343. type: MoveType,
  1344. parentSuspense: HostSuspenseBoundary | null = null
  1345. ) {
  1346. if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
  1347. move(vnode.component!.subTree, container, anchor, type)
  1348. return
  1349. }
  1350. if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
  1351. vnode.suspense!.move(container, anchor, type)
  1352. return
  1353. }
  1354. if (vnode.type === Fragment) {
  1355. hostInsert(vnode.el!, container, anchor)
  1356. const children = vnode.children as HostVNode[]
  1357. for (let i = 0; i < children.length; i++) {
  1358. move(children[i], container, anchor, type)
  1359. }
  1360. hostInsert(vnode.anchor!, container, anchor)
  1361. } else {
  1362. // Plain element
  1363. const { el, transition, shapeFlag } = vnode
  1364. const needTransition =
  1365. type !== MoveType.REORDER &&
  1366. shapeFlag & ShapeFlags.ELEMENT &&
  1367. transition != null
  1368. if (needTransition) {
  1369. if (type === MoveType.ENTER) {
  1370. transition!.beforeEnter(el!)
  1371. hostInsert(el!, container, anchor)
  1372. queuePostRenderEffect(() => transition!.enter(el!), parentSuspense)
  1373. } else {
  1374. const { leave, delayLeave, afterLeave } = transition!
  1375. const remove = () => hostInsert(el!, container, anchor)
  1376. const performLeave = () => {
  1377. leave(el!, () => {
  1378. remove()
  1379. afterLeave && afterLeave()
  1380. })
  1381. }
  1382. if (delayLeave) {
  1383. delayLeave(el!, remove, performLeave)
  1384. } else {
  1385. performLeave()
  1386. }
  1387. }
  1388. } else {
  1389. hostInsert(el!, container, anchor)
  1390. }
  1391. }
  1392. }
  1393. function unmount(
  1394. vnode: HostVNode,
  1395. parentComponent: ComponentInternalInstance | null,
  1396. parentSuspense: HostSuspenseBoundary | null,
  1397. doRemove?: boolean
  1398. ) {
  1399. const {
  1400. el,
  1401. props,
  1402. ref,
  1403. type,
  1404. children,
  1405. dynamicChildren,
  1406. shapeFlag,
  1407. anchor,
  1408. transition
  1409. } = vnode
  1410. // unset ref
  1411. if (ref !== null && parentComponent !== null) {
  1412. setRef(ref, null, parentComponent, null)
  1413. }
  1414. if (shapeFlag & ShapeFlags.COMPONENT) {
  1415. if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
  1416. ;(parentComponent!.sink as KeepAliveSink).deactivate(vnode)
  1417. } else {
  1418. unmountComponent(vnode.component!, parentSuspense, doRemove)
  1419. }
  1420. return
  1421. }
  1422. if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
  1423. vnode.suspense!.unmount(parentSuspense, doRemove)
  1424. return
  1425. }
  1426. if (props != null && props.onVnodeBeforeUnmount != null) {
  1427. invokeDirectiveHook(props.onVnodeBeforeUnmount, parentComponent, vnode)
  1428. }
  1429. const shouldRemoveChildren = type === Fragment && doRemove
  1430. if (dynamicChildren != null) {
  1431. unmountChildren(
  1432. dynamicChildren,
  1433. parentComponent,
  1434. parentSuspense,
  1435. shouldRemoveChildren
  1436. )
  1437. } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
  1438. unmountChildren(
  1439. children as HostVNode[],
  1440. parentComponent,
  1441. parentSuspense,
  1442. shouldRemoveChildren
  1443. )
  1444. }
  1445. if (doRemove) {
  1446. const remove = () => {
  1447. hostRemove(vnode.el!)
  1448. if (anchor != null) hostRemove(anchor)
  1449. if (
  1450. transition != null &&
  1451. !transition.persisted &&
  1452. transition.afterLeave
  1453. ) {
  1454. transition.afterLeave()
  1455. }
  1456. }
  1457. if (
  1458. vnode.shapeFlag & ShapeFlags.ELEMENT &&
  1459. transition != null &&
  1460. !transition.persisted
  1461. ) {
  1462. const { leave, delayLeave } = transition
  1463. const performLeave = () => leave(el!, remove)
  1464. if (delayLeave) {
  1465. delayLeave(vnode.el!, remove, performLeave)
  1466. } else {
  1467. performLeave()
  1468. }
  1469. } else {
  1470. remove()
  1471. }
  1472. }
  1473. if (props != null && props.onVnodeUnmounted != null) {
  1474. queuePostRenderEffect(() => {
  1475. invokeDirectiveHook(props.onVnodeUnmounted!, parentComponent, vnode)
  1476. }, parentSuspense)
  1477. }
  1478. }
  1479. function unmountComponent(
  1480. instance: ComponentInternalInstance,
  1481. parentSuspense: HostSuspenseBoundary | null,
  1482. doRemove?: boolean
  1483. ) {
  1484. // HMR
  1485. if (__BUNDLER__ && __DEV__ && instance.type.__hmrId != null) {
  1486. unregisterHMR(instance)
  1487. }
  1488. const { bum, effects, update, subTree, um, da, isDeactivated } = instance
  1489. // beforeUnmount hook
  1490. if (bum !== null) {
  1491. invokeHooks(bum)
  1492. }
  1493. if (effects !== null) {
  1494. for (let i = 0; i < effects.length; i++) {
  1495. stop(effects[i])
  1496. }
  1497. }
  1498. // update may be null if a component is unmounted before its async
  1499. // setup has resolved.
  1500. if (update !== null) {
  1501. stop(update)
  1502. unmount(subTree, instance, parentSuspense, doRemove)
  1503. }
  1504. // unmounted hook
  1505. if (um !== null) {
  1506. queuePostRenderEffect(um, parentSuspense)
  1507. }
  1508. // deactivated hook
  1509. if (
  1510. da !== null &&
  1511. !isDeactivated &&
  1512. instance.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
  1513. ) {
  1514. queuePostRenderEffect(da, parentSuspense)
  1515. }
  1516. queuePostFlushCb(() => {
  1517. instance.isUnmounted = true
  1518. })
  1519. // A component with async dep inside a pending suspense is unmounted before
  1520. // its async dep resolves. This should remove the dep from the suspense, and
  1521. // cause the suspense to resolve immediately if that was the last dep.
  1522. if (
  1523. __FEATURE_SUSPENSE__ &&
  1524. parentSuspense !== null &&
  1525. !parentSuspense.isResolved &&
  1526. !parentSuspense.isUnmounted &&
  1527. instance.asyncDep !== null &&
  1528. !instance.asyncResolved
  1529. ) {
  1530. parentSuspense.deps--
  1531. if (parentSuspense.deps === 0) {
  1532. parentSuspense.resolve()
  1533. }
  1534. }
  1535. }
  1536. function unmountChildren(
  1537. children: HostVNode[],
  1538. parentComponent: ComponentInternalInstance | null,
  1539. parentSuspense: HostSuspenseBoundary | null,
  1540. doRemove?: boolean,
  1541. start: number = 0
  1542. ) {
  1543. for (let i = start; i < children.length; i++) {
  1544. unmount(children[i], parentComponent, parentSuspense, doRemove)
  1545. }
  1546. }
  1547. function getNextHostNode(vnode: HostVNode): HostNode | null {
  1548. if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
  1549. return getNextHostNode(vnode.component!.subTree)
  1550. }
  1551. if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
  1552. return vnode.suspense!.next()
  1553. }
  1554. return hostNextSibling((vnode.anchor || vnode.el)!)
  1555. }
  1556. function setRef(
  1557. ref: string | Function | Ref,
  1558. oldRef: string | Function | Ref | null,
  1559. parent: ComponentInternalInstance,
  1560. value: HostNode | ComponentPublicInstance | null
  1561. ) {
  1562. const refs = parent.refs === EMPTY_OBJ ? (parent.refs = {}) : parent.refs
  1563. const renderContext = toRaw(parent.renderContext)
  1564. // unset old ref
  1565. if (oldRef !== null && oldRef !== ref) {
  1566. if (isString(oldRef)) {
  1567. refs[oldRef] = null
  1568. const oldSetupRef = renderContext[oldRef]
  1569. if (isRef(oldSetupRef)) {
  1570. oldSetupRef.value = null
  1571. }
  1572. } else if (isRef(oldRef)) {
  1573. oldRef.value = null
  1574. }
  1575. }
  1576. if (isString(ref)) {
  1577. const setupRef = renderContext[ref]
  1578. if (isRef(setupRef)) {
  1579. setupRef.value = value
  1580. }
  1581. refs[ref] = value
  1582. } else if (isRef(ref)) {
  1583. ref.value = value
  1584. } else if (isFunction(ref)) {
  1585. callWithErrorHandling(ref, parent, ErrorCodes.FUNCTION_REF, [value, refs])
  1586. } else if (__DEV__) {
  1587. warn('Invalid template ref type:', value, `(${typeof value})`)
  1588. }
  1589. }
  1590. const render: RootRenderFunction<
  1591. HostNode,
  1592. HostElement & {
  1593. _vnode: HostVNode | null
  1594. }
  1595. > = (vnode, container) => {
  1596. if (vnode == null) {
  1597. if (container._vnode) {
  1598. unmount(container._vnode, null, null, true)
  1599. }
  1600. } else {
  1601. patch(container._vnode || null, vnode, container)
  1602. }
  1603. flushPostFlushCbs()
  1604. container._vnode = vnode
  1605. }
  1606. return {
  1607. render,
  1608. createApp: createAppAPI(render)
  1609. }
  1610. }
  1611. // https://en.wikipedia.org/wiki/Longest_increasing_subsequence
  1612. function getSequence(arr: number[]): number[] {
  1613. const p = arr.slice()
  1614. const result = [0]
  1615. let i, j, u, v, c
  1616. const len = arr.length
  1617. for (i = 0; i < len; i++) {
  1618. const arrI = arr[i]
  1619. if (arrI !== 0) {
  1620. j = result[result.length - 1]
  1621. if (arr[j] < arrI) {
  1622. p[i] = j
  1623. result.push(i)
  1624. continue
  1625. }
  1626. u = 0
  1627. v = result.length - 1
  1628. while (u < v) {
  1629. c = ((u + v) / 2) | 0
  1630. if (arr[result[c]] < arrI) {
  1631. u = c + 1
  1632. } else {
  1633. v = c
  1634. }
  1635. }
  1636. if (arrI < arr[result[u]]) {
  1637. if (u > 0) {
  1638. p[i] = result[u - 1]
  1639. }
  1640. result[u] = i
  1641. }
  1642. }
  1643. }
  1644. u = result.length
  1645. v = result[u - 1]
  1646. while (u-- > 0) {
  1647. result[u] = v
  1648. v = p[v]
  1649. }
  1650. return result
  1651. }