Skip to content

Instantly share code, notes, and snippets.

@loicknuchel
Created November 5, 2022 12:47
Show Gist options
  • Save loicknuchel/06254682f8badb6353e46316682ce79e to your computer and use it in GitHub Desktop.
Save loicknuchel/06254682f8badb6353e46316682ce79e to your computer and use it in GitHub Desktop.
  1. https://github.com/conduktor/scalaIO_2022/blob/main/src/main/elm/Rest/Main.elm#L11

You can use Browser.document instead as the Elm app takes the whole page

  1. https://github.com/conduktor/scalaIO_2022/blob/main/src/main/elm/Rest/HttpRequests.elm#L149

I would put this in the Main.elm, replacing Msg. It will avoid the Cmd.map in the init for example. In functions in HttpRequests I would give the message as parameter. Ex:

listNames : (Result Http.Error (List TopicName) -> msg) -> Cmd msg
listNames toMsg =
    Http.get { url = crossOrigin targetHost [ "names" ] [], expect = Http.expectJson toMsg topicNamesDecoder }

With this, your init would look like:

init : () -> ( Model, Cmd Msg )
init _ =
    ( LoadingNames, listNames GotNames )
  1. https://github.com/conduktor/scalaIO_2022/blob/main/src/main/elm/Rest/HttpRequests.elm#L99

It's personal preference but I would prefer result |> Result.map (\count -> ( topicName, count )) |> GotRecordCount instead of GotRecordCount (Result.map (\count -> ( topicName, count )) result) Also, I would put everything in the decoder. Having:

loadRecordCount : TopicName -> Cmd Msg
loadRecordCount ((TopicName topicName) as topic) =
    Http.get
        { url = crossOrigin targetHost [ "topics", topicName, "records" ] [ string "fields" "count" ]
        , expect = Http.expectJson GotRecordCount recordCountDecoder
        }
recordCountDecoder topic =
    Decode.map (\count -> ( topicName, RecordCount count )) Decode.int
  1. https://github.com/conduktor/scalaIO_2022/blob/main/src/main/elm/Rest/Main.elm#L44

Usually I pattern match only on the msg, and eventually inside again on the model.

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
  case msg of
    GotNames result -> ...

Maybe, in you case, as you have very different state in your model, I could match first on the model as it's a huge distinction... Anyway, it's just syntax ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment