Coder Social home page Coder Social logo

elm-limiter's Introduction

elm-limiter

Throttling and debouncing for messages and values.

A Complete Example

module Main exposing (main)

import Browser
import Html
import Html.Attributes
import Html.Events
import Limiter exposing (Limiter)


{- Main --------------------------------------------------------------------- -}
main : Program () Model Msg
main =
    Browser.element
        { init = init
        , update = update
        , view = view
        , subscriptions = always Sub.none
        }


{- Model -------------------------------------------------------------------- -}
type alias Model =
    { counter : Int
    , rawInput : String
    , debouncer : Limiter Msg
    , throttler : Limiter Msg
    }

init : () -> ( Model, Cmd Msg )
init _ =
    ( { counter = 0
      , rawInput = ""
      , debouncer = Limiter.debounce DebounceMsg 500
      , throttler = Limiter.throttle ThrottleMsg 100
      }
    , Cmd.none
    )


{- Update ------------------------------------------------------------------- -}
type Msg
    = Increment
    | GotInput String
    | SearchFor String
    | DebounceMsg (Limiter.Msg Msg)
    | ThrottleMsg (Limiter.Msg Msg)

update : Msg -> Model -> ( Model, Cmd Msg )
update msg modl =
    case msg of
        Increment ->
            ( { model | counter = model.counter + 1 }
            , Cmd.none
            )

        GotInput input ->
            Limiter.push (SearchFor input) model.throttler
                |> Tuple.mapFirst
                    (\throttler ->
                        { model
                            | throttler = throttler
                            , rawInput = input
                        }
                    )

        SearchFor input ->
            ( model
              -- Imagine we wanted to search for something on the backend. We'd
              -- be sending unnecessary requests if we search every keypress!
            , searchBackendFor input
            )

        DebounceMsg debounceMsg ->
            Limiter.update debounceMsg model.debouncer
                |> Tuple.mapFirst (\debouncer -> { model | debouncer = debouncer })

        ThrottlerMsg throttleMsg ->
            Limiter.update throttleMsg model.throtther
                |> Tuple.mapFirst (\throttler -> { model | throttler = throttler })


{- View --------------------------------------------------------------------- -}
view : Model -> Html Msg
view model =
    Html.div []
        [ Html.div []
            [ Html.button
                [ Html.Events.onClick (Limiter.event Increment model.debouncer) ]
                [ Html.text "+" ]
            , Html.p []
                [ Html.text <| String.fromInt model.counter ]
            ]
        , Html.div []
            [ Html.span []
                [ Html.text "Search for: " ]
            , Html.input
                [ Html.Events.onInput GotInput
                , Html.Attributes.value model.rawInpt
                ] []
            ]
        ]

elm-limiter's People

Contributors

brzezinskip avatar hayleigh-dot-dev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.