forked from unconed/mathbox
-
Notifications
You must be signed in to change notification settings - Fork 7
/
vdom.coffee
321 lines (258 loc) · 8.7 KB
/
vdom.coffee
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# Quick'n'dirty Virtual DOM diffing
# with a poor man's React for components
#
# This is for rendering HTML with data from a GL readback. See DOM examples.
HEAP = []
id = 0
# Static render components
Types = {
###
# el('example', props, children);
example: MathBox.DOM.createClass({
render: (el, props, children) ->
# VDOM node
return el('span', { className: "foo" }, "Hello World")
})
###
}
descriptor = () ->
id: id++
type: null
props: null
children: null
rendered: null
instance: null
hint = (n) ->
n *= 2
n = Math.max 0, HEAP.length - n
HEAP.push descriptor() for i in [0...n]
element = (type, props, children) ->
el = if HEAP.length then HEAP.pop() else descriptor()
el.type = type ? 'div'
el.props = props ? null
el.children = children ? null
# Can't use `arguments` here to pass children as direct args, it de-optimizes label emitters
el
recycle = (el) ->
return unless el.type
children = el.children
el.type = el.props = el.children = el.instance = null
HEAP.push el
recycle child for child in children if children?
return
apply = (el, last, node, parent, index) ->
if el?
if !last?
# New node
return mount el, parent, index
else
# Literal DOM node
if el instanceof Node
same = el == last
return if same
else
# Check compatibility
same = typeof el == typeof last and
last != null and el != null and
el.type == last.type
if !same
# Not compatible: unmount and remount
unmount last.instance, node
node.remove()
return mount el, parent, index
else
# Maintain component ref
el.instance = last.instance
# Check if it's a component
type = if el.type?.isComponentClass then el.type else Types[el.type]
# Prepare to diff props and children
props = last?.props
nextProps = el .props
children = last?.children ? null
nextChildren = el .children
nextProps.children = nextChildren if nextProps?
# Component
if type?
# See if it changed
dirty = node._COMPONENT_DIRTY
dirty = true if props? != nextProps?
dirty = true if children != nextChildren
if props? and nextProps?
dirty = true for key of props when !nextProps.hasOwnProperty key if !dirty
dirty = true for key, value of nextProps when (ref = props[key]) != value if !dirty
if dirty
comp = last.instance
el.props ?= {}
el.props[k] ?= v for k, v of comp.defaultProps
el.props.children = el.children
comp.willReceiveProps? el.props
should = node._COMPONENT_FORCE || (comp.shouldUpdate?(el.props) ? true)
if should
nextState = comp.getNextState()
comp.willUpdate? el.props, nextState
prevProps = comp.props
prevState = comp.applyNextState()
comp.props = el.props
comp.children = el.children
if should
el = el.rendered = comp.render? element, el.props, el.children
apply el, last.rendered, node, parent, index
comp.didUpdate? prevProps, prevState
return
else
# VDOM node
unset node, key, props[key] for key of props when !nextProps.hasOwnProperty key if props?
set node, key, value, ref for key, value of nextProps when (ref = props[key]) != value and key != 'children' if nextProps?
# Diff children
if nextChildren?
if typeof nextChildren in ['string', 'number']
# Insert text directly
if nextChildren != children
node.textContent = nextChildren
else
if nextChildren.type?
# Single child
apply nextChildren, children, node.childNodes[0], node, 0
else
# Diff children
childNodes = node.childNodes
if children?
apply child, children[i], childNodes[i], node, i for child, i in nextChildren
else
apply child, null, childNodes[i], node, i for child, i in nextChildren
else if children?
# Unmount all child components
unmount null, node
# Remove all children
node.innerHTML = ''
return
if last?
# Removed node
unmount last.instance, node
last.node.remove()
mount = (el, parent, index = 0) ->
type = if el.type?.isComponentClass then el.type else Types[el.type]
# Literal DOM node
if el instanceof Node
node = el
else
if type?
# Component
ctor = if el.type?.isComponentClass then el.type else Types[el.type]
# No component class found
if !ctor
el = el.rendered = element 'noscript'
node = mount el, parent, index
return node
# Construct component class
el.instance = comp = new ctor parent
el.props ?= {}
el.props[k] ?= v for k, v of comp.defaultProps
el.props.children = el.children
# Do initial state transition
comp.props = el.props
comp.children = el.children
comp.setState comp.getInitialState?()
comp.willMount?()
# Render
el = el.rendered = comp.render? element, el.props, el.children
node = mount el, parent, index
# Finish mounting and remember component/node association
comp.didMount? el
node._COMPONENT = comp
return node
else if typeof el in ['string', 'number']
# Text
node = document.createTextNode el
else
# VDOM Node
node = document.createElement el.type
set node, key, value for key, value of el.props
children = el.children
if children?
if typeof children in ['string', 'number']
# Insert text directly
node.textContent = children
else
if children.type?
# Single child
mount children, node, 0
else
# Insert children
mount child, node, i for child, i in children
parent.insertBefore node, parent.childNodes[index]
return node
unmount = (comp, node) ->
if comp
comp.willUnmount?()
delete comp[k] for k of comp
for child in node.childNodes
unmount child._COMPONENT, child
delete child._COMPONENT
prop = (key) ->
return true if typeof document == 'undefined'
return key if document.documentElement.style[key]?
key = key[0].toUpperCase() + key.slice 1
prefixes = ['webkit', 'moz', 'ms', 'o']
return prefix + key for prefix in prefixes when document.documentElement.style[prefix + key]?
map = {}
map[key] = prop key for key in ['transform']
set = (node, key, value, orig) ->
if key == 'style'
for k, v of value when orig?[k] != v
node.style[map[k] ? k] = v
return
if node[key]?
node[key] = value
return
if node instanceof Node
node.setAttribute key, value
return
unset = (node, key, orig) ->
if key == 'style'
for k, v of orig
node.style[map[k] ? k] = ''
return
if node[key]?
node[key] = undefined
if node instanceof Node
node.removeAttribute key
return
createClass = (prototype) ->
aliases = {
willMount: 'componentWillMount'
didMount: 'componentDidMount'
willReceiveProps: 'componentWillReceiveProps'
shouldUpdate: 'shouldComponentUpdate'
willUpdate: 'componentWillUpdate'
didUpdate: 'componentDidUpdate'
willUnmount: 'componentWillUnmount'
}
prototype[a] ?= prototype[b] for a, b of aliases
class Component
constructor: (node, @props = {}, @state = null, @children = null) ->
bind = (f, self) -> if typeof f == 'function' then f.bind self else f
@[k] = bind v, @ for k, v of prototype
nextState = null
@setState = (state) ->
nextState ?= if state then nextState ? {} else null
nextState[k] = v for k, v of state
node._COMPONENT_DIRTY = true
return
@forceUpdate = () ->
node._COMPONENT_FORCE = node._COMPONENT_DIRTY = true
el = node
while el = el.parentNode
if el._COMPONENT
el._COMPONENT_FORCE = true
@getNextState = () -> nextState
@applyNextState = () ->
node._COMPONENT_FORCE = node._COMPONENT_DIRTY = false
prevState = @state
[nextState, @state] = [null, nextState]
prevState
return
Component.isComponentClass = true
Component.prototype.defaultProps = prototype.getDefaultProps?() ? {}
Component
module.exports = {element, recycle, apply, hint, Types, createClass}