在let表达式中使用elm进行去Bounce
我想知道为什么这不起作用。我正在尝试从视图中消除影响,但不是用户事件。根据想法,这应该进入continiuos流,这将发生一次,但每隔几秒钟。该体系结构的主要思想是,事件可能从不同的地方触发,但只会发生一次。我制作了一个简单的示例应用程序:在let表达式中使用elm进行去Bounce,elm,debounce,Elm,Debounce,我想知道为什么这不起作用。我正在尝试从视图中消除影响,但不是用户事件。根据想法,这应该进入continiuos流,这将发生一次,但每隔几秒钟。该体系结构的主要思想是,事件可能从不同的地方触发,但只会发生一次。我制作了一个简单的示例应用程序: module Main exposing (main) import Html exposing (Html) import Html import Process import Task import Debug import Time import
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)
模块主显示(主)
导入Html(Html)
导入Html
导入过程
导入任务
导入调试
导入时间
导入控件(控件)
进口管制。反盎司为反盎司
main:程序从不使用模型Msg
主要=
Html.program
{init=init
,视图=视图
,update=update
,订阅=订阅
}
类型别名模型=
{计数器:Int
,state:Control.state-Msg
}
初始化:(模型,Cmd Msg)
初始化=
{counter=0,state=Control.initialState}
! [延迟(时间秒*3)子消息
订阅模式=
无
输入味精
=Deb(控制味精)
|连续去抖动
更新:Msg->Model->(Model,Cmd-Msg)
更新msg模型=
味精案例
Deb debMsg->
Control.update(\s->{model | state=s})model.state debMsg
持续去抖动->
让
x=Debug.log“持续去抖动”
_=去抖动连续去抖动
在里面
({model | counter=model.counter+1},Cmd.none)
去盎司:味精->味精
去盎司=
让
x=Debug.log“去盎司”
在里面
去盎司.尾随去盎司(3*次.秒)
延迟:Time.Time->msg->Cmd msg
延迟时间=
睡眠时间
|>Task.and然后(始终为Task.perform identity
查看:模型->Html消息
视图模型=
Html.text(toString model.counter)
在您的示例应用程序中,您在
init
函数中只启动了continuousdebouning
消息一次,因此正如预期的那样,计数器只会增加一次。您可能希望在更新函数中再次启动continuousdebouning
我认为这实现了你的目标:
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)
模块主显示(主)
导入Html(Html)
导入Html
导入过程
导入任务
导入调试
导入时间
导入控件(控件)
进口管制。反盎司为反盎司
main:程序从不使用模型Msg
主要=
Html.program
{init=init
,视图=视图
,update=update
,订阅=订阅
}
类型别名模型=
{计数器:Int
,state:Control.state-Msg
}
初始化:(模型,Cmd Msg)
初始化=
{counter=0,state=Control.initialState}
![递增计数器]
递增计数器:Cmd Msg
incrementCounter=Cmd.map去盎司模型->(模型,Cmd Msg)
更新msg模型=
味精案例
Deb debMsg->
Control.update(\s->{model | state=s})model.state debMsg
持续去抖动->
让
x=Debug.log“持续去抖动”
在里面
({model | counter=model.counter+1},递增计数器)
去盎司:味精->味精
去盎司=
让
x=Debug.log“去盎司”
在里面
去盎司.尾随去盎司(3*次.秒)
延迟:Time.Time->msg->Cmd msg
延迟时间=
睡眠时间
|>Task.and然后(始终为Task.perform identity
查看:模型->Html消息
视图模型=
Html.text(toString model.counter)
@Tomas answer符合您的要求。由于其在“let”语句中的声明,您似乎预期会发生去Bounce。Debug.log是Elm中唯一生成副作用的函数。其他影响需要在
update
函数中“请求”(因为它将模型和cmd msgs返回到运行时)。