0

The elm person at our company has left and we now have an elm codebase and we just have to learn it. I think I got most of this ticket done but I need to add This code to the Update.elm:

update : Config parentMsg parentModel -> parentModel -> Msg -> Model -> ( Model, Cmd Msg )
update config parentModel msg model =
    case msg of
        OnUrlChange url ->
            ( model
            , { location = url }
                |> featureapp.syncCmd
            )
    // etc...

If I'm understanding this is the TypeScript equivalent of:

function update(config, parentModel, msg, model) {
    case (msg) {
        'OnUrlChange': 
            return function (url){
                // lets pretend an array is a tuple.
                return  [model, featureapp.syncCmd({location: url})];
            }
         break;
    }

}

I'm trying to add this:

function update(config, parentModel, msg, model) {
    case (msg) {
        'OnUrlChange': 
            return function (url: Url.url){
                // \/ \/ THIS IS WHAT I WANT TO ADD  \/ \/
                trackPageNavigation(Url.toString(url));

                // lets pretend an array is a tuple.
                return  [model, featureapp.syncCmd({location: url})];
            }
         break;
    }

}

edit:

As per The Docs,

Each is handed to the runtime at the same time, and since each can perform arbitrary operations in the world, there are no ordering guarantees about the results.

That, does not work because I do not want to return trackPageNavigation. I want to only return featureapp.syncCmd. Or maybe I'm misunderstanding what is going on here. I assumed that

        OnUrlChange url ->
            ( model
            , { location = url }
                |> featureapp.syncCmd
            )

is returning a tuple of (model, Cmd Msg). Does it not care which one it returns? This is for the Update.elm.

JonTroncoso
  • 791
  • 1
  • 8
  • 22
  • @JanTojnar It does not because `Cmd.batch` will return a random one of those and it MUST be `featureapp.syncCmd`. – JonTroncoso Oct 07 '22 at 22:56
  • @JanTojnar On second thought, that may be an assumption I'm bringing from my experience with other languages. Does it matter what is returned in this context? I'm not sure what a `Cmd Msg` is. From what I can understand it is a `Cmd` of type `Msg`. But `trackPageNotification` returns `Cmd a`. Why would that be acceptable? It seems to compile but it doesn't make sense why that would be. Like the type of `Cmd` doesn't matter? – JonTroncoso Oct 07 '22 at 23:10
  • 1
    You really need to learn the basics of a language if you're going to maintain an application written in it. Even if just to ask questions about it. You're making several flawed assumptions here, and are not including all the necessary information. The biggest one is that you seem to think the language has side-effects, and that the return type of `trackPageNavigation` is irrelevant and can be ignored. But Elm is a pure functional language. `trackPageNavigation` will do nothing if its return value isn't in some way affecting what is returned by `update`. – glennsl Oct 08 '22 at 09:37
  • You are 100% correct which is why we're only doing the bare minimum with this codebase while we port it to react. Nobody here wants to learn elm and elm devs either don't exist, are not interested in working with elm again, or are way overpriced so hiring is not an option. To your point, I am asking questions to learn about it. I have gone through some tutorials but when you are thrown in the fire and need to solve 1 problem, RTFM tends to take more time then asking specific questions. Especially with a unique language like elm that doesn't follow established norms which means re-education. – JonTroncoso Oct 10 '22 at 18:36

1 Answers1

3

Use Cmd.batch. It returns a new command that, when executed, will cause the listed commands to be executed (not in any particular order, though).

The msg type parameter of Cmd describes what values will the runtime pass to the update function when the command executes. Since trackPageNotification is likely calling a port, which causes a JavaScript code to be called instead of firing the update function with specific Msg, a type variable a is used, which makes the function polymorphic. That allows it to be used anywhere Cmd needs to be created, no matter the model’s msg type.

Jan Tojnar
  • 5,306
  • 3
  • 29
  • 49
  • Thank you very much for the detailed explanation. I have some learning/unlearning to do before I can fully understand elm. – JonTroncoso Oct 10 '22 at 18:40