debounce v0.1.1 Debounce

A process-based debouncer for Elixir.

What is a debouncer?

A debouncer is responsible for calling a function with a delay, but if that function is called multiple times within the delay period, the time is reset and delay is counted again. In other words, the function will be called after a delay period has elapsed from the last application.

Each time, the debounced function is called, a new task is started.

Example

iex> {:ok, pid} = Debounce.start_link({Kernel, :send, [self(), "Hello"]}, 100)
iex> Debounce.apply(pid)  # Schedules call in 100 ms
iex> :timer.sleep(50)
iex> Debounce.apply(pid)  # Resets timer back to 100 ms
iex> :timer.sleep(100)
iex> receive do msg -> msg end
"Hello"                   # Timer elapsed
iex> Debounce.apply(pid)  # Schedules call in 100 ms
iex> Debounce.cancel(pid) # Cancels scheduled call
:ok

Summary

Functions

Schedules call to the current debouncer’s function

Cancels any scheduled call to the current debouncer’s function

Changes the function the debouncer is applying

Changes the delay the debouncer operates with

Immediately invokes the current debouncer’s function

Starts a Debounce process without links (outside of a supervision tree)

Starts a Debounce process linked to the current process

Synchronously stops the debouncer with the given reason

Types

apply()
apply() :: (() -> term) | mfargs
debouncer()
debouncer() :: :gen_statem.server_ref
mfargs()
mfargs() :: {module, atom, [term]}
option()
option() :: {:name, GenServer.name} | :gen_statem.start_opt
time()
time() :: non_neg_integer

Functions

apply(debouncer, args \\ [])
apply(debouncer, [term]) :: :ok

Schedules call to the current debouncer’s function.

If the function is a fun, calls it with provided args. If the function is an mfargs/0 tuple, appends provided args to the original ones.

If this function is called again withing the current debouncer’s timeout value, the time will reset.

cancel(debouncer)
cancel(debouncer) :: :ok

Cancels any scheduled call to the current debouncer’s function.

change_function(debouncer, new_function)
change_function(debouncer, apply) :: :ok

Changes the function the debouncer is applying.

Affects only future calls to apply/2.

change_timeout(debouncer, new_timeout)
change_timeout(debouncer, time) :: :ok

Changes the delay the debouncer operates with.

Affects only future calls to apply/2.

flush(debouncer, args)
flush(debouncer, [term]) :: :ok

Immediately invokes the current debouncer’s function.

If the function is a fun, calls it with provided args. If the function is an mfargs/0 tuple, appends provided args to the original ones.

start(apply, timeout, opts \\ [])
start(apply, time, [option]) :: :gen_statem.start_ret

Starts a Debounce process without links (outside of a supervision tree).

See start_link/3 for more information.

start_link(apply, timeout, opts \\ [])
start_link(apply, time, [option]) :: :gen_statem.start_ret

Starts a Debounce process linked to the current process.

This can be used to start the Debounce as part of a supervision tree.

Delays invoking apply until after timeout millisecnds have elapsed since the last time the apply/2 function was called.

Options

  • :name- used for name registration, like in GenServer.start_link/3.
  • all other options supported by :gen_statem.start_link/4
stop(debouncer, reason \\ :normal, timeout \\ :infinity)
stop(debouncer, reason :: term, timeout) :: :ok

Synchronously stops the debouncer with the given reason.