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

Version Path
trackler-2.2.1.180 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.179 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.178 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.177 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.176 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.175 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.174 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.173 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.172 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.171 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.170 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.169 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.167 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.166 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.165 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.164 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.163 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.162 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.161 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.160 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt