forked from Freak613/stage0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreuseNodes.js
49 lines (44 loc) · 1.54 KB
/
reuseNodes.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
import { noOpUpdate } from './utils';
export function reuseNodes(parent, renderedValues, data, createFn, noOp = noOpUpdate, beforeNode, afterNode) {
if (data.length === 0) {
if (beforeNode !== undefined || afterNode !== undefined) {
let node = beforeNode !== undefined ? beforeNode.nextSibling : parent.firstChild,
tmp
if (afterNode === undefined) afterNode = null
while(node !== afterNode) {
tmp = node.nextSibling
parent.removeChild(node)
node = tmp
}
} else {
parent.textContent = ""
}
return
}
if (renderedValues.length > data.length) {
let i = renderedValues.length,
tail = afterNode !== undefined ? afterNode.previousSibling : parent.lastChild,
tmp
while(i > data.length) {
tmp = tail.previousSibling
parent.removeChild(tail)
tail = tmp
i--
}
}
let _head = beforeNode ? beforeNode.nextSibling : parent.firstChild
if (_head === afterNode) _head = undefined
let _mode = afterNode ? 1 : 0
for(let i = 0, item, head = _head, mode = _mode; i < data.length; i++) {
item = data[i]
if (head) {
noOp(head, item)
} else {
head = createFn(item)
mode ? parent.insertBefore(head, afterNode) : parent.appendChild(head)
}
head = head.nextSibling
if (head === afterNode) head = null
}
}
export default reuseNodes