Remote / Server-derived state
Mutations and live data
A mutation flows through the ordinary Foldkit path — UI Message → update →
Data.mutate → Command — and returns to the Model as a RemoteMessage:
Diagram source
sequenceDiagram
participant UI
participant update
participant Data
participant Server as server
UI->>update: ClickedRename
update->>Data: Data.mutate
Data->>Server: Command (RPC)
Server-->>update: MutationSucceeded { requestId, entities, now }
update->>Data: Data.reduceData.mutate applies MutationStarted to the Model (the request id comes from
the Model's own sequence, so update stays pure) and returns the Command whose
Message settles it. That Message carries the normalized entity patches and
now, Effect's Clock read when the answer lands, which dates what they
write. (Data.mutate's now option is the write's time, sent to the server;
it does not date the answer.) The typed Output is what
Remote.mutateInto returns. Settling is idempotent per requestId, so a transport retry
cannot apply the same change twice. Remote.mutateInto is the one-step
imperative form for SSR and tests.
A failure is kept until the application answers it (Data.failedWrites,
Data.retry, Data.dismiss). A request nothing answered (it could not be
sent, or what came back was no Remote answer, such as a gateway's 502 page)
fails Unreachable (RemoteUnreachable), apart from a refusal, because the
server may or may not have run it. A write sent durable is stored first, in
outbox(Data, …)'s KeyValueStore, and sent in order by one tab; after a reload,
or a send that came back Unreachable, it is sent again under the same
operation id, which a ledgered writer answers from its record. A refusal
settles it as Failed, as any write.
Before it shows or sends a declared write, Data.mutate refuses what the
client can already tell the server would: a write a held Guard may says no
to, and one that breaks an invariant of its Entity. That is a UI shortcut, not
enforcement: a may that says yes decides nothing, since the server checks
every write by its rules for the request's principal.
Optimistic changes belong to the mutation: MutationStarted carries its entity
patches and connection changes (ConnectionChange.prepend/append/remove), and
success or failure releases them together by request id. Patches are ordered
layers over the base store, not inverse patches: the visible store is
recomputed as base + layers, so overlapping layers rebase for free. Connection
changes are overlays outside the server-known region; a result's confirmed
connections take the place of the request's own, so a temporary edge becomes
the real one in place.
A list is read live with liveQuery from foldkit-remote/live. A live source that
watches lists (foldkit-remote-drizzle's pollLive) runs each list's window
again as the subscriber and sends the rows that join, change or leave it;
the server admits at most 32 lists per stream, their first counted toward
maxIdsPerEntity.
On the server, RemoteServer.liveHub turns "these fields of this entity
changed" into the right patch for each subscriber: it re-reads the changed
fields a subscriber selects through the entity's own source, under that
subscriber's principal, so authorization and computed fields take the normal
path and a subscriber selecting none of the fields costs nothing.
Live data is a stream (Effect RPC streaming, a text/event-stream through
httpWithLive (foldkit-remote/live), or a port). Each stream has a monotonic cursor:
duplicates are ignored, and an event ahead of the cursor is a gap — it is
not applied. A gap or a broken transport restarts the stream after a backoff and
refetches what it covers, since no server replays what was missed. Entity events
update the store; connection events change membership and ordering. A deleted
entity is a tombstone that every connection listing it skips, a live removal
hides a known edge until a fresh page brings it back, and a merged page prunes
the settled overlays it supersedes.