dwayne / elm-debouncer / 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

init : Debouncer a

Create a debouncer.

Configurations


type Config a msg

The configuration for a debouncer.


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

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

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 : Basics.Bool
, invokeOnTrailing : Basics.Bool
, wait : Basics.Int
, maxWait : Maybe Basics.Int
, onReady : a -> msg
, onChange : Msg -> msg 
}

The configuration options used by the custom configuration constructor.

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, Platform.Cmd.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.

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, Platform.Cmd.Cmd msg )

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

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

cancel : Debouncer a -> Debouncer a

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