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

1646 lines
44 KiB
TypeScript
Raw Normal View History

import {
Text,
Fragment,
Empty,
2019-05-28 09:19:47 +00:00
Portal,
2019-05-28 05:27:31 +00:00
normalizeVNode,
VNode,
2019-09-07 15:28:40 +00:00
VNodeChildren,
2019-09-09 17:59:53 +00:00
Suspense,
createVNode
2019-05-28 09:19:47 +00:00
} from './vnode'
import {
2019-09-06 16:58:31 +00:00
ComponentInternalInstance,
2019-05-29 02:43:27 +00:00
createComponentInstance,
2019-09-09 20:00:50 +00:00
setupStatefulComponent,
2019-09-10 16:08:30 +00:00
handleSetupResult
2019-05-28 09:19:47 +00:00
} from './component'
2019-09-06 15:25:11 +00:00
import {
renderComponentRoot,
shouldUpdateComponent
} from './componentRenderUtils'
2019-06-03 05:44:45 +00:00
import {
isString,
EMPTY_OBJ,
EMPTY_ARR,
isReservedProp,
isFunction
} from '@vue/shared'
2019-05-28 11:59:54 +00:00
import { queueJob, queuePostFlushCb, flushPostFlushCbs } from './scheduler'
import {
effect,
stop,
ReactiveEffectOptions,
isRef,
Ref,
toRaw
} 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 { PatchFlags } from './patchFlags'
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'
2019-09-06 20:58:32 +00:00
import { ComponentPublicInstance } from './componentPublicInstanceProxy'
import { App, createAppAPI } from './apiApp'
2019-09-10 15:01:11 +00:00
import {
SuspenseBoundary,
createSuspenseBoundary,
normalizeSuspenseChildren
} from './suspense'
2019-05-29 03:01:39 +00:00
const prodEffectOptions = {
scheduler: queueJob
}
2019-05-29 03:01:39 +00:00
function createDevEffectOptions(
2019-09-06 16:58:31 +00:00
instance: ComponentInternalInstance
2019-05-29 03:01:39 +00:00
): ReactiveEffectOptions {
return {
scheduler: queueJob,
onTrack: instance.rtc
? e => invokeHooks(instance.rtc as Function[], e)
: void 0,
onTrigger: instance.rtg
? e => invokeHooks(instance.rtg as Function[], e)
: void 0
}
}
function isSameType(n1: VNode, n2: VNode): boolean {
return n1.type === n2.type && n1.key === n2.key
}
2019-05-29 03:01:39 +00:00
function invokeHooks(hooks: Function[], arg?: any) {
2019-05-28 11:59:54 +00:00
for (let i = 0; i < hooks.length; i++) {
2019-05-29 03:01:39 +00:00
hooks[i](arg)
2019-05-28 11:59:54 +00:00
}
}
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,
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
2019-05-28 05:27:31 +00:00
parentNode(node: HostNode): HostNode | null
nextSibling(node: HostNode): HostNode | null
2019-09-06 20:58:32 +00:00
querySelector(selector: string): HostElement | null
}
2019-09-06 20:58:32 +00:00
export type RootRenderFunction<HostNode, HostElement> = (
vnode: VNode<HostNode, HostElement> | null,
dom: HostElement | string
2019-09-02 20:09:34 +00:00
) => void
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>
2019-09-10 16:08:30 +00:00
type HostSuspsenseBoundary = 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,
querySelector: hostQuerySelector
} = options
2019-05-25 15:51:20 +00:00
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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 && !isSameType(n1, n2)) {
2019-05-28 05:27:31 +00:00
anchor = getNextHostNode(n1)
2019-09-10 16:08:30 +00:00
unmount(n1, parentComponent, parentSuspense, true)
2019-05-25 15:51:20 +00:00
n1 = null
}
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 Empty:
processEmptyNode(n1, n2, container, anchor)
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
2019-09-07 15:28:40 +00:00
case Suspense:
2019-09-09 20:28:32 +00:00
if (__FEATURE_SUSPENSE__) {
processSuspense(
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 (__DEV__) {
warn(`Suspense is not enabled in the version of Vue you are using.`)
}
2019-09-07 15:28:40 +00:00
break
2019-05-28 09:19:47 +00:00
default:
2019-08-22 15:12:37 +00:00
if (shapeFlag & ShapeFlags.ELEMENT) {
2019-06-03 01:43:28 +00:00
processElement(
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-08-30 14:36:30 +00:00
} else if (shapeFlag & ShapeFlags.COMPONENT) {
2019-06-03 01:43:28 +00:00
processComponent(
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-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 processEmptyNode(
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 = hostCreateComment('')), container, anchor)
2018-11-02 05:21:38 +00:00
} else {
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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
)
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean
) {
const tag = vnode.type as string
isSVG = isSVG || tag === 'svg'
const el = (vnode.el = hostCreateElement(tag, isSVG))
const { props, shapeFlag } = vnode
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)
}
2019-09-01 02:17:46 +00:00
if (props.vnodeBeforeMount != null) {
invokeDirectiveHook(props.vnodeBeforeMount, parentComponent, vnode)
}
2018-09-19 15:35:38 +00:00
}
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,
2019-06-03 01:43:28 +00:00
isSVG
)
2018-09-19 15:35:38 +00:00
}
2019-05-27 07:28:56 +00:00
hostInsert(el, container, anchor)
2019-09-01 02:17:46 +00:00
if (props != null && props.vnodeMounted != null) {
queuePostFlushCb(() => {
invokeDirectiveHook(props.vnodeMounted, parentComponent, vnode)
})
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
start: number = 0
) {
2019-05-25 15:51:20 +00:00
for (let i = start; i < children.length; i++) {
2019-05-28 05:27:31 +00:00
const child = (children[i] = normalizeVNode(children[i]))
2019-09-10 16:08:30 +00:00
patch(
null,
child,
container,
anchor,
parentComponent,
parentSuspense,
isSVG
)
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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-05-25 15:51:20 +00:00
const { 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
2019-09-01 02:17:46 +00:00
if (newProps.vnodeBeforeUpdate != null) {
2019-09-02 16:09:29 +00:00
invokeDirectiveHook(newProps.vnodeBeforeUpdate, parentComponent, n2, n1)
2019-09-01 02:17:46 +00:00
}
2019-06-01 09:43:41 +00:00
if (patchFlag) {
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
const propsToUpdate = n2.dynamicProps as string[]
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
}
2019-05-25 15:51:20 +00:00
} else if (!optimized) {
// 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) {
// children fast path
2019-09-06 20:58:32 +00:00
const olddynamicChildren = n1.dynamicChildren as HostVNode[]
2019-05-25 15:51:20 +00:00
for (let i = 0; i < dynamicChildren.length; i++) {
2019-06-03 01:43:28 +00:00
patch(
olddynamicChildren[i],
dynamicChildren[i],
el,
null,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG,
true
)
}
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.vnodeUpdated != null) {
queuePostFlushCb(() => {
2019-09-02 16:09:29 +00:00
invokeDirectiveHook(newProps.vnodeUpdated, parentComponent, n2, n1)
2019-09-01 02:17:46 +00:00
})
}
}
function patchProps(
2019-09-06 20:58:32 +00:00
el: HostElement,
vnode: HostVNode,
oldProps: any,
2019-06-03 01:43:28 +00:00
newProps: any,
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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) {
2019-06-03 05:44:45 +00:00
if (isReservedProp(key)) continue
2019-05-25 15:51:20 +00:00
if (!(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
}
}
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean,
optimized: boolean
) {
2019-09-06 20:58:32 +00:00
const fragmentStartAnchor = (n2.el = n1
? n1.el
: hostCreateComment('')) as HostNode
2019-05-27 05:48:40 +00:00
const fragmentEndAnchor = (n2.anchor = n1
? n1.anchor
2019-09-06 20:58:32 +00:00
: hostCreateComment('')) as HostNode
2019-05-25 15:51:20 +00:00
if (n1 == null) {
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,
2019-06-03 01:43:28 +00:00
isSVG
)
2018-09-19 15:35:38 +00:00
} else {
2019-06-03 01:43:28 +00:00
patchChildren(
n1,
n2,
container,
fragmentEndAnchor,
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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)
: null)
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,
2019-06-03 01:43:28 +00:00
isSVG
)
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-09-06 20:58:32 +00:00
const target = (n2.target = n1.target) as HostElement
2019-08-22 15:12:37 +00:00
if (patchFlag === PatchFlags.TEXT) {
hostSetElementText(target, children as string)
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)
: null)
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)
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
processEmptyNode(n1, n2, container, anchor)
}
2019-09-07 15:28:40 +00:00
function processSuspense(
n1: HostVNode | null,
n2: HostVNode,
container: HostElement,
anchor: HostNode | null,
parentComponent: ComponentInternalInstance | null,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | null,
2019-09-07 15:28:40 +00:00
isSVG: boolean,
2019-09-10 16:08:30 +00:00
optimized: boolean
2019-09-07 15:28:40 +00:00
) {
if (n1 == null) {
const suspense = (n2.suspense = createSuspenseBoundary(
n2,
parentSuspense,
hostCreateElement('div'),
resolveSuspense
))
function resolveSuspense() {
const { subTree, fallbackTree, bufferedJobs, vnode } = suspense
2019-09-09 21:24:42 +00:00
// unmount fallback tree
2019-09-10 16:08:30 +00:00
unmount(fallbackTree as HostVNode, parentComponent, suspense, true)
2019-09-09 20:00:50 +00:00
// move content from off-dom container to actual container
move(subTree as HostVNode, container, anchor)
const el = (vnode.el = (subTree as HostVNode).el as HostNode)
// suspense as the root node of a component...
if (parentComponent && parentComponent.subTree === vnode) {
parentComponent.vnode.el = el
updateHOCHostEl(parentComponent, el)
}
2019-09-09 20:00:50 +00:00
// check if there is a pending parent suspense
let parent = suspense.parent
let hasUnresolvedAncestor = false
while (parent) {
if (!parent.isResolved) {
// found a pending parent suspense, merge buffered post jobs
// into that parent
parent.bufferedJobs.push(...bufferedJobs)
2019-09-09 20:00:50 +00:00
hasUnresolvedAncestor = true
break
}
}
// no pending parent suspense, flush all jobs
if (!hasUnresolvedAncestor) {
queuePostFlushCb(bufferedJobs)
2019-09-09 20:00:50 +00:00
}
suspense.isResolved = true
// invoke @resolve event
const onResolve = vnode.props && vnode.props.onResolve
if (isFunction(onResolve)) {
onResolve()
}
}
2019-09-10 15:01:11 +00:00
const { content, fallback } = normalizeSuspenseChildren(n2)
suspense.subTree = content
suspense.fallbackTree = fallback
// start mounting the content subtree in an off-dom container
2019-09-09 20:00:50 +00:00
// TODO should buffer postQueue jobs on current boundary
2019-09-10 15:01:11 +00:00
patch(
2019-09-09 17:59:53 +00:00
null,
2019-09-10 15:01:11 +00:00
content,
suspense.container,
2019-09-09 17:59:53 +00:00
null,
parentComponent,
2019-09-10 16:08:30 +00:00
suspense,
2019-09-09 17:59:53 +00:00
isSVG,
optimized
)
2019-09-07 15:28:40 +00:00
// now check if we have encountered any async deps
2019-09-09 17:59:53 +00:00
if (suspense.deps > 0) {
2019-09-10 15:01:11 +00:00
// mount the fallback tree
patch(
2019-09-09 21:24:42 +00:00
null,
2019-09-10 15:01:11 +00:00
fallback,
2019-09-09 21:24:42 +00:00
container,
2019-09-10 15:01:11 +00:00
anchor,
parentComponent,
2019-09-10 16:08:30 +00:00
suspense,
2019-09-10 15:01:11 +00:00
isSVG,
optimized
2019-09-09 21:24:42 +00:00
)
2019-09-10 15:01:11 +00:00
n2.el = fallback.el
2019-09-09 17:59:53 +00:00
} else {
2019-09-10 15:01:11 +00:00
// Suspense has no async deps. Just resolve.
2019-09-09 17:59:53 +00:00
suspense.resolve()
}
2019-09-07 15:28:40 +00:00
} else {
2019-09-10 16:08:30 +00:00
const suspense = (n2.suspense = n1.suspense) as HostSuspsenseBoundary
2019-09-09 20:00:50 +00:00
suspense.vnode = n2
2019-09-10 15:01:11 +00:00
const { content, fallback } = normalizeSuspenseChildren(n2)
const oldSubTree = (suspense.oldSubTree = suspense.subTree)
2019-09-10 15:01:11 +00:00
suspense.subTree = content
const oldFallbackTree = (suspense.oldFallbackTree = suspense.fallbackTree)
suspense.fallbackTree = fallback
2019-09-09 20:00:50 +00:00
if (!suspense.isResolved) {
2019-09-10 15:01:11 +00:00
patch(
oldSubTree,
content,
suspense.container,
null,
parentComponent,
2019-09-10 16:08:30 +00:00
suspense,
2019-09-10 15:01:11 +00:00
isSVG,
optimized
)
if (suspense.deps > 0) {
// still pending. patch the fallback tree.
patch(
oldFallbackTree,
fallback,
container,
anchor,
parentComponent,
2019-09-10 16:08:30 +00:00
suspense,
2019-09-10 15:01:11 +00:00
isSVG,
optimized
)
n2.el = fallback.el
} else {
suspense.resolve()
}
2019-09-09 17:59:53 +00:00
} else {
2019-09-09 20:00:50 +00:00
// just normal patch inner content as a fragment
2019-09-10 15:01:11 +00:00
patch(
oldSubTree,
2019-09-10 15:01:11 +00:00
content,
2019-09-09 20:00:50 +00:00
container,
2019-09-10 15:01:11 +00:00
anchor,
2019-09-09 20:00:50 +00:00
parentComponent,
2019-09-10 16:08:30 +00:00
suspense,
2019-09-09 20:00:50 +00:00
isSVG,
optimized
)
2019-09-10 15:01:11 +00:00
n2.el = content.el
2019-09-09 17:59:53 +00:00
}
2019-09-07 15:28:40 +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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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-09-10 16:08:30 +00:00
mountComponent(
n2,
container,
anchor,
parentComponent,
parentSuspense,
isSVG
)
2019-05-28 05:27:31 +00:00
} else {
2019-09-06 16:58:31 +00:00
const instance = (n2.component =
n1.component) as ComponentInternalInstance
2019-09-09 20:28:32 +00:00
2019-09-09 20:00:50 +00:00
// async still pending
2019-09-09 20:28:32 +00:00
if (
__FEATURE_SUSPENSE__ &&
instance.asyncDep &&
!instance.asyncResolved
) {
2019-09-09 20:00:50 +00:00
return
}
2019-09-09 20:28:32 +00:00
if (shouldUpdateComponent(n1, n2, optimized)) {
2019-09-09 20:00:50 +00:00
// normal update
instance.next = n2
2019-05-28 09:19:47 +00:00
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) {
setRef(
n2.ref,
n1 && n1.ref,
2019-06-03 05:44:45 +00:00
parentComponent,
2019-09-06 16:58:31 +00:00
(n2.component as ComponentInternalInstance).renderProxy
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | null,
2019-06-03 01:43:28 +00:00
isSVG: boolean
2019-05-28 05:27:31 +00:00
) {
2019-09-06 16:58:31 +00:00
const instance: ComponentInternalInstance = (initialVNode.component = createComponentInstance(
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-08-30 14:36:30 +00:00
if (__DEV__) {
pushWarningContext(initialVNode)
}
2019-08-28 16:13:36 +00:00
// resolve props and slots for setup context
const propsOptions = (initialVNode.type as any).props
resolveProps(instance, initialVNode.props, propsOptions)
resolveSlots(instance, initialVNode.children)
// setup stateful logic
if (initialVNode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
setupStatefulComponent(instance)
}
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) {
2019-09-09 20:00:50 +00:00
throw new Error('Async component without a suspense boundary!')
}
2019-09-10 16:08:30 +00:00
parentSuspense.deps++
instance.asyncDep.then(asyncSetupResult => {
2019-09-10 16:08:30 +00:00
parentSuspense.deps--
// retry from this component
instance.asyncResolved = true
handleSetupResult(instance, asyncSetupResult)
2019-09-10 16:08:30 +00:00
setupRenderEffect(
instance,
parentSuspense,
initialVNode,
container,
anchor,
isSVG
)
updateHOCHostEl(instance, initialVNode.el as HostNode)
2019-09-10 16:08:30 +00:00
if (parentSuspense.deps === 0) {
parentSuspense.resolve()
}
2019-09-09 20:00:50 +00:00
})
2019-09-09 21:24:42 +00:00
// give it a placeholder
const placeholder = (instance.subTree = createVNode(Empty))
processEmptyNode(null, placeholder, container, anchor)
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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
let mounted = false
2019-06-03 05:44:45 +00:00
instance.update = effect(function componentEffect() {
2019-08-28 16:13:36 +00:00
if (!mounted) {
2019-05-29 03:36:16 +00:00
const subTree = (instance.subTree = renderComponentRoot(instance))
2019-05-29 02:47:09 +00:00
// beforeMount hook
if (instance.bm !== null) {
invokeHooks(instance.bm)
}
2019-09-10 16:08:30 +00:00
patch(null, subTree, container, anchor, instance, parentSuspense, isSVG)
2019-06-03 04:40:21 +00:00
initialVNode.el = subTree.el
// mounted hook
if (instance.m !== null) {
queuePostFlushCb(instance.m)
}
2019-08-28 16:13:36 +00:00
mounted = true
} else {
2019-06-03 05:44:45 +00:00
// updateComponent
2019-05-29 05:43:46 +00:00
// This is triggered by mutation of component's own state (next: null)
2019-09-06 20:58:32 +00:00
// OR parent calling processComponent (next: HostVNode)
const { next } = instance
2019-08-30 14:36:30 +00:00
if (__DEV__) {
pushWarningContext(next || instance.vnode)
}
2019-06-03 01:43:28 +00:00
if (next !== null) {
2019-06-03 04:40:21 +00:00
// update from parent
next.component = instance
instance.vnode = next
instance.next = null
2019-09-09 20:00:50 +00:00
resolveProps(instance, next.props, (initialVNode.type as any).props)
2019-05-31 10:07:43 +00:00
resolveSlots(instance, next.children)
}
2019-05-29 02:43:27 +00:00
const prevTree = instance.subTree
const nextTree = (instance.subTree = renderComponentRoot(instance))
2019-05-29 02:47:09 +00:00
// beforeUpdate hook
if (instance.bu !== null) {
invokeHooks(instance.bu)
}
2019-06-03 05:44:45 +00:00
// reset refs
// only needed if previous patch had refs
if (instance.refs !== EMPTY_OBJ) {
instance.refs = {}
}
patch(
prevTree,
nextTree,
2019-06-03 05:44:45 +00:00
// parent may have changed if it's in a portal
2019-09-06 20:58:32 +00:00
hostParentNode(prevTree.el as HostNode) as HostElement,
2019-06-03 05:44:45 +00:00
// anchor may have changed if it's in a fragment
2019-06-03 01:43:28 +00:00
getNextHostNode(prevTree),
instance,
2019-09-10 16:08:30 +00:00
parentSuspense,
2019-06-03 01:43:28 +00:00
isSVG
)
2019-06-03 04:40:21 +00:00
let current = instance.vnode
current.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)
}
// upated hook
if (instance.u !== null) {
queuePostFlushCb(instance.u)
2019-05-28 05:27:31 +00:00
}
2019-08-30 14:36:30 +00:00
if (__DEV__) {
popWarningContext()
}
2019-05-28 05:27:31 +00:00
}
2019-05-29 03:01:39 +00:00
}, __DEV__ ? createDevEffectOptions(instance) : prodEffectOptions)
2019-05-28 05:27:31 +00:00
}
function updateHOCHostEl(
{ vnode, parent }: ComponentInternalInstance,
el: HostNode
) {
while (parent && parent.subTree === vnode) {
;(vnode = parent.vnode).el = el
parent = parent.parent
}
}
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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
2019-05-25 15:51:20 +00:00
// fast path
const { patchFlag, shapeFlag } = n2
2019-06-01 09:43:41 +00:00
if (patchFlag) {
2019-08-22 15:12:37 +00:00
if (patchFlag & PatchFlags.KEYED) {
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
2019-08-22 15:12:37 +00:00
} else if (patchFlag & PatchFlags.UNKEYED) {
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
)
}
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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++) {
2019-05-28 05:27:31 +00:00
const nextChild = (c2[i] = 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,
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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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]
2019-05-28 05:27:31 +00:00
const n2 = (c2[i] = normalizeVNode(c2[i]))
2019-05-27 05:48:40 +00:00
if (isSameType(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]
2019-05-28 05:27:31 +00:00
const n2 = (c2[e2] = normalizeVNode(c2[e2]))
2019-05-27 05:48:40 +00:00
if (isSameType(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
2019-05-27 05:48:40 +00:00
while (i <= e2) {
2019-06-03 01:43:28 +00:00
patch(
null,
(c2[i] = normalizeVNode(c2[i])),
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<any, number> = new Map()
for (i = s2; i <= e2; i++) {
2019-05-28 05:27:31 +00:00
const nextChild = (c2[i] = 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 = []
2019-05-27 07:28:56 +00:00
for (i = 0; i < toBePatched; i++) newIndexToOldIndexMap.push(0)
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++) {
2019-09-06 20:58:32 +00:00
if (isSameType(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)
} else {
j--
}
}
}
}
}
2019-09-06 20:58:32 +00:00
function move(
vnode: HostVNode,
container: HostElement,
anchor: HostNode | null
) {
2019-06-03 05:44:45 +00:00
if (vnode.component !== null) {
2019-05-29 02:43:27 +00:00
move(vnode.component.subTree, container, anchor)
2019-05-28 05:27:31 +00:00
return
}
if (__FEATURE_SUSPENSE__ && vnode.type === Suspense) {
move((vnode.suspense as any).subTree, container, anchor)
return
}
2019-05-27 07:28:56 +00:00
if (vnode.type === Fragment) {
2019-09-06 20:58:32 +00:00
hostInsert(vnode.el as HostNode, container, anchor)
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)
2019-05-27 07:28:56 +00:00
}
2019-09-06 20:58:32 +00:00
hostInsert(vnode.anchor as HostNode, container, anchor)
2019-05-27 07:28:56 +00:00
} else {
2019-09-06 20:58:32 +00:00
hostInsert(vnode.el as HostNode, 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,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | null,
doRemove?: boolean
) {
2019-09-01 02:17:46 +00:00
const {
props,
ref,
type,
component,
suspense,
2019-09-01 02:17:46 +00:00
children,
dynamicChildren,
shapeFlag,
anchor
} = vnode
// unset ref
2019-09-01 02:17:46 +00:00
if (ref !== null && parentComponent !== null) {
setRef(ref, null, parentComponent, null)
}
2019-09-01 02:17:46 +00:00
if (component != null) {
2019-09-10 16:08:30 +00:00
unmountComponent(component, parentSuspense, doRemove)
2019-05-28 05:27:31 +00:00
return
}
if (__FEATURE_SUSPENSE__ && suspense != null) {
2019-09-10 16:08:30 +00:00
unmount(
suspense.subTree as HostVNode,
parentComponent,
parentSuspense,
doRemove
)
return
}
2019-09-01 02:17:46 +00:00
if (props != null && props.vnodeBeforeUnmount != null) {
invokeDirectiveHook(props.vnodeBeforeUnmount, parentComponent, vnode)
}
const shouldRemoveChildren = type === Fragment && doRemove
if (dynamicChildren != null) {
2019-09-10 16:08:30 +00:00
unmountChildren(
dynamicChildren,
parentComponent,
parentSuspense,
shouldRemoveChildren
)
2019-09-01 02:17:46 +00:00
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
unmountChildren(
2019-09-06 20:58:32 +00:00
children as HostVNode[],
parentComponent,
2019-09-10 16:08:30 +00:00
parentSuspense,
shouldRemoveChildren
)
}
2019-05-25 15:51:20 +00:00
if (doRemove) {
2019-09-06 20:58:32 +00:00
hostRemove(vnode.el as HostNode)
2019-09-01 02:17:46 +00:00
if (anchor != null) hostRemove(anchor)
}
if (props != null && props.vnodeUnmounted != null) {
queuePostFlushCb(() => {
invokeDirectiveHook(props.vnodeUnmounted, parentComponent, vnode)
})
2018-09-26 22:34:21 +00:00
}
}
2019-09-06 16:58:31 +00:00
function unmountComponent(
instance: ComponentInternalInstance,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | null,
2019-09-06 16:58:31 +00:00
doRemove?: boolean
) {
const { bum, effects, update, subTree, um } = 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])
}
}
stop(update)
2019-09-10 16:08:30 +00:00
unmount(subTree, instance, parentSuspense, doRemove)
2019-05-29 15:44:59 +00:00
// unmounted hook
if (um !== null) {
queuePostFlushCb(um)
}
}
function unmountChildren(
2019-09-06 20:58:32 +00:00
children: HostVNode[],
2019-09-06 16:58:31 +00:00
parentComponent: ComponentInternalInstance | null,
2019-09-10 16:08:30 +00:00
parentSuspense: HostSuspsenseBoundary | 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
}
2019-09-06 20:58:32 +00:00
function getNextHostNode(vnode: HostVNode): HostNode | null {
2019-05-28 05:27:31 +00:00
return vnode.component === null
2019-09-06 20:58:32 +00:00
? hostNextSibling((vnode.anchor || vnode.el) as HostNode)
2019-05-29 02:43:27 +00:00
: getNextHostNode(vnode.component.subTree)
2019-05-28 05:27:31 +00:00
}
2019-06-03 05:44:45 +00:00
function setRef(
ref: string | Function | Ref<any>,
oldRef: string | Function | Ref<any> | 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)) {
2019-06-03 05:44:45 +00:00
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-09-06 20:58:32 +00:00
function render(vnode: HostVNode | null, rawContainer: HostElement | string) {
let container: any = rawContainer
if (isString(container)) {
container = hostQuerySelector(container)
if (!container) {
2019-09-02 20:09:34 +00:00
if (__DEV__) {
warn(
2019-09-06 20:58:32 +00:00
`Failed to locate root container: ` + `querySelector returned null.`
2019-09-02 20:09:34 +00:00
)
}
return
}
}
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
let j
let u
let v
let 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
}