Debounce (debounce v1.0.0) View Source

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

Link to this section 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.

Link to this section Types

Specs

apply() :: (() -> term()) | mfargs()

Specs

debouncer() :: :gen_statem.server_ref()

Specs

mfargs() :: {module(), atom(), [term()]}

Specs

option() :: {:name, GenServer.name()} | :gen_statem.start_opt()

Specs

time() :: non_neg_integer()

Link to this section Functions

Link to this function

apply(debouncer, args \\ [])

View Source

Specs

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.

Specs

cancel(debouncer()) :: :ok

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

Link to this function

change_function(debouncer, new_function)

View Source

Specs

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

Changes the function the debouncer is applying.

Affects only future calls to apply/2.

Link to this function

change_timeout(debouncer, new_timeout)

View Source

Specs

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

Changes the delay the debouncer operates with.

Affects only future calls to apply/2.

Specs

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.

Link to this function

start(apply, timeout, opts \\ [])

View Source

Specs

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.

Link to this function

start_link(apply, timeout, opts \\ [])

View Source

Specs

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

Link to this function

stop(debouncer, reason \\ :normal, timeout \\ :infinity)

View Source

Specs

stop(debouncer(), reason :: term(), timeout()) :: :ok

Synchronously stops the debouncer with the given reason.