commonMain.org.brightify.hyperdrive.property.impl.DeferredToImmediateObservablePropertyWrapper.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.DeferredObservableProperty
import org.brightify.hyperdrive.property.ObservableProperty
import org.brightify.hyperdrive.utils.Optional
internal class DeferredToImmediateObservablePropertyWrapper(
private val initialValue: T,
private val wrapped: DeferredObservableProperty,
): ObservableProperty, DeferredObservableProperty.Listener {
private val listeners = ValueChangeListenerHandler(this)
override var value: T = initialValue
private set
init {
wrapped.addListener(this)
}
override fun valueDidChange(oldValue: Optional, newValue: T) {
listeners.runNotifyingListeners(newValue) {
value = it
}
}
override fun addListener(listener: ObservableProperty.Listener): CancellationToken = listeners.addListener(listener)
override fun removeListener(listener: ObservableProperty.Listener) = listeners.removeListener(listener)
}