Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger multiple message in one function in Elm 0.18?

Tags:

elm

I want to trigger multiple messages when window resize in subscription.

Something like:

subscription : Model -> Sub Msg
subscription model =
  Window.resizes (\{width, height} ->
    Sidebar "hide"
    Layout "card"
    Search <| Name ""
    Screen width height
  )

How do I active them at once?

like image 240
Knovour Avatar asked Dec 18 '22 09:12

Knovour


1 Answers

I'm also interested in seeing what other would answer. But here is what I would do.

In short, you can make a single parent message which calls other child messages. andThen function just helps to concatenate the update calls.

andThen : Msg -> ( Model, Cmd msg ) -> ( Model, Cmd msg )
andThen msg ( model, cmd ) =
    let
        ( newmodel, newcmd ) =
            update msg model
    in
        newmodel ! [ cmd, newcmd ]


update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
    case Debug.log "message" msg of
        DoABC ->
            update DoA model
                |> andThen DoB
                |> andThen DoC
like image 182
Tosh Avatar answered May 02 '23 00:05

Tosh