Skip to Content
Effetch Query
The cache

Keep copies in sync

Sync a record's cached copies whenever a query fetches it.

Writing an entity reaches every cached 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. Entity sync closes that gap: each fetch writes its copies over the older ones everywhere. This page shows how to turn it on, which copy wins, and what it costs.

The examples import api.ts and todos.ts from Getting started, and entities.ts from Entities.

Turn on sync

Give the client the entities to keep in sync:

import {  } from "@monarcode/effetch-query";

import {  } from "./api.js";
import {  } from "./entities.js";

export const  = .({
  : ,
  : { : [] },
});

With that, 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:

  1. The home screen fetches the five todos. Nothing else is cached yet.
  2. A tap opens the detail screen. Its placeholder, Entity.get, shows the list's copy at once, as Show a placeholder sets up. The detail's fetch lands with the todo done, and the sync writes it over the home list's copy. Back on the home screen, the todo shows as done.
  3. 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.

Which 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 write is newer still. A write, such as Entity.set with a mutation's result, Query.set or a live push, counts as newer than every fetch already in flight, so none of them syncs an older copy over it in another query. The written query's own fetch is different:

  • After Query.set or Entity.set, a fetch of that query already in flight still settles over the write. So after Entity.set, a detail fetch that was already in flight shows what it fetched.
  • A live push is applied again to what that fetch returns.

A sync doesn't copy a written todo to other queries either: an entity write already reached every query that held it, and an optimistic one may be rolled back.

What syncs

Only a fetch syncs. Writes, live pushes and stream queries don't, so a sync never starts another sync. A sync updates the todos a query already holds:

  • 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.
  • Some queries are neither read nor written, and are left to their next fetch: one that's invalidated and not in use, that shows a failure, that has no value or no schema, that's a stream query, or that opted out.

Partial records

A sync replaces copies whole: it writes the newer todo over the older one, and 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 an index that lags behind the database.
export const  = .(
  .("GET /index/todos", { : .() }),
  { : { : 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 with sync on. 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.

What's next

  • Entities: define an entity, and write every copy of a record from a mutation.
  • Query options: entities: { sync: false } and the other options a query takes.
  • Schemas that transform: how a query's value crosses from the server, and why its schema must encode.

On this page