# Keep copies in sync (https://0758e0c7.effetch-query.pages.dev/docs/mutations/entity-sync)

Writing an [entity](https://0758e0c7.effetch-query.pages.dev/docs/mutations/entities) 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](https://0758e0c7.effetch-query.pages.dev/docs/getting-started), and `entities.ts` from [Entities](https://0758e0c7.effetch-query.pages.dev/docs/mutations/entities).

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

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

export const syncedClient = QueryClient.make({
  layer: ApiClientLive,
  entities: { sync: [Todos] },
});
```

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:

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 ([Show a placeholder](https://0758e0c7.effetch-query.pages.dev/docs/queries/placeholders)). 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.
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.

**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.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 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 after `Entity.set`, a
  detail fetch that was already in flight shows what it fetched.
- A value sent from the server ([Server rendering](https://0758e0c7.effetch-query.pages.dev/docs/server-rendering)) is
  older than any fetch in the browser.
- If a mutation's `optimistic` changed 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, `live` pushes and
  [stream queries](https://0758e0c7.effetch-query.pages.dev/docs/queries/streams) 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 through
  `ctx.result` refetches, as after any write
  ([Queries that read other queries](https://0758e0c7.effetch-query.pages.dev/docs/queries/dependent)).
- An `id` that throws, or a listener that throws as a query is written,
  is logged with `Effect.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:

```ts
import * as Schema from "effect/Schema";

import { Fetch } from "@monarcode/effetch";

import { Todo } from "./api.js";
import { api } from "./todos.js";

// Served from a search index that lags behind the database.
export const searchTodos = api.query(
  Fetch.endpoint("GET /todos/search", { output: Schema.Array(Todo) }),
  { entities: { sync: 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.