Keep copies in sync
Sync a record's cached copies whenever a query fetches it.
Writing an entity reaches every copy, but a fetch only updates its own query: a todo's detail refetches and shows it done, while the lists still show it open until they refetch. Give the client the entities to keep in sync, and each fetch writes its copies over the older ones everywhere:
The examples import api.ts and todos.ts from Getting started, and entities.ts from Entities.
import { } from "@monarcode/effetch-query";
import { } from "./api.js";
import { } from "./entities.js";
export const = .({
: ,
: { : [] },
});Now when any query's fetch succeeds, each todo in its value is written
over the older copies with the same ID, in every other cached query whose
schema holds Todo. No other query fetches, and each one written keeps
its own age, so a stale list still refetches when it's next used.
An example. Say an app has a list of the five most recent todos on its home screen, a detail screen that opens when a todo is tapped, and a page that lists every todo with an infinite query:
- The home screen fetches the five todos. Nothing else is cached yet.
- A tap opens the detail screen. Its placeholder,
Entity.get, shows the list's copy at once (Show a placeholder). The detail's fetch lands with the todo now done, and the sync writes it over the home list's copy. Back on the home screen, the todo shows as done. - The "all todos" page loads its first page. Its todos are newer than the copies cached so far, so the sync writes them over the detail and the home list. Loading the next page sends only that page's todos; the pages already loaded aren't counted as new.
The newer copy wins. A copy is as new as the fetch it came from, by when that fetch started:
- A slow response that lands after a newer one doesn't overwrite it, and the query that fetched it shows the newer copy instead. A retry counts from when it started. A refetch of an infinite query requests its pages one after the other, and each page counts from when its own request started.
- A write, such as
Entity.setwith a mutation's result,Query.setor alivepush, counts as newer than every fetch already in flight, so none of them overwrites it. A sync doesn't copy a written todo to other queries, though: an entity write already reached every query that held it, and an optimistic one may be rolled back. So afterEntity.set, a detail fetch that was already in flight shows what it fetched. - A value sent from the server (Server rendering) is older than any fetch in the browser.
- If a mutation's
optimisticchanged a copy that a newer fetch then synced over, and the mutation fails, the rollback puts the synced copy back.
What syncs, and what doesn't:
- Only a fetch syncs. Writes,
livepushes and stream queries don't, so a sync never starts another sync. - A query that's invalidated and not in use, that shows a failure, that has no value or no schema, or that opted out, is neither read nor written: it's left to its next fetch.
- A query that doesn't hold a todo doesn't gain it. A sync never adds a
row to a list, so invalidate the lists a new todo should join, as with
Entity.set. - A copy equal to the newer one (
Equal.equals) isn't written, so its readers don't render again. - A fetch doesn't write the queries it read through
ctx.result, so it doesn't start itself again. A query that reads a written query throughctx.resultrefetches, as after any write (Queries that read other queries). - An
idthat throws, or a listener that throws as a query is written, is logged withEffect.logError. The fetch keeps its value.
Copies are replaced whole, not merged. A sync writes the newer todo
over the older one; it doesn't combine their fields. If some endpoint's
todos are partial, such as a list whose decode fills the fields it
doesn't send with defaults, a fetch of that list writes its partial todos
over the full ones in the cached details. Make every decoded Todo
complete, or opt the partial queries out:
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
import { } from "./api.js";
import { } from "./todos.js";
// Served from a search index that lags behind the database.
export const = .(
.("GET /todos/search", { : .() }),
{ : { : false } },
);A query that opts out is left out both ways: its fetches write no other
query, and no other query's fetch writes it. Entity writes still reach
it.
Entity.get placeholders still help. A sync runs after a fetch, and a
placeholder fills a detail screen before its first fetch, from the copy
already cached. Use both.
Cost. Each fetch of a query that holds a synced entity reads every
cached query that holds it, once per synced entity: about one id call
and one lookup per cached copy. For a few lists of tens of rows, that's
nothing. For an entity held in many large lists, or a query that polls
often, opt out the queries that don't need it.