jinjor/elm-debounce/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Debounce

The Debouncer. See the full example here.

  • This module works with the Elm Architecture.
  • You can choose the strategy and define how commands are sent.

Types

type Debounce a msg

The state of the debouncer.

It is parameterized with the value type a and the user's message tyle msg.

type Msg

The messages that are used internally.

Initialize

type alias Config msg =
{ strategy : Strategy
, transform : Msg -> msg
}

Config contains the debouncing strategy and the message transformation.

init : Config msg -> Debounce a msg

Initialize the debouncer. Call this from your init function.

Strategies

Strategy defines the timing when commands are sent.

soon : Time -> Strategy

Send command as soon as possible, with given rate limit. (a.k.a. Throttle)

Note: The first command will be sent immidiately.

soonAfter : Time -> Time -> Strategy

Similar to soon, but send first command after offset time.

later : Time -> Strategy

Send command after becomming stable, with given rate limit. (a.k.a. Debounce)

Sending Commands

type alias Send a msg =
a -> List a -> ( List a, Cmd msg )

This function consumes values and send a command.

If you want to postpone sending, return the values back to keep them.

takeLast : (a -> Cmd msg) -> Send a msg

Send a command using the latest value.

takeAll : (a -> List a -> Cmd msg) -> Send a msg

Send a command using all the accumulated values.

Update

update : Send a msg -> Msg -> Debounce a msg -> ( Debounce a msg, Cmd msg )

This is the component's update function following the Elm Architecture.

e.g. Saving the last value.

(debounce, cmd) =
  Debounce.update
    (Debounce.takeLast save) -- save : value -> Cmd Msg
    msg
    model.debounce

The sending logic can depend on the current model. If you want to stop sending, return Cmd.none.

push : a -> Debounce a msg -> ( Debounce a msg, Cmd msg )

Push a value into the debouncer.