Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr3068 :: Keeping track of downloads in Elm

Tuula shows how to keep track of what data is being downloaded in Elm

<< First, < Previous, , Latest >>

Thumbnail of Tuula
Hosted by Tuula on Wednesday, 2020-05-06 is flagged as Clean and is released under a CC-BY-SA license.
Elm, programming. (Be the first).
The show is available on the Internet Archive at: https://archive.org/details/hpr3068

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:13:11

general.

Background

I have page that requests several resources from server. To keep track what is going on, I initially had model like:

type alias Model =
    { availableChassis : List Chassis
    , chassisLoaded : Bool
    , chassisLoading : Bool
    ...
    }

Problem with this is that I have to remember to check those boolean flags while rendering on screen. And it’s possible to have inconsistent state (both loading and loaded).

Solution

We can model state with algebraic datatypes and we don’t even have to write it by ourselves as there’s RemoteData library.

Now we can change our model to following:

import RemoteData exposing (RemoteData(..), WebData)

type alias Model =
    { availableChassis : WebData (List Chassis)
    }
  • availableChassis has four states it can be in:
    • NotAsked, data isn’t available and it hasn’t been requested from server
    • Loading, data isn’t available, but it has been requested from server
    • Success (List Chassis), data has been loaded from server
    • Failure Http.Error, there was error while loading data

For example, while rendering the view, you could do

    case model.availableChassis of
        NotAsked ->
            renderEmptyTable

        Loading ->
            renderLoadingTable

        Success chassis ->
            renderChassisList chassis

        Failure error ->
            renderErrorMessage error

Comments

Subscribe to the comments RSS feed.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
Who is the host of this show?
What does HPR mean to you?