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
Specs
debouncer() :: :gen_statem.server_ref()
Specs
Specs
option() :: {:name, GenServer.name()} | :gen_statem.start_opt()
Specs
time() :: non_neg_integer()
Link to this section Functions
Specs
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.
Specs
Changes the function the debouncer is applying.
Affects only future calls to apply/2.
Specs
Changes the delay the debouncer operates with.
Affects only future calls to apply/2.
Specs
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.
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.
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
:name- used for name registration, like inGenServer.start_link/3.- all other options supported by
:gen_statem.start_link/4
Specs
Synchronously stops the debouncer with the given reason.