Scoop API

scoop:scoop.ui:0.1.0

src/state/observation.scoop

Selected production source · src/state/observation.scoop

1package scoop.ui
3import scoop.surface.AnimationReadObserver
4import scoop.collections.HashMap
6private val stateSequence = AtomicLong(0L)
8internal struct StateSignalId(val value: Long): Equals<StateSignalId>, Hash {
9 public override operator fun equals(other: StateSignalId): Boolean = value == other.value
10 public override fun hash(): Long = value.hash()
13/** The same signal invalidates a window node or an application root. */
14internal interface StateInvalidationTarget {
15 fun stateChanged(invalidation: UiInvalidation)
18internal class StateSignal {
19 val id = StateSignalId(stateSequence.fetchAdd(1L, MemoryOrder.Relaxed))
20 private val observers = ArrayList<StateSubscription>()
22 fun subscribe(node: StateInvalidationTarget): StateSubscription {
23 val subscription = StateSubscription(Some(this), Some(node), observers.size)
24 observers.add(subscription)
25 return subscription
26 }
27 fun remove(subscription: StateSubscription) {
28 val index = subscription.index
29 val last = observers.size - 1L
30 if (index != last) {
31 val moved = observers[last]
32 observers[index] = moved
33 moved.index = index
34 }
35 observers.removeAt(last)
36 }
37 fun changed(invalidation: UiInvalidation = UiInvalidation.None) {
38 for (observer in observers.toArray()) {
39 when (observer.node) {
40 case Some(val node) -> {
41 node.stateChanged(invalidation)
42 }
43 case None -> {}
44 }
45 }
46 }
49/** Closing a binding removes it without searching its source's other observers. */
50internal class StateSubscription(
51 private var source: StateSignal?,
52 var node: StateInvalidationTarget?,
53 var index: Long
54): Closeable {
55 public override fun close() {
56 val owner = source ?: return
57 owner.remove(this)
58 source = None
59 node = None
60 index = -1L
61 }
64internal class DependencyCollector {
65 val sources = HashMap<StateSignalId, StateSignal>()
66 fun read(source: StateSignal) {
67 sources[source.id] = source
68 }
71internal fun replaceStateDependencies(
72 target: StateInvalidationTarget,
73 dependencies: HashMap<StateSignalId, StateSubscription>,
74 collector: DependencyCollector
75) {
76 val next = collector.sources
77 val removed = ArrayList<StateSignalId>()
78 for (entry in dependencies) {
79 if (!next.containsKey(entry.key)) {
80 entry.value.close()
81 removed.add(entry.key)
82 }
83 }
84 for (id in removed)
85 dependencies.remove(id)
86 for (entry in next)
87 if (!dependencies.containsKey(entry.key))
88 dependencies[entry.key] = entry.value.subscribe(target)
91internal class UiEvaluation(
92 val collector: DependencyCollector? = None,
93 val preparing: Boolean = false,
94 val frameReads: FrameReads? = None,
95 val measurement: MountedNode? = None
98internal fun currentEvaluation(): UiEvaluation? {
99 val current = contextOrNone<UiEvaluation>() ?: return None
100 return if (current.preparing)
101 None
102 else
103 Some(current)
106internal fun <T> evaluateUi(
107 collector: DependencyCollector?,
108 frameReads: FrameReads? = None,
109 measurement: MountedNode? = None,
110 block: () -> T
111): T {
112 val evaluation = UiEvaluation(collector, frameReads = frameReads, measurement = measurement)
113 val observer: AnimationReadObserver = frameReads ?: IgnoreAnimationReads
114 context(evaluation) {
115 context(observer) {
116 return block()
117 }
118 }
121/** Item preparation is a lifecycle boundary, even when requested by a layout callback. */
122internal fun <T> prepareUi(block: () -> T): T {
123 val evaluation = UiEvaluation(preparing = true)
124 val observer: AnimationReadObserver = IgnoreAnimationReads
125 context(evaluation) {
126 context(observer) {
127 return block()
128 }
129 }