commonMain.org.brightify.hyperdrive.property.impl.GetterSetterBoundProperty.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.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)
}