gpt4 book ai didi

elm - 如何从更新中触发更新调用

转载 作者:行者123 更新时间:2023-12-04 13:56:49 24 4
gpt4 key购买 nike

我遇到了一种情况,我有两种方法可以在 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 )

我想知道的是,是否有办法稍微解决一下这个问题,并导致 KeyDown 案例触发 PlayerNote note Msg 而不是复制功能。我试过用涉及 Task 的东西替换 Cmd.none 并直接调用 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

关于elm - 如何从更新中触发更新调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48648792/

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