Recovering Live Views
Learning Objectives
- You can distinguish connection status from the last confirmed application value.
- You can explain when a connection transition calls for a recovery read.
- You can keep a newer observation when an older event or HTTP response arrives later.
- You can update a live detail view while retaining proposed input, focus, and HTTP feedback.
The server now publishes committed public snapshots and can deliver them over SSE. Suppose a queue display loses its connection while the queue changes. Its last observation remains useful, but reconnection alone does not establish the current value. The browser needs a fresh snapshot without letting a delayed read undo a newer event.
An editable listing adds another concern: a new price may arrive while a buyer is typing a bid. Updating confirmed information should preserve that proposal. We will trace a recovery controller for the queue, then apply the same version rule to an editable listing with supplied HTTP helpers and an EventSource adapter.
Client recovery after a connection gap
Track connection status separately from the last confirmed queue snapshot. A disconnected view keeps that snapshot. A false-to-true connection transition requests one fresh snapshot; a repeated connected notification requests nothing. A first snapshot establishes the value, and later observations replace it only at a strictly newer version.
The controller below receives typed connection and snapshot events. Its source
owns reads and reports read failures separately. refresh() starts a read; its
result arrives through the subscription. Trace how the controller handles each
arrival.
type Snapshot = { length: number; version: number };type QueueEvent = | { kind: "connection"; connected: boolean } | { kind: "snapshot"; value: Snapshot };type View = { connected: boolean; snapshot: Snapshot | null };type Source = { subscribe(listener: (event: QueueEvent) => void): () => void; refresh(): void;};
const watchQueue = (source: Source, render: (view: View) => void) => { let stopped = false; let view: View = { connected: false, snapshot: null }; render(view); const unsubscribe = source.subscribe((event) => { if (stopped) return; if (event.kind === "snapshot") { if (view.snapshot && event.value.version <= view.snapshot.version) return; view = { ...view, snapshot: { ...event.value } }; } else { const reconnect = !view.connected && event.connected; view = { ...view, connected: event.connected }; render(view); if (reconnect) source.refresh(); return; } render(view); }); source.refresh(); return () => { if (!stopped) { stopped = true; unsubscribe(); } };};Trace a disconnect with version 6 already displayed. The connection becomes false while version 6 stays visible. Reconnection starts a read and marks the view connected. An event supplies version 9 before that read returns version 8: the snapshot branch retains 9. A second connected notification starts no extra read. On removal, cleanup unsubscribes, and the stopped guard also ignores a callback that was already queued.
Copying an accepted snapshot preserves its observed values. The controller changes connection state without treating it as a new queue value. Its initial read and every reconnect read obey the same version rule as incoming events.
To check that policy, hold a recovery read pending, deliver a newer event, then resolve the older read. Inspect the displayed version after each step; elapsed time alone does not establish their order.
Preserve an editable live view
For a listing, the same version rule applies to prices instead of queue lengths. The next exercise supplies event parsing and reconnection. Merge newer public fields into detail data and connect the watcher to the component’s lifetime. Keep title and description: the public snapshot omits those detail fields.
The Copper lamp view follows one buyer’s unfinished proposal through several arrivals. Read each row as the same page after the stated arrival.
| Arrival | Confirmed price | Proposed bid | HTTP feedback |
|---|---|---|---|
| Buyer types while version 6 is displayed. | 600 | 725, with focus in the input. | None. |
| Event supplies version 9. | 900 | Still 725, with focus retained. | None. |
| Earlier refresh returns version 8. | Still 900 at version 9. | Still 725. | Unchanged. |
| Buyer presses Place bid; HTTP returns 409. | Still the newest observation. | Still 725. | Bid not accepted. |
| Event supplies version 10. | 1000 | Still 725. | Still Bid not accepted. |
A bid response can also arrive after a newer event. Apply its confirmed data through the same version rule and retain the HTTP result as this buyer’s acceptance or rejection feedback. Update the price in place so the focused input remains mounted.
Keep an editable detail view current
0 / 45 points
Goal
Keep a listing observation current while retaining the user’s proposed bid.
Files and supplied code
Edit only client/src/lib/live/listing-events.ts and client/src/lib/components/ListingDetail.svelte. Use Files / Show file tree to select them. The complete host, bid/read API and EventSource adapter are supplied.
watchListing(id, receive, state) opens one connection, supplies valid snapshots and connection text (live / reconnecting), and returns cleanup. readListing() and placeBid(amount) resolve to typed detail data; HTTP rejection remains separate from event delivery. The starter supplies form markup and HTTP feedback.
Supplied API signatures:
declare const readListing: () => Promise<Detail>;declare const placeBid: (amount: number) => Promise<Detail>;declare const watchListing: ( id: number, receive: (value: Snapshot) => void, state: (value: "live" | "reconnecting") => void,) => () => void;The function returned by watchListing closes that watcher. Either callback can arrive after the component has begun cleanup, so the component must also ignore late callbacks.
Task
Complete applySnapshot(current,next). Accept an observation only for the same listing ID and a strictly larger version. Merge only currentPrice, endsAt, status and version, preserving the title, description and other detail fields. Otherwise retain the current observation.
Connect the supplied watcher after the first successful load of listing 12. Apply its observations through applySnapshot, using the same version rule for subsequent reads and accepted bid responses.
Show the adapter’s connection status (live or reconnecting) in data-live-state, retaining the confirmed view when delivery is interrupted. When the component is removed, stop the watcher and ignore late callbacks.
An incoming observation must preserve the user’s proposal, keyboard focus and HTTP feedback; a changed price does not accept that user’s bid. The supplied adapter owns event parsing and reconnection, so no second connection or polling loop is needed. Inputs follow the given types.
Examples
An event advances version 6 to 9. A bid response at version 7 cannot replace 9. A version for another listing does nothing. A typed proposal remains in the focused input while the price changes; reconnecting retains the confirmed price.
Check and submit
Choose Submit solution to run the checks. Only the two named files are submitted. Public merging and competing-result ordering are worth 15 points each; retained proposal/focus/feedback is worth 5 points and connection lifecycle 10.
The completed view can advance its confirmed price while retaining the buyer’s proposal and feedback. Leaving the component stops its watcher and prevents late callbacks from updating it. An occasional bid still uses its HTTP result for acceptance. Next, we compare this arrangement with a two-way channel.
Check Your Understanding
- Why should a disconnected display retain its last confirmed observation?
- Which connection transition needs a recovery read, and why should duplicate connected notifications not repeat it?
- How can a controlled read-and-event sequence expose a stale update that a fixed delay would miss?
- How do an event, a proposed bid, and an HTTP bid result contribute different information?
- What must stop when a component is removed, including work that was already pending?