如何从更新中触发更新调用
How to trigger update call from within update
我有一种情况,我有两种方法可以在 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
消息而不是重复功能。我尝试用涉及 Task 的东西替换 Cmd.none
并直接调用更新,但它似乎不起作用。
我是不是用完全错误的方式来解决这个问题?这不是 elm 中真正允许的东西吗?
回想一下,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 应用程序中播放音符,并且我会跟踪当前正在播放的音符。当前代码在这里:
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
消息而不是重复功能。我尝试用涉及 Task 的东西替换 Cmd.none
并直接调用更新,但它似乎不起作用。
我是不是用完全错误的方式来解决这个问题?这不是 elm 中真正允许的东西吗?
回想一下,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