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.139 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.138 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.137 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.136 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.135 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.134 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.133 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.132 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.131 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.130 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.129 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.128 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.127 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.126 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.125 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.124 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.123 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.122 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.121 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt
trackler-2.2.1.120 tracks/kotlin/exercises/react/.meta/src/reference/kotlin/React.kt