Hello friends, is it possible to implement a debou...
# tornadofx
m
Hello friends, is it possible to implement a debounce action on the button click?
s
Could be done easily with coroutines using an actor.
m
Your question is stated imprecisely. If you want to implement debounce on the action event of a button, you could transform the events into a Kotlin Flow, with something like this:
Copy code
fun Button.actions(): Flow<Unit> = callbackFlow {
    val handler =  EventHandler<ActionEvent> {
        offer(Unit)
    }
    addEventHandler(ActionEvent.ACTION, handler)

    awaitClose { removeEventHandler(ActionEvent.ACTION, handler) }
}
and then use the
debounce()
method: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/debounce.html
Obviously, there are many other ways. This is just a modern idiomatic way to implement debounce with Kotlin.
I didn't test the code.
m
Copy code
I thought there was something ready from tornadoofx
I just want to prevent the user from being able to give several button clicks
m
It's another thing from implementing debouncing. Anyway, you can disable the any control when you don't want people to interact with it.