scoop:scoop.ui:0.1.0
src/state/observation.scoop
Selected production source · src/state/observation.scoop
1
package scoop.ui3
import scoop.surface.AnimationReadObserver4
import scoop.collections.HashMap6
private val stateSequence = AtomicLong(0L)8
internal struct StateSignalId(val value: Long): Equals<StateSignalId>, Hash {9
public override operator fun equals(other: StateSignalId): Boolean = value == other.value10
public override fun hash(): Long = value.hash()11
}13
/** The same signal invalidates a window node or an application root. */14
internal interface StateInvalidationTarget {15
fun stateChanged(invalidation: UiInvalidation)16
}18
internal 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 subscription26
}27
fun remove(subscription: StateSubscription) {28
val index = subscription.index29
val last = observers.size - 1L30
if (index != last) {31
val moved = observers[last]32
observers[index] = moved33
moved.index = index34
}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
}47
}49
/** Closing a binding removes it without searching its source's other observers. */50
internal class StateSubscription(51
private var source: StateSignal?,52
var node: StateInvalidationTarget?,53
var index: Long54
): Closeable {55
public override fun close() {56
val owner = source ?: return57
owner.remove(this)58
source = None59
node = None60
index = -1L61
}62
}64
internal class DependencyCollector {65
val sources = HashMap<StateSignalId, StateSignal>()66
fun read(source: StateSignal) {67
sources[source.id] = source68
}69
}71
internal fun replaceStateDependencies(72
target: StateInvalidationTarget,73
dependencies: HashMap<StateSignalId, StateSubscription>,74
collector: DependencyCollector75
) {76
val next = collector.sources77
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)89
}91
internal class UiEvaluation(92
val collector: DependencyCollector? = None,93
val preparing: Boolean = false,94
val frameReads: FrameReads? = None,95
val measurement: MountedNode? = None96
)98
internal fun currentEvaluation(): UiEvaluation? {99
val current = contextOrNone<UiEvaluation>() ?: return None100
return if (current.preparing)101
None102
else103
Some(current)104
}106
internal fun <T> evaluateUi(107
collector: DependencyCollector?,108
frameReads: FrameReads? = None,109
measurement: MountedNode? = None,110
block: () -> T111
): T {112
val evaluation = UiEvaluation(collector, frameReads = frameReads, measurement = measurement)113
val observer: AnimationReadObserver = frameReads ?: IgnoreAnimationReads114
context(evaluation) {115
context(observer) {116
return block()117
}118
}119
}121
/** Item preparation is a lifecycle boundary, even when requested by a layout callback. */122
internal fun <T> prepareUi(block: () -> T): T {123
val evaluation = UiEvaluation(preparing = true)124
val observer: AnimationReadObserver = IgnoreAnimationReads125
context(evaluation) {126
context(observer) {127
return block()128
}129
}130
}