RFC 0039 — Shown at once: a write in every window before the server answers
Status: implemented (2026-11-20) · Target: 0.50.0 · builds on RFC 0002, RFC 0007 and RFC 0017 · see Decisions
Summary
Over a Laravel backend, a change takes two round trips to show: the write, then the lists re-read from the server once it's done. Offline it doesn't show at all: the write waits in the outbox, and the lists keep showing what the server last said until it's sent, maybe hours later.
This RFC shows the change at once, in every window, while the write is on its way or waiting, and takes it back if the server says no:
#[command(can = "customers.update")]
async fn customers_update(ctx: Ctx, id: i64, input: CustomerInput) -> elyra::Result<Customer> {
Ok(ctx.get::<Backend>()
.put(format!("/api/customers/{id}"))
.body(&input)
.optimistic() // shown in every list and view now
.queue_offline()
.resource()
.await?)
}
<Table page={customers} pending={$pending.customers} /> <!-- a dot on a row not yet sent -->
- Every live list and view that read the record shows the new values the moment the command starts, from the offline copy, without asking the server.
- While it's on its way or waiting in the outbox, every read of that record or its lists, from the server or from the copy, is shown with the change applied, so nothing flickers back.
- When the server says yes, the lists re-read as they do today; when it
says no (
422,403,409,412), the change is taken back in every window, and the error is where it is today: the command's, or the outbox's. $pendingsays which records have a change not yet sent, for a dot on the row.
Motivation
- A desktop app is expected to feel local. On the web a spinner after Save is normal; in a desktop app the row is expected to change when the dialog closes. Today it changes after 100–300 ms online, and offline only once the network is back.
- Offline is where it hurts. RFC 0007 lets a write wait, and says "Saved, it will be sent when you're back online", but every list in the app still shows the old name. The user can't tell their change was kept, and the next edit starts from the old values.
- The parts are there. The offline copy (RFC 0007) keeps every read's answer; RFC 0017 knows which record and lists a write touches; the outbox knows which writes haven't been sent. What's missing is applying a waiting write to what's read.
Current state (0.49)
- A write to
/api/customers/42re-runs, once it succeeds, the live queries that read record 42 and customers' lists (RFC 0017'swrite_keys). Each re-run is a request to the server. - The offline copy (
elyra_backend_cache) keeps the last answer to eachGET, per user and URL. Offline, reads answer from it at once; online, they go to the server. - The outbox keeps writes made with
.queue_offline()that got no answer, with their method, path and JSON body, until they're sent or refused. Refused ones move tofailed, with the server's message. - Nothing applies a write to a read. A list shows what the server or the copy said, whatever is waiting.
Design
Marking a write
.optimistic() on a PUT, PATCH or DELETE to a record
(/api/customers/42, or /api/customers/42/notes/7). Its JSON body says
what changes; for a body that isn't the record's fields,
.optimistic_with(json!({ "name": "Acme AS" })) says it instead.
A POST that creates a record isn't optimistic in this RFC (see the open
questions): where a new record goes in a sorted, filtered, paged list is
the server's to say.
What a read shows
While an optimistic write is pending, from when the command sends it
until the server answers, or for as long as it waits in the outbox, every
GET of the backend has it applied to its JSON before the command that
read it sees it:
- The record (
GET /api/customers/42): its fields, indatawhen it's wrapped as an API Resource is, are replaced by the write's, only those the record already has. A field the server doesn't send back (password,password_confirmation) isn't added, and one it computes (updated_at,full_name) stays as it was. - A list (
GET /api/customers?…): the item whose record key (id, or RFC 0017's.record_key(..)) is 42, indata(a page) or the array itself, is changed the same way. ADELETEtakes it out (andtotalgoes down by one, when there is one). - Nested resources (
/api/customers/42/notes/7) the same, under their own lists. - Several pending writes to one record apply in the order they were made.
It's applied to the server's answer and to the copy's alike, so a list re-read while the write is in flight doesn't flicker back to the old name. Reads the app makes outside live queries see it too: the copy is what the app believes.
At once
When an optimistic write starts, the live queries that read its record and
lists (its write_keys) re-run from the copy, with the write applied,
without asking the server. That's what makes it "at once": a re-run is a
read of the local database, a fraction of a millisecond. A query whose
answer isn't in the copy (never read, or bigger than 2 MiB) re-runs from
the server as today, with the write applied.
So .optimistic() needs .offline(): the copy is where "at once" comes
from. Without it, .optimistic() still keeps a re-read from flickering
back, but the change shows only after a round trip.
When the server answers
- Yes: the write is no longer pending; the lists re-run from the
server, as they do today, and show what the server made of it (its
updated_at, a computed field). - No (
422,403,404,409,412, or another refusal): no longer pending, and the lists re-run, so every window shows the record as the server has it. Online, the command gets its error as today; from the outbox, the write is infailedas today, andOutboxFailedis dispatched. - No answer: with
.queue_offline(), it waits in the outbox and stays pending, across restarts (the outbox row says it's optimistic). Without, it's anofflineerror and no longer pending.
In the frontend
import { pending } from "@elyra/runtime";
$pending; // { customers: [42], "customers/42/notes": [7] }
$pending.customers; // the customers with a change not yet sent
@elyra/ui's <Table pending={ids}> draws a small dot at the start of
those rows, with "Not sent yet" for a screen reader and as a tooltip. Any
other component can use $pending the same way.
Security
Nothing new crosses the IPC boundary: the page reads $pending (resource
names and record keys it already has) and nothing else. A pending write is
the app's own, made by a command; it changes what the app shows, never
what's sent. Signing out ends every pending write with the outbox it's in
(RFC 0007), and the copy with it.
Testing
- Applying: unit tests over JSON shapes: a record, wrapped and not; a
page; a plain array; a nested resource; a field the record doesn't have;
a
DELETEwith atotal; two writes to one record; a custom record key. - At once: a live query over a fake server whose write answers after
500 ms sees the new name before the answer, without a
GETto the server. - Taken back: the same with a
422, a412and a403. - Offline: a queued write is shown, survives a restart, and goes when it's sent or refused.
- Against Laravel in CI's backend job: update a customer with the plug
pulled, see it in a live list, push the plug in, see the server's
updated_at.
Implementation plan
- Applying a write to JSON (pure functions), and the pending set (the in-flight writes and the outbox's optimistic rows).
.optimistic(),.optimistic_with(..); applied inBackendRequest::sendto everyGET's answer; the outbox row's flag.- Re-running from the copy at once; the yes, no and no-answer paths.
$pending(elyra:pending) and<Table pending>.- Docs:
docs/backend.md(a section),docs/live-queries.md,docs/ui/table.md; the generated commands ofmake:resource --backend(see the open questions).
Not in this RFC
- Creating optimistically. See the open questions.
- Optimism for the local database. A local write is already a fraction of a millisecond, and the live queries follow it.
- Merging a waiting write with someone else's change: that's RFC 0007's conflicts, unchanged.
- Undo. A pending write can be discarded through the outbox, as today.
Alternatives considered
- The command patches the lists itself (a closure over the live query's value). Each command would need to know every list's shape, and the copy and the outbox wouldn't know.
- Writing the change into the copy instead of applying it when read. The copy would then hold something the server never said, and a refused write would need undoing in every answer it touched. Applying on read keeps the copy the server's, and taking it back is just no longer applying it.
- In the frontend, as TanStack Query does. Each window would hold its own guess, the offline copy and the outbox are in the shell, and a restart would lose it.
As built
BackendRequest::optimistic()andoptimistic_with(&fields). The write is pending from the momentsend()starts: what reads its record and lists re-runs then (RFC 0017'swrite_keys), and aGETtouched by a write still on its way answers from the copy with it applied. A read is touched by a write to its record (/api/customers/42) or to an item of the list it is (/api/customers?…), for the same server and user.- Applied in
send(), to everyGET's JSON answer from the server or the copy, oldest write first. A record wrapped indataor not; a list as a page'sdataor a plain array, matched by.record_key(..); aDELETElowerstotaland an API Resource collection'smeta.total. Paths whose last segment isn't an id (a number, a UUID, a ULID) aren't optimistic, as they aren't records in RFC 0017 either. - The outbox row carries the mark as a header it keeps but never sends
(
x-elyra-optimistic, withoptimistic_with's fields when given). Each time the outbox is counted again, which is after every change to it, it hands the backend its marked rows, every owner's; what started or stopped being applied re-runs. So a restart shows them again, and a write sent, refused or discarded from the outbox stops being applied. - A write's own body is what's applied when
optimistic_withisn't given: a multipart body (a file) applies nothing, which is whymake:resource --backenduses.optimistic_with(&input)on its update. $pendingiselyra:pending(/__auth/pendingto read it), per resource path after/api/, with numeric keys as numbers.HttpFake::slow(method, url, wait)answers afterwait, so a test can see what's shown before the server answers.- Tested: unit tests over the JSON shapes and the pending set (7);
tests/optimistic.rsover a slow fake server with a real SQLite copy (4): a rename shown in a list and a record before the answer, without aGET; a422taken back; a rename queued offline, shown across a restart and sent without the mark; a delete.<Table pending>(2) and$pending(1) in the frontend. Against a real Laravel app, in CI's backend job (laravel_offline.rs): a rename made with the plug pulled shows in the live list at once while Laravel still has the old name, is sent once it's back, and the list then shows Laravel's record.
Decisions
Settled on 2026-11-20, all as recommended:
- Creates are left out: a
POSTisn't optimistic in this RFC. - Opt-in with
.optimistic();make:resource --backendgenerates it on its update and delete commands. - Fields the record doesn't have are never added; a body that isn't
the record's shape uses
.optimistic_with(..). - Without
.offline()it's allowed: no flicker, but no "at once", and a warning in the log once. $pendingis per resource path, with the record keys under each.<Table pending>draws the dot at the start of those rows wheneverpendingis given, labelled "Not sent yet" (inlabels).