Skip to content
Documentation
On this page

App interfaces · Optimistic mutations

App interfaces · Optimistic mutations from the built-in skills in Executor 2.0.0-beta.79.

This page covers Optimistic mutations from the built-in skills shipped with Executor 2.0.0-beta.79. It contains app-authoring/ui.md.

Previous: App interfaces · Desktop app

Optimistic mutations

Use the Convex-style withOptimisticUpdate helper on a callable mutation:

const listRef = queryReference<typeof list>("list");
const todos = client.queryAtom(listRef, {}, array(Todo));
const setDone = client
  .mutation(mutationReference<typeof setTodoDone>("setTodoDone"), Todo)
  .withOptimisticUpdate((store, input) => {
    const rows = store.getQuery(listRef, {});
    if (rows !== undefined) {
      store.setQuery(
        listRef,
        {},
        rows.map((row) => (row.id === input.id ? { ...row, done: input.done } : row)),
      );
    }
  });
await setDone({ id, done: true });

getQuery reads the projected value, or undefined when the query is not loaded. getAllQueries(reference) returns { input, value } for every mounted argument variant. Update filtered lists deliberately. setQuery replaces a mounted value and validates it with that query’s output schema. It does not create a subscription. The complete live-inbox example returned by framework.describe shows insertion with a temporary ID. Create IDs before calling the mutation, and pass them as input.

A temporary ID is only a UI placeholder. The example’s server ignores clientId and returns a row with a database-generated ID; the client does not rewrite IDs in later mutation arguments. If you add per-row actions, use distinguishable temporary IDs (for example, "pending:" + crypto.randomUUID()) and disable edit, toggle and delete for those rows. Enable them when reconciliation replaces the placeholder with the authoritative row. Awaiting the create Promise alone is not sufficient: it resolves before the placeholder is necessarily replaced. Do not send a temporary ID to a database mutation or assume queued writes map it.

The callback must be synchronous, pure, and return nothing. It can run repeatedly over newer server data. Do not mutate values, make network calls, or generate IDs inside it. A failed callback before execution prevents that write. A callback that fails on a later replay loses its projection and reports a browser error; it does not change the outcome of a write already sent.

Use one client per page and create stable query atoms outside components or with useMemo. Components using the same client, operation and input share a cache. One-shot client.query calls do not populate it. Dispose the client when its owner is removed; page navigation disposes it automatically.

All invocations project immediately in call order. Writes from this client run one at a time. The framework pauses old subscriptions, sends a write once, and reads mounted queries again before sending the next write. Each reconciliation read has a 30-second timeout; reads for unmounted queries are cancelled. Pending projections replay over those fresh results. A rejected write removes only its own projection. No component rollback snapshots or manual invalidation are needed.

The mutation Promise resolves when the server acknowledges the write. Its projection remains until reconciliation finishes. A read failure appears in the query’s error, with its last authoritative value, and releases the queue. Subscriptions then resume and can recover. A lost write response can leave the outcome uncertain; the client reconciles but never retries the write automatically. Other clients remain independent. This API does not provide offline writes or atomic visibility across separate clients or different queries.

The client requests the browser’s leave-page warning while any mutation is queued or waiting for its response. It removes the warning once those Promises settle, even if query reconciliation continues. The queue lives in the page: choosing to leave, force-closing the browser, or a browser that suppresses the warning can still discard unsent writes. Optimistic UI is not proof of a saved write, and this warning does not provide durable background delivery.

Describe AppClient.mutation, AppMutation.withOptimisticUpdate, and OptimisticLocalStore.getAllQueries for their generated signatures.

Was this page helpful?