dwayne/elm-debouncer/1.0.0

Debouncer

Debounce or throttle your actions.

Debouncer

type Debouncer a

The type variable a represents the type of the argument the action depends upon.

For e.g. if you're debouncing a search field, your ability to perform the search will most likely depend upon a query string. So, in this case, a will be String.

Constructors

Create a debouncer.

Configurations

type Config a msg

The configuration for a debouncer.

type alias SimpleConfigOptions a msg =
{ wait : Int
, onReady : a -> msg
, onChange : Msg -> msg
}

The configuration options used by the trailing, leading, and throttle configuration constructors.

  • wait: A non-negative (>= 0) integer that represents the number of milliseconds to delay. Its exact meaning depends on the configuration constructor you've used.
  • onReady: The message constructor to use to create the message that will be sent to your update function when it's time to perform the action you've been delaying.
  • onChange: Convert Msg to your message type. Basically, say how to Cmd.map the commands that will be returned from call and update.
trailing : SimpleConfigOptions a msg -> Config a msg

Create the configuration for a trailing edge debouncer.

Suppose wait = 400ms, then you can expect the following behaviour:

--aaa--a-a--a-----b---b-bb--b-------c-------
-----------------a---------------b-------c--

Note: Each - represents 100ms.

Play with the trailing edge demo to better understand how this configuration really works.

leading : SimpleConfigOptions a msg -> Config a msg

Create the configuration for a leading edge debouncer.

Suppose wait = 400ms, then you can expect the following behaviour:

--aaa--a-a--a-----b---b-bb--b-------c-------
--a---------------b-----------------c-------

Note: Each - represents 100ms.

Play with the leading edge demo to better understand how this configuration really works.

throttle : SimpleConfigOptions a msg -> Config a msg

Create the configuration for a throttler.

Suppose wait = 400ms, then you can expect the following behaviour:

--aaa--a-a--a-----b---b-bb--b-------c-------
--a---a----a----a-b---b----b----b---c-------

Note: Each - represents 100ms.

Play with the throttle demo to better understand how this configuration really works.

type alias CustomConfigOptions a msg =
{ invokeOnLeading : Bool
, invokeOnTrailing : Bool
, wait : Int
, maxWait : Maybe Int
, onReady : a -> msg
, onChange : Msg -> msg
}

The configuration options used by the custom configuration constructor.

  • invokeOnLeading: Whether or not to send the onReady constructed message immediately upon calling call.
  • invokeOnTrailing: Whether or not to send the onReady constructed message wait milliseconds after you last called call.
  • maxWait: If it's Just n then n is a non-negative integer (>= wait) that represents the maximum number of milliseconds to delay. Once that time is up the onReady constructed message is sent.

Note 1: wait, onReady, and onChange are the same as in SimpleConfigOptions.

Note 2: The custom configuration options were heavily influenced by Lodash's debounce function.

custom : CustomConfigOptions a msg -> Config a msg

Create a fully customized configuration.

Operations

call : Config a msg -> a -> Debouncer a -> ( Debouncer a, Cmd msg )

Call this function instead of performing the action which you want to delay. It will update the debouncer and determine when to send the onReady arg message.

  • onReady: The message constructor you set when creating your configuration.
  • arg: The argument, of type a, you passed to call.

You handle the onReady arg message by performing the action which you've been delaying.

type Msg

The messages that are handled by the update function.

update : Config a msg -> Msg -> Debouncer a -> ( Debouncer a, Cmd msg )

Update the debouncer and determine when to send the onReady lastArg message.

  • onReady: The message constructor you set when creating your configuration.
  • lastArg: The argument, of type a, that you passed the last time you invoked call.

You handle the onReady lastArg message by performing the action which you've been delaying.

It will reset the debouncer and you will not receive any previously scheduled onReady constructed messages.