vue3-yuanma/packages/runtime-core/src/renderer.ts

1798 lines
50 KiB
TypeScript
Raw Normal View History

import {
Text,
Fragment,
Comment,
2019-05-28 09:19:47 +00:00
Portal,
cloneIfMounted,
2019-05-28 05:27:31 +00:00
normalizeVNode,
VNode,
2019-09-07 15:28:40 +00:00
VNodeChildren,
createVNode,
isSameVNodeType
2019-05-28 09:19:47 +00:00
} from './vnode'
import {
2019-09-06 16:58:31 +00:00
ComponentInternalInstance,
defineComponentInstance,
2019-09-09 20:00:50 +00:00
setupStatefulComponent,
2019-10-22 15:26:48 +00:00
Component,
Data
2019-05-28 09:19:47 +00:00
} from './component'
2019-09-06 15:25:11 +00:00
import {
renderComponentRoot,
shouldUpdateComponent,
updateHOCHostEl
2019-09-06 15:25:11 +00:00
} from './componentRenderUtils'
2019-06-03 05:44:45 +00:00
import {
isString,
EMPTY_OBJ,
EMPTY_ARR,
isReservedProp,
2019-09-11 00:53:28 +00:00
isFunction,
PatchFlags
2019-06-03 05:44:45 +00:00
} from '@vue/shared'
2019-05-28 11:59:54 +00:00
import { queueJob, queuePostFlushCb, flushPostFlushCbs } from './scheduler'
import {
effect,
stop,
ReactiveEffectOptions,
isRef,
Ref,
2019-10-22 15:26:48 +00:00
toRaw,
DebuggerEvent
} from '@vue/reactivity'
2019-05-29 03:36:16 +00:00
import { resolveProps } from './componentProps'
2019-05-31 10:07:43 +00:00
import { resolveSlots } from './componentSlots'
2019-08-22 15:12:37 +00:00
import { ShapeFlags } from './shapeFlags'
2019-08-30 14:36:30 +00:00
import { pushWarningContext, popWarningContext, warn } from './warning'
2019-08-31 21:06:39 +00:00
import { invokeDirectiveHook } from './directives'
import { ComponentPublicInstance } from './componentProxy'
import { App, createAppAPI } from './apiCreateApp'
import {
SuspenseBoundary,
queueEffectWithSuspense,
SuspenseImpl
} from './components/Suspense'
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import { KeepAliveSink, isKeepAlive } from './components/KeepAlive'
import { registerHMR, unregisterHMR } from './hmr'
2019-09-11 00:53:28 +00:00
2019-12-13 22:57:21 +00:00
const __HMR__ = __BUNDLER__ && __DEV__
2019-09-06 20:58:32 +00:00
export interface RendererOptions<HostNode = any, HostElement = any> {
patchProp(
2019-09-06 20:58:32 +00:00
el: HostElement,
key: string,
value: any,
oldValue: any,
2019-12-16 18:33:10 +00:00
isSVG?: boolean,
2019-09-06 20:58:32 +00:00
prevChildren?: VNode<HostNode, HostElement>[],
2019-09-06 16:58:31 +00:00
parentComponent?: ComponentInternalInstance | null,
2019-09-10 16:08:30 +00:00
parentSuspense?: SuspenseBoundary<HostNode, HostElement> | null,
unmountChildren?: (
2019-09-06 20:58:32 +00:00
children: VNode<HostNode, HostElement>[],
2019-09-10 16:08:30 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: SuspenseBoundary<HostNode, HostElement> | null
) => void
): void
2019-09-06 20:58:32 +00:00
insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void
remove(el: HostNode): void
2019-09-06 20:58:32 +00:00
createElement(type: string, isSVG?: boolean): HostElement
createText(text: string): HostNode
createComment(text: string): HostNode
setText(node: HostNode, text: string): void
2019-09-06 20:58:32 +00:00
setElementText(node: HostElement, text: string): void
parentNode(node: HostNode): HostElement | null
nextSibling(node: HostNode): HostNode | null
2019-09-06 20:58:32 +00:00
querySelector(selector: string): HostElement | null
2019-12-16 18:33:10 +00:00
setScopeId(el: HostNode, id: string): void
}
2019-09-06 20:58:32 +00:00
export type RootRenderFunction<HostNode, HostElement> = (
vnode: VNode<HostNode, HostElement> | null,
dom: HostElement
2019-09-02 20:09:34 +00:00
) => 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
}
}
2019-10-30 02:28:38 +00:00
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
2019-09-06 20:58:32 +00:00
/**
* 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>
2019-09-06 20:58:32 +00:00
2018-09-19 15:35:38 +00:00
const {
2019-05-27 07:28:56 +00:00
insert: hostInsert,
remove: hostRemove,
2019-05-25 15:51:20 +00:00
patchProp: hostPatchProp,
createElement: hostCreateElement,
createText: hostCreateText,
createComment: hostCreateComment,
setText: hostSetText,
setElementText: hostSetElementText,
2019-05-28 05:27:31 +00:00
parentNode: hostParentNode,
2019-05-29 08:10:25 +00:00
nextSibling: hostNextSibling,
2019-12-16 18:33:10 +00:00
querySelector: hostQuerySelector,
setScopeId: hostSetScopeId
} = options
2019-05-25 15:51:20 +00:00
const internals: RendererInternals<HostNode, HostElement> = {
patch,
unmount,
move,
next: getNextHostNode,
options
}
function patch(
2019-09-06 20:58:32 +00:00
n1: HostVNode | null, // null means this is a mount
n2: HostVNode,
container: HostElement,
anchor: HostNode | null = null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null = null,
parentSuspense: HostSuspenseBoundary | null = null,
2019-06-03 01:43:28 +00:00
isSVG: boolean = false,
2019-06-02 08:35:19 +00:00
optimized: boolean = false
) {
2019-05-25 15:51:20 +00:00
// patching & not same type, unmount old tree
if (n1 != null && !isSameVNodeType(n1, n2)) {
anchor = getNextHostNode(n1)
unmount(n1, parentComponent, parentSuspense, true)
n1 = null
2019-05-25 15:51:20 +00:00
}
2019-06-02 08:35:19 +00:00
const { type, shapeFlag } = n2
2019-05-28 09:19:47 +00:00
switch (type) {
case Text:
processText(n1, n2, container, anchor)
break
case Comment:
processCommentNode(n1, n2, container, anchor)
2019-05-28 09:19:47 +00:00
break
case Fragment:
2019-06-03 01:43:28 +00:00
processFragment(
n1,
n2,
container,
anchor,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized
)
2019-05-28 09:19:47 +00:00
break
case Portal:
2019-06-03 01:43:28 +00:00
processPortal(
n1,
n2,
container,
anchor,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized
)
2019-05-28 09:19:47 +00:00
break
default:
if (shapeFlag & ShapeFlags.ELEMENT) {
processElement(
2019-09-09 20:28:32 +00:00
n1,
n2,
container,
anchor,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-09-09 20:28:32 +00:00
isSVG,
optimized
)
} else if (shapeFlag & ShapeFlags.COMPONENT) {
processComponent(
2019-06-03 01:43:28 +00:00
n1,
n2,
container,
anchor,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized
)
} else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
;(type as typeof SuspenseImpl).process(
2019-06-03 01:43:28 +00:00
n1,
n2,
container,
anchor,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized,
internals
2019-06-03 01:43:28 +00:00
)
2019-08-30 14:36:30 +00:00
} else if (__DEV__) {
2019-09-06 20:58:32 +00:00
warn('Invalid HostVNode type:', n2.type, `(${typeof n2.type})`)
2019-05-28 09:19:47 +00:00
}
2018-09-19 15:35:38 +00:00
}
}
function processText(
2019-09-06 20:58:32 +00:00
n1: HostVNode | null,
n2: HostVNode,
container: HostElement,
anchor: HostNode | null
) {
2019-05-25 15:51:20 +00:00
if (n1 == null) {
2019-05-27 07:28:56 +00:00
hostInsert(
(n2.el = hostCreateText(n2.children as string)),
container,
anchor
)
} else {
2019-09-06 20:58:32 +00:00
const el = (n2.el = n1.el) as HostNode
2019-05-25 15:51:20 +00:00
if (n2.children !== n1.children) {
hostSetText(el, n2.children as string)
2018-11-02 05:21:38 +00:00
}
2018-09-19 15:35:38 +00:00
}
}
function processCommentNode(
2019-09-06 20:58:32 +00:00
n1: HostVNode | null,
n2: HostVNode,
container: HostElement,
anchor: HostNode | null
) {
2019-05-25 15:51:20 +00:00
if (n1 == null) {
hostInsert(
(n2.el = hostCreateComment((n2.children as string) || '')),
container,
anchor
)
2018-11-02 05:21:38 +00:00
} else {
// there's no support for dynamic comments
2019-05-25 15:51:20 +00:00
n2.el = n1.el
2018-09-19 15:35:38 +00:00
}
}
function processElement(
2019-09-06 20:58:32 +00:00
n1: HostVNode | null,
n2: HostVNode,
container: HostElement,
anchor: HostNode | null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
optimized: boolean
) {
2019-05-25 15:51:20 +00:00
if (n1 == null) {
2019-09-10 16:08:30 +00:00
mountElement(
n2,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
optimized
2019-09-10 16:08:30 +00:00
)
2018-09-19 15:35:38 +00:00
} else {
2019-09-10 16:08:30 +00:00
patchElement(n1, n2, parentComponent, parentSuspense, isSVG, optimized)
2018-09-19 15:35:38 +00:00
}
2019-06-03 05:44:45 +00:00
if (n2.ref !== null && parentComponent !== null) {
setRef(n2.ref, n1 && n1.ref, parentComponent, n2.el)
2019-06-03 05:44:45 +00:00
}
2018-09-19 15:35:38 +00:00
}
2019-06-03 01:43:28 +00:00
function mountElement(
2019-09-06 20:58:32 +00:00
vnode: HostVNode,
container: HostElement,
anchor: HostNode | null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
isSVG: boolean,
optimized: boolean
2019-06-03 01:43:28 +00:00
) {
const tag = vnode.type as string
isSVG = isSVG || tag === 'svg'
const el = (vnode.el = hostCreateElement(tag, isSVG))
2019-12-16 18:33:10 +00:00
const { props, shapeFlag, transition, scopeId } = vnode
// props
if (props != null) {
for (const key in props) {
2019-06-03 05:44:45 +00:00
if (isReservedProp(key)) continue
2019-06-03 01:43:28 +00:00
hostPatchProp(el, key, props[key], null, isSVG)
}
if (props.onVnodeBeforeMount != null) {
invokeDirectiveHook(props.onVnodeBeforeMount, parentComponent, vnode)
2019-09-01 02:17:46 +00:00
}
2018-09-19 15:35:38 +00:00
}
2019-12-16 18:33:10 +00:00
// scopeId
2019-12-16 21:45:10 +00:00
if (__BUNDLER__) {
if (scopeId !== null) {
hostSetScopeId(el, scopeId)
}
2019-12-16 18:33:10 +00:00
const treeOwnerId = parentComponent && parentComponent.type.__scopeId
// vnode's own scopeId and the current patched component's scopeId is
// different - this is a slot content node.
if (treeOwnerId != null && treeOwnerId !== scopeId) {
2019-12-16 21:45:10 +00:00
hostSetScopeId(el, treeOwnerId + '-s')
2019-12-16 18:33:10 +00:00
}
}
// children
2019-08-22 15:12:37 +00:00
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
hostSetElementText(el, vnode.children as string)
2019-08-22 15:12:37 +00:00
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
2019-06-03 01:43:28 +00:00
mountChildren(
2019-09-06 20:58:32 +00:00
vnode.children as HostVNodeChildren,
2019-06-03 01:43:28 +00:00
el,
null,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
isSVG,
optimized || vnode.dynamicChildren !== null
2019-06-03 01:43:28 +00:00
)
2018-09-19 15:35:38 +00:00
}
if (transition != null && !transition.persisted) {
transition.beforeEnter(el)
}
2019-05-27 07:28:56 +00:00
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)
2019-09-11 00:53:28 +00:00
}, parentSuspense)
2019-08-31 21:06:39 +00:00
}
2018-09-19 15:35:38 +00:00
}
function mountChildren(
2019-09-06 20:58:32 +00:00
children: HostVNodeChildren,
container: HostElement,
anchor: HostNode | null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
optimized: boolean,
start: number = 0
) {
2019-05-25 15:51:20 +00:00
for (let i = start; i < children.length; i++) {
const child = (children[i] = optimized
? cloneIfMounted(children[i] as HostVNode)
: normalizeVNode(children[i]))
2019-09-10 16:08:30 +00:00
patch(
null,
child,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
optimized
2019-09-10 16:08:30 +00:00
)
2018-09-19 15:35:38 +00:00
}
}
2019-06-03 01:43:28 +00:00
function patchElement(
2019-09-06 20:58:32 +00:00
n1: HostVNode,
n2: HostVNode,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
optimized: boolean
) {
2019-09-06 20:58:32 +00:00
const el = (n2.el = n1.el) as HostElement
2019-12-13 22:57:21 +00:00
let { patchFlag, dynamicChildren } = n2
2019-05-28 09:19:47 +00:00
const oldProps = (n1 && n1.props) || EMPTY_OBJ
const newProps = n2.props || EMPTY_OBJ
2018-09-19 15:35:38 +00:00
if (newProps.onVnodeBeforeUpdate != null) {
invokeDirectiveHook(newProps.onVnodeBeforeUpdate, parentComponent, n2, n1)
2019-09-01 02:17:46 +00:00
}
2019-12-13 22:57:21 +00:00
if (__HMR__ && parentComponent && parentComponent.renderUpdated) {
// HMR updated, force full diff
patchFlag = 0
optimized = false
2019-12-16 22:57:34 +00:00
dynamicChildren = null
2019-12-13 22:57:21 +00:00
}
if (patchFlag > 0) {
2019-05-25 15:51:20 +00:00
// 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)
2018-09-19 15:35:38 +00:00
2019-08-22 15:12:37 +00:00
if (patchFlag & PatchFlags.FULL_PROPS) {
// element props contain dynamic keys, full diff needed
2019-09-10 16:08:30 +00:00
patchProps(
el,
n2,
oldProps,
newProps,
parentComponent,
parentSuspense,
isSVG
)
} else {
// class
// this flag is matched when the element has dynamic class bindings.
2019-08-22 15:12:37 +00:00
if (patchFlag & PatchFlags.CLASS) {
if (oldProps.class !== newProps.class) {
2019-06-03 01:43:28 +00:00
hostPatchProp(el, 'class', newProps.class, null, isSVG)
}
2018-09-19 15:35:38 +00:00
}
// style
// this flag is matched when the element has dynamic style bindings
2019-08-22 15:12:37 +00:00
if (patchFlag & PatchFlags.STYLE) {
2019-06-03 01:43:28 +00:00
hostPatchProp(el, 'style', newProps.style, oldProps.style, isSVG)
}
2019-05-25 15:51:20 +00:00
// 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
2019-08-22 15:12:37 +00:00
if (patchFlag & PatchFlags.PROPS) {
// if the flag is present then dynamicProps must be non-null
2019-10-05 14:09:34 +00:00
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,
2019-06-03 01:43:28 +00:00
isSVG,
2019-09-06 20:58:32 +00:00
n1.children as HostVNode[],
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
unmountChildren
)
}
2018-09-19 15:35:38 +00:00
}
}
}
2019-05-25 15:51:20 +00:00
// text
// This flag is matched when the element has only dynamic text children.
// this flag is terminal (i.e. skips children diffing).
2019-08-22 15:12:37 +00:00
if (patchFlag & PatchFlags.TEXT) {
2019-05-25 15:51:20 +00:00
if (n1.children !== n2.children) {
hostSetElementText(el, n2.children as string)
2018-09-19 15:35:38 +00:00
}
2019-05-25 15:51:20 +00:00
return // terminal
2018-09-19 15:35:38 +00:00
}
} else if (!optimized && dynamicChildren == null) {
2019-05-25 15:51:20 +00:00
// unoptimized, full diff
2019-09-10 16:08:30 +00:00
patchProps(
el,
n2,
oldProps,
newProps,
parentComponent,
parentSuspense,
isSVG
)
2018-09-19 15:35:38 +00:00
}
2019-05-25 15:51:20 +00:00
if (dynamicChildren != null) {
patchBlockChildren(
n1.dynamicChildren!,
dynamicChildren,
el,
parentComponent,
parentSuspense,
isSVG
)
2019-05-25 15:51:20 +00:00
} else if (!optimized) {
// full diff
2019-09-10 16:08:30 +00:00
patchChildren(n1, n2, el, null, parentComponent, parentSuspense, isSVG)
}
2019-09-01 02:17:46 +00:00
if (newProps.onVnodeUpdated != null) {
queuePostRenderEffect(() => {
invokeDirectiveHook(newProps.onVnodeUpdated, parentComponent, n2, n1)
2019-09-11 00:53:28 +00:00
}, parentSuspense)
2019-09-01 02:17:46 +00:00
}
}
// 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]
// Determine the container (parent element) for the patch.
// - 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 the case of a component, it could contain anything.
// In other cases, the parent container is not actually used so we just
// pass the block element here to avoid a DOM parentNode call.
const container =
oldVNode.type === Fragment ||
oldVNode.type === Comment ||
oldVNode.shapeFlag & ShapeFlags.COMPONENT
? hostParentNode(oldVNode.el!)!
: fallbackContainer
patch(
oldVNode,
newChildren[i],
container,
null,
parentComponent,
parentSuspense,
isSVG,
true
)
}
}
function patchProps(
2019-09-06 20:58:32 +00:00
el: HostElement,
vnode: HostVNode,
2019-10-22 15:26:48 +00:00
oldProps: Data,
newProps: Data,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean
) {
2019-05-25 15:51:20 +00:00
if (oldProps !== newProps) {
for (const key in newProps) {
2019-06-03 05:44:45 +00:00
if (isReservedProp(key)) continue
2019-05-25 15:51:20 +00:00
const next = newProps[key]
const prev = oldProps[key]
if (next !== prev) {
hostPatchProp(
el,
key,
next,
prev,
2019-06-03 01:43:28 +00:00
isSVG,
2019-09-06 20:58:32 +00:00
vnode.children as HostVNode[],
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
unmountChildren
)
2018-09-26 21:10:34 +00:00
}
2018-09-19 15:35:38 +00:00
}
2019-05-28 09:19:47 +00:00
if (oldProps !== EMPTY_OBJ) {
2019-05-25 15:51:20 +00:00
for (const key in oldProps) {
if (!isReservedProp(key) && !(key in newProps)) {
hostPatchProp(
el,
key,
null,
null,
2019-06-03 01:43:28 +00:00
isSVG,
2019-09-06 20:58:32 +00:00
vnode.children as HostVNode[],
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
unmountChildren
)
2019-05-25 15:51:20 +00:00
}
}
}
2018-09-19 15:35:38 +00:00
}
}
2019-11-04 16:24:37 +00:00
let devFragmentID = 0
function processFragment(
2019-09-06 20:58:32 +00:00
n1: HostVNode | null,
n2: HostVNode,
container: HostElement,
anchor: HostNode | null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
optimized: boolean
) {
2019-11-04 16:24:37 +00:00
const showID = __DEV__ && !__TEST__
const fragmentStartAnchor = (n2.el = n1
? n1.el
: hostCreateComment(showID ? `fragment-${devFragmentID}-start` : ''))!
2019-05-27 05:48:40 +00:00
const fragmentEndAnchor = (n2.anchor = n1
? n1.anchor
2019-11-04 16:24:37 +00:00
: hostCreateComment(showID ? `fragment-${devFragmentID}-end` : ''))!
2019-12-13 22:57:21 +00:00
2019-12-16 22:57:34 +00:00
let { patchFlag, dynamicChildren } = n2
if (patchFlag > 0) {
optimized = true
2019-11-04 16:24:37 +00:00
}
2019-12-13 22:57:21 +00:00
if (__HMR__ && parentComponent && parentComponent.renderUpdated) {
// HMR updated, force full diff
patchFlag = 0
optimized = false
2019-12-16 22:57:34 +00:00
dynamicChildren = null
2019-12-13 22:57:21 +00:00
}
2019-05-25 15:51:20 +00:00
if (n1 == null) {
if (showID) {
devFragmentID++
}
2019-05-27 07:28:56 +00:00
hostInsert(fragmentStartAnchor, container, anchor)
hostInsert(fragmentEndAnchor, container, anchor)
// a fragment can only have array children
2019-09-07 21:10:57 +00:00
// since they are either generated by the compiler, or implicitly created
// from arrays.
2019-06-03 01:43:28 +00:00
mountChildren(
2019-09-06 20:58:32 +00:00
n2.children as HostVNodeChildren,
2019-06-03 01:43:28 +00:00
container,
fragmentEndAnchor,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
isSVG,
optimized
2019-06-03 01:43:28 +00:00
)
2018-09-19 15:35:38 +00:00
} else {
2019-12-16 22:57:34 +00:00
if (patchFlag & PatchFlags.STABLE_FRAGMENT && dynamicChildren != null) {
// a stable fragment (template root or <template v-for>) doesn't need to
// patch children order, but it may contain dynamicChildren.
patchBlockChildren(
n1.dynamicChildren!,
2019-12-16 22:57:34 +00:00
dynamicChildren,
container,
parentComponent,
parentSuspense,
isSVG
)
} else {
// keyed / unkeyed, or manual fragments.
// for keyed & unkeyed, since they are compiler generated from v-for,
2019-12-13 10:49:01 +00:00
// each child is guaranteed to be a block so the fragment will never
// have dynamicChildren.
patchChildren(
n1,
n2,
container,
fragmentEndAnchor,
parentComponent,
parentSuspense,
isSVG,
optimized
)
}
2018-09-19 15:35:38 +00:00
}
}
2019-05-29 08:10:25 +00:00
function processPortal(
2019-09-06 20:58:32 +00:00
n1: HostVNode | null,
n2: HostVNode,
container: HostElement,
anchor: HostNode | null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
optimized: boolean
2019-05-29 08:10:25 +00:00
) {
const targetSelector = n2.props && n2.props.target
const { patchFlag, shapeFlag, children } = n2
2019-05-29 08:10:25 +00:00
if (n1 == null) {
const target = (n2.target = isString(targetSelector)
? hostQuerySelector(targetSelector)
: targetSelector)
2019-05-29 08:10:25 +00:00
if (target != null) {
2019-08-22 15:12:37 +00:00
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
hostSetElementText(target, children as string)
2019-08-22 15:12:37 +00:00
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
2019-06-03 01:43:28 +00:00
mountChildren(
2019-09-06 20:58:32 +00:00
children as HostVNodeChildren,
2019-06-03 01:43:28 +00:00
target,
null,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
isSVG,
optimized
2019-06-03 01:43:28 +00:00
)
2019-05-29 08:10:25 +00:00
}
2019-08-30 14:36:30 +00:00
} else if (__DEV__) {
warn('Invalid Portal target on mount:', target, `(${typeof target})`)
2019-05-29 08:10:25 +00:00
}
} else {
// update content
2019-10-05 14:09:34 +00:00
const target = (n2.target = n1.target)!
2019-08-22 15:12:37 +00:00
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
)
2019-05-29 08:10:25 +00:00
} else if (!optimized) {
2019-09-10 16:08:30 +00:00
patchChildren(
n1,
n2,
target,
null,
parentComponent,
parentSuspense,
isSVG
)
2019-05-29 08:10:25 +00:00
}
// target changed
if (targetSelector !== (n1.props && n1.props.target)) {
const nextTarget = (n2.target = isString(targetSelector)
? hostQuerySelector(targetSelector)
: targetSelector)
2019-05-29 08:10:25 +00:00
if (nextTarget != null) {
// move content
2019-08-22 15:12:37 +00:00
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
2019-05-29 08:10:25 +00:00
hostSetElementText(target, '')
hostSetElementText(nextTarget, children as string)
2019-08-22 15:12:37 +00:00
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
2019-09-06 20:58:32 +00:00
for (let i = 0; i < (children as HostVNode[]).length; i++) {
move(
(children as HostVNode[])[i],
nextTarget,
null,
MoveType.REORDER
)
2019-05-29 08:10:25 +00:00
}
}
2019-08-30 14:36:30 +00:00
} else if (__DEV__) {
warn('Invalid Portal target on update:', target, `(${typeof target})`)
2019-05-29 08:10:25 +00:00
}
}
}
// insert an empty node as the placeholder for the portal
processCommentNode(n1, n2, container, anchor)
2019-05-29 08:10:25 +00:00
}
2019-05-28 02:28:25 +00:00
function processComponent(
2019-09-06 20:58:32 +00:00
n1: HostVNode | null,
n2: HostVNode,
container: HostElement,
anchor: HostNode | null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
optimized: boolean
2019-05-28 05:27:31 +00:00
) {
if (n1 == null) {
2019-11-03 01:26:25 +00:00
if (n2.shapeFlag & ShapeFlags.COMPONENT_KEPT_ALIVE) {
2019-10-30 02:28:38 +00:00
;(parentComponent!.sink as KeepAliveSink).activate(
n2,
container,
anchor
)
} else {
mountComponent(
n2,
container,
anchor,
parentComponent,
parentSuspense,
isSVG
)
}
2019-05-28 05:27:31 +00:00
} else {
2019-10-05 14:09:34 +00:00
const instance = (n2.component = n1.component)!
2019-09-09 20:28:32 +00:00
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)
}
2019-09-12 05:52:14 +00:00
updateComponentPreRender(instance, n2)
if (__DEV__) {
popWarningContext()
}
return
} else {
// normal update
instance.next = n2
// instance.update is the reactive effect runner.
instance.update()
}
} else {
2019-09-09 20:00:50 +00:00
// no update needed. just copy over properties
2019-05-31 18:14:49 +00:00
n2.component = n1.component
n2.el = n1.el
}
2019-05-28 05:27:31 +00:00
}
2019-06-03 05:44:45 +00:00
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)
2019-06-03 05:44:45 +00:00
}
2019-05-28 05:27:31 +00:00
}
function mountComponent(
2019-09-06 20:58:32 +00:00
initialVNode: HostVNode,
container: HostElement,
anchor: HostNode | null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean
2019-05-28 05:27:31 +00:00
) {
const instance: ComponentInternalInstance = (initialVNode.component = defineComponentInstance(
2019-08-28 16:13:36 +00:00
initialVNode,
2019-06-03 01:43:28 +00:00
parentComponent
2019-05-28 11:36:15 +00:00
))
2019-08-28 16:13:36 +00:00
2019-12-13 22:57:21 +00:00
if (__HMR__ && instance.type.__hmrId != null) {
registerHMR(instance)
}
2019-08-30 14:36:30 +00:00
if (__DEV__) {
pushWarningContext(initialVNode)
}
2019-10-30 02:28:38 +00:00
const Comp = initialVNode.type as Component
// inject renderer internals for keepAlive
if (isKeepAlive(initialVNode)) {
2019-10-30 02:28:38 +00:00
const sink = instance.sink as KeepAliveSink
sink.renderer = internals
sink.parentSuspense = parentSuspense
}
2019-08-28 16:13:36 +00:00
// resolve props and slots for setup context
2019-10-30 02:28:38 +00:00
const propsOptions = Comp.props
2019-08-28 16:13:36 +00:00
resolveProps(instance, initialVNode.props, propsOptions)
resolveSlots(instance, initialVNode.children)
// setup stateful logic
if (initialVNode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
setupStatefulComponent(instance, parentSuspense)
2019-08-28 16:13:36 +00:00
}
2019-09-09 20:00:50 +00:00
// setup() is async. This component relies on async logic to be resolved
// before proceeding
2019-09-09 20:28:32 +00:00
if (__FEATURE_SUSPENSE__ && instance.asyncDep) {
2019-09-10 16:08:30 +00:00
if (!parentSuspense) {
if (__DEV__) warn('async setup() is used without a suspense boundary!')
return
2019-09-09 20:00:50 +00:00
}
parentSuspense.registerDep(instance, setupRenderEffect)
2019-09-09 21:24:42 +00:00
// give it a placeholder
const placeholder = (instance.subTree = createVNode(Comment))
processCommentNode(null, placeholder, container, anchor)
2019-09-09 21:24:42 +00:00
initialVNode.el = placeholder.el
2019-09-09 20:00:50 +00:00
return
}
2019-09-10 16:08:30 +00:00
setupRenderEffect(
instance,
parentSuspense,
initialVNode,
container,
anchor,
isSVG
)
2019-09-09 20:00:50 +00:00
if (__DEV__) {
popWarningContext()
}
}
function setupRenderEffect(
instance: ComponentInternalInstance,
parentSuspense: HostSuspenseBoundary | null,
2019-09-09 20:00:50 +00:00
initialVNode: HostVNode,
container: HostElement,
anchor: HostNode | null,
isSVG: boolean
) {
2019-08-28 16:13:36 +00:00
// create reactive effect for rendering
2019-10-22 15:52:29 +00:00
instance.update = effect(function componentEffect() {
2019-11-23 04:32:53 +00:00
if (!instance.isMounted) {
2019-10-22 15:52:29 +00:00
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)
}
2019-10-31 01:41:28 +00:00
// activated hook for keep-alive roots.
2019-10-31 03:32:29 +00:00
if (
instance.a !== null &&
2019-11-03 01:26:25 +00:00
instance.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
2019-10-31 03:32:29 +00:00
) {
2019-10-31 01:41:28 +00:00
queuePostRenderEffect(instance.a, parentSuspense)
}
2019-11-23 04:32:53 +00:00
instance.isMounted = true
2019-10-22 15:52:29 +00:00
} else {
// updateComponent
// This is triggered by mutation of component's own state (next: null)
// OR parent calling processComponent (next: HostVNode)
const { next } = instance
2019-08-30 14:36:30 +00:00
2019-10-22 15:52:29 +00:00
if (__DEV__) {
pushWarningContext(next || instance.vnode)
}
2019-08-30 14:36:30 +00:00
2019-10-22 15:52:29 +00:00
if (next !== null) {
updateComponentPreRender(instance, next)
}
const nextTree = renderComponentRoot(instance)
2019-10-22 15:52:29 +00:00
const prevTree = instance.subTree
instance.subTree = nextTree
2019-10-22 15:52:29 +00:00
// 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)
}
2019-08-30 14:36:30 +00:00
2019-10-22 15:52:29 +00:00
if (__DEV__) {
popWarningContext()
2019-08-30 14:36:30 +00:00
}
2019-10-22 15:52:29 +00:00
}
}, __DEV__ ? createDevEffectOptions(instance) : prodEffectOptions)
2019-05-28 05:27:31 +00:00
}
2019-09-12 05:52:14 +00:00
function updateComponentPreRender(
instance: ComponentInternalInstance,
nextVNode: HostVNode
) {
nextVNode.component = instance
instance.vnode = nextVNode
instance.next = null
2019-10-08 16:43:13 +00:00
resolveProps(instance, nextVNode.props, (nextVNode.type as Component).props)
resolveSlots(instance, nextVNode.children)
}
function patchChildren(
2019-09-06 20:58:32 +00:00
n1: HostVNode | null,
n2: HostVNode,
container: HostElement,
anchor: HostNode | null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
optimized: boolean = false
) {
2019-05-25 15:51:20 +00:00
const c1 = n1 && n1.children
const prevShapeFlag = n1 ? n1.shapeFlag : 0
2019-05-25 15:51:20 +00:00
const c2 = n2.children
2018-09-19 15:35:38 +00:00
const { patchFlag, shapeFlag } = n2
if (patchFlag === PatchFlags.BAIL) {
optimized = false
}
// fast path
if (patchFlag > 0) {
if (patchFlag & PatchFlags.KEYED_FRAGMENT) {
2019-05-25 15:51:20 +00:00
// this could be either fully-keyed or mixed (some keyed some not)
// presence of patchFlag means children are guaranteed to be arrays
patchKeyedChildren(
2019-09-06 20:58:32 +00:00
c1 as HostVNode[],
c2 as HostVNodeChildren,
container,
anchor,
2019-06-03 01:43:28 +00:00
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized
)
2019-05-25 15:51:20 +00:00
return
} else if (patchFlag & PatchFlags.UNKEYED_FRAGMENT) {
2019-05-25 15:51:20 +00:00
// unkeyed
patchUnkeyedChildren(
2019-09-06 20:58:32 +00:00
c1 as HostVNode[],
c2 as HostVNodeChildren,
container,
anchor,
2019-06-03 01:43:28 +00:00
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized
)
return
}
}
// children has 3 possibilities: text, array or no children.
2019-08-22 15:12:37 +00:00
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
2019-05-25 15:51:20 +00:00
// text children fast path
2019-08-22 15:12:37 +00:00
if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
2019-09-10 16:08:30 +00:00
unmountChildren(c1 as HostVNode[], parentComponent, parentSuspense)
2018-09-19 15:35:38 +00:00
}
2019-08-23 19:27:17 +00:00
if (c2 !== c1) {
hostSetElementText(container, c2 as string)
}
2019-05-25 15:51:20 +00:00
} else {
if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
// prev children was array
2019-08-22 15:12:37 +00:00
if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
// two arrays, cannot assume anything, do full diff
patchKeyedChildren(
2019-09-06 20:58:32 +00:00
c1 as HostVNode[],
c2 as HostVNodeChildren,
container,
anchor,
2019-06-03 01:43:28 +00:00
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized
)
} else {
// no new children, just unmount old
2019-09-10 16:08:30 +00:00
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,
2019-09-10 16:08:30 +00:00
parentSuspense,
isSVG,
optimized
)
}
2018-11-02 05:09:00 +00:00
}
}
2018-09-26 22:34:21 +00:00
}
function patchUnkeyedChildren(
2019-09-06 20:58:32 +00:00
c1: HostVNode[],
c2: HostVNodeChildren,
container: HostElement,
anchor: HostNode | null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
optimized: boolean
) {
2019-05-28 09:19:47 +00:00
c1 = c1 || EMPTY_ARR
c2 = c2 || EMPTY_ARR
2019-05-25 15:51:20 +00:00
const oldLength = c1.length
const newLength = c2.length
const commonLength = Math.min(oldLength, newLength)
let i
for (i = 0; i < commonLength; i++) {
const nextChild = (c2[i] = optimized
? cloneIfMounted(c2[i] as HostVNode)
: normalizeVNode(c2[i]))
2019-06-03 01:43:28 +00:00
patch(
c1[i],
nextChild,
container,
null,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized
)
2018-09-26 22:34:21 +00:00
}
2019-05-25 15:51:20 +00:00
if (oldLength > newLength) {
// remove old
2019-09-10 16:08:30 +00:00
unmountChildren(c1, parentComponent, parentSuspense, true, commonLength)
2019-05-25 15:51:20 +00:00
} else {
// mount new
2019-09-10 16:08:30 +00:00
mountChildren(
c2,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
optimized,
2019-09-10 16:08:30 +00:00
commonLength
)
2018-09-26 22:34:21 +00:00
}
}
2019-05-25 15:51:20 +00:00
// can be all-keyed or mixed
function patchKeyedChildren(
2019-09-06 20:58:32 +00:00
c1: HostVNode[],
c2: HostVNodeChildren,
container: HostElement,
parentAnchor: HostNode | null,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
optimized: boolean
) {
2019-05-27 05:48:40 +00:00
let i = 0
2019-05-27 07:28:56 +00:00
const l2 = c2.length
let e1 = c1.length - 1 // prev ending index
let e2 = l2 - 1 // next ending index
2019-05-27 05:48:40 +00:00
// 1. sync from start
// (a b) c
// (a b) d e
while (i <= e1 && i <= e2) {
const n1 = c1[i]
const n2 = (c2[i] = optimized
? cloneIfMounted(c2[i] as HostVNode)
: normalizeVNode(c2[i]))
if (isSameVNodeType(n1, n2)) {
2019-06-03 01:43:28 +00:00
patch(
n1,
n2,
container,
parentAnchor,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized
)
2019-05-27 05:48:40 +00:00
} else {
break
}
i++
}
// 2. sync from end
// a (b c)
// d e (b c)
while (i <= e1 && i <= e2) {
const n1 = c1[e1]
const n2 = (c2[e2] = optimized
? cloneIfMounted(c2[e2] as HostVNode)
: normalizeVNode(c2[e2]))
if (isSameVNodeType(n1, n2)) {
2019-06-03 01:43:28 +00:00
patch(
n1,
n2,
container,
parentAnchor,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized
)
2019-05-27 05:48:40 +00:00
} 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
2019-09-06 20:58:32 +00:00
const anchor =
nextPos < l2 ? (c2[nextPos] as HostVNode).el : parentAnchor
const n2 = (c2[i] = optimized
? cloneIfMounted(c2[i] as HostVNode)
: normalizeVNode(c2[i]))
2019-05-27 05:48:40 +00:00
while (i <= e2) {
2019-06-03 01:43:28 +00:00
patch(
null,
n2,
2019-06-03 01:43:28 +00:00
container,
anchor,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG
)
2019-05-27 05:48:40 +00:00
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) {
2019-09-10 16:08:30 +00:00
unmount(c1[i], parentComponent, parentSuspense, true)
2019-05-27 05:48:40 +00:00
i++
}
}
2019-05-27 07:28:56 +00:00
// 5. unknown sequence
2019-05-27 07:59:02 +00:00
// [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
2019-05-27 05:48:40 +00:00
else {
2019-05-27 07:59:02 +00:00
const s1 = i // prev starting index
const s2 = i // next starting index
2019-05-27 07:28:56 +00:00
// 5.1 build key:index map for newChildren
const keyToNewIndexMap: Map<string | number, number> = new Map()
2019-05-27 07:28:56 +00:00
for (i = s2; i <= e2; i++) {
const nextChild = (c2[i] = optimized
? cloneIfMounted(c2[i] as HostVNode)
: normalizeVNode(c2[i]))
2019-05-27 07:28:56 +00:00
if (nextChild.key != null) {
2019-08-30 14:36:30 +00:00
if (__DEV__ && keyToNewIndexMap.has(nextChild.key)) {
warn(
`Duplicate keys found during update:`,
JSON.stringify(nextChild.key),
`Make sure keys are unique.`
)
}
2019-05-27 07:28:56 +00:00
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
2019-05-27 07:59:02 +00:00
// used to track whether any node has moved
2019-05-27 07:28:56 +00:00
let maxNewIndexSoFar = 0
2019-05-27 07:59:02 +00:00
// 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
2019-05-27 07:28:56 +00:00
for (i = s1; i <= e1; i++) {
const prevChild = c1[i]
if (patched >= toBePatched) {
2019-05-27 07:59:02 +00:00
// all new children have been patched so this can only be a removal
2019-09-10 16:08:30 +00:00
unmount(prevChild, parentComponent, parentSuspense, true)
2019-05-27 07:28:56 +00:00
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
2019-05-27 07:59:02 +00:00
for (j = s2; j <= e2; j++) {
if (
newIndexToOldIndexMap[j - s2] === 0 &&
isSameVNodeType(prevChild, c2[j] as HostVNode)
) {
2019-05-27 07:28:56 +00:00
newIndex = j
break
}
}
}
if (newIndex === undefined) {
2019-09-10 16:08:30 +00:00
unmount(prevChild, parentComponent, parentSuspense, true)
2019-05-27 07:28:56 +00:00
} else {
newIndexToOldIndexMap[newIndex - s2] = i + 1
if (newIndex >= maxNewIndexSoFar) {
maxNewIndexSoFar = newIndex
} else {
moved = true
}
2019-06-03 01:43:28 +00:00
patch(
prevChild,
2019-09-06 20:58:32 +00:00
c2[newIndex] as HostVNode,
2019-06-03 01:43:28 +00:00
container,
null,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
optimized
)
2019-05-27 07:28:56 +00:00
patched++
}
}
2019-05-27 07:59:02 +00:00
// 5.3 move and mount
// generate longest stable subsequence only when nodes have moved
2019-05-27 07:28:56 +00:00
const increasingNewIndexSequence = moved
? getSequence(newIndexToOldIndexMap)
2019-05-28 09:19:47 +00:00
: EMPTY_ARR
2019-05-27 07:28:56 +00:00
j = increasingNewIndexSequence.length - 1
2019-05-27 07:59:02 +00:00
// looping backwards so that we can use last patched node as anchor
2019-05-27 07:28:56 +00:00
for (i = toBePatched - 1; i >= 0; i--) {
const nextIndex = s2 + i
2019-09-06 20:58:32 +00:00
const nextChild = c2[nextIndex] as HostVNode
2019-05-27 07:28:56 +00:00
const anchor =
2019-09-06 20:58:32 +00:00
nextIndex + 1 < l2
? (c2[nextIndex + 1] as HostVNode).el
: parentAnchor
2019-05-27 07:28:56 +00:00
if (newIndexToOldIndexMap[i] === 0) {
// mount new
2019-09-10 16:08:30 +00:00
patch(
null,
nextChild,
container,
anchor,
parentComponent,
parentSuspense,
isSVG
)
2019-05-27 07:28:56 +00:00
} else if (moved) {
2019-05-27 07:59:02 +00:00
// move if:
// There is no stable subsequence (e.g. a reverse)
// OR current node is not among the stable sequence
2019-05-27 07:28:56 +00:00
if (j < 0 || i !== increasingNewIndexSequence[j]) {
move(nextChild, container, anchor, MoveType.REORDER)
2019-05-27 07:28:56 +00:00
} else {
j--
}
}
}
}
}
2019-09-06 20:58:32 +00:00
function move(
vnode: HostVNode,
container: HostElement,
anchor: HostNode | null,
type: MoveType,
parentSuspense: HostSuspenseBoundary | null = null
2019-09-06 20:58:32 +00:00
) {
if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
move(vnode.component!.subTree, container, anchor, type)
2019-05-28 05:27:31 +00:00
return
}
if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
vnode.suspense!.move(container, anchor, type)
return
}
2019-05-27 07:28:56 +00:00
if (vnode.type === Fragment) {
2019-10-05 14:09:34 +00:00
hostInsert(vnode.el!, container, anchor)
2019-09-06 20:58:32 +00:00
const children = vnode.children as HostVNode[]
2019-05-27 07:28:56 +00:00
for (let i = 0; i < children.length; i++) {
move(children[i], container, anchor, type)
2019-05-27 07:28:56 +00:00
}
2019-10-05 14:09:34 +00:00
hostInsert(vnode.anchor!, container, anchor)
2019-05-27 07:28:56 +00:00
} 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)
}
2019-05-27 05:48:40 +00:00
}
2018-09-26 22:34:21 +00:00
}
function unmount(
2019-09-06 20:58:32 +00:00
vnode: HostVNode,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
doRemove?: boolean
) {
const { props, ref, children, dynamicChildren, shapeFlag } = vnode
2019-09-01 02:17:46 +00:00
// unset ref
2019-09-01 02:17:46 +00:00
if (ref !== null && parentComponent !== null) {
setRef(ref, null, parentComponent, null)
}
if (shapeFlag & ShapeFlags.COMPONENT) {
2019-11-03 01:26:25 +00:00
if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
2019-10-30 02:28:38 +00:00
;(parentComponent!.sink as KeepAliveSink).deactivate(vnode)
} else {
unmountComponent(vnode.component!, parentSuspense, doRemove)
}
2019-05-28 05:27:31 +00:00
return
}
if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
vnode.suspense!.unmount(parentSuspense, doRemove)
return
}
if (props != null && props.onVnodeBeforeUnmount != null) {
invokeDirectiveHook(props.onVnodeBeforeUnmount, parentComponent, vnode)
2019-09-01 02:17:46 +00:00
}
if (dynamicChildren != null) {
// fast path for block nodes: only need to unmount dynamic children.
unmountChildren(dynamicChildren, parentComponent, parentSuspense)
2019-09-01 02:17:46 +00:00
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
unmountChildren(children as HostVNode[], parentComponent, parentSuspense)
}
2019-05-25 15:51:20 +00:00
if (doRemove) {
remove(vnode)
}
if (props != null && props.onVnodeUnmounted != null) {
queuePostRenderEffect(() => {
invokeDirectiveHook(props.onVnodeUnmounted!, parentComponent, vnode)
}, parentSuspense)
}
}
function remove(vnode: HostVNode) {
const { type, el, anchor, children, transition } = vnode
const performRemove = () => {
hostRemove(el!)
if (anchor != null) hostRemove(anchor)
if (
transition != null &&
!transition.persisted &&
transition.afterLeave
) {
transition.afterLeave()
}
}
if (type === Fragment) {
performRemove()
removeChildren(children as HostVNode[])
return
}
if (
vnode.shapeFlag & ShapeFlags.ELEMENT &&
transition != null &&
!transition.persisted
) {
const { leave, delayLeave } = transition
const performLeave = () => leave(el!, performRemove)
if (delayLeave) {
delayLeave(vnode.el!, performRemove, performLeave)
} else {
performLeave()
}
} else {
performRemove()
2019-09-01 02:17:46 +00:00
}
}
2019-09-01 02:17:46 +00:00
function removeChildren(children: HostVNode[]) {
for (let i = 0; i < children.length; i++) {
remove(children[i])
2018-09-26 22:34:21 +00:00
}
}
2019-09-06 16:58:31 +00:00
function unmountComponent(
instance: ComponentInternalInstance,
parentSuspense: HostSuspenseBoundary | null,
2019-09-06 16:58:31 +00:00
doRemove?: boolean
) {
2019-12-13 22:57:21 +00:00
if (__HMR__ && instance.type.__hmrId != null) {
unregisterHMR(instance)
}
2019-10-31 01:41:28 +00:00
const { bum, effects, update, subTree, um, da, isDeactivated } = instance
2019-05-29 15:44:59 +00:00
// 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)
}
2019-05-29 15:44:59 +00:00
// unmounted hook
if (um !== null) {
queuePostRenderEffect(um, parentSuspense)
}
2019-10-31 01:41:28 +00:00
// deactivated hook
2019-10-31 03:32:29 +00:00
if (
da !== null &&
!isDeactivated &&
2019-11-03 01:26:25 +00:00
instance.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
2019-10-31 03:32:29 +00:00
) {
2019-10-31 01:41:28 +00:00
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(
2019-09-06 20:58:32 +00:00
children: HostVNode[],
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
parentSuspense: HostSuspenseBoundary | null,
2019-05-27 05:48:40 +00:00
doRemove?: boolean,
start: number = 0
) {
2019-05-25 15:51:20 +00:00
for (let i = start; i < children.length; i++) {
2019-09-10 16:08:30 +00:00
unmount(children[i], parentComponent, parentSuspense, doRemove)
2018-11-02 05:21:38 +00:00
}
2018-09-19 15:35:38 +00:00
}
function getNextHostNode(vnode: HostVNode): HostNode | null {
if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
return getNextHostNode(vnode.component!.subTree)
2019-09-12 05:52:14 +00:00
}
if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
return vnode.suspense!.next()
2019-09-12 05:52:14 +00:00
}
return hostNextSibling((vnode.anchor || vnode.el)!)
2019-05-28 05:27:31 +00:00
}
2019-06-03 05:44:45 +00:00
function setRef(
2019-10-09 18:01:53 +00:00
ref: string | Function | Ref,
oldRef: string | Function | Ref | null,
2019-09-06 16:58:31 +00:00
parent: ComponentInternalInstance,
2019-09-06 20:58:32 +00:00
value: HostNode | ComponentPublicInstance | null
2019-06-03 05:44:45 +00:00
) {
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
}
}
2019-06-03 05:44:45 +00:00
if (isString(ref)) {
const setupRef = renderContext[ref]
if (isRef(setupRef)) {
setupRef.value = value
}
2019-06-03 05:44:45 +00:00
refs[ref] = value
} else if (isRef(ref)) {
ref.value = value
2019-08-30 14:36:30 +00:00
} else if (isFunction(ref)) {
callWithErrorHandling(ref, parent, ErrorCodes.FUNCTION_REF, [value, refs])
2019-08-30 14:36:30 +00:00
} else if (__DEV__) {
warn('Invalid template ref type:', value, `(${typeof value})`)
2019-06-03 05:44:45 +00:00
}
}
2019-12-22 17:25:04 +00:00
type HostRootElement = HostElement & { _vnode: HostVNode | null }
const render: RootRenderFunction<HostNode, HostElement> = (
vnode,
container: HostRootElement
) => {
2019-05-29 05:43:46 +00:00
if (vnode == null) {
2019-09-06 20:58:32 +00:00
if (container._vnode) {
2019-09-10 16:08:30 +00:00
unmount(container._vnode, null, null, true)
2019-05-29 05:43:46 +00:00
}
} else {
2019-09-06 20:58:32 +00:00
patch(container._vnode || null, vnode, container)
2019-05-29 05:43:46 +00:00
}
2019-05-28 11:36:15 +00:00
flushPostFlushCbs()
2019-09-06 20:58:32 +00:00
container._vnode = vnode
}
return {
render,
createApp: createAppAPI(render)
}
}
2019-05-27 07:28:56 +00:00
// 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
2019-05-27 07:28:56 +00:00
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
}