Remote / foldkit-remote
RemoteData: what does the Model know right now?
A Remote Projection tracks presence separately from values using a closed six-state
RemoteData union: missing data starts as Initial (no fetch active) or
Loading (fetch in flight), settles to Ready on success (or NotFound when
the server reports absence, or Failed when decoding/fetching fails), and
moves to Refreshing during a refetch while keeping the previous value on
screen.
The states are:
Initial— required data is absent and nothing is currently fetching it.Loading— required data is absent and a read, or a list's query, is in flight.Ready— every selected field is present and decodes.Refreshing— the current value remains visible while it is being refetched, or, for a list, while some of its rows are still being read.Failed— stored server data does not decode against the Selection, the read or query behind it failed, or the server settled a selected field without a value (anUnavailableerror; see When a field is withheld). A value that was already shown is kept asprevious.NotFound— the entity is represented by a tombstone: a live event deleted it, or the server was asked for it by id and answered with nothing about it. That id is then known absent, whether it never existed, is gone, or is hidden whole from this principal. It is not planned again untilData.refreshforces it or a write brings it back. The target of a returned ref is not marked this way: a server need not expand a relation that rides on a request, and the planner asks for such a target by id next, which is when its absence is learned.
Initial is intentionally different from Loading. A Projection belonging to
no active Surface may remain Initial forever. Rendering a spinner for
Initial can therefore hide an activation/wiring mistake; Loading is the
state that actually means "wait for this request."
When a read sits at Initial and you expected data, ask the Model why with
why; see Why is it still Initial?.
RemoteData.match is exhaustive, so adding or omitting a state is visible at
compile time. RemoteData.answered(data) is whether a read has an answer yet:
Ready, Refreshing, Failed or NotFound, not Initial or Loading.
RemoteData.held(data) is the value it holds, as an Option: Ready's,
Refreshing's, or the last value a failed refresh kept; none in every other
state.
Drawing one: RemoteData.render
Most views draw these six states three ways, under one policy: useful data
stays on screen. RemoteData.render is that fold.
RemoteData.render(project, {
loading: () => ProjectSkeleton(),
notFound: () => NoSuchProject(),
failed: error => ErrorView(error),
data: (value, freshness) =>
ProjectView({ project: value, dimmed: freshness._tag !== 'Fresh' }),
})Initial and Loading reach loading; Ready and Refreshing reach data;
a Failed that still carries the value it had reaches data too, so a read
that failed does not throw away what the reader was already looking at. Only a
Failed with nothing to show reaches failed.
The data branch is told which it got, as a Freshness — exported, so a view
helper that takes one can name it, and folds it with its exhaustive
Freshness.match:
Freshness | What it means |
|---|---|
Fresh | This is the current answer. |
Refreshing | A newer answer is on its way; this one is still good. |
Stale | A read failed; this is what was last known good, with its error. |
It is one tag rather than a pair of booleans because a value cannot be both refreshing and stale, and a type that can say so invites a view to handle a state that never arrives.
notFound is its own branch and not optional. A row the server answered for
and does not have is neither loading nor a failure; drawing it as either is a
spinner that never ends or an error nobody can act on.
Reach for match instead when the six states really do draw differently — it
stays the exhaustive fold, and render does not replace it.
Transforming a read before drawing it
Keep the read whole in the Surface: the Projection Data.get returns carries
what Remote fetches, and a Surface or Projection.struct that holds it keeps
that. Transform the value it gives instead, with RemoteData.map: it changes
the value of Ready, Refreshing and a failed read's held value, and leaves
the state and its error as they were. map takes the value first, so in a
pipe it is called from a lambda:
const title = pipe(
project,
data => RemoteData.map(data, ({ name }) => name),
name => RemoteData.map(name, text => text.toUpperCase()),
)title is a RemoteData<string>, so the view still folds every state with
render or match. What TypeScript refuses here: a missing case in match,
render or Freshness.match (the error names it), a case the union lacks,
.value before narrowing to Ready or Refreshing, a failed read's held
value used as if it were always there, and a read of another application's
Model in this one's Surface. What it cannot see: a ternary over _tag (pnpm
tags:check refuses it in a package's or example's src), a cast, a Model
that structurally contains another application's, and which of two bindings
over one Model made a read. RemoteData.all joins reads when a view
needs them together (below); a view that wants another precedence folds each
read with its own match.
Joining reads: RemoteData.all
RemoteData.all({ project: projectRead, owner: ownerRead }) takes a struct of reads and answers one
RemoteData of the struct of their values, so a view that needs all of them
draws once from the values passed without fetching anything itself. The first
state any entry holds decides the result, in order: Failed > NotFound >
Initial > Loading > Refreshing > Ready.
Failed carries the first failed key's error. When every entry holds a value
the result is Refreshing if any entry is, else Ready: it is as fresh as its
stalest entry. A Failed result carries previous (the struct of values) only
when every entry still held one (a Ready, a Refreshing, or a Failed with
its own previous), so render draws that set stale instead of an error alone.
With no entries it is Ready of an empty struct.
const page = RemoteData.all({ project: projectRead, owner: ownerRead })
// RemoteData<{ readonly project: { name: string }; readonly owner: { handle: string } }>An entry that is not a RemoteData is a type error. Which of NotFound and
Failed should win is a view decision: the order above is this function's, and
a view that wants another folds each read with match.