Debounce notfication of an Observable

Is there a simple way to debounce a function call like in js’ Lodash?

I want to notfiy an Observable at most once every 0.25s.

This is useful for performance reasons in Makie.

obs = Observable(1)
notify(obs, debounce=0.25)

According to my quick search, you might be looking for async_latest. The docs link is:
async_latest doc

You would write:

obs = Observable(1)
async_latest(obs)

The example in the docs is specifically relevant to updating graphics.

Does not seem to do the trick in my case. This looks like it requires a debounce.

This is my attempt at a debounce function but the function has something wrong with it:

function debounce(func::Function, wait::Float64)
    timer = nothing
    function debounced_func()
        if timer === nothing
            timer = Timer(wait) do
                (func)()
                timer = nothing
            end
        end
    end
    return debounced_func
end

debounced_notify = debounce(0.50) do
      notify(...)
end

debounced_notify()

Maybe it would work if I could use async_latest on this:

    onmouseleftdrag(mouseevents) do event
        notify(...)
    end

See Observables.throttle .

1 Like