-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff1.html
288 lines (266 loc) · 9.78 KB
/
diff1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/@vue/[email protected]/dist/reactivity.global.js"></script>
</head>
<body>
<div id="app"></div>
</body>
<script>
// 双端diff算法
const vnode = {
type: 'h1',
props: {
'class': 'box'
},
children: 'hello'
}
function createRenderer() {
const patchKeyedChildren = (n1, n2, container) => {
const oldChildren = n1.children
const newChildren = n2.children
// 四个索引值
let oldStartIdx = 0
let oldEndIdx = oldChildren.length - 1
let newStartIdx = 0
let newEndIdx = newChildren.length - 1
// 四个索引值所对应的虚拟节点
let oldStartVNode = oldChildren[oldStartIdx]
let oldEndVNode = oldChildren[oldEndIdx]
let newStartVNode = newChildren[newStartIdx]
let newEndVNode = newChildren[newEndIdx]
while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
if (!oldStartVNode) {
oldStartVNode = oldChildren[++oldStartIdx]
} else if (!oldEndVNode) {
oldEndVNode = oldChildren[--oldEndIdx]
} else if (oldStartVNode.key === newStartVNode.key) {
// 因为两者都处于头部,所以无需进行dom移动
patch(oldStartVNode, newStartVNode, container)
oldStartVNode = oldChildren[++oldStartIdx]
newStartVNode = newChildren[++newStartIdx]
} else if (oldEndVNode.key === newEndVNode.key) {
patch(oldEndVNode, newEndVNode, container)
// 因为两者都处于尾部,所以无需进行dom移动
oldEndVNode = oldChildren[--oldEndIdx]
newEndVNode = newChildren[--newEndIdx]
} else if (oldStartVNode.key === newEndVNode.key) {
patch(oldStartVNode, newEndVNode, container)
insert(oldStartVNode.el, container, oldEndVNode.el.nextSibling)
oldStartVNode = oldChildren[++oldStartIdx]
newEndVNode = newChildren[--newEndIdx]
} else if (oldEndVNode.key === newStartVNode.key) {
patch(oldEndVNode, newStartVNode, container)
insert(oldEndVNode.el, container, oldStartVNode.el) // dom移动
oldEndVNode = oldChildren[--oldEndIdx]
newStartVNode = newChildren[++newStartVNode]
} else {
// 非理想状态的处理方式
const idxInOld = oldChildren.findIndex(node => node.key === newStartVNode.key)
if (idxInOld > 0) { // 将真实dom移动到头部
const vnodeToMove = oldChildren[idxInOld]
patch(vnodeToMove, newStartVNode, container)
insert(vnodeToMove.el, container, oldStartVNode.el)
oldChildren[idxInOld] = undefined
newStartVNode = newChildren[++newStartIdx]
} else {
// 表明是新增dom, 传入锚点,进行挂载
patch(null, newStartVNode, container, oldStartVNode.el)
}
newStartVNode = newChildren[++newStartIdx]
}
}
if (oldEndIdx < oldStartIdx && newStartIdx <= newEndIdx) {
// 防止遗漏
for (let i = newStartIdx; i < newEndIdx; i++) {
patch(null, newChildren[i], container, oldStartVNode.el)
}
} else if (newEndIdx < newStartIdx && oldStartIdx <= oldEndIdx) {
// 移除不存在的元素
for (let i = oldStartIdx; i < oldEndIdx; i++) {
unmount(oldChildren[i])
}
}
}
const patchChildren = (n1, n2, container) => { // 更新children
if (typeof n2.children === 'string') { // 新子节点是文本子节点
if (Array.isArray(n1.children)) {
n1.children.forEach(c => unmount(c)) // 逐个卸载旧子节点
}
setElementText(container, n2.children)
} else if (Array.isArray(n2.children)) { // 新子节点是一组子节点
if (Array.isArray(n1.children)) {
patchKeyedChildren(n1, n2, container)
} else {
// 旧节点要么是文本子节点,要么不存在
setElementText(container, '') // 先清空容器
n2.children.forEach(c => patch(null, c, container)) // 再将新的一组子节点逐个进行挂载
}
} else {
// 新子节点不存在
if (Array.isArray(n1.children)) {
n1.children.forEach(c => unmount(c)) // 旧子节点是一组,逐个进行卸载
} else if (typeof n1.children === 'string') {
setElementText(container, '') // 旧子节点是文本,直接进行清空即可
}
}
}
const patchElement = (n1, n2) => { // 打补丁(更新)
const el = n2.el = n1.el
const oldProps = n1.props
const newProps = n2.props
// 第一步:更新这个props
for (const key in newProps) {
if (newProps[key] !== oldProps[key]) {
patchProps(el, key, oldProps[key], newProps[key])
}
}
for (const key in oldProps) {
if (!(key in newProps)) {
patchProps(el, key, oldProps[key], null)
}
}
// 第二步:更新children
patchChildren(n1, n2, el)
}
const shouldSetAsProps = (el, key) => {
// 判断dom properties是否是只读,如果是只读通过setAttribute来设置属性
if (key === 'form' && el.tagName === 'INPUT') {
return false
}
return key in el
}
const mountElement = (vnode, container, anchor) => { // 第一次挂载过程
const el = vnode.el = createElement(vnode.type) // 让vnode.el引用真实dom元素
if (vnode.props) { // 处理vnode中的props
for (const key in vnode.props) {
// el.setAttribute(key, vnode.props[key]) // 直接设置有缺陷
patchProps(el, key, null, vnode.props[key])
}
}
if (typeof vnode.children === 'string') { // 处理vnode中的children
setElementText(el, vnode.children)
} else if (Array.isArray(vnode.children)) {
vnode.children.forEach(child => {
patch(null, child, el)
})
}
insert(el, container, anchor)
}
const patch = (n1, n2, container, anchor) => {
if (n1 && n1.type !== n2.type) { //如果新旧node的type不同,直接卸载旧的node
unmount(n1)
n1 = null
}
const { type } = n2
if (type === 'string') { // 普通标签元素
if (!n1) { // 挂载
mountElement(n2, container, anchor)
} else { // 打补丁(更新)
patchElement(n1, n2)
}
} else if (type === 'object') { // 组件
} else if (type === Text) { // 文本节点
if (!n1) {
const el = n2.el = createText(n2.children)
insert(el, container)
} else {
const el = n2.el = n1.el
if (n2.children !== n1.children) {
setText(el, n2.children)
}
}
} else if (type === Fragment) { // 片段
if (!n1) {
n2.children.forEach(c => patch(null, c, container))
} else {
patchChildren(n1, n2, container)
}
}
}
const unmount = (vnode) => { // 卸载操作
if (vnode.type === Fragment) {
vnode.children.forEach(c => unmount(c))
return
}
const parent = vnode.el.parent
if (parent) parent.removeChild(vnode.el)
}
const render = (vnode, container) => {
if (vnode) {
patch(container._vnode, vnode, container) // 挂载或更新
} else {
if (container._vnode) {
// container.innerHTML = '' // 卸载,这样的卸载操作有缺陷
unmount(container._vnode)
}
}
container._vnode = vnode
}
return {
render
}
}
const renderer = createRenderer(
{
createElement(tag) {
return document.createElement(tag)
},
createText(text) {
return document.createTextNode(text)
},
setText(el, text) {
el.nodeValue = text
},
setElementText(el, text) {
el.textContent = text
},
insert(el, parent, anchor = null) {
parent.insertBefore(el, anchor)
},
patchProps(el, key, preValue, nextValue) { // 用来对dom对象上的props进行更新操作
if (/^on/.test(key)) { // 对事件进行处理
let invokers = el._vei || (el._vei = {})
const invoker = invokers[key]
const name = key.slice(2).toLowerCase()
if (nextValue) {
if (!invoker) { // 添加绑定
invoker = el._vei[key] = (e) => { // 伪造事件处理函数
if (e.timestamp < invoker.attached) return;
if (Array.isArray(invoker.value)) { // 判断同一个事件绑定多个处理函数
invoker.value.forEach(fn => fn(e))
} else {
invoker.value(e)
}
}
invoker.value = nextValue
invoker.attached = performance.now()
el.addEventListener(name, invoker)
} else { // 更新,直接更新invoker的value值即可
invoker.value = nextValue
}
} else if (invoker) { // 卸载
el.removeEventListener(name, invoker)
}
} else if (key === 'class') { // 对class做特殊处理, className的性能比setAttribute好得多
el.className = nextValue || ''
} else if (shouldSetAsProps(el, key)) {
const type = typeof el[key]
if (type === 'boolean' && nextValue === '') {
el[key] = true
} else {
el[key] = nextValue
}
} else {
el.setAttribute(key, nextValue)
}
}
}
)
renderer.render(vnode, document.getElementById('app'))
</script>
</html>