Skip to content

How to debounce message handling? #1061

Closed Answered by TimLariviere
h0lg asked this question in Q&A
Discussion options

You must be logged in to vote

Unlike in Fabulous 1 where we could debounce several events and only dispatch the message one time, Fabulous 2 doesn't expose the dispatch function anymore for convenience and security, so the same trick is not possible.

Instead, you would need to handle the debouncing yourself using 2 messages: one for reacting to every events and one for the actual change. Like so:

let debounce (timeout: int) (fn: unit -> 'msg) =
    let mutable cts: CancellationTokenSource = null
    fun () ->
        Cmd.ofSub (fun dispatch ->
            if cts <> null then
                cts.Cancel()
                cts.Dispose()
  
            cts <- new CancellationTokenSource()
  
            Device.StartTimer(T…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@TimLariviere
Comment options

@TimLariviere
Comment options

Answer selected by h0lg
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants