Scoop API

scoop:scoop.ui:0.1.0

src/runtime/reconcile.scoop

Selected production source · src/runtime/reconcile.scoop

1package scoop.ui
3internal fun MountedNode.reconcile(descriptions: Array<View>, turn: UpdateTurn) {
4 val previous = children
5 val plan =
6 matchSiblings(
7 previous,
8 descriptions,
9 { it.view.key },
10 { it.key },
11 { child, description -> child.active && child.view.sameType(description) }
12 )
13 val used = plan.used
14 // Remove every stale interaction before running any unmount callback.
15 var index = 0L
16 for (child in previous) {
17 if (!used[index])
18 child.deactivate()
19 index += 1L
20 }
21 var failure: Throwable? = None
22 index = 0L
23 for (child in previous) {
24 if (!used[index])
25 failure = cleanupUi(failure) { child.unmount() }
26 index += 1L
27 }
28 throwUi(failure)
29 if (!root.isOpen)
30 return
32 val next = ArrayList<MountedNode>()
33 val created = ArrayList<MountedNode>()
34 try {
35 var position = 0L
36 for (match in plan.positions) {
37 val child =
38 when (match) {
39 case Some(val value) -> previous[value]
40 case None -> {
41 val value = createNode(root, Some(this), descriptions[position])
42 created.add(value)
43 value
44 }
45 }
46 next.add(child)
47 position += 1L
48 }
49 } catch (error: Throwable) {
50 var cleanup: Throwable? = Some(error)
51 for (child in created)
52 cleanup = cleanupUi(cleanup) { child.unmount() }
53 throwUi(cleanup)
54 }
55 // All surviving and newly allocated nodes remain reachable if a callback fails.
56 children = next.toArray()
57 var changed = previous.size != children.size
58 if (!changed)
59 for (position in 0L ..< children.size)
60 if (previous[position] !== children[position])
61 changed = true
62 if (changed) {
63 root.semantics?.structureChanged()
64 request(UiInvalidation.Layout)
65 }
66 index = 0L
67 for (child in children) {
68 child.replace(descriptions[index])
69 child.rebuild(turn)
70 index += 1L
71 }