opvasger/loadable/1.0.0

NOTE: the latest version is 3.2.1

Unavailable. Nobody can download this version any more, with elm or with the Lamdera compiler: github.com/opvasger/loadable is gone (deleted or private); Software Heritage not asked yet (this run's --swh-calls were used up); no fork with tag 1.0.0 found (31 same-name repos checked); no cached copy. Use another version.

Details

Loadable

This module helps you model values loaded, for example, from a database over Http/GraphQL.

These three definitions are the bread and butter of the module:

  1. Add a loadable to your model-definition.
  2. Request some data, and update your loadable to expect it.
  3. Receive a result, and transform it into a loadable, that then goes into your model.

The next logical step is to model your views according to the model. For example with mdgriffith/elm-ui:

viewProductList : Loadable.FromHttp (List Product) -> Element Msg
viewProductList products =
    let
        viewErrorAndRetry =
             Loadable.toError products
                |> Maybe.map (viewErrorAndRetryOverlay getProductsCmd)
                |> Maybe.withDefault Element.none

        viewLoading =
            if Loadable.isLoading products then
                viewLoadingIndicator
            else
                Element.none

        productElements =
             Loadable.toValue products
                |> Maybe.map (List.indexedMap viewProductListItem)
                |> Maybe.withDefault []
    in
    Element.column
        [ Element.inFront viewErrorAndRetry,
        , Element.inFront viewLoading
        ]
        productElements
type Loadable error value
= Idle
| Loading
| Success value
| Failure error
| Reloading value
| ReloadFailure error value

A representation of data-loading. This would usually go into your model.

expectUpdate : Loadable error value -> Loadable error value

Update your loadable to expect a new value. Use this when you expect a new value to arrive soon.

fromResult : Result error value -> Loadable error value

Convert a result into a loadable. Use this when a new value arrives, and your loadable is expecting the update.

Queries

isLoading : Loadable error value -> Bool

Is the loadable expecting a new value?

isStale : Loadable error value -> Bool

Does the loadable contain a stale value? Staleness implies a reload is underway or failed, in which case the value from the most recent load is available.

hasValue : Loadable error value -> Bool

Is any value, stale or otherwise, contained within the loadable?

hasError : Loadable error value -> Bool

Has the loadable failed on the latest load or reload?

Conversion

map :
(value -> otherValue)
-> Loadable error value
-> Loadable error otherValue

Transform the value of a loadable.

(error -> otherError)
-> Loadable error value
-> Loadable otherError value

Transform the error of a loadable.

toValue : Loadable error value -> Maybe value

Extract the value of a loadable, should there be one.

toError : Loadable error value -> Maybe error

Extract the error of a loadable, should there be one.

Aliases

Use these aliases to tidy up your model-definition

type alias FromGraphql value =
Loadable (Error value) value

data over GraphQL is a good use-case for loadables.

type alias FromHttp value =
Loadable Error value

data over Http is a good use-case for loadables.