All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.jetbrains.datalore.base.observable.property.DerivedProperty.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * Copyright (c) 2019. JetBrains s.r.o.
 * Use of this source code is governed by the MIT license that can be found in the LICENSE file.
 */

package jetbrains.datalore.base.observable.property

import jetbrains.datalore.base.observable.collections.Collections
import jetbrains.datalore.base.observable.event.EventHandler
import jetbrains.datalore.base.observable.event.EventSource
import jetbrains.datalore.base.registration.Registration

/**
 * Simplified version of [BaseDerivedProperty] which can depend on generic observable objects.
 */
abstract class DerivedProperty(initialValue: ValueT, vararg deps: EventSource<*>) :
        BaseDerivedProperty(initialValue) {

    private val myDeps: Array> = Collections.arrayCopy(deps)
    private var myRegistrations: Array? = null

    override fun doAddListeners() {
        myRegistrations = Array(myDeps.size) { i -> register(myDeps[i]) }
    }

    private fun  register(dep: EventSource): Registration {
        return dep.addHandler(object : EventHandler {
            override fun onEvent(event: EventT) {
                somethingChanged()
            }
        })
    }

    override fun doRemoveListeners() {
        for (r in myRegistrations!!) {
            r.remove()
        }
        myRegistrations = null
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy