This module extends Data preserving the information when reloading the same source.
It helps in scenarios like the following routine:
Data was never requested
Recyclable.NeverAskedRequest was sent
Pipe it into:
|> Recyclable.toLoadingAt this point, it will be:
Recyclable.LoadingRequest's Response was received (a Failure error)
Pipe it into:
|> Recyclable.mergeResponse responseAt this point, it will be:
Recyclable.Failure errorUser press "Retry" button, a new request was sent
Pipe it into:
|> Recyclable.toLoadingAt this point, it will be:
Recyclable.LoadingRequest's Response was received (a Success data)
Pipe it into:
|> Recyclable.mergeResponse responseAt this point, it will be:
Recyclable.Ready dataUser press "Refresh" button, a new request was sent
Pipe it into:
|> Recyclable.toLoadingAt this point, it will be:
Recyclable.Recycling data Recyclable.LoadingRequest's Response was received (a Failure error)
Pipe it into:
|> Recyclable.mergeResponse responseAt this point, it will be:
Recyclable.Ready (Recyclable.Recycling data (Recyclable.Failure error))User press "Refresh" button, a new request was sent
Pipe it into:
|> Recyclable.toLoadingAt this point, it will be:
Recyclable.Recycling data Recyclable.LoadingRequest's Response was received (a Success data)
Pipe it into:
|> Recyclable.mergeResponse responseAt this point, it will be:
Recyclable.Ready dataIndicates FailureStage and LoadingStage stage when data is/was being fetched.
While Recyclable can model any type of errors,
the most common one Paack has encountered is when fetching data from a Graphql query,
and getting back a GraphqlError.
Because of that, GraphqlHttpRecyclable is provided as a useful alias.
First, when initializing a "Model", you will use either:
Recyclable.NeverAsked
(Recyclable.firstLoading, modelRequestCmd)
It's very common to initialize the model already requesting the data,
use firstLoading in this case. Like this:
init : RequestConfig -> ( Model, Cmd Msg )
init requestConfig =
( Recyclable.firstLoading
, modelRequestCmd requestConfig
)Then, on "update" you're gonna be using either:
This is the update routine for when overwriting the current data with a new freshily-fetched response.
update msg model =
case msg of
CardFetched response ->
{ model
| card =
Recyclable.mergeResponse response model.card
}Returns a Recyclable to its loading state.
Keeping the information when available.
Convert a Response, probably produced from a query result, to a Recyclable value.
NOTE: As this function discards the previous information,
in most cases you should be using mergeResponse instead.
True when Ready _.
NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.
True when _ (Failure _).
NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.
True when _ (Failure (Custom _)).
NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.
True when _ (Failure (Transport _)).
NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.
True when _ Loading.
NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.
True when NeverAsked.
NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.
Transforms Failure error into Just error, and anything else into Nothing.
NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.
Apply a function to the values in Recycling value _ and Ready value.
If the data is anything else, the same value will propagate through.
Transform a Failure (Custom a) value. If the data is Failure (Custom a), it will be converted.
If the data is anything else, the same value will propagate through.
Transform a Failure (Transport a) value. If the data is Failure (Transport a), it will be converted.
If the data is anything else, the same value will propagate through.
Transform a Failure value. If the data is Failure, it will be converted.
If the data is anything else, the same value will propagate through.
If the recyclable is Success return the value,
but if the recyclable is anything else, then return a given default value.
NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.
For doing pipes instead of switch-case
A representation for fetchable data with eight states:
First routine states:
NotAskedLoadingFailure (Custom error)Failure (Transport error)Ready dataFuture cycles states:
Recycling data LoadingStageRecycling data (FailureStage (Custom error))Recycling data (FailureStage (Transport error))