This repository has been archived by the owner on Mar 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.master.js
226 lines (199 loc) · 4.83 KB
/
index.master.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
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
const CREATE = 'CREATE'
const REMOVE = 'REMOVE'
const REPLACE = 'REPLACE'
const UPDATE = 'UPDATE'
const SET_PROP = 'SET_PROP'
const REMOVE_PROP = 'REMOVE PROP'
//// DIFF
function changed(node1, node2) {
return typeof node1 !== typeof node2 ||
typeof node1 === 'string' && node1 !== node2 ||
node1.type !== node2.type
}
function diffProps(newNode, oldNode) {
const patches = []
const props = Object.assign({}, newNode.props, oldNode.props)
Object.keys(props).forEach(name => {
const newVal = newNode.props[name]
const oldVal = oldNode.props[name]
if(!newVal) {
patches.push({type: REMOVE_PROP, name, value: oldVal})
} else if (!oldVal || newVal !== oldVal) {
patches.push({type: SET_PROP, name, value: newVal})
}
})
return patches
}
function diffChildren(newNode, oldNode) {
const patches = []
const patchesLength = Math.max(
newNode.children.length,
oldNode.children.length
)
for (let i = 0; i < patchesLength; i++) {
patches[i] = diff(
newNode.children[i],
oldNode.children[i]
)
}
return patches
}
/*
stage 1 UPDATE return only type
stage 2 UPDATE return add children
stage 3 UPDATE full
*/
function diff(newNode, oldNode) { //@
if(!oldNode) {
return {type: CREATE, newNode}
}
if(!newNode) {
return {type: REMOVE}
}
if(changed(newNode, oldNode)) {
return {type: REPLACE, newNode}
}
if(newNode.type) {
return {
type: UPDATE,
props: diffProps(newNode, oldNode),
children: diffChildren(newNode, oldNode),
}
}
}
//// PATCH
/*
Stage 1 single element:
if (typeof node === 'string') {
return document.createTextNode(node)
}
return document.createElement(node.type)
Stage 2 children:
if (typeof node === 'string') {
return document.createTextNode(node)
}
const el = document.createElement(node.type)
node.children
.map(createElement)
.forEach(el.appendChild.bind(el))
return el
Stage 3: add
setProps(el, node.props)
*/
function createElement(node) { //@
if (typeof node === 'string') {
return document.createTextNode(node)
}
const el = document.createElement(node.type)
setProps(el, node.props)
node.children
.map(createElement)
.forEach(el.appendChild.bind(el))
return el
}
/*
stage 1:
target.setAttribute(name, value)
stage 2: add
if (name === 'className') {
return target.setAttribute('class', value)
}
*/
function setProp(target, name, value) { //@
if (name === 'className') {
return target.setAttribute('class', value)
}
target.setAttribute(name, value)
}
function setProps(target, props) {
Object.keys(props).forEach(name => {
setProp(target, name, props[name])
})
}
// Start with the last line,
// then className
function removeProp(target, name, value) {
if (name === 'className') {
return target.removeAttribute('class')
}
target.removeAttribute(name)
}
function patchProps(parent, patches) {
for (let i = 0; i < patches.length; i++) {
const propPatch = patches[i]
const {type, name, value} = propPatch
if (type === SET_PROP) {
setProp(parent, name, value)
}
if (type === REMOVE_PROP) {
removeProp(parent, name, value)
}
}
}
/*
stage 1 no props or children
stage 2 + children
const {children} = patches
for (let i = 0; i < children.length; i++) {
patch(el, children[i], i)
}
stage 3 + props
*/
function patch(parent, patches, index = 0) { //@
if (!patches) { return }
const el = parent.childNodes[index]
switch (patches.type) {
case CREATE: {
const {newNode} = patches
const newEl = createElement(newNode)
return parent.appendChild(newEl)
}
case REMOVE: {
return parent.removeChild(el)
}
case REPLACE: {
const {newNode} = patches
const newEl = createElement(newNode)
return parent.replaceChild(newEl, el)
}
case UPDATE: {
const {props, children} = patches
patchProps(el, props)
for (let i = 0; i < children.length; i++) {
patch(el, children[i], i)
}
}
}
}
//// My Application
function flatten(arr) {
return [].concat.apply([], arr)
}
function h(type, props, ...children) {
props = props || {}
return { type, props, children: flatten(children) }
}
/*
Stage 1 JSX -> h:
return <ul id="cool" className="foo">
<li className="test">text 1</li>
<li>text 2</li>
</ul>
*/
function view(count) { //@
const r = [...Array(count).keys()]
return <ul id="cool" className={`my-class-${count % 3}`}>
{ r.map(n => <li>item {(count * n).toString()}</li>) }
</ul>
}
function tick(el, count) {
const patches = diff(view(count + 1), view(count))
patch(el, patches)
console.log(count, patches)
if(count > 20) { return }
setTimeout(() => tick(el, count + 1), 500)
}
function render(el) { //@
el.appendChild(createElement(view(0)))
setTimeout(() => tick(el, 0), 500)
}