This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
patch.js
168 lines (150 loc) · 5.99 KB
/
patch.js
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
const render = require('./render')
const updateProps = require('./update-props')
const updateRef = require('./update-ref')
function patch (oldVirtualNode, newVirtualNode, options) {
const oldNode = oldVirtualNode.domNode
if (newVirtualNode === oldVirtualNode) return oldNode
if (virtualNodesAreEqual(oldVirtualNode, newVirtualNode)) {
let newNode
if (newVirtualNode.text != null) {
oldNode.nodeValue = newVirtualNode.text
newNode = oldNode
} else {
if (typeof newVirtualNode.tag === 'function') {
newNode = updateComponent(oldVirtualNode, newVirtualNode, options)
} else {
updateChildren(oldNode, oldVirtualNode.children, newVirtualNode.children, options)
updateProps(oldNode, oldVirtualNode, newVirtualNode, options)
newNode = oldNode
}
}
newVirtualNode.domNode = newNode
if (newNode !== oldNode && oldNode.parentNode) {
oldNode.parentNode.replaceChild(newNode, oldNode)
}
return newNode
} else {
const parentNode = oldNode.parentNode
const nextSibling = oldNode.nextSibling
removeVirtualNode(oldVirtualNode, options && options.refs)
const newNode = render(newVirtualNode, options)
if (parentNode) parentNode.insertBefore(newNode, nextSibling)
newVirtualNode.domNode = newNode
return newNode
}
}
function updateComponent (oldVirtualNode, newVirtualNode, options) {
const {component, props: oldProps} = oldVirtualNode
let {props: newProps, children: newChildren} = newVirtualNode
newVirtualNode.component = component
const refs = options && options.refs
if (refs) updateRef(component, oldProps && oldProps.ref, newProps && newProps.ref, refs)
component.update(newProps || {}, newChildren)
return component.element
}
let mapPool = [new Map(), new Map(), new Map(), new Map()]
function updateChildren (parentElement, oldChildren, newChildren, options) {
var oldStartIndex = 0
var oldEndIndex = oldChildren.length - 1
var oldStartChild = oldChildren[0]
var oldEndChild = oldChildren[oldEndIndex]
var newStartIndex = 0
var newEndIndex = newChildren.length - 1
var newStartChild = newChildren[0]
var newEndChild = newChildren[newEndIndex]
var oldIndicesByKey
while (oldStartIndex <= oldEndIndex && newStartIndex <= newEndIndex) {
if (!oldStartChild) {
oldStartChild = oldChildren[++oldStartIndex]
} else if (!oldEndChild) {
oldEndChild = oldChildren[--oldEndIndex]
} else if (virtualNodesAreEqual(oldStartChild, newStartChild)) {
patch(oldStartChild, newStartChild, options)
oldStartChild = oldChildren[++oldStartIndex]
newStartChild = newChildren[++newStartIndex]
} else if (virtualNodesAreEqual(oldEndChild, newEndChild)) {
patch(oldEndChild, newEndChild, options)
oldEndChild = oldChildren[--oldEndIndex]
newEndChild = newChildren[--newEndIndex]
} else if (virtualNodesAreEqual(oldStartChild, newEndChild)) {
patch(oldStartChild, newEndChild, options)
parentElement.insertBefore(oldStartChild.domNode, oldEndChild.domNode.nextSibling)
oldStartChild = oldChildren[++oldStartIndex]
newEndChild = newChildren[--newEndIndex]
} else if (virtualNodesAreEqual(oldEndChild, newStartChild)) {
patch(oldEndChild, newStartChild, options)
parentElement.insertBefore(oldEndChild.domNode, oldStartChild.domNode);
oldEndChild = oldChildren[--oldEndIndex]
newStartChild = newChildren[++newStartIndex]
} else {
if (!oldIndicesByKey) {
if (mapPool.length > 0) {
oldIndicesByKey = mapPool.pop()
oldIndicesByKey.clear()
} else {
oldIndicesByKey = new Map()
}
mapOldKeysToIndices(oldIndicesByKey, oldChildren, oldStartIndex, oldEndIndex)
}
var key = getKey(newStartChild)
var oldIndex = key ? oldIndicesByKey.get(key) : null
if (oldIndex == null) {
parentElement.insertBefore(render(newStartChild, options), oldStartChild.domNode)
newStartChild = newChildren[++newStartIndex]
} else {
var oldChildToMove = oldChildren[oldIndex]
patch(oldChildToMove, newStartChild, options)
oldChildren[oldIndex] = undefined
parentElement.insertBefore(oldChildToMove.domNode, oldStartChild.domNode)
newStartChild = newChildren[++newStartIndex]
}
}
}
if (oldStartIndex > oldEndIndex) {
var subsequentElement = newChildren[newEndIndex + 1] ? newChildren[newEndIndex + 1].domNode : null
for (let i = newStartIndex; i <= newEndIndex; i++) {
parentElement.insertBefore(render(newChildren[i], options), subsequentElement)
}
} else if (newStartIndex > newEndIndex) {
for (let i = oldStartIndex; i <= oldEndIndex; i++) {
var child = oldChildren[i]
if (child) removeVirtualNode(child, options && options.refs)
}
}
if (oldIndicesByKey) mapPool.push(oldIndicesByKey)
}
function removeVirtualNode (virtualNode, refs, removeDOMNode = true) {
const {domNode, props, children, component} = virtualNode
const ref = props && props.ref
if (component) {
if (typeof ref === 'function') ref(null)
else if (refs && ref && refs[ref] === component) delete refs[ref]
if (component.destroy) component.destroy()
} else {
if (typeof ref === 'function') ref(null)
else if (refs && ref && refs[ref] === domNode) delete refs[ref]
if (children) {
for (let i = 0; i < children.length; i++) {
removeVirtualNode(children[i], refs, false)
}
}
}
if (removeDOMNode) domNode.remove()
}
function virtualNodesAreEqual (oldVirtualNode, newVirtualNode) {
return (
getKey(oldVirtualNode) === getKey(newVirtualNode)
&& oldVirtualNode.tag === newVirtualNode.tag
)
}
function getKey (virtualNode) {
return virtualNode.props ? virtualNode.props.key : undefined
}
function mapOldKeysToIndices (oldIndicesByKey, children, startIndex, endIndex) {
for (let i = startIndex; i <= endIndex; i++) {
const key = getKey(children[i])
if (key) oldIndicesByKey.set(key, i)
}
return oldIndicesByKey
}
module.exports = patch