Sha256: 98346b0d12edbe5e7663598226c6409bd755994b64ff6bc85d6b6a12eeda133d
Contents?: true
Size: 1.84 KB
Versions: 305
Compression:
Stored size: 1.84 KB
Contents
class Reactor<T>() { abstract inner class Cell { abstract val value: T internal val dependents = mutableListOf<ComputeCell>() } interface Subscription { fun cancel() } inner class InputCell(initialValue: T) : Cell() { override var value: T = initialValue set(newValue) { field = newValue dependents.forEach { it.propagate() } dependents.forEach { it.fireCallbacks() } } } inner class ComputeCell private constructor(val newValue: () -> T) : Cell() { override var value: T = newValue() private set private var lastCallbackValue = value private var callbacksIssued = 0 private val activeCallbacks = mutableMapOf<Int, (T) -> Any>() constructor(vararg cells: Cell, f: (List<T>) -> T) : this({ f(cells.map { it.value }) }) { for (cell in cells) { cell.dependents.add(this) } } fun addCallback(f: (T) -> Any): Subscription { val id = callbacksIssued callbacksIssued++ activeCallbacks[id] = f return object : Subscription { override fun cancel() { activeCallbacks.remove(id) } } } internal fun propagate() { val nv = newValue() if (nv == value) { return } value = nv dependents.forEach { it.propagate() } } internal fun fireCallbacks() { if (value == lastCallbackValue) { return } lastCallbackValue = value for (cb in activeCallbacks.values) { cb(value) } dependents.forEach { it.fireCallbacks() } } } }
Version data entries
305 entries across 305 versions & 1 rubygems