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

commonMain.org.brightify.hyperdrive.property.impl.ObservableObjectObservableProperty.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.ObservableObject
import org.brightify.hyperdrive.property.MutableObservableProperty
import org.brightify.hyperdrive.property.ObservableProperty

internal class ObservableObjectObservableProperty(
    initialValue: T,
    private val equalityPolicy: ObservableProperty.EqualityPolicy,
): MutableObservableProperty, ObservableObject.ChangeTracking.Listener {
    override var value: T = initialValue
        set(newValue) {
            if (equalityPolicy.isEqual(field, newValue)) { return }
            field.changeTracking.removeListener(this)
            listeners.runNotifyingListeners(newValue) {
                field = it
            }
            field.changeTracking.addListener(this)
        }

    init {
        initialValue.changeTracking.addListener(this)
    }

    override fun onObjectWillChange() {
        listeners.notifyValueWillChange(value, value)
    }

    override fun onObjectDidChange() {
        listeners.notifyValueDidChange(value, value)
    }

    private val listeners = ValueChangeListenerHandler(this)

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy