我有一种情况,我有两种方法可以在Elm应用程序中播放音符,并且我会跟踪当前正在播放的音符.目前的代码在这里:
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
PlayNote note ->
let
updatedCurrentPlaying =
note :: model.currentPlaying
in
( { model | currentPlaying = updatedCurrentPlaying }, Cmd.none )
KeyDown keyCode ->
let
note =
List.filter (\x -> x.keyCode == keyCode) model.notes
|> List.head
updatedCurrentPlaying =
case note of
Nothing ->
model.currentPlaying
Just a ->
a :: model.currentPlaying
in
( { model | currentPlaying = updatedCurrentPlaying }, Cmd.none )
Run Code Online (Sandbox Code Playgroud)
我想知道的是,无论如何都要干这一点,并导致KeyDown案例触发PlayerNote note消息而不是重复功能.我已经尝试用Cmd.none涉及Task的东西替换并直接调用update但它似乎不起作用.
我是以完全错误的方式来做这件事的吗?这不是榆树真正允许的吗?
回想一下,这update只是一个函数,可以像任何其他函数一样调用.您可以通过递归方式调用它PlayNote Msg:
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
PlayNote note ->
let
updatedCurrentPlaying =
note :: model.currentPlaying
in
( { model | currentPlaying = updatedCurrentPlaying }, Cmd.none )
KeyDown keyCode ->
let
note =
List.filter (\x -> x.keyCode == keyCode) model.notes
|> List.head
in
case note of
Nothing ->
( model, Cmd.none )
Just a ->
update (PlayNote a) model
Run Code Online (Sandbox Code Playgroud)