let 表达式中使用 elm 进行反跳

问题描述 投票:0回答:1

我很想理解为什么这不起作用。我正在尝试消除抖动,但不是从视图中消除用户事件。按照想法,这应该进入连续流,该流将发生一次,但每隔几秒发生一次。这种架构的主要思想是事件可能从不同的地方触发,但只会发生一次。我制作了一个简单的示例应用程序:

module Main exposing (main)

import Html exposing (Html)


import Html
import Process
import Task
import Debug
import Time
import Control exposing (Control)
import Control.Debounce as Debounce


main : Program Never Model Msg
main =
    Html.program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }


type alias Model =
    { counter : Int
    , state : Control.State Msg
    }


init : ( Model, Cmd Msg )
init =
  { counter = 0, state = Control.initialState }
  ! [ delay (Time.second * 3) <| ContinuousDebouncing ]


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.none


type Msg
    = Deb (Control Msg)
    | ContinuousDebouncing


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of

        Deb debMsg ->
            Control.update (\s -> { model | state = s }) model.state debMsg

        ContinuousDebouncing ->
            let
                x = Debug.log "ContinuousDebouncing"
                _ = debounce ContinuousDebouncing
            in
                ( { model | counter = model.counter + 1 }, Cmd.none )


debounce : Msg -> Msg
debounce =
    let
        x = Debug.log "debounce"
    in
        Debounce.trailing Deb (3 * Time.second)


delay : Time.Time -> msg -> Cmd msg
delay time msg =
  Process.sleep time
  |> Task.andThen (always <| Task.succeed msg)
  |> Task.perform identity


view : Model -> Html Msg
view model =
    Html.text (toString model.counter)

https://ellie-app.com/tvQ3L6dGrqa1

elm debouncing
1个回答
2
投票

我正在行使其 GDPR 权利来删除我的贡献

© www.soinside.com 2019 - 2024. All rights reserved.