| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736 |
- import {
- Text,
- Fragment,
- Comment,
- Portal,
- normalizeVNode,
- VNode,
- VNodeChildren,
- createVNode,
- isSameVNodeType
- } from './vnode'
- import {
- ComponentInternalInstance,
- createComponentInstance,
- setupStatefulComponent,
- Component,
- Data
- } from './component'
- import {
- renderComponentRoot,
- shouldUpdateComponent,
- updateHOCHostEl
- } from './componentRenderUtils'
- import {
- isString,
- EMPTY_OBJ,
- EMPTY_ARR,
- isReservedProp,
- isFunction,
- PatchFlags
- } from '@vue/shared'
- import { queueJob, queuePostFlushCb, flushPostFlushCbs } from './scheduler'
- import {
- effect,
- stop,
- ReactiveEffectOptions,
- isRef,
- Ref,
- toRaw,
- DebuggerEvent
- } from '@vue/reactivity'
- import { resolveProps } from './componentProps'
- import { resolveSlots } from './componentSlots'
- import { ShapeFlags } from './shapeFlags'
- import { pushWarningContext, popWarningContext, warn } from './warning'
- import { invokeDirectiveHook } from './directives'
- import { ComponentPublicInstance } from './componentProxy'
- import { App, createAppAPI } from './apiApp'
- import {
- SuspenseBoundary,
- queueEffectWithSuspense,
- SuspenseImpl
- } from './components/Suspense'
- import { ErrorCodes, callWithErrorHandling } from './errorHandling'
- import { KeepAliveSink, isKeepAlive } from './components/KeepAlive'
- import { registerHMR, unregisterHMR } from './hmr'
- export interface RendererOptions<HostNode = any, HostElement = any> {
- patchProp(
- el: HostElement,
- key: string,
- value: any,
- oldValue: any,
- isSVG: boolean,
- prevChildren?: VNode<HostNode, HostElement>[],
- parentComponent?: ComponentInternalInstance | null,
- parentSuspense?: SuspenseBoundary<HostNode, HostElement> | null,
- unmountChildren?: (
- children: VNode<HostNode, HostElement>[],
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary<HostNode, HostElement> | null
- ) => void
- ): void
- insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void
- remove(el: HostNode): void
- createElement(type: string, isSVG?: boolean): HostElement
- createText(text: string): HostNode
- createComment(text: string): HostNode
- setText(node: HostNode, text: string): void
- setElementText(node: HostElement, text: string): void
- parentNode(node: HostNode): HostElement | null
- nextSibling(node: HostNode): HostNode | null
- querySelector(selector: string): HostElement | null
- }
- export type RootRenderFunction<HostNode, HostElement> = (
- vnode: VNode<HostNode, HostElement> | null,
- dom: HostElement
- ) => void
- // An object exposing the internals of a renderer, passed to tree-shakeable
- // features so that they can be decoupled from this file.
- export interface RendererInternals<HostNode = any, HostElement = any> {
- patch: (
- n1: VNode<HostNode, HostElement> | null, // null means this is a mount
- n2: VNode<HostNode, HostElement>,
- container: HostElement,
- anchor?: HostNode | null,
- parentComponent?: ComponentInternalInstance | null,
- parentSuspense?: SuspenseBoundary<HostNode, HostElement> | null,
- isSVG?: boolean,
- optimized?: boolean
- ) => void
- unmount: (
- vnode: VNode<HostNode, HostElement>,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: SuspenseBoundary<HostNode, HostElement> | null,
- doRemove?: boolean
- ) => void
- move: (
- vnode: VNode<HostNode, HostElement>,
- container: HostElement,
- anchor: HostNode | null,
- type: MoveType,
- parentSuspense?: SuspenseBoundary<HostNode, HostElement> | null
- ) => void
- next: (vnode: VNode<HostNode, HostElement>) => HostNode | null
- options: RendererOptions<HostNode, HostElement>
- }
- export const enum MoveType {
- ENTER,
- LEAVE,
- REORDER
- }
- const prodEffectOptions = {
- scheduler: queueJob
- }
- function createDevEffectOptions(
- instance: ComponentInternalInstance
- ): ReactiveEffectOptions {
- return {
- scheduler: queueJob,
- onTrack: instance.rtc ? e => invokeHooks(instance.rtc!, e) : void 0,
- onTrigger: instance.rtg ? e => invokeHooks(instance.rtg!, e) : void 0
- }
- }
- export function invokeHooks(hooks: Function[], arg?: DebuggerEvent) {
- for (let i = 0; i < hooks.length; i++) {
- hooks[i](arg)
- }
- }
- export const queuePostRenderEffect = __FEATURE_SUSPENSE__
- ? queueEffectWithSuspense
- : queuePostFlushCb
- /**
- * The createRenderer function accepts two generic arguments:
- * HostNode and HostElement, corresponding to Node and Element types in the
- * host environment. For example, for runtime-dom, HostNode would be the DOM
- * `Node` interface and HostElement would be the DOM `Element` interface.
- *
- * Custom renderers can pass in the platform specific types like this:
- *
- * ``` js
- * const { render, createApp } = createRenderer<Node, Element>({
- * patchProp,
- * ...nodeOps
- * })
- * ```
- */
- export function createRenderer<
- HostNode extends object = any,
- HostElement extends HostNode = any
- >(
- options: RendererOptions<HostNode, HostElement>
- ): {
- render: RootRenderFunction<HostNode, HostElement>
- createApp: () => App<HostElement>
- } {
- type HostVNode = VNode<HostNode, HostElement>
- type HostVNodeChildren = VNodeChildren<HostNode, HostElement>
- type HostSuspenseBoundary = SuspenseBoundary<HostNode, HostElement>
- const {
- insert: hostInsert,
- remove: hostRemove,
- patchProp: hostPatchProp,
- createElement: hostCreateElement,
- createText: hostCreateText,
- createComment: hostCreateComment,
- setText: hostSetText,
- setElementText: hostSetElementText,
- parentNode: hostParentNode,
- nextSibling: hostNextSibling,
- querySelector: hostQuerySelector
- } = options
- const internals: RendererInternals<HostNode, HostElement> = {
- patch,
- unmount,
- move,
- next: getNextHostNode,
- options
- }
- function patch(
- n1: HostVNode | null, // null means this is a mount
- n2: HostVNode,
- container: HostElement,
- anchor: HostNode | null = null,
- parentComponent: ComponentInternalInstance | null = null,
- parentSuspense: HostSuspenseBoundary | null = null,
- isSVG: boolean = false,
- optimized: boolean = false
- ) {
- // patching & not same type, unmount old tree
- if (n1 != null && !isSameVNodeType(n1, n2)) {
- anchor = getNextHostNode(n1)
- unmount(n1, parentComponent, parentSuspense, true)
- n1 = null
- }
- const { type, shapeFlag } = n2
- switch (type) {
- case Text:
- processText(n1, n2, container, anchor)
- break
- case Comment:
- processCommentNode(n1, n2, container, anchor)
- break
- case Fragment:
- processFragment(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- break
- case Portal:
- processPortal(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- break
- default:
- if (shapeFlag & ShapeFlags.ELEMENT) {
- processElement(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else if (shapeFlag & ShapeFlags.COMPONENT) {
- processComponent(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
- ;(type as typeof SuspenseImpl).process(
- n1,
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized,
- internals
- )
- } else if (__DEV__) {
- warn('Invalid HostVNode type:', n2.type, `(${typeof n2.type})`)
- }
- }
- }
- function processText(
- n1: HostVNode | null,
- n2: HostVNode,
- container: HostElement,
- anchor: HostNode | null
- ) {
- if (n1 == null) {
- hostInsert(
- (n2.el = hostCreateText(n2.children as string)),
- container,
- anchor
- )
- } else {
- const el = (n2.el = n1.el) as HostNode
- if (n2.children !== n1.children) {
- hostSetText(el, n2.children as string)
- }
- }
- }
- function processCommentNode(
- n1: HostVNode | null,
- n2: HostVNode,
- container: HostElement,
- anchor: HostNode | null
- ) {
- if (n1 == null) {
- hostInsert(
- (n2.el = hostCreateComment((n2.children as string) || '')),
- container,
- anchor
- )
- } else {
- // there's no support for dynamic comments
- n2.el = n1.el
- }
- }
- function processElement(
- n1: HostVNode | null,
- n2: HostVNode,
- container: HostElement,
- anchor: HostNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) {
- if (n1 == null) {
- mountElement(
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else {
- patchElement(n1, n2, parentComponent, parentSuspense, isSVG, optimized)
- }
- if (n2.ref !== null && parentComponent !== null) {
- setRef(n2.ref, n1 && n1.ref, parentComponent, n2.el)
- }
- }
- function mountElement(
- vnode: HostVNode,
- container: HostElement,
- anchor: HostNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) {
- const tag = vnode.type as string
- isSVG = isSVG || tag === 'svg'
- const el = (vnode.el = hostCreateElement(tag, isSVG))
- const { props, shapeFlag, transition } = vnode
- if (props != null) {
- for (const key in props) {
- if (isReservedProp(key)) continue
- hostPatchProp(el, key, props[key], null, isSVG)
- }
- if (props.onVnodeBeforeMount != null) {
- invokeDirectiveHook(props.onVnodeBeforeMount, parentComponent, vnode)
- }
- }
- if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
- hostSetElementText(el, vnode.children as string)
- } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- mountChildren(
- vnode.children as HostVNodeChildren,
- el,
- null,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized || vnode.dynamicChildren !== null
- )
- }
- if (transition != null && !transition.persisted) {
- transition.beforeEnter(el)
- }
- hostInsert(el, container, anchor)
- const vnodeMountedHook = props && props.onVnodeMounted
- if (
- vnodeMountedHook != null ||
- (transition != null && !transition.persisted)
- ) {
- queuePostRenderEffect(() => {
- vnodeMountedHook &&
- invokeDirectiveHook(vnodeMountedHook, parentComponent, vnode)
- transition && !transition.persisted && transition.enter(el)
- }, parentSuspense)
- }
- }
- function mountChildren(
- children: HostVNodeChildren,
- container: HostElement,
- anchor: HostNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean,
- start: number = 0
- ) {
- for (let i = start; i < children.length; i++) {
- const child = optimized
- ? (children[i] as HostVNode)
- : (children[i] = normalizeVNode(children[i]))
- patch(
- null,
- child,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- }
- }
- function patchElement(
- n1: HostVNode,
- n2: HostVNode,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) {
- const el = (n2.el = n1.el) as HostElement
- const { patchFlag, dynamicChildren } = n2
- const oldProps = (n1 && n1.props) || EMPTY_OBJ
- const newProps = n2.props || EMPTY_OBJ
- if (newProps.onVnodeBeforeUpdate != null) {
- invokeDirectiveHook(newProps.onVnodeBeforeUpdate, parentComponent, n2, n1)
- }
- if (patchFlag > 0) {
- // the presence of a patchFlag means this element's render code was
- // generated by the compiler and can take the fast path.
- // in this path old node and new node are guaranteed to have the same shape
- // (i.e. at the exact same position in the source template)
- if (patchFlag & PatchFlags.FULL_PROPS) {
- // element props contain dynamic keys, full diff needed
- patchProps(
- el,
- n2,
- oldProps,
- newProps,
- parentComponent,
- parentSuspense,
- isSVG
- )
- } else {
- // class
- // this flag is matched when the element has dynamic class bindings.
- if (patchFlag & PatchFlags.CLASS) {
- if (oldProps.class !== newProps.class) {
- hostPatchProp(el, 'class', newProps.class, null, isSVG)
- }
- }
- // style
- // this flag is matched when the element has dynamic style bindings
- if (patchFlag & PatchFlags.STYLE) {
- hostPatchProp(el, 'style', newProps.style, oldProps.style, isSVG)
- }
- // props
- // This flag is matched when the element has dynamic prop/attr bindings
- // other than class and style. The keys of dynamic prop/attrs are saved for
- // faster iteration.
- // Note dynamic keys like :[foo]="bar" will cause this optimization to
- // bail out and go through a full diff because we need to unset the old key
- if (patchFlag & PatchFlags.PROPS) {
- // if the flag is present then dynamicProps must be non-null
- const propsToUpdate = n2.dynamicProps!
- for (let i = 0; i < propsToUpdate.length; i++) {
- const key = propsToUpdate[i]
- const prev = oldProps[key]
- const next = newProps[key]
- if (prev !== next) {
- hostPatchProp(
- el,
- key,
- next,
- prev,
- isSVG,
- n1.children as HostVNode[],
- parentComponent,
- parentSuspense,
- unmountChildren
- )
- }
- }
- }
- }
- // text
- // This flag is matched when the element has only dynamic text children.
- // this flag is terminal (i.e. skips children diffing).
- if (patchFlag & PatchFlags.TEXT) {
- if (n1.children !== n2.children) {
- hostSetElementText(el, n2.children as string)
- }
- return // terminal
- }
- } else if (!optimized && dynamicChildren == null) {
- // unoptimized, full diff
- patchProps(
- el,
- n2,
- oldProps,
- newProps,
- parentComponent,
- parentSuspense,
- isSVG
- )
- }
- if (dynamicChildren != null) {
- patchBlockChildren(
- n1.dynamicChildren!,
- dynamicChildren,
- el,
- parentComponent,
- parentSuspense,
- isSVG
- )
- } else if (!optimized) {
- // full diff
- patchChildren(n1, n2, el, null, parentComponent, parentSuspense, isSVG)
- }
- if (newProps.onVnodeUpdated != null) {
- queuePostRenderEffect(() => {
- invokeDirectiveHook(newProps.onVnodeUpdated, parentComponent, n2, n1)
- }, parentSuspense)
- }
- }
- // The fast path for blocks.
- function patchBlockChildren(
- oldChildren: HostVNode[],
- newChildren: HostVNode[],
- fallbackContainer: HostElement,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean
- ) {
- for (let i = 0; i < newChildren.length; i++) {
- const oldVNode = oldChildren[i]
- patch(
- oldVNode,
- newChildren[i],
- // - In the case of a Fragment, we need to provide the actual parent
- // of the Fragment itself so it can move its children.
- // - In the case of a Comment, this is likely a v-if toggle, which also
- // needs the correct parent container.
- // In other cases, the parent container is not actually used so we just
- // pass the block element here to avoid a DOM parentNode call.
- oldVNode.type === Fragment || oldVNode.type === Comment
- ? hostParentNode(oldVNode.el!)!
- : fallbackContainer,
- null,
- parentComponent,
- parentSuspense,
- isSVG,
- true
- )
- }
- }
- function patchProps(
- el: HostElement,
- vnode: HostVNode,
- oldProps: Data,
- newProps: Data,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean
- ) {
- if (oldProps !== newProps) {
- for (const key in newProps) {
- if (isReservedProp(key)) continue
- const next = newProps[key]
- const prev = oldProps[key]
- if (next !== prev) {
- hostPatchProp(
- el,
- key,
- next,
- prev,
- isSVG,
- vnode.children as HostVNode[],
- parentComponent,
- parentSuspense,
- unmountChildren
- )
- }
- }
- if (oldProps !== EMPTY_OBJ) {
- for (const key in oldProps) {
- if (!isReservedProp(key) && !(key in newProps)) {
- hostPatchProp(
- el,
- key,
- null,
- null,
- isSVG,
- vnode.children as HostVNode[],
- parentComponent,
- parentSuspense,
- unmountChildren
- )
- }
- }
- }
- }
- }
- let devFragmentID = 0
- function processFragment(
- n1: HostVNode | null,
- n2: HostVNode,
- container: HostElement,
- anchor: HostNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) {
- const showID = __DEV__ && !__TEST__
- const fragmentStartAnchor = (n2.el = n1
- ? n1.el
- : hostCreateComment(showID ? `fragment-${devFragmentID}-start` : ''))!
- const fragmentEndAnchor = (n2.anchor = n1
- ? n1.anchor
- : hostCreateComment(showID ? `fragment-${devFragmentID}-end` : ''))!
- if (showID) {
- devFragmentID++
- }
- if (n1 == null) {
- hostInsert(fragmentStartAnchor, container, anchor)
- hostInsert(fragmentEndAnchor, container, anchor)
- // a fragment can only have array children
- // since they are either generated by the compiler, or implicitly created
- // from arrays.
- mountChildren(
- n2.children as HostVNodeChildren,
- container,
- fragmentEndAnchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else {
- patchChildren(
- n1,
- n2,
- container,
- fragmentEndAnchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- }
- }
- function processPortal(
- n1: HostVNode | null,
- n2: HostVNode,
- container: HostElement,
- anchor: HostNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) {
- const targetSelector = n2.props && n2.props.target
- const { patchFlag, shapeFlag, children } = n2
- if (n1 == null) {
- const target = (n2.target = isString(targetSelector)
- ? hostQuerySelector(targetSelector)
- : targetSelector)
- if (target != null) {
- if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
- hostSetElementText(target, children as string)
- } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- mountChildren(
- children as HostVNodeChildren,
- target,
- null,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- }
- } else if (__DEV__) {
- warn('Invalid Portal target on mount:', target, `(${typeof target})`)
- }
- } else {
- // update content
- const target = (n2.target = n1.target)!
- if (patchFlag === PatchFlags.TEXT) {
- hostSetElementText(target, children as string)
- } else if (n2.dynamicChildren) {
- // fast path when the portal happens to be a block root
- patchBlockChildren(
- n1.dynamicChildren!,
- n2.dynamicChildren,
- container,
- parentComponent,
- parentSuspense,
- isSVG
- )
- } else if (!optimized) {
- patchChildren(
- n1,
- n2,
- target,
- null,
- parentComponent,
- parentSuspense,
- isSVG
- )
- }
- // target changed
- if (targetSelector !== (n1.props && n1.props.target)) {
- const nextTarget = (n2.target = isString(targetSelector)
- ? hostQuerySelector(targetSelector)
- : targetSelector)
- if (nextTarget != null) {
- // move content
- if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
- hostSetElementText(target, '')
- hostSetElementText(nextTarget, children as string)
- } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- for (let i = 0; i < (children as HostVNode[]).length; i++) {
- move(
- (children as HostVNode[])[i],
- nextTarget,
- null,
- MoveType.REORDER
- )
- }
- }
- } else if (__DEV__) {
- warn('Invalid Portal target on update:', target, `(${typeof target})`)
- }
- }
- }
- // insert an empty node as the placeholder for the portal
- processCommentNode(n1, n2, container, anchor)
- }
- function processComponent(
- n1: HostVNode | null,
- n2: HostVNode,
- container: HostElement,
- anchor: HostNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) {
- if (n1 == null) {
- if (n2.shapeFlag & ShapeFlags.COMPONENT_KEPT_ALIVE) {
- ;(parentComponent!.sink as KeepAliveSink).activate(
- n2,
- container,
- anchor
- )
- } else {
- mountComponent(
- n2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG
- )
- }
- } else {
- const instance = (n2.component = n1.component)!
- if (shouldUpdateComponent(n1, n2, parentComponent, optimized)) {
- if (
- __FEATURE_SUSPENSE__ &&
- instance.asyncDep &&
- !instance.asyncResolved
- ) {
- // async & still pending - just update props and slots
- // since the component's reactive effect for render isn't set-up yet
- if (__DEV__) {
- pushWarningContext(n2)
- }
- updateComponentPreRender(instance, n2)
- if (__DEV__) {
- popWarningContext()
- }
- return
- } else {
- // normal update
- instance.next = n2
- // instance.update is the reactive effect runner.
- instance.update()
- }
- } else {
- // no update needed. just copy over properties
- n2.component = n1.component
- n2.el = n1.el
- }
- }
- if (n2.ref !== null && parentComponent !== null) {
- if (__DEV__ && !(n2.shapeFlag & ShapeFlags.STATEFUL_COMPONENT)) {
- pushWarningContext(n2)
- warn(
- `Functional components do not support "ref" because they do not ` +
- `have instances.`
- )
- popWarningContext()
- }
- setRef(n2.ref, n1 && n1.ref, parentComponent, n2.component!.proxy)
- }
- }
- function mountComponent(
- initialVNode: HostVNode,
- container: HostElement,
- anchor: HostNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean
- ) {
- const instance: ComponentInternalInstance = (initialVNode.component = createComponentInstance(
- initialVNode,
- parentComponent
- ))
- // HMR
- if (__BUNDLER__ && __DEV__ && instance.type.__hmrId != null) {
- registerHMR(instance)
- }
- if (__DEV__) {
- pushWarningContext(initialVNode)
- }
- const Comp = initialVNode.type as Component
- // inject renderer internals for keepAlive
- if (isKeepAlive(initialVNode)) {
- const sink = instance.sink as KeepAliveSink
- sink.renderer = internals
- sink.parentSuspense = parentSuspense
- }
- // resolve props and slots for setup context
- const propsOptions = Comp.props
- resolveProps(instance, initialVNode.props, propsOptions)
- resolveSlots(instance, initialVNode.children)
- // setup stateful logic
- if (initialVNode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
- setupStatefulComponent(instance, parentSuspense)
- }
- // setup() is async. This component relies on async logic to be resolved
- // before proceeding
- if (__FEATURE_SUSPENSE__ && instance.asyncDep) {
- if (!parentSuspense) {
- if (__DEV__) warn('async setup() is used without a suspense boundary!')
- return
- }
- parentSuspense.registerDep(instance, setupRenderEffect)
- // give it a placeholder
- const placeholder = (instance.subTree = createVNode(Comment))
- processCommentNode(null, placeholder, container, anchor)
- initialVNode.el = placeholder.el
- return
- }
- setupRenderEffect(
- instance,
- parentSuspense,
- initialVNode,
- container,
- anchor,
- isSVG
- )
- if (__DEV__) {
- popWarningContext()
- }
- }
- function setupRenderEffect(
- instance: ComponentInternalInstance,
- parentSuspense: HostSuspenseBoundary | null,
- initialVNode: HostVNode,
- container: HostElement,
- anchor: HostNode | null,
- isSVG: boolean
- ) {
- // create reactive effect for rendering
- instance.update = effect(function componentEffect() {
- if (!instance.isMounted) {
- const subTree = (instance.subTree = renderComponentRoot(instance))
- // beforeMount hook
- if (instance.bm !== null) {
- invokeHooks(instance.bm)
- }
- patch(null, subTree, container, anchor, instance, parentSuspense, isSVG)
- initialVNode.el = subTree.el
- // mounted hook
- if (instance.m !== null) {
- queuePostRenderEffect(instance.m, parentSuspense)
- }
- // activated hook for keep-alive roots.
- if (
- instance.a !== null &&
- instance.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
- ) {
- queuePostRenderEffect(instance.a, parentSuspense)
- }
- instance.isMounted = true
- } else {
- // updateComponent
- // This is triggered by mutation of component's own state (next: null)
- // OR parent calling processComponent (next: HostVNode)
- const { next } = instance
- if (__DEV__) {
- pushWarningContext(next || instance.vnode)
- }
- if (next !== null) {
- updateComponentPreRender(instance, next)
- }
- const nextTree = renderComponentRoot(instance)
- const prevTree = instance.subTree
- instance.subTree = nextTree
- // beforeUpdate hook
- if (instance.bu !== null) {
- invokeHooks(instance.bu)
- }
- // reset refs
- // only needed if previous patch had refs
- if (instance.refs !== EMPTY_OBJ) {
- instance.refs = {}
- }
- patch(
- prevTree,
- nextTree,
- // parent may have changed if it's in a portal
- hostParentNode(prevTree.el as HostNode) as HostElement,
- // anchor may have changed if it's in a fragment
- getNextHostNode(prevTree),
- instance,
- parentSuspense,
- isSVG
- )
- instance.vnode.el = nextTree.el
- if (next === null) {
- // self-triggered update. In case of HOC, update parent component
- // vnode el. HOC is indicated by parent instance's subTree pointing
- // to child component's vnode
- updateHOCHostEl(instance, nextTree.el)
- }
- // updated hook
- if (instance.u !== null) {
- queuePostRenderEffect(instance.u, parentSuspense)
- }
- if (__DEV__) {
- popWarningContext()
- }
- }
- }, __DEV__ ? createDevEffectOptions(instance) : prodEffectOptions)
- }
- function updateComponentPreRender(
- instance: ComponentInternalInstance,
- nextVNode: HostVNode
- ) {
- nextVNode.component = instance
- instance.vnode = nextVNode
- instance.next = null
- resolveProps(instance, nextVNode.props, (nextVNode.type as Component).props)
- resolveSlots(instance, nextVNode.children)
- }
- function patchChildren(
- n1: HostVNode | null,
- n2: HostVNode,
- container: HostElement,
- anchor: HostNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean = false
- ) {
- const c1 = n1 && n1.children
- const prevShapeFlag = n1 ? n1.shapeFlag : 0
- const c2 = n2.children
- const { patchFlag, shapeFlag } = n2
- if (patchFlag === PatchFlags.BAIL) {
- optimized = false
- }
- // fast path
- if (patchFlag > 0) {
- optimized = true
- if (patchFlag & PatchFlags.KEYED_FRAGMENT) {
- // this could be either fully-keyed or mixed (some keyed some not)
- // presence of patchFlag means children are guaranteed to be arrays
- patchKeyedChildren(
- c1 as HostVNode[],
- c2 as HostVNodeChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- return
- } else if (patchFlag & PatchFlags.UNKEYED_FRAGMENT) {
- // unkeyed
- patchUnkeyedChildren(
- c1 as HostVNode[],
- c2 as HostVNodeChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- return
- }
- }
- // children has 3 possibilities: text, array or no children.
- if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
- // text children fast path
- if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- unmountChildren(c1 as HostVNode[], parentComponent, parentSuspense)
- }
- if (c2 !== c1) {
- hostSetElementText(container, c2 as string)
- }
- } else {
- if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- // prev children was array
- if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- // two arrays, cannot assume anything, do full diff
- patchKeyedChildren(
- c1 as HostVNode[],
- c2 as HostVNodeChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else {
- // no new children, just unmount old
- unmountChildren(
- c1 as HostVNode[],
- parentComponent,
- parentSuspense,
- true
- )
- }
- } else {
- // prev children was text OR null
- // new children is array OR null
- if (prevShapeFlag & ShapeFlags.TEXT_CHILDREN) {
- hostSetElementText(container, '')
- }
- // mount new if array
- if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- mountChildren(
- c2 as HostVNodeChildren,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- }
- }
- }
- }
- function patchUnkeyedChildren(
- c1: HostVNode[],
- c2: HostVNodeChildren,
- container: HostElement,
- anchor: HostNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) {
- c1 = c1 || EMPTY_ARR
- c2 = c2 || EMPTY_ARR
- const oldLength = c1.length
- const newLength = c2.length
- const commonLength = Math.min(oldLength, newLength)
- let i
- for (i = 0; i < commonLength; i++) {
- const nextChild = optimized
- ? (c2[i] as HostVNode)
- : (c2[i] = normalizeVNode(c2[i]))
- patch(
- c1[i],
- nextChild,
- container,
- null,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- }
- if (oldLength > newLength) {
- // remove old
- unmountChildren(c1, parentComponent, parentSuspense, true, commonLength)
- } else {
- // mount new
- mountChildren(
- c2,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized,
- commonLength
- )
- }
- }
- // can be all-keyed or mixed
- function patchKeyedChildren(
- c1: HostVNode[],
- c2: HostVNodeChildren,
- container: HostElement,
- parentAnchor: HostNode | null,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- isSVG: boolean,
- optimized: boolean
- ) {
- let i = 0
- const l2 = c2.length
- let e1 = c1.length - 1 // prev ending index
- let e2 = l2 - 1 // next ending index
- // 1. sync from start
- // (a b) c
- // (a b) d e
- while (i <= e1 && i <= e2) {
- const n1 = c1[i]
- const n2 = optimized
- ? (c2[i] as HostVNode)
- : (c2[i] = normalizeVNode(c2[i]))
- if (isSameVNodeType(n1, n2)) {
- patch(
- n1,
- n2,
- container,
- parentAnchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else {
- break
- }
- i++
- }
- // 2. sync from end
- // a (b c)
- // d e (b c)
- while (i <= e1 && i <= e2) {
- const n1 = c1[e1]
- const n2 = optimized
- ? (c2[i] as HostVNode)
- : (c2[e2] = normalizeVNode(c2[e2]))
- if (isSameVNodeType(n1, n2)) {
- patch(
- n1,
- n2,
- container,
- parentAnchor,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- } else {
- break
- }
- e1--
- e2--
- }
- // 3. common sequence + mount
- // (a b)
- // (a b) c
- // i = 2, e1 = 1, e2 = 2
- // (a b)
- // c (a b)
- // i = 0, e1 = -1, e2 = 0
- if (i > e1) {
- if (i <= e2) {
- const nextPos = e2 + 1
- const anchor =
- nextPos < l2 ? (c2[nextPos] as HostVNode).el : parentAnchor
- while (i <= e2) {
- patch(
- null,
- optimized ? (c2[i] as HostVNode) : (c2[i] = normalizeVNode(c2[i])),
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG
- )
- i++
- }
- }
- }
- // 4. common sequence + unmount
- // (a b) c
- // (a b)
- // i = 2, e1 = 2, e2 = 1
- // a (b c)
- // (b c)
- // i = 0, e1 = 0, e2 = -1
- else if (i > e2) {
- while (i <= e1) {
- unmount(c1[i], parentComponent, parentSuspense, true)
- i++
- }
- }
- // 5. unknown sequence
- // [i ... e1 + 1]: a b [c d e] f g
- // [i ... e2 + 1]: a b [e d c h] f g
- // i = 2, e1 = 4, e2 = 5
- else {
- const s1 = i // prev starting index
- const s2 = i // next starting index
- // 5.1 build key:index map for newChildren
- const keyToNewIndexMap: Map<string | number, number> = new Map()
- for (i = s2; i <= e2; i++) {
- const nextChild = optimized
- ? (c2[i] as HostVNode)
- : (c2[i] = normalizeVNode(c2[i]))
- if (nextChild.key != null) {
- if (__DEV__ && keyToNewIndexMap.has(nextChild.key)) {
- warn(
- `Duplicate keys found during update:`,
- JSON.stringify(nextChild.key),
- `Make sure keys are unique.`
- )
- }
- keyToNewIndexMap.set(nextChild.key, i)
- }
- }
- // 5.2 loop through old children left to be patched and try to patch
- // matching nodes & remove nodes that are no longer present
- let j
- let patched = 0
- const toBePatched = e2 - s2 + 1
- let moved = false
- // used to track whether any node has moved
- let maxNewIndexSoFar = 0
- // works as Map<newIndex, oldIndex>
- // Note that oldIndex is offset by +1
- // and oldIndex = 0 is a special value indicating the new node has
- // no corresponding old node.
- // used for determining longest stable subsequence
- const newIndexToOldIndexMap = new Array(toBePatched)
- for (i = 0; i < toBePatched; i++) newIndexToOldIndexMap[i] = 0
- for (i = s1; i <= e1; i++) {
- const prevChild = c1[i]
- if (patched >= toBePatched) {
- // all new children have been patched so this can only be a removal
- unmount(prevChild, parentComponent, parentSuspense, true)
- continue
- }
- let newIndex
- if (prevChild.key != null) {
- newIndex = keyToNewIndexMap.get(prevChild.key)
- } else {
- // key-less node, try to locate a key-less node of the same type
- for (j = s2; j <= e2; j++) {
- if (
- newIndexToOldIndexMap[j - s2] === 0 &&
- isSameVNodeType(prevChild, c2[j] as HostVNode)
- ) {
- newIndex = j
- break
- }
- }
- }
- if (newIndex === undefined) {
- unmount(prevChild, parentComponent, parentSuspense, true)
- } else {
- newIndexToOldIndexMap[newIndex - s2] = i + 1
- if (newIndex >= maxNewIndexSoFar) {
- maxNewIndexSoFar = newIndex
- } else {
- moved = true
- }
- patch(
- prevChild,
- c2[newIndex] as HostVNode,
- container,
- null,
- parentComponent,
- parentSuspense,
- isSVG,
- optimized
- )
- patched++
- }
- }
- // 5.3 move and mount
- // generate longest stable subsequence only when nodes have moved
- const increasingNewIndexSequence = moved
- ? getSequence(newIndexToOldIndexMap)
- : EMPTY_ARR
- j = increasingNewIndexSequence.length - 1
- // looping backwards so that we can use last patched node as anchor
- for (i = toBePatched - 1; i >= 0; i--) {
- const nextIndex = s2 + i
- const nextChild = c2[nextIndex] as HostVNode
- const anchor =
- nextIndex + 1 < l2
- ? (c2[nextIndex + 1] as HostVNode).el
- : parentAnchor
- if (newIndexToOldIndexMap[i] === 0) {
- // mount new
- patch(
- null,
- nextChild,
- container,
- anchor,
- parentComponent,
- parentSuspense,
- isSVG
- )
- } else if (moved) {
- // move if:
- // There is no stable subsequence (e.g. a reverse)
- // OR current node is not among the stable sequence
- if (j < 0 || i !== increasingNewIndexSequence[j]) {
- move(nextChild, container, anchor, MoveType.REORDER)
- } else {
- j--
- }
- }
- }
- }
- }
- function move(
- vnode: HostVNode,
- container: HostElement,
- anchor: HostNode | null,
- type: MoveType,
- parentSuspense: HostSuspenseBoundary | null = null
- ) {
- if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
- move(vnode.component!.subTree, container, anchor, type)
- return
- }
- if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
- vnode.suspense!.move(container, anchor, type)
- return
- }
- if (vnode.type === Fragment) {
- hostInsert(vnode.el!, container, anchor)
- const children = vnode.children as HostVNode[]
- for (let i = 0; i < children.length; i++) {
- move(children[i], container, anchor, type)
- }
- hostInsert(vnode.anchor!, container, anchor)
- } else {
- // Plain element
- const { el, transition, shapeFlag } = vnode
- const needTransition =
- type !== MoveType.REORDER &&
- shapeFlag & ShapeFlags.ELEMENT &&
- transition != null
- if (needTransition) {
- if (type === MoveType.ENTER) {
- transition!.beforeEnter(el!)
- hostInsert(el!, container, anchor)
- queuePostRenderEffect(() => transition!.enter(el!), parentSuspense)
- } else {
- const { leave, delayLeave, afterLeave } = transition!
- const remove = () => hostInsert(el!, container, anchor)
- const performLeave = () => {
- leave(el!, () => {
- remove()
- afterLeave && afterLeave()
- })
- }
- if (delayLeave) {
- delayLeave(el!, remove, performLeave)
- } else {
- performLeave()
- }
- }
- } else {
- hostInsert(el!, container, anchor)
- }
- }
- }
- function unmount(
- vnode: HostVNode,
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- doRemove?: boolean
- ) {
- const {
- el,
- props,
- ref,
- type,
- children,
- dynamicChildren,
- shapeFlag,
- anchor,
- transition
- } = vnode
- // unset ref
- if (ref !== null && parentComponent !== null) {
- setRef(ref, null, parentComponent, null)
- }
- if (shapeFlag & ShapeFlags.COMPONENT) {
- if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
- ;(parentComponent!.sink as KeepAliveSink).deactivate(vnode)
- } else {
- unmountComponent(vnode.component!, parentSuspense, doRemove)
- }
- return
- }
- if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
- vnode.suspense!.unmount(parentSuspense, doRemove)
- return
- }
- if (props != null && props.onVnodeBeforeUnmount != null) {
- invokeDirectiveHook(props.onVnodeBeforeUnmount, parentComponent, vnode)
- }
- const shouldRemoveChildren = type === Fragment && doRemove
- if (dynamicChildren != null) {
- unmountChildren(
- dynamicChildren,
- parentComponent,
- parentSuspense,
- shouldRemoveChildren
- )
- } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
- unmountChildren(
- children as HostVNode[],
- parentComponent,
- parentSuspense,
- shouldRemoveChildren
- )
- }
- if (doRemove) {
- const remove = () => {
- hostRemove(vnode.el!)
- if (anchor != null) hostRemove(anchor)
- if (
- transition != null &&
- !transition.persisted &&
- transition.afterLeave
- ) {
- transition.afterLeave()
- }
- }
- if (
- vnode.shapeFlag & ShapeFlags.ELEMENT &&
- transition != null &&
- !transition.persisted
- ) {
- const { leave, delayLeave } = transition
- const performLeave = () => leave(el!, remove)
- if (delayLeave) {
- delayLeave(vnode.el!, remove, performLeave)
- } else {
- performLeave()
- }
- } else {
- remove()
- }
- }
- if (props != null && props.onVnodeUnmounted != null) {
- queuePostRenderEffect(() => {
- invokeDirectiveHook(props.onVnodeUnmounted!, parentComponent, vnode)
- }, parentSuspense)
- }
- }
- function unmountComponent(
- instance: ComponentInternalInstance,
- parentSuspense: HostSuspenseBoundary | null,
- doRemove?: boolean
- ) {
- // HMR
- if (__BUNDLER__ && __DEV__ && instance.type.__hmrId != null) {
- unregisterHMR(instance)
- }
- const { bum, effects, update, subTree, um, da, isDeactivated } = instance
- // beforeUnmount hook
- if (bum !== null) {
- invokeHooks(bum)
- }
- if (effects !== null) {
- for (let i = 0; i < effects.length; i++) {
- stop(effects[i])
- }
- }
- // update may be null if a component is unmounted before its async
- // setup has resolved.
- if (update !== null) {
- stop(update)
- unmount(subTree, instance, parentSuspense, doRemove)
- }
- // unmounted hook
- if (um !== null) {
- queuePostRenderEffect(um, parentSuspense)
- }
- // deactivated hook
- if (
- da !== null &&
- !isDeactivated &&
- instance.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
- ) {
- queuePostRenderEffect(da, parentSuspense)
- }
- queuePostFlushCb(() => {
- instance.isUnmounted = true
- })
- // A component with async dep inside a pending suspense is unmounted before
- // its async dep resolves. This should remove the dep from the suspense, and
- // cause the suspense to resolve immediately if that was the last dep.
- if (
- __FEATURE_SUSPENSE__ &&
- parentSuspense !== null &&
- !parentSuspense.isResolved &&
- !parentSuspense.isUnmounted &&
- instance.asyncDep !== null &&
- !instance.asyncResolved
- ) {
- parentSuspense.deps--
- if (parentSuspense.deps === 0) {
- parentSuspense.resolve()
- }
- }
- }
- function unmountChildren(
- children: HostVNode[],
- parentComponent: ComponentInternalInstance | null,
- parentSuspense: HostSuspenseBoundary | null,
- doRemove?: boolean,
- start: number = 0
- ) {
- for (let i = start; i < children.length; i++) {
- unmount(children[i], parentComponent, parentSuspense, doRemove)
- }
- }
- function getNextHostNode(vnode: HostVNode): HostNode | null {
- if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
- return getNextHostNode(vnode.component!.subTree)
- }
- if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
- return vnode.suspense!.next()
- }
- return hostNextSibling((vnode.anchor || vnode.el)!)
- }
- function setRef(
- ref: string | Function | Ref,
- oldRef: string | Function | Ref | null,
- parent: ComponentInternalInstance,
- value: HostNode | ComponentPublicInstance | null
- ) {
- const refs = parent.refs === EMPTY_OBJ ? (parent.refs = {}) : parent.refs
- const renderContext = toRaw(parent.renderContext)
- // unset old ref
- if (oldRef !== null && oldRef !== ref) {
- if (isString(oldRef)) {
- refs[oldRef] = null
- const oldSetupRef = renderContext[oldRef]
- if (isRef(oldSetupRef)) {
- oldSetupRef.value = null
- }
- } else if (isRef(oldRef)) {
- oldRef.value = null
- }
- }
- if (isString(ref)) {
- const setupRef = renderContext[ref]
- if (isRef(setupRef)) {
- setupRef.value = value
- }
- refs[ref] = value
- } else if (isRef(ref)) {
- ref.value = value
- } else if (isFunction(ref)) {
- callWithErrorHandling(ref, parent, ErrorCodes.FUNCTION_REF, [value, refs])
- } else if (__DEV__) {
- warn('Invalid template ref type:', value, `(${typeof value})`)
- }
- }
- const render: RootRenderFunction<
- HostNode,
- HostElement & {
- _vnode: HostVNode | null
- }
- > = (vnode, container) => {
- if (vnode == null) {
- if (container._vnode) {
- unmount(container._vnode, null, null, true)
- }
- } else {
- patch(container._vnode || null, vnode, container)
- }
- flushPostFlushCbs()
- container._vnode = vnode
- }
- return {
- render,
- createApp: createAppAPI(render)
- }
- }
- // https://en.wikipedia.org/wiki/Longest_increasing_subsequence
- function getSequence(arr: number[]): number[] {
- const p = arr.slice()
- const result = [0]
- let i, j, u, v, c
- const len = arr.length
- for (i = 0; i < len; i++) {
- const arrI = arr[i]
- if (arrI !== 0) {
- j = result[result.length - 1]
- if (arr[j] < arrI) {
- p[i] = j
- result.push(i)
- continue
- }
- u = 0
- v = result.length - 1
- while (u < v) {
- c = ((u + v) / 2) | 0
- if (arr[result[c]] < arrI) {
- u = c + 1
- } else {
- v = c
- }
- }
- if (arrI < arr[result[u]]) {
- if (u > 0) {
- p[i] = result[u - 1]
- }
- result[u] = i
- }
- }
- }
- u = result.length
- v = result[u - 1]
- while (u-- > 0) {
- result[u] = v
- v = p[v]
- }
- return result
- }
|