``` fun <T, R> ObservableValue<T>.debo...
# tornadofx
o
Copy code
fun <T, R> ObservableValue<T>.debounce(duration: Duration, evaluate: (T?) -> R?): ObservableValue<R> {
    val pause = PauseTransition(duration)
    val property = SimpleObjectProperty<R>()
    property.set(evaluate(value))
    onChange { inputValue ->
        pause.setOnFinished { property.set(evaluate(inputValue)) }
        pause.playFromStart()
    }
    return property
}
r
You could probably pull the
setOnFinished
call out of the
onChange
call so you're not changing the property every time. Something like
Copy code
pause.setOnFinished { property.set(evaluate(value)) }
onChange { pause.playFromStart() }
o
But it should be using latest
inputValue
? Not sure I understand
a
The latest value of the Observable can be fetched by calling
getValue()
(in Kotlin
value
).
1
r
@orangy There is a subtle difference between your code and mine, but it shouldn't show in practice. In yours, when the user changes the input, the new value is passed into the function and will be set on timeout. In mine, the new value is not passed anywhere, and the value of the property is queried on timeout (which means it may be better to use an invalidation listener in my case instead of a change listener).
o
Right, but I plan to run
evaluate
in a background thread eventually 😉
r
In that case, yours is the way to go 🙂