Elm更新无限循环

Elm update infinite loop

Elm 的新手,所以我可能遗漏了一些明显的东西。

我正在开发一个使用 annaghi/dnd-list 的 Elm 应用程序。我遇到了更新调用的无限循环。单击一个元素,然后单击另一个元素时会发生这种情况。这是代码:

config : DnDList.Config Player
config =
    { beforeUpdate = \_ _ list -> list
    , movement = DnDList.Free
    , listen = DnDList.OnDrag
    , operation = DnDList.Swap
    }


system : DnDList.System Player Msg
system =
    DnDList.create config DndMsg


type alias Model =
    { navKey : Nav.Key
    , room : WebData Room
    , dnd : DnDList.Model
    , startError : Maybe String
    }

type Msg
    = RoomReceived (WebData Room)
    | DndMsg DnDList.Msg

...

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        RoomReceived room ->
            ( { model | room = room }, Cmd.none )
        DndMsg message ->
            let
                room = model.room
            in
            case room of
                RemoteData.Success actualRoom ->
                    let
                        ( dnd, players ) =
                            system.update message model.dnd actualRoom.players

                        updatedRoom = RemoteData.map 
                            (\roomData -> 
                                { roomData | players = players }
                            ) room
                    in
                    ( { model | dnd = dnd, room = updatedRoom }
                    , system.commands model.dnd
                    )
                _ ->
                    ( model, Cmd.none )

当我将行 system.commands model.dnd 更改为 Cmd.none 时,没有无限循环调用更新函数,但也没有任何反应。在 dnd-list 库中不断调用的消息是 GotDropElement (Ok dropElement)

再次,Elm 的新手,所以这可能是一个结构不佳的问题,但我们将不胜感激任何帮助。

谢谢!

尝试按照 system.commands 中的流程进行操作。可能它最终会再次发送消息 DndMsg,这就是导致问题的原因。

通常认为从命令发送消息不是一个好习惯。

如果您无法解决问题,Ellie (https://ellie-app.com/new) 中有问题代码的工作示例会有所帮助。

想通了。必须添加订阅才能监听鼠标事件


currentSubs : Model -> Sub Msg
currentSubs model =
    case model.page of
        GameRoomPage pageModel ->
            GameRoom.subscriptions pageModel
                |> Sub.map GameRoomMsg
        _ ->
            always Sub.none model

...

main : Program () Model Msg
main =
    Browser.application
        { view = view
        , init = init
        , update = update
        , subscriptions = currentSubs
        , onUrlRequest = LinkClicked
        , onUrlChange = UrlChanged
        }