2019-05-25 23:51:20 +08:00
|
|
|
// TODO:
|
|
|
|
// - component
|
|
|
|
// - lifecycle
|
2019-05-26 15:19:44 +08:00
|
|
|
// - app context
|
|
|
|
// - svg
|
2019-05-25 23:51:20 +08:00
|
|
|
// - refs
|
|
|
|
// - hydration
|
2019-05-26 15:19:44 +08:00
|
|
|
// - warning context
|
|
|
|
// - parent chain
|
|
|
|
// - reused nodes (warning)
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
import {
|
|
|
|
Text,
|
|
|
|
Fragment,
|
|
|
|
Empty,
|
|
|
|
createVNode,
|
|
|
|
VNode,
|
|
|
|
VNodeChildren
|
|
|
|
} from './h.js'
|
2019-05-25 23:51:20 +08:00
|
|
|
import { TEXT, CLASS, STYLE, PROPS, KEYED, UNKEYED } from './patchFlags'
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2019-05-25 23:51:20 +08:00
|
|
|
const emptyArr: any[] = []
|
2019-05-26 15:19:44 +08:00
|
|
|
const emptyObj: { [key: string]: any } = {}
|
2018-11-01 16:05:09 +08:00
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function isSameType(n1: VNode, n2: VNode): boolean {
|
|
|
|
return n1.type === n2.type && n1.key === n2.key
|
|
|
|
}
|
|
|
|
|
|
|
|
export type HostNode = any
|
|
|
|
|
|
|
|
export interface RendererOptions {
|
|
|
|
patchProp(
|
|
|
|
el: HostNode,
|
|
|
|
key: string,
|
|
|
|
value: any,
|
|
|
|
oldValue: any,
|
|
|
|
isSVG: boolean,
|
|
|
|
prevChildren?: VNode[],
|
|
|
|
unmountChildren?: (children: VNode[]) => void
|
|
|
|
): void
|
|
|
|
insert(el: HostNode, parent: HostNode, anchor?: HostNode): void
|
|
|
|
remove(el: HostNode): void
|
|
|
|
createElement(type: string): HostNode
|
|
|
|
createText(text: string): HostNode
|
|
|
|
createComment(text: string): HostNode
|
|
|
|
setText(node: HostNode, text: string): void
|
|
|
|
setElementText(node: HostNode, text: string): void
|
|
|
|
nextSibling(node: HostNode): HostNode | null
|
|
|
|
}
|
2018-11-02 05:08:33 +08:00
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
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,
|
|
|
|
nextSibling: hostNextSibling
|
2019-05-26 15:19:44 +08:00
|
|
|
} = options
|
2019-05-25 23:51:20 +08:00
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function patch(
|
|
|
|
n1: VNode | null, // null means this is a mount
|
|
|
|
n2: VNode,
|
|
|
|
container: HostNode,
|
|
|
|
anchor?: HostNode,
|
|
|
|
optimized?: boolean
|
|
|
|
) {
|
2019-05-25 23:51:20 +08:00
|
|
|
// patching & not same type, unmount old tree
|
|
|
|
if (n1 != null && !isSameType(n1, n2)) {
|
2019-05-27 13:48:40 +08:00
|
|
|
anchor = hostNextSibling(n1.anchor || n1.el)
|
2019-05-25 23:51:20 +08:00
|
|
|
unmount(n1, true)
|
|
|
|
n1 = null
|
|
|
|
}
|
|
|
|
|
|
|
|
const { type } = n2
|
|
|
|
if (type === Text) {
|
|
|
|
processText(n1, n2, container, anchor)
|
|
|
|
} else if (type === Empty) {
|
|
|
|
processEmptyNode(n1, n2, container, anchor)
|
|
|
|
} else if (type === Fragment) {
|
|
|
|
processFragment(n1, n2, container, anchor, optimized)
|
|
|
|
} else if (typeof type === 'function') {
|
|
|
|
// TODO Component
|
2018-09-19 23:35:38 +08:00
|
|
|
} else {
|
2019-05-25 23:51:20 +08:00
|
|
|
processElement(n1, n2, container, anchor, optimized)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function processText(
|
|
|
|
n1: VNode | null,
|
|
|
|
n2: VNode,
|
|
|
|
container: HostNode,
|
|
|
|
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
|
|
|
|
)
|
2018-10-12 05:14:39 +08:00
|
|
|
} else {
|
2019-05-25 23:51:20 +08:00
|
|
|
const el = (n2.el = n1.el)
|
|
|
|
if (n2.children !== n1.children) {
|
2019-05-26 15:19:44 +08:00
|
|
|
hostSetText(el, n2.children as string)
|
2018-11-02 13:21:38 +08:00
|
|
|
}
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function processEmptyNode(
|
|
|
|
n1: VNode | null,
|
|
|
|
n2: VNode,
|
|
|
|
container: HostNode,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function processElement(
|
|
|
|
n1: VNode | null,
|
|
|
|
n2: VNode,
|
|
|
|
container: HostNode,
|
|
|
|
anchor?: HostNode,
|
|
|
|
optimized?: boolean
|
|
|
|
) {
|
2019-05-25 23:51:20 +08:00
|
|
|
// mount
|
|
|
|
if (n1 == null) {
|
|
|
|
mountElement(n2, container, anchor)
|
2018-09-19 23:35:38 +08:00
|
|
|
} else {
|
2019-05-26 15:19:44 +08:00
|
|
|
patchElement(n1, n2, optimized)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function mountElement(vnode: VNode, container: HostNode, anchor?: HostNode) {
|
|
|
|
const el = (vnode.el = hostCreateElement(vnode.type as string))
|
2019-05-25 23:51:20 +08:00
|
|
|
if (vnode.props != null) {
|
|
|
|
for (const key in vnode.props) {
|
2019-05-26 15:19:44 +08:00
|
|
|
hostPatchProp(el, key, vnode.props[key], null, false)
|
2018-11-09 03:09:52 +08:00
|
|
|
}
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
2019-05-25 23:51:20 +08:00
|
|
|
if (typeof vnode.children === 'string') {
|
|
|
|
hostSetElementText(el, vnode.children)
|
2019-05-26 15:19:44 +08:00
|
|
|
} else if (vnode.children != null) {
|
2019-05-25 23:51:20 +08:00
|
|
|
mountChildren(vnode.children, el)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
2019-05-27 15:28:56 +08:00
|
|
|
hostInsert(el, container, anchor)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function mountChildren(
|
|
|
|
children: VNodeChildren,
|
|
|
|
container: HostNode,
|
|
|
|
anchor?: HostNode,
|
|
|
|
start: number = 0
|
|
|
|
) {
|
2019-05-25 23:51:20 +08:00
|
|
|
for (let i = start; i < children.length; i++) {
|
|
|
|
const child = (children[i] = normalizeChild(children[i]))
|
|
|
|
patch(null, child, container, anchor)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function normalizeChild(child: any): VNode {
|
2019-05-25 23:51:20 +08:00
|
|
|
if (child == null) {
|
2019-05-26 15:19:44 +08:00
|
|
|
// empty placeholder
|
2019-05-25 23:51:20 +08:00
|
|
|
return createVNode(Empty)
|
|
|
|
} else if (Array.isArray(child)) {
|
2019-05-26 15:19:44 +08:00
|
|
|
// fragment
|
2019-05-25 23:51:20 +08:00
|
|
|
return createVNode(Fragment, null, child)
|
2019-05-26 15:19:44 +08:00
|
|
|
} else if (typeof child === 'object') {
|
|
|
|
// already vnode
|
|
|
|
return child as VNode
|
2019-05-25 23:51:20 +08:00
|
|
|
} else {
|
2019-05-26 15:19:44 +08:00
|
|
|
// primitive types
|
|
|
|
return createVNode(Text, null, child + '')
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function patchElement(n1: VNode, n2: VNode, optimized?: boolean) {
|
2019-05-25 23:51:20 +08:00
|
|
|
const el = (n2.el = n1.el)
|
|
|
|
const { patchFlag, dynamicChildren } = n2
|
|
|
|
const oldProps = (n1 && n1.props) || emptyObj
|
|
|
|
const newProps = n2.props || emptyObj
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2019-05-25 23:51:20 +08:00
|
|
|
if (patchFlag != null) {
|
|
|
|
// 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-05-25 23:51:20 +08:00
|
|
|
// class
|
|
|
|
// this flag is matched when the element has dynamic class bindings.
|
|
|
|
if (patchFlag & CLASS) {
|
|
|
|
// TODO handle full class API, potentially optimize at compilation stage?
|
|
|
|
if (oldProps.class !== newProps.class) {
|
2019-05-26 15:38:55 +08:00
|
|
|
hostPatchProp(el, 'class', newProps.class, null, false)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:51:20 +08:00
|
|
|
// style
|
|
|
|
// this flag is matched when the element has dynamic style bindings
|
|
|
|
// TODO separate static and dynamic styles?
|
|
|
|
if (patchFlag & STYLE) {
|
2019-05-26 15:38:55 +08:00
|
|
|
hostPatchProp(el, 'style', newProps.style, oldProps.style, false)
|
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
|
|
|
|
if (patchFlag & PROPS) {
|
2019-05-26 15:19:44 +08:00
|
|
|
// if the flag is present then dynamicProps must be non-null
|
|
|
|
const propsToUpdate = n2.dynamicProps as string[]
|
2019-05-25 23:51:20 +08:00
|
|
|
for (let i = 0; i < propsToUpdate.length; i++) {
|
|
|
|
const key = propsToUpdate[i]
|
|
|
|
const prev = oldProps[key]
|
|
|
|
const next = newProps[key]
|
|
|
|
if (prev !== next) {
|
2019-05-26 15:19:44 +08:00
|
|
|
hostPatchProp(
|
|
|
|
el,
|
|
|
|
key,
|
|
|
|
next,
|
|
|
|
prev,
|
|
|
|
false,
|
|
|
|
n1.children as VNode[],
|
|
|
|
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).
|
|
|
|
if (patchFlag & TEXT) {
|
|
|
|
if (n1.children !== n2.children) {
|
2019-05-26 15:19:44 +08:00
|
|
|
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
|
2019-05-26 15:19:44 +08:00
|
|
|
patchProps(el, n2, oldProps, newProps)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 23:51:20 +08:00
|
|
|
if (dynamicChildren != null) {
|
|
|
|
// children fast path
|
2019-05-26 15:19:44 +08:00
|
|
|
const olddynamicChildren = n1.dynamicChildren as VNode[]
|
2019-05-25 23:51:20 +08:00
|
|
|
for (let i = 0; i < dynamicChildren.length; i++) {
|
|
|
|
patch(olddynamicChildren[i], dynamicChildren[i], el, null, true)
|
2018-10-03 01:59:11 +08:00
|
|
|
}
|
2019-05-25 23:51:20 +08:00
|
|
|
} else if (!optimized) {
|
|
|
|
// full diff
|
|
|
|
patchChildren(n1, n2, el)
|
2018-10-03 01:59:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function patchProps(
|
|
|
|
el: HostNode,
|
|
|
|
vnode: VNode,
|
|
|
|
oldProps: any,
|
|
|
|
newProps: any
|
|
|
|
) {
|
2019-05-25 23:51:20 +08:00
|
|
|
if (oldProps !== newProps) {
|
|
|
|
for (const key in newProps) {
|
|
|
|
const next = newProps[key]
|
|
|
|
const prev = oldProps[key]
|
|
|
|
if (next !== prev) {
|
2019-05-26 15:19:44 +08:00
|
|
|
hostPatchProp(
|
|
|
|
el,
|
|
|
|
key,
|
|
|
|
next,
|
|
|
|
prev,
|
|
|
|
false,
|
|
|
|
vnode.children as VNode[],
|
|
|
|
unmountChildren
|
|
|
|
)
|
2018-09-27 05:10:34 +08:00
|
|
|
}
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
2019-05-25 23:51:20 +08:00
|
|
|
if (oldProps !== emptyObj) {
|
|
|
|
for (const key in oldProps) {
|
|
|
|
if (!(key in newProps)) {
|
2019-05-26 15:19:44 +08:00
|
|
|
hostPatchProp(
|
|
|
|
el,
|
|
|
|
key,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
vnode.children as VNode[],
|
|
|
|
unmountChildren
|
|
|
|
)
|
2019-05-25 23:51:20 +08:00
|
|
|
}
|
2018-10-03 01:59:11 +08:00
|
|
|
}
|
|
|
|
}
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function processFragment(
|
|
|
|
n1: VNode | null,
|
|
|
|
n2: VNode,
|
|
|
|
container: HostNode,
|
|
|
|
anchor?: HostNode,
|
|
|
|
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)
|
2019-05-26 15:19:44 +08:00
|
|
|
// a fragment can only have array children
|
2019-05-27 13:48:40 +08:00
|
|
|
mountChildren(n2.children as VNodeChildren, container, fragmentEndAnchor)
|
2018-09-19 23:35:38 +08:00
|
|
|
} else {
|
2019-05-27 13:48:40 +08:00
|
|
|
patchChildren(n1, n2, container, fragmentEndAnchor, optimized)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function patchChildren(
|
|
|
|
n1: VNode | null,
|
|
|
|
n2: VNode,
|
|
|
|
container: HostNode,
|
|
|
|
anchor?: HostNode,
|
|
|
|
optimized?: boolean
|
|
|
|
) {
|
2019-05-25 23:51:20 +08:00
|
|
|
const c1 = n1 && n1.children
|
|
|
|
const c2 = n2.children
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2019-05-25 23:51:20 +08:00
|
|
|
// fast path
|
|
|
|
const { patchFlag } = n2
|
|
|
|
if (patchFlag != null) {
|
|
|
|
if (patchFlag & KEYED) {
|
|
|
|
// this could be either fully-keyed or mixed (some keyed some not)
|
2019-05-26 15:19:44 +08:00
|
|
|
// presence of patchFlag means children are guaranteed to be arrays
|
|
|
|
patchKeyedChildren(
|
|
|
|
c1 as VNode[],
|
|
|
|
c2 as VNodeChildren,
|
|
|
|
container,
|
|
|
|
anchor,
|
|
|
|
optimized
|
|
|
|
)
|
2019-05-25 23:51:20 +08:00
|
|
|
return
|
|
|
|
} else if (patchFlag & UNKEYED) {
|
|
|
|
// unkeyed
|
2019-05-26 15:19:44 +08:00
|
|
|
patchUnkeyedChildren(
|
|
|
|
c1 as VNode[],
|
|
|
|
c2 as VNodeChildren,
|
|
|
|
container,
|
|
|
|
anchor,
|
|
|
|
optimized
|
|
|
|
)
|
2018-11-09 03:09:52 +08:00
|
|
|
return
|
|
|
|
}
|
2018-09-20 11:56:40 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 23:51:20 +08:00
|
|
|
if (typeof c2 === 'string') {
|
|
|
|
// text children fast path
|
|
|
|
if (Array.isArray(c1)) {
|
2019-05-26 15:19:44 +08:00
|
|
|
unmountChildren(c1 as VNode[])
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
2019-05-25 23:51:20 +08:00
|
|
|
hostSetElementText(container, c2)
|
|
|
|
} else {
|
|
|
|
if (typeof c1 === 'string') {
|
2019-05-26 15:19:44 +08:00
|
|
|
hostSetElementText(container, '')
|
|
|
|
if (c2 != null) {
|
|
|
|
mountChildren(c2, container, anchor)
|
|
|
|
}
|
|
|
|
} else if (Array.isArray(c1)) {
|
|
|
|
if (Array.isArray(c2)) {
|
|
|
|
// two arrays, cannot assume anything, do full diff
|
|
|
|
patchKeyedChildren(c1 as VNode[], c2, container, anchor, optimized)
|
|
|
|
} else {
|
|
|
|
// c2 is null in this case
|
2019-05-27 13:48:40 +08:00
|
|
|
unmountChildren(c1 as VNode[], true)
|
2019-05-26 15:19:44 +08:00
|
|
|
}
|
2018-11-02 13:09:00 +08:00
|
|
|
}
|
2018-11-02 05:08:33 +08:00
|
|
|
}
|
2018-09-27 06:34:21 +08:00
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function patchUnkeyedChildren(
|
|
|
|
c1: VNode[],
|
|
|
|
c2: VNodeChildren,
|
|
|
|
container: HostNode,
|
|
|
|
anchor?: HostNode,
|
|
|
|
optimized?: boolean
|
|
|
|
) {
|
2019-05-25 23:51:20 +08:00
|
|
|
c1 = c1 || emptyArr
|
|
|
|
c2 = c2 || emptyArr
|
|
|
|
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] = normalizeChild(c2[i]))
|
|
|
|
patch(c1[i], nextChild, container, null, optimized)
|
2018-09-27 06:34:21 +08:00
|
|
|
}
|
2019-05-25 23:51:20 +08:00
|
|
|
if (oldLength > newLength) {
|
|
|
|
// remove old
|
2019-05-27 13:48:40 +08:00
|
|
|
unmountChildren(c1, true, commonLength)
|
2019-05-25 23:51:20 +08:00
|
|
|
} else {
|
|
|
|
// mount new
|
|
|
|
mountChildren(c2, container, anchor, commonLength)
|
2018-09-27 06:34:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:51:20 +08:00
|
|
|
// can be all-keyed or mixed
|
2019-05-26 15:19:44 +08:00
|
|
|
function patchKeyedChildren(
|
|
|
|
c1: VNode[],
|
|
|
|
c2: VNodeChildren,
|
|
|
|
container: HostNode,
|
2019-05-27 15:28:56 +08:00
|
|
|
parentAnchor?: HostNode,
|
2019-05-26 15:19:44 +08:00
|
|
|
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]
|
|
|
|
const n2 = (c2[i] = normalizeChild(c2[i]))
|
|
|
|
if (isSameType(n1, n2)) {
|
2019-05-27 15:28:56 +08:00
|
|
|
patch(n1, n2, container, parentAnchor, 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]
|
|
|
|
const n2 = (c2[e2] = normalizeChild(c2[e2]))
|
|
|
|
if (isSameType(n1, n2)) {
|
2019-05-27 15:28:56 +08:00
|
|
|
patch(n1, n2, container, parentAnchor, 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-05-27 15:28:56 +08:00
|
|
|
patch(null, (c2[i] = normalizeChild(c2[i])), container, anchor)
|
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], true)
|
|
|
|
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++) {
|
|
|
|
const nextChild = (c2[i] = normalizeChild(c2[i]))
|
|
|
|
if (nextChild.key != null) {
|
|
|
|
// TODO warn duplicate keys
|
|
|
|
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
|
2019-05-27 15:28:56 +08:00
|
|
|
unmount(prevChild, true)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let newIndex
|
|
|
|
if (prevChild.key != null) {
|
|
|
|
newIndex = keyToNewIndexMap.get(prevChild.key)
|
|
|
|
} else {
|
|
|
|
// key-less node, try to locate a key-less node of the same type
|
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, true)
|
|
|
|
} else {
|
|
|
|
newIndexToOldIndexMap[newIndex - s2] = i + 1
|
|
|
|
if (newIndex >= maxNewIndexSoFar) {
|
|
|
|
maxNewIndexSoFar = newIndex
|
|
|
|
} else {
|
|
|
|
moved = true
|
|
|
|
}
|
|
|
|
patch(prevChild, c2[newIndex] as VNode, container, null, optimized)
|
|
|
|
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)
|
|
|
|
: emptyArr
|
|
|
|
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
|
|
|
|
patch(null, nextChild, container, anchor)
|
|
|
|
} 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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function unmount(vnode: VNode, doRemove?: boolean) {
|
2019-05-27 13:48:40 +08:00
|
|
|
const shouldRemoveChildren = vnode.type === Fragment && doRemove
|
2019-05-26 15:19:44 +08:00
|
|
|
if (vnode.dynamicChildren != null) {
|
2019-05-27 13:48:40 +08:00
|
|
|
unmountChildren(vnode.dynamicChildren, shouldRemoveChildren)
|
2019-05-26 15:19:44 +08:00
|
|
|
} else if (Array.isArray(vnode.children)) {
|
2019-05-27 13:48:40 +08:00
|
|
|
unmountChildren(vnode.children as VNode[], shouldRemoveChildren)
|
2019-05-26 15:19:44 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
function unmountChildren(
|
|
|
|
children: VNode[],
|
2019-05-27 13:48:40 +08:00
|
|
|
doRemove?: boolean,
|
|
|
|
start: number = 0
|
2019-05-26 15:19:44 +08:00
|
|
|
) {
|
2019-05-25 23:51:20 +08:00
|
|
|
for (let i = start; i < children.length; i++) {
|
|
|
|
unmount(children[i], doRemove)
|
2018-11-02 13:21:38 +08:00
|
|
|
}
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
|
2019-05-26 15:19:44 +08:00
|
|
|
return function render(vnode: VNode, dom: HostNode): VNode {
|
2019-05-25 23:51:20 +08:00
|
|
|
patch(dom._vnode, vnode, dom)
|
|
|
|
return (dom._vnode = vnode)
|
2018-10-17 03:47:51 +08:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|