Resilient Interfaces & Progressive Enhancement

Synchronization & Conflicts


Learning Objectives

  • You can keep proposed text, its base version and fetched current state separate.
  • You can preserve work through a failed read, another conflict or an uncertain response.
  • You can require an explicit reviewed decision before another versioned write.

An editor restores “Monday lab notes”, based on document version 3. Another editor has since stored “Other writer” at version 4. The proposal survived, but it is now based on an older version. Reading version 4 must not silently replace the proposal or submit it again.

Submit the proposal’s expected version

The hidden form version identifies the state on which the proposal was based. It remains caller-controlled input: Hono checks identity, permission and expected version in its authoritative operation. A conditional update has this shape:

await sql`
UPDATE documents
SET title = ${title}, version = version + 1
WHERE id = ${id} AND owner_id = ${userId} AND version = ${expectedVersion}
RETURNING id, title, version
`;

The tagged template passes the values as query parameters. The complete service distinguishes missing, denied and stale records without leaking restricted data, as in Part 3. The page keeps using that service instead of reproducing its transaction rules.

Expected version 3 no longer matches version 4, so the API returns 409. The proposal and base version remain. The next operation is a permitted read for comparison, rather than another attempt to write.

Use the supplied editor’s fields

Use these fields directly in the supplied editor:

FieldWhat it represents
title, versionThe local proposal and the server version it was based on.
currentA separately fetched permitted record, or null before a successful read.
phaseediting, reading, review, read-failed or uncertain.
busyWhether the current operation keeps its controls unavailable.
messageFeedback about the observed result.
activeWhether this editor still exists when asynchronous work finishes.

An accepted response is handled by supplied code: it adopts confirmed data, clears current and returns to editing. You complete readCurrent, discard and the enhanced callback’s 409 branch.

Read, compare and choose

In readCurrent, set phase = "reading" and busy = true, then await readCurrentDraft(id). On success, pass the result to the supplied showComparison helper, which stores current, exposes the comparison and focuses its rendered heading. Keep the local title and version untouched.

On failure, retain the proposal and use phase = "read-failed" with “Current read failed. Keep your proposal.” Offer another comparison read. Ignore results after teardown using active, and release busy only while this editor is active.

Once the read succeeds, the user has two choices:

  • Discard proposal and use current copies current.title and current.version into the local fields, clears current and returns to editing with “Current title adopted locally.” It sends no write.
  • Reapply reviewed title submits the chosen proposal using the reviewed current version. The button’s request wiring is supplied.

If another editor reaches version 5 first, reapplying expected version 4 must conflict again. Keep the proposal, read again and wait for another explicit decision. Figure 1 follows that return to a read.

Figure 1. A conflict leads to comparison, followed by explicit discard or reviewed resubmission.

Keep an uncertain result uncertain

If an action response is lost, the API may have committed. The supplied callback enters phase = "uncertain" and preserves the proposal. An explicit read returns current state. The first write might have committed version 4 before another writer produced version 5. The earlier request’s outcome remains unknown.

Evidence from a later read

0 / 5 points

An edit may have committed before its response was lost. Before the client rereads, another writer changes the same document. What can the reread alone establish?

Reconnecting permits a read; it never authorizes automatic replay. The same boundary matters for reservations, bids and publication. If access is lost or the draft disappears, keep the proposal readable and explain that it cannot currently be saved. A newer version cannot bypass ownership or draft rules.

Key the editor by identity and resource so a change destroys the old instance. Its late read must not appear under Bela’s identity, and Aino’s proposal must never be submitted under Bela’s session.

Follow the completed interaction

The checks observe this interaction:

  1. Proposed title contains “Monday lab notes”, based on version 3. Another editor has stored “Other writer”, version 4.
  2. Save title receives 409. After a successful read, Current server title shows “Other writer” and “Version 4”. The proposal remains “Monday lab notes”; feedback says “Compare before reapplying.”
  3. The visitor chooses local discard or reviewed reapplication. Discard changes only the local fields. If version 4 still matches, reapplication yields “Server accepted the title.” Otherwise another comparison is required.

Complete the handout’s three branches while retaining the supplied form, API, focus and lifecycle behavior.

A title editor after a conflict

0 / 55 points

Complete the three unfinished branches in lib/ReviewedEditor.svelte: readCurrent, discard and the 409 branch of the enhanced submit callback. The native/enhanced form, action, private API, comparison/focus helper and lifecycle guard are supplied.

Read for comparison

readCurrentDraft(id) performs one permitted read. It resolves to { id, title, version } or rejects. Keep the proposed title and its base version separate from this read result.

While reading, set phase = "reading" and busy = true. On success, pass the returned record to the supplied showComparison helper. On rejection, retain the proposal, set phase = "read-failed" and set message to Current read failed. Keep your proposal. Use active to ignore results after teardown, and release busy only while the editor is still active.

Handle the next decision

In the 409 branch, call readCurrent without sending another mutation. In discard, copy current.title and current.version into the local editor, clear current, set phase = "editing" and set message to Current title adopted locally. Discarding is a local decision and sends no write.

The supplied reapply button posts the reviewed current version. A second 409 requires another read and decision.

Lost action responses enter the supplied uncertain state. From there, an explicit read establishes a comparison without repeating the write. Reconnecting alone never authorizes a write.

For example, a version-3 proposal may be compared with version 4. Reapplying uses expected version 4. If another writer has already reached version 5, that request produces another conflict.

Choose Submit solution. Comparison: 15 points; failed read: 5; discard/reapply/second conflict: 15; explicit recovery read after uncertainty without replay: 10. Existing native/focus and forbidden-read/teardown regression checks award 5 points each.

Check Your Understanding

  1. Which fields retain the proposal while current changes?
  2. What should a failed comparison read preserve and permit next?
  3. Why does local discard send no write?
  4. What remains unknown after a lost response and a later successful read?
  5. Why does a second conflict require another read and decision?