gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:58:39 24 4
gpt4 key购买 nike

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

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

最佳答案

在您的示例应用中,您仅在 init 函数中触发了一次 ContinouslyDebouncing 消息,因此正如预期的那样,计数器仅递增一次。您可能想在更新函数中再次触发 ContinouslyDebouncing

我认为这实现了您所追求的目标:

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 }
! [ incrementCounter ]

incrementCounter : Cmd Msg
incrementCounter = Cmd.map debounce <| 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"
in
( { model | counter = model.counter + 1 }, incrementCounter )


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/tPymgfNwYda1

关于elm - 在 let 表达式中使用 elm 进行反跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50806235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com