commonMain.org.brightify.hyperdrive.property.impl.ObservableObjectObservableProperty.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of runtime Show documentation
Show all versions of runtime Show documentation
Hyperdrive implementation that's needed for observations and such
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)
}