Remote / foldkit-remote
Reading, policies, and prefetch
Data.get(selection, id) returns a pure Projection. live(Data, selection, id)
returns the same kind of Projection but marks its requirements as live so
Data.subscriptions also follows server changes.
What Remote should do when the Model already contains the selected fields is a
RemotePolicy:
RemotePolicy.cacheFirst(default) — fetch missing, stale, or re-windowed data.RemotePolicy.staleWhileRevalidate({ maxAge })— keep a present value or list visible and refetch it when it is older thanmaxAge; a list is asked for again whole.RemotePolicy.networkOnly— request every selected field and list; cached values stay visible while the request is in flight.RemotePolicy.cacheOnly— request nothing. What the store holds shows, and what changes the store still reaches the read (an answer another read brought, a live event, a write); what it lacks readsInitial, andwhysaysCacheOnly. A value held stale readsRefreshing, though nothing fetches it until the policy changes;whysaysCacheOnlyfor it too. A read in flight when the policy changes to it is interrupted, and its entry takes back theLoadingit set (ReadAbandoned), so what it lacks readsInitialrather thanLoadingfor good.
A refreshing policy emits RefreshStarted before the read, naming the fields
and the held lists it asks for again though the Model holds them (aged out,
forced, or under another window), so the Projection becomes Refreshing
without discarding the old value, and the page answering a list replaces what
it held. What it reads because it is missing or already stale it does not
name: Foldkit reduces the mark after the Messages already queued, an answer to
that very field among them, and a mark that made the answer stale again had
two reads of one row refresh each other for good. Planning accepts now
as an input, so tests can control time. It defaults to Date.now, read at each
use, so fake timers (vi.useFakeTimers) move it; Effect's TestClock does not,
because a plan runs outside Effect, so a test that drives TestClock passes
now. A mutation's answer is dated by another clock: Effect's Clock, read
when the answer lands, so a test of how written rows age drives TestClock.
Data.mutate's now option is the write's time, sent to the server; it does
not date the answer.
A policy belongs to one subscriptions call, and covers every read in it. Reads
under different policies take a call each:
const projectPage = Surface.at(ProjectPage, model =>
model.route._tag === 'project' ? Option.some({ projectId: model.route.projectId }) : Option.none(),
)
const owner = Data.active('Owner', () => Option.some(Data.get(UserSummary, 'u1')))
const pageEntries = Data.subscriptions({ page: projectPage })
// Retention is the domain's: one `retain` roots every read, so the second
// call's is left out rather than spread over the first's.
const { retain: _domainRetain, ...ownerEntries } = Data.subscriptions(
{ owner },
{ policy: RemotePolicy.staleWhileRevalidate({ maxAge: 5_000 }) },
)
const subscriptions = Subscription.make<Model, Message, RemoteClient>()(() => ({
...pageEntries,
...ownerEntries,
}))Retention stays the domain's: a call's retain entry roots every read any call
of the domain names, so neither call collects what the other reads. Keeping one
retain entry is all it takes; it waits for its own call's grace.
Data.wiring is one call, so it takes a policy per entry instead: policies
is keyed by the active record's keys, and an entry it does not name reads under
policy. Here the owner ages while the page reads cacheFirst:
const pageWiring = Data.wiring(
{ page: projectPage, owner },
{ policies: { owner: RemotePolicy.staleWhileRevalidate({ maxAge: 5_000 }) } },
)A key the record lacks is a type error. A list two entries read is held stale
once left unless both age it, as with two calls. The wiring's subscriptions
are named by the same keys, so a test reads pageWiring.subscriptions['owner.read']
with no !.
A call's policy can also be picked from the Model, so a screen that works
offline reads what it holds while offline and asks once it is back. isOnline
reads a field the application keeps, from navigator.onLine and its events or
a failed request:
const offlineEntries = Data.subscriptions(
{ page: projectPage },
{ policy: model => (isOnline(model) ? RemotePolicy.cacheFirst : RemotePolicy.cacheOnly) },
)A change of policy changes the read entry's plan, so Foldkit restarts it: going
back online under cacheFirst asks for exactly what cacheOnly left missing,
and under networkOnly for that and everything held again. A live read keeps
its stream under any policy: it is how changes reach the store, and it
reconnects on its own. Data.prefetch and Remote.prefetch take the same
option and pick the policy from the Model they are given.
Time reaches Remote only as a Message. The plan is a function of the Remote
model, what is asked, and now, and it runs again only when one of those
changes; a page that sits still past its
maxAge would otherwise never be looked at again. So a read entry under
staleWhileRevalidate knows when the earliest value it holds ages out, sleeps
until then under the Effect clock, and emits RefreshStarted for what is due.
That marks the fields and lists stale in the Model, and the plan that follows
fetches them, the same path a refresh button takes. Nothing reads the clock while a
Projection is read, so a Model reads the same twice, and a recorded one
replays.
For SSR, route prefetch, hover prefetch, and tests, run the same plan explicitly:
import { Effect } from 'effect'
const loaded = await Effect.runPromise(
Data.prefetch(model, ProjectPage.projection({ projectId }), {
policy: RemotePolicy.staleWhileRevalidate({ maxAge: 30_000 }),
}).pipe(Effect.provide(clientLayer)),
)Data.prefetch performs I/O explicitly and returns the Model with the resulting
Remote Messages reduced into it. It never changes the semantics of the
Projection itself.
A render that fetches nothing (SSR, a prerender) needs everything a page's
active Surfaces read, and one read can decide what another asks for: a page's
Blocks are known once its document is. Data.satisfy prefetches every active
Surface, cache-first, pass after pass until a pass plans nothing. A pass sends
what every Surface plans as one read, so a page of several Surfaces makes one
client call a pass; a Surface that another's read reveals is read in the next
pass, wherever it comes in actives:
const ready = await Effect.runPromise(
Data.satisfy(model, actives).pipe(Effect.provide(clientLayer)),
)actives is the record Data.wiring takes. A Surface.each family is one
entry of it: its parent's requirements join its instances', so the parent is
fetched before what it reveals. The passes are bounded (8 by
default, { passes } to change it); a Surface still reading after the last
fails the Effect with RemoteUnsatisfied, which names it — a still-reading
family instance as Child[key] — rather than
rendering it loading. A read the server leaves unanswered settles as missing,
so it ends the loop; what runs out the passes is a chain whose every read
reveals one more.
Preloading a destination from update
prefetch and satisfy return a Model, which suits a render that has none
yet. A running application has one, and it moves on while a read is out, so
writing the returned Model back would undo what happened meanwhile.
Data.preload is satisfy as a Command: it reads what actives would read
in the Model you give it (the live one with the route set to where the reader
may go) and yields one Preloaded, carrying the Messages that wrote it,
which update reduces into the Model as it is by then:
const project = Surface.at(ProjectPage, model =>
model.route._tag === 'project' ? Option.some({ projectId: model.route.projectId }) : Option.none(),
)
// The reader is over the link to p2: read its page before they arrive.
const preload = Data.preload({ ...model, route: { _tag: 'project', projectId: 'p2' } }, { project })
// update returns { model, commands: [preload] }; under Remote.fold, foldData.preload(...)It never fails: a read that fails, or a client that dies, ends it with what was read before, and the destination reads the rest when the reader arrives. A Surface of another application is refused when the Command is made. What it reads is kept like any other read until what is retained next changes, so preload close to the navigation you expect (a hover, a focus), not far ahead of it.
Refreshing from update
To revalidate what a screen already declares — a refresh button, a focus
regained — hand Data.refresh the active record the wiring takes:
case 'ClickedRefresh':
return { model: Data.refresh(model, actives) }Each entry is resolved for this Model as the read entries resolve it, so a
Surface.at page reads with the params the route gives it, and an inactive
entry asks for nothing. Add a Surface to actives and its refresh comes with
it. One entry (Data.refresh(model, actives.project)), a Projection, or a
Surface without params narrows it.
Data.refresh marks the Model so Data.subscriptions refetches what is
shown: every selected field the store holds reads Refreshing, every entity it
knew to be absent is asked for again (it reads NotFound, and stays out of every
list, until the answer says it exists), and every loaded connection is invalidated; the Data.subscriptions read entries then refetch it,
since stale data is planned again under every policy, so the page is requested
once. The Projection must be observed, as it is while it is on screen; for data
nothing observes, use Data.prefetch with RemotePolicy.networkOnly.
- A refreshed connection is asked for again as one page the size of the widest window reading it (including what "load more" grew), which replaces what it held, so items the server removed or reordered follow it.
- A read already in flight restarts instead of landing after the refresh.
- Refreshing what is already refreshing, or nothing, returns the same Model.
{ failed: true } asks again only what failed: each entity read whose fields
(or a related row's) failed, and each list whose query did. That is a "Try
again": what loaded keeps its value, and a list keeps its later pages, which a
whole refresh would replace with one page.
case 'ClickedTryAgain':
return { model: Data.refresh(model, actives, { failed: true }) }One list can also be named by its query and input, with no select or window
to restate: Data.refresh(model, Everyone.ref({})) invalidates that list for
every window reading it. It marks no row's fields, since a ref names no
selection; the refetched page carries what each read selects. That suits a
list another page reads, after a write whose answer cannot say it changed: a
list the Model never loaded is left alone, and one nothing observes now is
fetched when it is next read. With { failed: true } it is asked again only
if its query failed. A query the domain does not register is refused.
Forgetting everything: a change of principal
Everything Remote knows, it knows for a principal: a value the server gave
this session, a tombstone for an id it answered nothing about, a field it
settled as unavailable, the rows of a list. Log in, log out, switch
organization, and none of it is trustworthy. Data.forget is the one
boundary:
case 'SignedOut': {
return { model: Data.forget({ ...model, session: Option.none() }) }
}It returns the Model with every server-derived fact cleared—values,
tombstones, unavailable fields, connections, live cursors, failures, and the
reads in flight—so every active Surface's read and live entries restart and ask
again as whoever the client now is. A read or stream begun before is
interrupted, and an answer it had already sent, still queued behind the
forget, is dropped because each answer carries the generation it was asked
under. A mutation in flight is treated as applied, so its answer writes nothing
into the new store, and the mutation sequence is kept so request ids stay
unique.
Who the client is belongs to the RemoteClient layer or the server's session,
not to Remote. forget only says that what was known no longer is.