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

commonMain.org.brightify.hyperdrive.property.impl.GetterSetterBoundProperty.kt Maven / Gradle / Ivy

There is a newer version: 0.1.159
Show newest version
package org.brightify.hyperdrive.property.impl

import org.brightify.hyperdrive.CancellationToken
import org.brightify.hyperdrive.property.MutableObservableProperty
import org.brightify.hyperdrive.property.ObservableProperty
import org.brightify.hyperdrive.property.defaultEqualityPolicy

internal class GetterSetterBoundProperty(
    private val equalityPolicy: ObservableProperty.EqualityPolicy = defaultEqualityPolicy(),
    private val getter: () -> T,
    private val setter: (T) -> Unit,
): MutableObservableProperty {
    override var value: T = getter()
        set(newValue) {
            if (equalityPolicy.isEqual(value, newValue)) { return }
            listeners.runNotifyingListeners(newValue) {
                field = value
                setter(value)
            }
        }

    private val listeners = ValueChangeListenerHandler(this)

    override fun addListener(listener: ObservableProperty.Listener): CancellationToken = listeners.addListener(listener)

    override fun removeListener(listener: ObservableProperty.Listener): Unit = listeners.removeListener(listener)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy