Remote / foldkit-remote
Live data
Live reads come from foldkit-remote/live, an entry of its own: a page that
reads nothing live keeps none of the code that streams and applies changes.
live(Data, selection, id) is Data.get whose Projection active
Data.subscriptions follow server changes for. A list is read live by
liveQuery, which is Data.query following the rows that join, change or
leave it, with no row of its own to subscribe to:
import { live, liveQuery } from 'foldkit-remote/live'
const note = live(Data, NoteRow, 'n1')
const notes = liveQuery(Data, Notes, {}, { select: NoteRow, first: 50 })Each installs on the domain the code its live entry runs. A domain that reads
nothing live refuses a LiveReceived handed to it, naming itself, rather than
apply it with code it never loaded.
A Surface's live entry (<key>.live) subscribes the rows and the lists its
reads mark live in one stream, sending each list as its read sends it (query,
input, window, select). A list read live needs first, since a server reads
it again for every change it checks; liveQuery without one does not
type-check, and throws past the types. The
server admits a stream before it asks any live source: its lists' first
count toward maxIdsPerEntity with the rows it follows, and it may carry at
most MAX_LIVE_QUERIES (32) lists; past either it is refused
(RemoteLiveRefused, below). A live
source that watches lists (foldkit-remote-drizzle's pollLive) diffs it;
RemoteServer.liveHub watches none, so on a server with only a hub a live
list changes only when read again.
The Remote Model owns each stream's cursor. Live events carry monotonically
ordered cursors, counting on from the cursor the stream was opened after (0
for a new stream, so its first event is 1):
- an old/repeated cursor is a duplicate and is ignored;
- the next cursor is applied;
- a cursor ahead of the expected value is a gap and is not applied.
Entity patches update normalized fields, deletes create tombstones, and connection insert/remove/invalidate events reconcile the same connection model used by query pages and optimistic overlays. A stream failure becomes a Remote failure Message rather than mutating anything out of band.
A write answered before its stream catches up
A stream lags a write's answer: the answer comes back over HTTP while the
stream may still be sending changes committed before the write. A stream
sends in commit order, so until it sends the answer's values back, a patch of
its to those fields is older than the answer, or, from a source that
coalesces (pollLive), a later change that skipped the write. Neither
overwrites the answer: if the answer wrote Grace, an older stream patch
for Ada leaves Grace on screen while reading the field again (Refreshing),
until the stream echoes Grace (or a read asked after the answer returns) and
the wait ends.
The wait is per stream and per row (RemoteModel.echoes), and covers only
the fields the answer wrote: other fields of the same patch apply. A later
answer to the row, a live delete and forget end it too. Data.confirmed
keeps the answer as well, since the patch never reaches the store. A domain
that reads nothing live waits for nothing.
A live patch while a write is pending
While a write is pending, a stream's patch writes the base as always, so
Data.confirmed follows the server. What the read shows depends on the
layer. An absolute patch (values) shows over it. A relative one (adds,
computes) is worked out over the value the field held before the first
patch that landed during the write, not over the patch: the patch may be the
write's own echo, which the client cannot tell from another person's, and
adding the amount to it would show one too many until the answer. For example,
when likes starts at 10 with a pending +1 write, the screen shows 11
(confirmed: 10); when the stream echoes likes: 11, the screen stays at 11
(confirmed: 11) rather than jumping to 12, and stays 11 when the answer
lands.
Another person's like that arrives meanwhile shows when the answer lands; the
count is never too high. Remote's Model keeps the earlier value
(RemoteModel.kept) for each field a patch wrote, until an answer writes the
field, no write is pending, a live delete removes the row, forget runs, or a
minute has passed since the patch. A write that fails drops its layer and the
base, which already holds the stream's value, shows through.
The same record settles an answer that disagrees with a stream. The client
cannot tell whether a stream that patched the field during the write is behind
the write or past it (a cursor counts per subscription, and a stream that
coalesces may never send the write), so when the base as the stream left it
differs from the answer's value, the answer is written, as above, and the field
is read again (Refreshing meanwhile); the read's answer is what stays. An
answer that agrees with the stream, or for a write started after the patch,
reads nothing again. No protocol field is involved.
A read answered while the write is pending may already include it, and
nothing in the read says whether it does, so a relative layer does not add
its amount to the read's value: with a pending +1 over likes: 10, a read
answering 11 shows 11, the server's count, until the answer lands. An
add's answer that equals the read's value, or the read's value plus the
amount at the revision right after the read's, settles the field with no
second read. That second case needs a writer that answers the row's revision:
foldkit-remote-drizzle's writer(binding, { revision }), or
RemoteServer.memoryWriter(store, entity, { revision }). Without a revision
the field is read again.
When a stream breaks
A stream breaks when its transport fails or when an event arrives ahead of
its cursor (a gap). Either way, what the server said meanwhile is lost: no
server replays it, since after only numbers the events that follow. So
recovery is a refetch, and the live entry handles it automatically: on a break,
Remote marks the gap, increments restarts, backs off, emits RefreshStarted
for what the stream covers along with GapCleared, and resubscribes from the
cursor while the read entry refetches.
- The backoff is Sync's transport policy: 50 ms after the first break,
doubling for each break in a row, at most 5 s, with ±20% jitter.
retryBaseandmaxRetryDelayin theData.subscriptionsoptions change it. An event applied after a resubscribe starts it over. - A gap stays until the restart. An in-order event that a broken stream still delivers is applied, but does not clear it. The event that arrived ahead was dropped, and only the refetch brings back what it said.
- What a stream covers is its requirements. A list read live is the
live source's to re-send when the stream opens:
pollLivesends aConnectionInvalidatefor each list on every open for this reason.
liveStatus(Data, model, active) says how what one active entry reads live is
doing, for a view that shows it:
import { Match } from 'effect'
import { liveStatus } from 'foldkit-remote/live'
Match.value(liveStatus(Data, model, Watched)).pipe(
Match.tagsExhaustive({
Idle: () => 'not live',
Live: () => 'live',
Reconnecting: ({ attempt }) => `reconnecting (attempt ${attempt})`,
Refused: ({ error }) => `not live: ${error.message}`,
}),
)Reconnecting lasts from the break until the stream has resubscribed, and
carries the error, which is none for a gap. Refused is a stream the server
would not open for what it asks (RemoteLiveRefused: more rows of one Entity
than its maxIdsPerEntity, or more than 32 lists). Asking again would be refused again, so it is
not restarted until the entry's live requirements change, which opens a new
stream; a LiveRefused diagnostic records it once. RemoteModel.streams holds each
stream's restarts, its breaks in a row and its last error. forget clears it
with every other server-derived fact.
The live ledger (live, streams, gaps) keeps the 256 streams most
recently heard from. Retention cannot prune it by what is shown: its roots
merge every Surface's requirements, and a stream is one Surface's. A stream
dropped while still open has no cursor, so its next event, past 1, is a
gap: it restarts from the start and refetches what it covers.