Remote / foldkit-remote

When it does not do what you expect

Each of these is a design of the package showing through, not a fault. The symptom names the section; the cure is one call.

Why is it still Initial?

Initial means required data is absent and nothing is fetching it, so a spinner here hides a wiring mistake. Ask the Model with why, from the foldkit-remote/why entry (a development tool, kept apart so a page that never explains a read keeps none of it):

ts
import { why } from 'foldkit-remote/why'

why(Data, model, projection, { surfaces }) // the record you give Data.subscriptions
// { state: 'Initial',
//   because: [Why.NotFetching({ surfaces: ['ProjectPage'] }), Why.Fields({ … })],
//   message: 'ProjectPage reads it and is active, yet nothing is fetching it. …',
//   surfaces: Option.some(['ProjectPage']) }

The first reason is the one that decides Initial:

  • NotObserved: no active Surface reads it. The Surface is not in the record you gave Data.subscriptions, or its Surface.at function returns none for this Model (the route is not there yet).
  • NotFetching: an active Surface reads it and nothing started a request. Right after a Message that is the moment before the read entry asks; if it stays so, Remote's Subscriptions are not installed in the runtime.
  • CacheOnly: the active Surfaces reading it read under RemotePolicy.cacheOnly, which asks for nothing on purpose. It comes first for a stale value under that policy too.
  • NoActiveRecord: no surfaces were given, so it cannot tell which.

Data.plan(model, projection) shows whether Remote believes anything is missing at all.

Why is it Loading?

A list reads Loading when its page is missing and its query is out, and also when it holds its page and one row's field is being fetched. It reads Refreshing when the page is stale, a row is stale, or a row is left out until a field arrives. why tells them apart. because lists every fact the Model holds about what the read reads, by typed reference, never by the store's internal marks. The reason that decides the state comes first. Match them with Why.match:

WhyWhat it saysWhat restarts it
List { list, held, inFlight }list page is Missing, Evicted, or Stalethe read entry, while a Surface reads it
Invalidated { list, because }why a stale list went stale (write, live, or refresh)the same read entry
HeldForHistory { list }returned via Back/Forward within historyWindowa reader-initiated navigation
Fields { row, via, fields, held, inFlight }row fields are Missing, Evicted, or Stale (via: relation path)the read entry
ListFailed, FieldsFaileda request failedData.refresh, a field write, or a new page
Unavailable, Undecodable, OverlayOnlythe store holds what cannot be showna change of Selection or overlay
Deleted { row, by }server or pending write (by) deleted the row—
LiveReconnecting, LiveRefuseda live stream broke or was refusedbackoff for breaks; never for refusals
Pending { row, fields, by }a pending write shows these fieldsits answer
Joined, Left { list, row, by, terms }list shows or hides a row vs. its fetched pagesits answer

by is a ShownBy: Write { requestId, mutation }, Overlay { id } or Live { cursor }. terms are the list body's where predicates judged over the row as the client placed it, so a row that joined says which term put it there, and one the client could not judge says what it lacked:

ts
import { ShownBy, Why } from 'foldkit-remote/why'

// a project archived optimistically, in a list of active ones
Why.Left({
  list: { query: 'ByStatus', identity },
  row: { entity: 'Project', id: 'p1' },
  by: Option.some(ShownBy.Write({ requestId: 'r7', mutation: Option.some('Archive') })),
  terms: [{ term: 'Project.status = $status', truth: 'False', reason: Option.none() }],
})

truth is True, False, Null (SQL's unknown, which keeps a row out) or Unknown (the store lacks what it rests on). ReadWhy is a Schema: Schema.encodeSync(ReadWhy)(why) is JSON for a log or a devtools client. why reads the Model and nothing else: it plans nothing and fetches nothing.

Across a history. timeline(Data, start, history, { feed, post }, { surfaces }) takes the Messages update handled, each with the Model it left (what Foldkit's devtools records), and gives one step per change of a read's tag: the Message that made it, why at the Model it changed to, and regression when a read that showed a value stopped showing it with no reason a reader can see. Foldkit's devtools has no place for a panel of another package's, so the timeline is the data, not a panel.

Why is it stale?

Under the default RemotePolicy.cacheFirst, a present field is never asked for again. That is the point of a cache, and the reason a list shows what the server said last time. To see change:

  • On demand: Data.refresh(model, projection) from update marks what the screen reads Refreshing and the active Subscription fetches it again, old value still on screen. A refresh button, a window regaining focus.
  • By age: RemotePolicy.staleWhileRevalidate({ maxAge }) refetches a value or a list older than maxAge, and nothing younger.
  • Always: RemotePolicy.networkOnly requests every selected field, cached value visible meanwhile.
  • As it happens: live(Data, …) instead of Data.get, or liveQuery(Data, …) instead of Data.query, with a server that publishes changes; see Live data.

A list's age is the page that last answered it whole: its first page, or the one that replaced it after it went stale. A later page from "load more" does not date it. A row's age is that of the oldest field a read asks: a mutation's answer or a narrower read that writes one field leaves the others as old as they were, so a read of those is planned again when they are due, and a read of the new field alone dates only it. A row restored from storage or a server page carries one date for every field, so it is read again early at most. See Reading, policies, and prefetch.

Why did my change disappear?

A mutation's optimistic patches are layers over the store while the request is in flight. When the server answers, the layers come off and the cache shows what the server said:

  • It failed. MutationFailed removes the layers; Data.mutation(model, requestId) reads Failed with the error. Nothing retries it on its own (Data.retry sends it again; see When a write fails). A write survives a reload only when it is sent durable (outbox(Data, …)); an edit that must converge with other devices' offline edits belongs to foldkit-sync.
  • It succeeded, but the answer did not name the row. The answer carries what the active Surfaces and the request's layer show of each row it names (What an answer carries); a row it changed without naming (a post's count, after a like) keeps the old value until a read writes the new one. Name the row in the answer.
  • A preview shown with overlay stays until lift; a settled mutation never takes it along.

See Mutations and optimistic state.

Why does a row leave its list for a frame after a mutation?

The answer left out fields the row showed while the request was pending. Once the layer comes off, the store lacks them, so the read entry fetches them: until they arrive, a list leaves the row out (and reads Refreshing), and a read of that entity alone reads Loading. An answer carries what the active Surfaces read and the layer showed of every row it names, so this is a row the answer did not name, a field the server's read left out, or a server that answers without the read (one older than the client, or whose read failed). Remote.make({ verifyPredictions: true }) names each one: see Checking an answer against what was shown.

Why does a page I went back to read Refreshing?

Remote keeps what the screens you most recently left reached, within a budget of 1,000 rows and lists (Remote.make({ budget })). Under cacheFirst a list left is held stale, so going back shows its rows at once and reads Refreshing while the planner refetches it; the page that answers replaces what was held. Read under staleWhileRevalidate by every call that reads it, a list left keeps its age instead: going back inside maxAge draws it Ready and sends nothing, and past it reads Refreshing while it refetches. Only past the budget is a screen's data evicted, the screen left longest ago first, and then coming back reads Loading and refetches; why says the list or row is Evicted.

Back and Forward are different: they return to a page the reader has just seen, and refetching it only makes it flicker. Told that a navigation came through history, Remote reads a list left within Remote.make's historyWindow (five minutes by default) Ready, as it was, and asks for nothing; why says HeldForHistory. Past the window, after a navigation the reader started (a link, a Command that wrote the URL), or once the list was invalidated or changed while held, it refetches as above. The application says which kind each navigation is with the Navigated Message, reduced before the route changes; Site.routing does it given navigated:

ts
const Routing = Site.routing<Model, Message, Route, RemoteClient>({
  // site, route, parse, tags and completed as before
  navigated: (model, kind) => Data.reduce(model, { _tag: 'Navigated', kind }),
})

The window is judged by retention's clock, once the list is shown again: in the moment between the route changing and retention seeing it, a list past its window still shows as it was, then reads Refreshing while it refetches. To keep something no screen reads, name it in a retain entry or a connections list; see Retention.

Why does a value the server has read Failed?

Failed with no previous and no request error means the stored value did not decode against the Selection: the server's shape and the entity's Schema disagree; the error is a DecodeError with the Schema's message. An Unavailable error means the server answered without a field the Selection names and will not answer with it; see When a field is withheld. A failed request reads the same way, with the request's error; see When a read fails. A RemoteProtocolError means the client and server disagree on REMOTE_PROTOCOL_VERSION, and nothing is silently accepted.

Why did a live update not show?

Live events carry cursors. One behind the Model's is a duplicate and is dropped; one ahead of the next is a gap, recorded in RemoteModel.gaps and not applied, so the screen keeps what the server last said rather than a history with a hole in it. The live entry then restarts the stream and refetches what it covers; see When a stream breaks.