opvasger / loadable / Loadable

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


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.

update : Result error value -> Loadable error value -> Loadable error value

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

Query

isLoading : Loadable error value -> Basics.Bool

Is the loadable expecting a new value?

isStale : Loadable error value -> Basics.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 -> Basics.Bool

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

hasError : Loadable error value -> Basics.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.

map2 : (first -> second -> third) -> Loadable error first -> Loadable error second -> Loadable error third

Transform the values of two loadables, falling back to the weakest constructor of the two. Precedence is as follows:

  1. Failure
  2. Idle
  3. Loading
  4. ReloadFailure
  5. Reloading
  6. Success

map3 : (value -> secondValue -> thirdValue -> fourthValue) -> Loadable error value -> Loadable error secondValue -> Loadable error thirdValue -> Loadable error fourthValue

Refer to map2. Loadables are combined chronologically.

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

Transform the error of a loadable.

andThen : (another -> Loadable error value) -> Loadable error another -> Loadable error value

Sometimes values are loaded based on other loaded values - This function makes that situation more ergonomic.

withDefault : value -> Loadable error value -> value

Get the value of a loadable, or a default value if there is none.

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.

fromResult : Result error value -> Loadable error value

Transform a result into a loadable.