ru.hnau.jutils.producer.AlwaysProducer.kt Maven / Gradle / Ivy
package ru.hnau.jutils.producer
abstract class AlwaysProducer : Producer() {
companion object {
fun create(getter: () -> T): Producer =
object : AlwaysProducer() {
override val value: T
get() = getter.invoke()
}
fun create(value: T): Producer =
object : AlwaysProducer() {
override val value = value
}
}
protected abstract val value: T
protected fun onValueChanged() =
call(value)
override fun onAttached(listener: (T) -> Unit) {
super.onAttached(listener)
listener.invoke(value)
}
}