MCPcopy
hub / github.com/MithrilJS/mithril.js / updateNodes

Function updateNodes

render/render.js:269–426  ·  view source on GitHub ↗
(parent, old, vnodes, hooks, nextSibling, ns)

Source from the content-addressed store, hash-verified

267 // three of the diff algo.
268
269 function updateNodes(parent, old, vnodes, hooks, nextSibling, ns) {
270 if (old === vnodes || old == null && vnodes == null) return
271 else if (old == null || old.length === 0) createNodes(parent, vnodes, 0, vnodes.length, hooks, nextSibling, ns)
272 else if (vnodes == null || vnodes.length === 0) removeNodes(old, 0, old.length)
273 else {
274 var start = 0, oldStart = 0, isOldKeyed = null, isKeyed = null
275 for (; oldStart < old.length; oldStart++) {
276 if (old[oldStart] != null) {
277 isOldKeyed = old[oldStart].key != null
278 break
279 }
280 }
281 for (; start < vnodes.length; start++) {
282 if (vnodes[start] != null) {
283 isKeyed = vnodes[start].key != null
284 break
285 }
286 }
287 if (isKeyed === null && isOldKeyed == null) return // both lists are full of nulls
288 if (isOldKeyed !== isKeyed) {
289 removeNodes(old, oldStart, old.length)
290 createNodes(parent, vnodes, start, vnodes.length, hooks, nextSibling, ns)
291 } else if (!isKeyed) {
292 // Don't index past the end of either list (causes deopts).
293 var commonLength = old.length < vnodes.length ? old.length : vnodes.length
294 // Rewind if necessary to the first non-null index on either side.
295 // We could alternatively either explicitly create or remove nodes when `start !== oldStart`
296 // but that would be optimizing for sparse lists which are more rare than dense ones.
297 start = start < oldStart ? start : oldStart
298 for (; start < commonLength; start++) {
299 o = old[start]
300 v = vnodes[start]
301 if (o === v || o == null && v == null) continue
302 else if (o == null) createNode(parent, v, hooks, ns, getNextSibling(old, start + 1, nextSibling))
303 else if (v == null) removeNode(o)
304 else updateNode(parent, o, v, hooks, getNextSibling(old, start + 1, nextSibling), ns)
305 }
306 if (old.length > commonLength) removeNodes(old, start, old.length)
307 if (vnodes.length > commonLength) createNodes(parent, vnodes, start, vnodes.length, hooks, nextSibling, ns)
308 } else {
309 // keyed diff
310 var oldEnd = old.length - 1, end = vnodes.length - 1, map, o, v, oe, ve, topSibling
311
312 // bottom-up
313 while (oldEnd >= oldStart && end >= start) {
314 oe = old[oldEnd]
315 ve = vnodes[end]
316 if (oe == null) oldEnd--
317 else if (ve == null) end--
318 else if (oe.key === ve.key) {
319 if (oe !== ve) updateNode(parent, oe, ve, hooks, nextSibling, ns)
320 if (ve.dom != null) nextSibling = ve.dom
321 oldEnd--, end--
322 } else {
323 break
324 }
325 }
326 // top-down

Callers 3

updateFragmentFunction · 0.70
updateElementFunction · 0.70
renderFunction · 0.70

Calls 10

createNodesFunction · 0.70
removeNodesFunction · 0.70
createNodeFunction · 0.70
getNextSiblingFunction · 0.70
removeNodeFunction · 0.70
updateNodeFunction · 0.70
insertNodeFunction · 0.70
toFragmentFunction · 0.70
getKeyMapFunction · 0.70
makeLisIndicesFunction · 0.70

Tested by

no test coverage detected