Cmd with no message in Elm
Asked Answered
E

1

7

Is it possible to create a Cmd that sends no message on completion in Elm?

Specifically, I am trying to have an element grab the focus (programmically), but I don't need to be informed of the result:

Dom.focus "element-id"
    |> Task.attempt FocusReceived
...
FocusReceived result ->
    model ! []  -- result ignored

Is there any way to just have the Elm "engine" not send a message after this Cmd?

I know that my code (FocusReceived result -> model ! []) is a no-op, but I would like the message not to be sent at all.

Ezechiel answered 21/1, 2018 at 13:30 Comment(0)
W
6

No, a Msg is always required. It is a common idiom in typical Elm projects to include a Msg type constructor that does nothing named NoOp.

type Msg
    = NoOp
    | ...

The update function does what FocusReceived in your example does, namely, nothing.

case msg of
    NoOp ->
        model ! []
    ...
Wain answered 21/1, 2018 at 13:44 Comment(1)
That is what I suspected. In my case, I am going to have the main update function also trigger a command to save the state to outside storage, so I wanted a way to ignore commands that have no effect on the state. Thanks.Ezechiel

© 2022 - 2024 — McMap. All rights reserved.