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

1183 lines
31 KiB
TypeScript
Raw Normal View History

import {
Text,
Fragment,
Empty,
2019-05-28 17:19:47 +08:00
Portal,
2019-05-28 13:27:31 +08:00
normalizeVNode,
VNode,
VNodeChildren
2019-05-28 17:19:47 +08:00
} from './vnode'
import {
2019-05-28 19:36:15 +08:00
ComponentInstance,
2019-05-28 17:19:47 +08:00
renderComponentRoot,
2019-05-28 19:36:15 +08:00
shouldUpdateComponent,
2019-05-29 10:43:27 +08:00
createComponentInstance,
setupStatefulComponent
2019-05-28 17:19:47 +08:00
} from './component'
2019-06-03 13:44:45 +08:00
import {
isString,
EMPTY_OBJ,
EMPTY_ARR,
isReservedProp,
isFunction
} from '@vue/shared'
2019-05-28 19:59:54 +08:00
import { queueJob, queuePostFlushCb, flushPostFlushCbs } from './scheduler'
import {
effect,
stop,
ReactiveEffectOptions,
isRef,
Ref,
toRaw
} from '@vue/reactivity'
2019-05-29 11:36:16 +08:00
import { resolveProps } from './componentProps'
2019-05-31 18:07:43 +08:00
import { resolveSlots } from './componentSlots'
2019-08-22 23:12:37 +08:00
import { PatchFlags } from './patchFlags'
import { ShapeFlags } from './shapeFlags'
2019-08-30 22:36:30 +08:00
import { pushWarningContext, popWarningContext, warn } from './warning'
2019-09-01 05:06:39 +08:00
import { invokeDirectiveHook } from './directives'
2019-05-29 11:01:39 +08:00
const prodEffectOptions = {
scheduler: queueJob
}
2019-05-29 11:01:39 +08:00
function createDevEffectOptions(
instance: ComponentInstance
): 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 11:01:39 +08:00
function invokeHooks(hooks: Function[], arg?: any) {
2019-05-28 19:59:54 +08:00
for (let i = 0; i < hooks.length; i++) {
2019-05-29 11:01:39 +08:00
hooks[i](arg)
2019-05-28 19:59:54 +08:00
}
}
export type HostNode = any
export interface RendererOptions {
patchProp(
el: HostNode,
key: string,
value: any,
oldValue: any,
isSVG: boolean,
prevChildren?: VNode[],
parentComponent?: ComponentInstance | null,
unmountChildren?: (
children: VNode[],
parentComponent: ComponentInstance | null
) => void
): void
insert(el: HostNode, parent: HostNode, anchor?: HostNode): void
remove(el: HostNode): void
2019-06-03 09:43:28 +08:00
createElement(type: string, isSVG?: boolean): HostNode
createText(text: string): HostNode
createComment(text: string): HostNode
setText(node: HostNode, text: string): void
setElementText(node: HostNode, text: string): void
2019-05-28 13:27:31 +08:00
parentNode(node: HostNode): HostNode | null
nextSibling(node: HostNode): HostNode | null
2019-05-29 16:10:25 +08:00
querySelector(selector: string): HostNode | null
}
export function createRenderer(options: RendererOptions) {
2018-09-19 23:35:38 +08:00
const {
2019-05-27 15:28:56 +08:00
insert: hostInsert,
remove: hostRemove,
2019-05-25 23:51:20 +08:00
patchProp: hostPatchProp,
createElement: hostCreateElement,
createText: hostCreateText,
createComment: hostCreateComment,
setText: hostSetText,
setElementText: hostSetElementText,
2019-05-28 13:27:31 +08:00
parentNode: hostParentNode,
2019-05-29 16:10:25 +08:00
nextSibling: hostNextSibling,
querySelector: hostQuerySelector
} = options
2019-05-25 23:51:20 +08:00
function patch(
n1: VNode | null, // null means this is a mount
n2: VNode,
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode = null,
parentComponent: ComponentInstance | null = null,
isSVG: boolean = false,
2019-06-02 16:35:19 +08:00
optimized: boolean = false
) {
2019-05-25 23:51:20 +08:00
// patching & not same type, unmount old tree
if (n1 != null && !isSameType(n1, n2)) {
2019-05-28 13:27:31 +08:00
anchor = getNextHostNode(n1)
unmount(n1, parentComponent, true)
2019-05-25 23:51:20 +08:00
n1 = null
}
2019-06-02 16:35:19 +08:00
const { type, shapeFlag } = n2
2019-05-28 17:19:47 +08:00
switch (type) {
case Text:
processText(n1, n2, container, anchor)
break
case Empty:
processEmptyNode(n1, n2, container, anchor)
break
case Fragment:
2019-06-03 09:43:28 +08:00
processFragment(
n1,
n2,
container,
anchor,
parentComponent,
isSVG,
optimized
)
2019-05-28 17:19:47 +08:00
break
case Portal:
2019-06-03 09:43:28 +08:00
processPortal(
n1,
n2,
container,
anchor,
parentComponent,
isSVG,
optimized
)
2019-05-28 17:19:47 +08:00
break
default:
2019-08-22 23:12:37 +08:00
if (shapeFlag & ShapeFlags.ELEMENT) {
2019-06-03 09:43:28 +08:00
processElement(
n1,
n2,
container,
anchor,
parentComponent,
isSVG,
optimized
)
2019-08-30 22:36:30 +08:00
} else if (shapeFlag & ShapeFlags.COMPONENT) {
2019-06-03 09:43:28 +08:00
processComponent(
n1,
n2,
container,
anchor,
parentComponent,
isSVG,
optimized
)
2019-08-30 22:36:30 +08:00
} else if (__DEV__) {
warn('Invalid VNode type:', n2.type, `(${typeof n2.type})`)
2019-05-28 17:19:47 +08:00
}
2018-09-19 23:35:38 +08:00
}
}
function processText(
n1: VNode | null,
n2: VNode,
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode
) {
2019-05-25 23:51:20 +08:00
if (n1 == null) {
2019-05-27 15:28:56 +08:00
hostInsert(
(n2.el = hostCreateText(n2.children as string)),
container,
anchor
)
} else {
2019-05-25 23:51:20 +08:00
const el = (n2.el = n1.el)
if (n2.children !== n1.children) {
hostSetText(el, n2.children as string)
2018-11-02 13:21:38 +08:00
}
2018-09-19 23:35:38 +08:00
}
}
function processEmptyNode(
n1: VNode | null,
n2: VNode,
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode
) {
2019-05-25 23:51:20 +08:00
if (n1 == null) {
2019-05-27 15:28:56 +08:00
hostInsert((n2.el = hostCreateComment('')), container, anchor)
2018-11-02 13:21:38 +08:00
} else {
2019-05-25 23:51:20 +08:00
n2.el = n1.el
2018-09-19 23:35:38 +08:00
}
}
function processElement(
n1: VNode | null,
n2: VNode,
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode,
parentComponent: ComponentInstance | null,
isSVG: boolean,
optimized: boolean
) {
2019-05-25 23:51:20 +08:00
if (n1 == null) {
2019-06-03 09:43:28 +08:00
mountElement(n2, container, anchor, parentComponent, isSVG)
2018-09-19 23:35:38 +08:00
} else {
2019-06-03 09:43:28 +08:00
patchElement(n1, n2, parentComponent, isSVG, optimized)
2018-09-19 23:35:38 +08:00
}
2019-06-03 13:44:45 +08:00
if (n2.ref !== null && parentComponent !== null) {
setRef(n2.ref, n1 && n1.ref, parentComponent, n2.el)
2019-06-03 13:44:45 +08:00
}
2018-09-19 23:35:38 +08:00
}
2019-06-03 09:43:28 +08:00
function mountElement(
vnode: VNode,
container: HostNode,
anchor: HostNode,
parentComponent: ComponentInstance | null,
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 13:44:45 +08:00
if (isReservedProp(key)) continue
2019-06-03 09:43:28 +08:00
hostPatchProp(el, key, props[key], null, isSVG)
}
2019-09-01 05:06:39 +08:00
invokeDirectiveHook(props.vnodeBeforeMount, parentComponent, vnode)
2018-09-19 23:35:38 +08:00
}
2019-08-22 23:12:37 +08:00
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
hostSetElementText(el, vnode.children as string)
2019-08-22 23:12:37 +08:00
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
2019-06-03 09:43:28 +08:00
mountChildren(
vnode.children as VNodeChildren,
el,
null,
parentComponent,
isSVG
)
2018-09-19 23:35:38 +08:00
}
2019-05-27 15:28:56 +08:00
hostInsert(el, container, anchor)
2019-09-01 05:06:39 +08:00
if (props != null) {
invokeDirectiveHook(props.vnodeMounted, parentComponent, vnode)
}
2018-09-19 23:35:38 +08:00
}
function mountChildren(
children: VNodeChildren,
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode,
parentComponent: ComponentInstance | null,
isSVG: boolean,
start: number = 0
) {
2019-05-25 23:51:20 +08:00
for (let i = start; i < children.length; i++) {
2019-05-28 13:27:31 +08:00
const child = (children[i] = normalizeVNode(children[i]))
2019-06-03 09:43:28 +08:00
patch(null, child, container, anchor, parentComponent, isSVG)
2018-09-19 23:35:38 +08:00
}
}
2019-06-03 09:43:28 +08:00
function patchElement(
n1: VNode,
n2: VNode,
parentComponent: ComponentInstance | null,
isSVG: boolean,
optimized: boolean
) {
2019-05-25 23:51:20 +08:00
const el = (n2.el = n1.el)
const { patchFlag, dynamicChildren } = n2
2019-05-28 17:19:47 +08:00
const oldProps = (n1 && n1.props) || EMPTY_OBJ
const newProps = n2.props || EMPTY_OBJ
2018-09-19 23:35:38 +08:00
2019-06-01 17:43:41 +08:00
if (patchFlag) {
2019-05-25 23:51:20 +08: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 23:35:38 +08:00
2019-08-22 23:12:37 +08:00
if (patchFlag & PatchFlags.FULL_PROPS) {
// element props contain dynamic keys, full diff needed
patchProps(el, n2, oldProps, newProps, parentComponent, isSVG)
} else {
// class
// this flag is matched when the element has dynamic class bindings.
2019-08-22 23:12:37 +08:00
if (patchFlag & PatchFlags.CLASS) {
if (oldProps.class !== newProps.class) {
2019-06-03 09:43:28 +08:00
hostPatchProp(el, 'class', newProps.class, null, isSVG)
}
2018-09-19 23:35:38 +08:00
}
// style
// this flag is matched when the element has dynamic style bindings
2019-08-22 23:12:37 +08:00
if (patchFlag & PatchFlags.STYLE) {
2019-06-03 09:43:28 +08:00
hostPatchProp(el, 'style', newProps.style, oldProps.style, isSVG)
}
2019-05-25 23:51:20 +08: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 23:12:37 +08: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 09:43:28 +08:00
isSVG,
n1.children as VNode[],
parentComponent,
unmountChildren
)
}
2018-09-19 23:35:38 +08:00
}
}
}
2019-05-25 23:51:20 +08: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 23:12:37 +08:00
if (patchFlag & PatchFlags.TEXT) {
2019-05-25 23:51:20 +08:00
if (n1.children !== n2.children) {
hostSetElementText(el, n2.children as string)
2018-09-19 23:35:38 +08:00
}
2019-05-25 23:51:20 +08:00
return // terminal
2018-09-19 23:35:38 +08:00
}
2019-05-25 23:51:20 +08:00
} else if (!optimized) {
// unoptimized, full diff
patchProps(el, n2, oldProps, newProps, parentComponent, isSVG)
2018-09-19 23:35:38 +08:00
}
2019-05-25 23:51:20 +08:00
if (dynamicChildren != null) {
// children fast path
const olddynamicChildren = n1.dynamicChildren as VNode[]
2019-05-25 23:51:20 +08:00
for (let i = 0; i < dynamicChildren.length; i++) {
2019-06-03 09:43:28 +08:00
patch(
olddynamicChildren[i],
dynamicChildren[i],
el,
null,
parentComponent,
isSVG,
true
)
}
2019-05-25 23:51:20 +08:00
} else if (!optimized) {
// full diff
2019-06-03 09:43:28 +08:00
patchChildren(n1, n2, el, null, parentComponent, isSVG)
}
}
function patchProps(
el: HostNode,
vnode: VNode,
oldProps: any,
2019-06-03 09:43:28 +08:00
newProps: any,
parentComponent: ComponentInstance | null,
2019-06-03 09:43:28 +08:00
isSVG: boolean
) {
2019-05-25 23:51:20 +08:00
if (oldProps !== newProps) {
for (const key in newProps) {
2019-06-03 13:44:45 +08:00
if (isReservedProp(key)) continue
2019-05-25 23:51:20 +08:00
const next = newProps[key]
const prev = oldProps[key]
if (next !== prev) {
hostPatchProp(
el,
key,
next,
prev,
2019-06-03 09:43:28 +08:00
isSVG,
vnode.children as VNode[],
parentComponent,
unmountChildren
)
2018-09-27 05:10:34 +08:00
}
2018-09-19 23:35:38 +08:00
}
2019-05-28 17:19:47 +08:00
if (oldProps !== EMPTY_OBJ) {
2019-05-25 23:51:20 +08:00
for (const key in oldProps) {
2019-06-03 13:44:45 +08:00
if (isReservedProp(key)) continue
2019-05-25 23:51:20 +08:00
if (!(key in newProps)) {
hostPatchProp(
el,
key,
null,
null,
2019-06-03 09:43:28 +08:00
isSVG,
vnode.children as VNode[],
parentComponent,
unmountChildren
)
2019-05-25 23:51:20 +08:00
}
}
}
2018-09-19 23:35:38 +08:00
}
}
function processFragment(
n1: VNode | null,
n2: VNode,
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode,
parentComponent: ComponentInstance | null,
isSVG: boolean,
optimized: boolean
) {
2019-05-27 13:48:40 +08:00
const fragmentStartAnchor = (n2.el = n1 ? n1.el : hostCreateComment(''))
const fragmentEndAnchor = (n2.anchor = n1
? n1.anchor
: hostCreateComment(''))
2019-05-25 23:51:20 +08:00
if (n1 == null) {
2019-05-27 15:28:56 +08:00
hostInsert(fragmentStartAnchor, container, anchor)
hostInsert(fragmentEndAnchor, container, anchor)
// a fragment can only have array children
2019-06-03 09:43:28 +08:00
mountChildren(
n2.children as VNodeChildren,
container,
fragmentEndAnchor,
parentComponent,
isSVG
)
2018-09-19 23:35:38 +08:00
} else {
2019-06-03 09:43:28 +08:00
patchChildren(
n1,
n2,
container,
fragmentEndAnchor,
parentComponent,
isSVG,
optimized
)
2018-09-19 23:35:38 +08:00
}
}
2019-05-29 16:10:25 +08:00
function processPortal(
n1: VNode | null,
n2: VNode,
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode,
parentComponent: ComponentInstance | null,
isSVG: boolean,
optimized: boolean
2019-05-29 16:10:25 +08:00
) {
const targetSelector = n2.props && n2.props.target
const { patchFlag, shapeFlag, children } = n2
2019-05-29 16:10:25 +08:00
if (n1 == null) {
const target = (n2.target = isString(targetSelector)
? hostQuerySelector(targetSelector)
: null)
if (target != null) {
2019-08-22 23:12:37 +08:00
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
hostSetElementText(target, children as string)
2019-08-22 23:12:37 +08:00
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
2019-06-03 09:43:28 +08:00
mountChildren(
children as VNodeChildren,
target,
null,
parentComponent,
isSVG
)
2019-05-29 16:10:25 +08:00
}
2019-08-30 22:36:30 +08:00
} else if (__DEV__) {
warn('Invalid Portal target on mount:', target, `(${typeof target})`)
2019-05-29 16:10:25 +08:00
}
} else {
// update content
const target = (n2.target = n1.target)
2019-08-22 23:12:37 +08:00
if (patchFlag === PatchFlags.TEXT) {
hostSetElementText(target, children as string)
2019-05-29 16:10:25 +08:00
} else if (!optimized) {
2019-06-03 09:43:28 +08:00
patchChildren(n1, n2, target, null, parentComponent, isSVG)
2019-05-29 16:10:25 +08: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 23:12:37 +08:00
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
2019-05-29 16:10:25 +08:00
hostSetElementText(target, '')
hostSetElementText(nextTarget, children as string)
2019-08-22 23:12:37 +08:00
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
for (let i = 0; i < (children as VNode[]).length; i++) {
move((children as VNode[])[i], nextTarget, null)
2019-05-29 16:10:25 +08:00
}
}
2019-08-30 22:36:30 +08:00
} else if (__DEV__) {
warn('Invalid Portal target on update:', target, `(${typeof target})`)
2019-05-29 16:10:25 +08:00
}
}
}
// insert an empty node as the placeholder for the portal
processEmptyNode(n1, n2, container, anchor)
}
2019-05-28 10:28:25 +08:00
function processComponent(
n1: VNode | null,
n2: VNode,
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode,
parentComponent: ComponentInstance | null,
isSVG: boolean,
optimized: boolean
2019-05-28 13:27:31 +08:00
) {
if (n1 == null) {
2019-06-03 09:43:28 +08:00
mountComponent(n2, container, anchor, parentComponent, isSVG)
2019-05-28 13:27:31 +08:00
} else {
2019-05-28 19:36:15 +08:00
const instance = (n2.component = n1.component) as ComponentInstance
2019-06-01 02:14:49 +08:00
if (shouldUpdateComponent(n1, n2, optimized)) {
instance.next = n2
2019-05-28 17:19:47 +08:00
instance.update()
} else {
2019-06-01 02:14:49 +08:00
n2.component = n1.component
n2.el = n1.el
}
2019-05-28 13:27:31 +08:00
}
2019-06-03 13:44:45 +08:00
if (n2.ref !== null && parentComponent !== null) {
setRef(
n2.ref,
n1 && n1.ref,
2019-06-03 13:44:45 +08:00
parentComponent,
(n2.component as ComponentInstance).renderProxy
)
}
2019-05-28 13:27:31 +08:00
}
function mountComponent(
2019-06-03 12:40:21 +08:00
initialVNode: VNode,
2019-05-28 13:27:31 +08:00
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode,
parentComponent: ComponentInstance | null,
isSVG: boolean
2019-05-28 13:27:31 +08:00
) {
2019-06-03 12:40:21 +08:00
const instance: ComponentInstance = (initialVNode.component = createComponentInstance(
2019-08-29 00:13:36 +08:00
initialVNode,
2019-06-03 09:43:28 +08:00
parentComponent
2019-05-28 19:36:15 +08:00
))
2019-08-29 00:13:36 +08:00
2019-08-30 22:36:30 +08:00
if (__DEV__) {
pushWarningContext(initialVNode)
}
2019-08-29 00:13:36 +08: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)
}
// create reactive effect for rendering
let mounted = false
2019-06-03 13:44:45 +08:00
instance.update = effect(function componentEffect() {
2019-08-29 00:13:36 +08:00
if (!mounted) {
2019-05-29 11:36:16 +08:00
const subTree = (instance.subTree = renderComponentRoot(instance))
2019-05-29 10:47:09 +08:00
// beforeMount hook
if (instance.bm !== null) {
invokeHooks(instance.bm)
}
2019-06-03 09:43:28 +08:00
patch(null, subTree, container, anchor, instance, isSVG)
2019-06-03 12:40:21 +08:00
initialVNode.el = subTree.el
// mounted hook
if (instance.m !== null) {
queuePostFlushCb(instance.m)
}
2019-08-29 00:13:36 +08:00
mounted = true
} else {
2019-06-03 13:44:45 +08:00
// updateComponent
2019-05-29 13:43:46 +08:00
// This is triggered by mutation of component's own state (next: null)
// OR parent calling processComponent (next: VNode)
const { next } = instance
2019-08-30 22:36:30 +08:00
if (__DEV__) {
pushWarningContext(next || instance.vnode)
}
2019-06-03 09:43:28 +08:00
if (next !== null) {
2019-06-03 12:40:21 +08:00
// update from parent
next.component = instance
instance.vnode = next
instance.next = null
2019-08-29 00:13:36 +08:00
resolveProps(instance, next.props, propsOptions)
2019-05-31 18:07:43 +08:00
resolveSlots(instance, next.children)
}
2019-05-29 10:43:27 +08:00
const prevTree = instance.subTree
const nextTree = (instance.subTree = renderComponentRoot(instance))
2019-05-29 10:47:09 +08:00
// beforeUpdate hook
if (instance.bu !== null) {
invokeHooks(instance.bu)
}
2019-06-03 13:44:45 +08:00
// reset refs
// only needed if previous patch had refs
if (instance.refs !== EMPTY_OBJ) {
instance.refs = {}
}
patch(
prevTree,
nextTree,
2019-06-03 13:44:45 +08:00
// parent may have changed if it's in a portal
2019-05-29 16:10:25 +08:00
hostParentNode(prevTree.el),
2019-06-03 13:44:45 +08:00
// anchor may have changed if it's in a fragment
2019-06-03 09:43:28 +08:00
getNextHostNode(prevTree),
instance,
isSVG
)
2019-06-03 12:40:21 +08: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
let parent = instance.parent
while (parent && parent.subTree === current) {
;(current = parent.vnode).el = nextTree.el
parent = parent.parent
}
}
// upated hook
if (instance.u !== null) {
queuePostFlushCb(instance.u)
2019-05-28 13:27:31 +08:00
}
2019-08-30 22:36:30 +08:00
if (__DEV__) {
popWarningContext()
}
2019-05-28 13:27:31 +08:00
}
2019-05-29 11:01:39 +08:00
}, __DEV__ ? createDevEffectOptions(instance) : prodEffectOptions)
2019-08-30 22:36:30 +08:00
if (__DEV__) {
popWarningContext()
}
2019-05-28 13:27:31 +08:00
}
function patchChildren(
n1: VNode | null,
n2: VNode,
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode,
parentComponent: ComponentInstance | null,
isSVG: boolean,
optimized: boolean = false
) {
2019-05-25 23:51:20 +08:00
const c1 = n1 && n1.children
const prevShapeFlag = n1 ? n1.shapeFlag : 0
2019-05-25 23:51:20 +08:00
const c2 = n2.children
2018-09-19 23:35:38 +08:00
2019-05-25 23:51:20 +08:00
// fast path
const { patchFlag, shapeFlag } = n2
2019-06-01 17:43:41 +08:00
if (patchFlag) {
2019-08-22 23:12:37 +08:00
if (patchFlag & PatchFlags.KEYED) {
2019-05-25 23:51:20 +08:00
// this could be either fully-keyed or mixed (some keyed some not)
// presence of patchFlag means children are guaranteed to be arrays
patchKeyedChildren(
c1 as VNode[],
c2 as VNodeChildren,
container,
anchor,
2019-06-03 09:43:28 +08:00
parentComponent,
isSVG,
optimized
)
2019-05-25 23:51:20 +08:00
return
2019-08-22 23:12:37 +08:00
} else if (patchFlag & PatchFlags.UNKEYED) {
2019-05-25 23:51:20 +08:00
// unkeyed
patchUnkeyedChildren(
c1 as VNode[],
c2 as VNodeChildren,
container,
anchor,
2019-06-03 09:43:28 +08:00
parentComponent,
isSVG,
optimized
)
return
}
}
2019-08-22 23:12:37 +08:00
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
2019-05-25 23:51:20 +08:00
// text children fast path
2019-08-22 23:12:37 +08:00
if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
unmountChildren(c1 as VNode[], parentComponent)
2018-09-19 23:35:38 +08:00
}
2019-08-24 03:27:17 +08:00
if (c2 !== c1) {
hostSetElementText(container, c2 as string)
}
2019-05-25 23:51:20 +08:00
} else {
2019-08-22 23:12:37 +08:00
if (prevShapeFlag & ShapeFlags.TEXT_CHILDREN) {
hostSetElementText(container, '')
2019-08-22 23:12:37 +08:00
if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
2019-06-03 09:43:28 +08:00
mountChildren(
c2 as VNodeChildren,
container,
anchor,
parentComponent,
isSVG
)
}
2019-08-22 23:12:37 +08:00
} else if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
// two arrays, cannot assume anything, do full diff
patchKeyedChildren(
c1 as VNode[],
c2 as VNodeChildren,
container,
anchor,
2019-06-03 09:43:28 +08:00
parentComponent,
isSVG,
optimized
)
} else {
// c2 is null in this case
unmountChildren(c1 as VNode[], parentComponent, true)
}
2018-11-02 13:09:00 +08:00
}
}
2018-09-27 06:34:21 +08:00
}
function patchUnkeyedChildren(
c1: VNode[],
c2: VNodeChildren,
container: HostNode,
2019-06-03 09:43:28 +08:00
anchor: HostNode,
parentComponent: ComponentInstance | null,
isSVG: boolean,
optimized: boolean
) {
2019-05-28 17:19:47 +08:00
c1 = c1 || EMPTY_ARR
c2 = c2 || EMPTY_ARR
2019-05-25 23:51:20 +08: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 13:27:31 +08:00
const nextChild = (c2[i] = normalizeVNode(c2[i]))
2019-06-03 09:43:28 +08:00
patch(
c1[i],
nextChild,
container,
null,
parentComponent,
isSVG,
optimized
)
2018-09-27 06:34:21 +08:00
}
2019-05-25 23:51:20 +08:00
if (oldLength > newLength) {
// remove old
unmountChildren(c1, parentComponent, true, commonLength)
2019-05-25 23:51:20 +08:00
} else {
// mount new
2019-06-03 09:43:28 +08:00
mountChildren(c2, container, anchor, parentComponent, isSVG, commonLength)
2018-09-27 06:34:21 +08:00
}
}
2019-05-25 23:51:20 +08:00
// can be all-keyed or mixed
function patchKeyedChildren(
c1: VNode[],
c2: VNodeChildren,
container: HostNode,
2019-06-03 09:43:28 +08:00
parentAnchor: HostNode,
parentComponent: ComponentInstance | null,
isSVG: boolean,
optimized: boolean
) {
2019-05-27 13:48:40 +08:00
let i = 0
2019-05-27 15:28:56 +08:00
const l2 = c2.length
let e1 = c1.length - 1 // prev ending index
let e2 = l2 - 1 // next ending index
2019-05-27 13:48:40 +08:00
// 1. sync from start
// (a b) c
// (a b) d e
while (i <= e1 && i <= e2) {
const n1 = c1[i]
2019-05-28 13:27:31 +08:00
const n2 = (c2[i] = normalizeVNode(c2[i]))
2019-05-27 13:48:40 +08:00
if (isSameType(n1, n2)) {
2019-06-03 09:43:28 +08:00
patch(
n1,
n2,
container,
parentAnchor,
parentComponent,
isSVG,
optimized
)
2019-05-27 13:48:40 +08: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 13:27:31 +08:00
const n2 = (c2[e2] = normalizeVNode(c2[e2]))
2019-05-27 13:48:40 +08:00
if (isSameType(n1, n2)) {
2019-06-03 09:43:28 +08:00
patch(
n1,
n2,
container,
parentAnchor,
parentComponent,
isSVG,
optimized
)
2019-05-27 13:48:40 +08: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-05-27 15:28:56 +08:00
const anchor = nextPos < l2 ? (c2[nextPos] as VNode).el : parentAnchor
2019-05-27 13:48:40 +08:00
while (i <= e2) {
2019-06-03 09:43:28 +08:00
patch(
null,
(c2[i] = normalizeVNode(c2[i])),
container,
anchor,
parentComponent,
isSVG
)
2019-05-27 13:48:40 +08: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) {
unmount(c1[i], parentComponent, true)
2019-05-27 13:48:40 +08:00
i++
}
}
2019-05-27 15:28:56 +08:00
// 5. unknown sequence
2019-05-27 15:59:02 +08: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 13:48:40 +08:00
else {
2019-05-27 15:59:02 +08:00
const s1 = i // prev starting index
const s2 = i // next starting index
2019-05-27 15:28:56 +08: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 13:27:31 +08:00
const nextChild = (c2[i] = normalizeVNode(c2[i]))
2019-05-27 15:28:56 +08:00
if (nextChild.key != null) {
2019-08-30 22:36:30 +08: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 15:28:56 +08: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 15:59:02 +08:00
// used to track whether any node has moved
2019-05-27 15:28:56 +08:00
let maxNewIndexSoFar = 0
2019-05-27 15:59:02 +08: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 15:28:56 +08: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 15:59:02 +08:00
// all new children have been patched so this can only be a removal
unmount(prevChild, parentComponent, true)
2019-05-27 15:28:56 +08: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 15:59:02 +08:00
for (j = s2; j <= e2; j++) {
2019-05-27 15:28:56 +08:00
if (isSameType(prevChild, c2[j] as VNode)) {
newIndex = j
break
}
}
}
if (newIndex === undefined) {
unmount(prevChild, parentComponent, true)
2019-05-27 15:28:56 +08:00
} else {
newIndexToOldIndexMap[newIndex - s2] = i + 1
if (newIndex >= maxNewIndexSoFar) {
maxNewIndexSoFar = newIndex
} else {
moved = true
}
2019-06-03 09:43:28 +08:00
patch(
prevChild,
c2[newIndex] as VNode,
container,
null,
parentComponent,
isSVG,
optimized
)
2019-05-27 15:28:56 +08:00
patched++
}
}
2019-05-27 15:59:02 +08:00
// 5.3 move and mount
// generate longest stable subsequence only when nodes have moved
2019-05-27 15:28:56 +08:00
const increasingNewIndexSequence = moved
? getSequence(newIndexToOldIndexMap)
2019-05-28 17:19:47 +08:00
: EMPTY_ARR
2019-05-27 15:28:56 +08:00
j = increasingNewIndexSequence.length - 1
2019-05-27 15:59:02 +08:00
// looping backwards so that we can use last patched node as anchor
2019-05-27 15:28:56 +08:00
for (i = toBePatched - 1; i >= 0; i--) {
const nextIndex = s2 + i
const nextChild = c2[nextIndex] as VNode
const anchor =
nextIndex + 1 < l2 ? (c2[nextIndex + 1] as VNode).el : parentAnchor
if (newIndexToOldIndexMap[i] === 0) {
// mount new
2019-06-03 09:43:28 +08:00
patch(null, nextChild, container, anchor, parentComponent, isSVG)
2019-05-27 15:28:56 +08:00
} else if (moved) {
2019-05-27 15:59:02 +08:00
// move if:
// There is no stable subsequence (e.g. a reverse)
// OR current node is not among the stable sequence
2019-05-27 15:28:56 +08:00
if (j < 0 || i !== increasingNewIndexSequence[j]) {
move(nextChild, container, anchor)
} else {
j--
}
}
}
}
}
function move(vnode: VNode, container: HostNode, anchor: HostNode) {
2019-06-03 13:44:45 +08:00
if (vnode.component !== null) {
2019-05-29 10:43:27 +08:00
move(vnode.component.subTree, container, anchor)
2019-05-28 13:27:31 +08:00
return
}
2019-05-27 15:28:56 +08:00
if (vnode.type === Fragment) {
hostInsert(vnode.el, container, anchor)
const children = vnode.children as VNode[]
for (let i = 0; i < children.length; i++) {
hostInsert(children[i].el, container, anchor)
}
hostInsert(vnode.anchor, container, anchor)
} else {
hostInsert(vnode.el, container, anchor)
2019-05-27 13:48:40 +08:00
}
2018-09-27 06:34:21 +08:00
}
function unmount(
vnode: VNode,
parentComponent: ComponentInstance | null,
doRemove?: boolean
) {
// unset ref
if (vnode.ref !== null && parentComponent !== null) {
setRef(vnode.ref, null, parentComponent, null)
}
2019-05-28 19:36:15 +08:00
const instance = vnode.component
if (instance != null) {
2019-05-29 23:44:59 +08:00
unmountComponent(instance, doRemove)
2019-05-28 13:27:31 +08:00
return
}
2019-05-27 13:48:40 +08:00
const shouldRemoveChildren = vnode.type === Fragment && doRemove
if (vnode.dynamicChildren != null) {
unmountChildren(
vnode.dynamicChildren,
parentComponent,
shouldRemoveChildren
)
2019-08-22 23:12:37 +08:00
} else if (vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
unmountChildren(
vnode.children as VNode[],
parentComponent,
shouldRemoveChildren
)
}
2019-05-25 23:51:20 +08:00
if (doRemove) {
2019-05-27 15:28:56 +08:00
hostRemove(vnode.el)
if (vnode.anchor != null) hostRemove(vnode.anchor)
2018-09-27 06:34:21 +08:00
}
}
function unmountComponent(instance: ComponentInstance, doRemove?: boolean) {
const { bum, effects, update, subTree, um } = instance
2019-05-29 23:44:59 +08:00
// beforeUnmount hook
if (bum !== null) {
invokeHooks(bum)
}
if (effects !== null) {
for (let i = 0; i < effects.length; i++) {
stop(effects[i])
}
}
stop(update)
unmount(subTree, instance, doRemove)
2019-05-29 23:44:59 +08:00
// unmounted hook
if (um !== null) {
queuePostFlushCb(um)
}
}
function unmountChildren(
children: VNode[],
parentComponent: ComponentInstance | null,
2019-05-27 13:48:40 +08:00
doRemove?: boolean,
start: number = 0
) {
2019-05-25 23:51:20 +08:00
for (let i = start; i < children.length; i++) {
unmount(children[i], parentComponent, doRemove)
2018-11-02 13:21:38 +08:00
}
2018-09-19 23:35:38 +08:00
}
2019-05-28 13:27:31 +08:00
function getNextHostNode(vnode: VNode): HostNode {
return vnode.component === null
? hostNextSibling(vnode.anchor || vnode.el)
2019-05-29 10:43:27 +08:00
: getNextHostNode(vnode.component.subTree)
2019-05-28 13:27:31 +08:00
}
2019-06-03 13:44:45 +08:00
function setRef(
ref: string | Function | Ref<any>,
oldRef: string | Function | Ref<any> | null,
2019-06-03 13:44:45 +08:00
parent: ComponentInstance,
value: HostNode | ComponentInstance | null
2019-06-03 13:44:45 +08:00
) {
const refs = parent.refs === EMPTY_OBJ ? (parent.refs = {}) : parent.refs
const rawData = toRaw(parent.data)
// unset old ref
if (oldRef !== null && oldRef !== ref) {
if (isString(oldRef)) {
refs[oldRef] = null
const oldSetupRef = rawData[oldRef]
if (isRef(oldSetupRef)) {
oldSetupRef.value = null
}
} else if (isRef(oldRef)) {
oldRef.value = null
}
}
2019-06-03 13:44:45 +08:00
if (isString(ref)) {
const setupRef = rawData[ref]
if (isRef(setupRef)) {
setupRef.value = value
}
2019-06-03 13:44:45 +08:00
refs[ref] = value
} else if (isRef(ref)) {
ref.value = value
2019-08-30 22:36:30 +08:00
} else if (isFunction(ref)) {
2019-06-03 13:44:45 +08:00
ref(value, refs)
2019-08-30 22:36:30 +08:00
} else if (__DEV__) {
warn('Invalid template ref type:', value, `(${typeof value})`)
2019-06-03 13:44:45 +08:00
}
}
2019-05-29 13:43:46 +08:00
return function render(vnode: VNode | null, dom: HostNode): VNode | null {
if (vnode == null) {
if (dom._vnode) {
unmount(dom._vnode, null, true)
2019-05-29 13:43:46 +08:00
}
} else {
patch(dom._vnode, vnode, dom)
}
2019-05-28 19:36:15 +08:00
flushPostFlushCbs()
2019-05-25 23:51:20 +08:00
return (dom._vnode = vnode)
}
}
2019-05-27 15:28:56 +08: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
}