From 596dc13f1c465f586fb300480c446d0cb4b83fc1 Mon Sep 17 00:00:00 2001 From: anishshobithps Date: Tue, 10 Nov 2026 22:45:27 +0530 Subject: [PATCH] docs: describe how the viewer applies events, its links, keyboard, large files and reconnection --- apps/web/content/docs/architecture.mdx | 55 +++++++++++++++++++++++++- apps/web/content/docs/viewer.mdx | 46 +++++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/apps/web/content/docs/architecture.mdx b/apps/web/content/docs/architecture.mdx index b3c7266..30bf168 100644 --- a/apps/web/content/docs/architecture.mdx +++ b/apps/web/content/docs/architecture.mdx @@ -156,14 +156,65 @@ kinds of event on it: | `session_ended` | The broadcast stopped, or went quiet for too long. | Viewers only receive, so plain server-sent events are enough: ordinary HTTP, -and the browser reconnects on its own. When a stream drops, the viewer -re-reads the state and merges what it missed. +with no second channel to keep open. The viewer reads the stream itself rather +than leaving reconnection to the browser, so it can see why a connection was +refused and wait as long as the server asks. When a stream drops, the viewer +re-reads the state and merges what it missed. [How the viewer handles +this](#how-the-viewer-applies-events) is covered below. A broadcast that stops sending heartbeats is marked ended once its session has been quiet for 45 seconds, so a laptop that died doesn't look live forever. The CLI sends a heartbeat every 10 seconds, and skips one when a checkpoint was just sent, since the server counts that as a sign of life too. +### How the viewer applies events + +The viewer treats the stream as a source of facts about the tree, never as +text. A `checkpoint` event says which paths changed and their new hashes, and a +file's content is fetched by hash only when someone opens it. That keeps a busy +broadcast from costing more than the part of it on screen. + +```mermaid title="Inside the viewer" summary="The browser reads the event stream with fetch. Each event is validated, then ordered by sequence number, with duplicates dropped and gaps detected. Pending changes are merged into one batch, and only one batch is in flight at a time. The batch is applied once per animation frame to the tree and timeline stores, which the tree, timeline and editor read. When a file is opened, its content is fetched by hash and cached." caption="Everything before the stores runs off the main thread when the browser allows it. A slow page gets fewer, larger batches instead of a growing queue." +flowchart LR + stream["Event stream
GET /stream"] --> decode["Validate
drop malformed events"] + decode --> order["Order
duplicates · gaps"] + order --> merge["Merge
one batch in flight"] + merge -- "once per frame" --> stores["Stores
tree · timeline"] + stores --> ui["Tree · timeline · editor"] + ui -. "a file is opened" .-> cache["Content cache
by hash"] + cache -- "GET file" --> api["REST API"] +``` + +- **Validation.** Every event is checked against its schema before it is used. + Three bad events in a row make the viewer re-read the state instead of + guessing. +- **Ordering.** Events carry a sequence number. Duplicates are dropped, and a + gap makes the viewer re-read the state. While it does, new events wait and + are replayed afterwards. If more than 48 pile up, it stops replaying and + re-reads instead, well before the server would drop a slow reader at 64. +- **Merging.** Changes that arrive close together are combined: a file saved + five times is one update with its last hash, and a file added then deleted + is no update at all. The next batch is held until the page has applied the + last one, with at least 50 ms between them. +- **Large sessions.** Building the tree for 50,000 files is split into slices + across animation frames and published in one step, so the page stays + responsive while it loads. +- **Content.** Files are cached by hash, so a version seen once is not fetched + again. Hovering or focusing a file for a moment fetches it ahead of time, and + the browser's own cache keeps any hash-addressed file for a year. + +#### When the connection drops + +| What happens | What the viewer does | +| --- | --- | +| The stream closes or errors | Shows **Reconnecting**, keeps the last state on screen, and retries after a delay that starts at one second, grows to 30, and is randomised so viewers don't retry together. | +| The server answers 429 or 503 | Waits for the `Retry-After` it was given. | +| Nothing arrives for 45 seconds | Treats the stream as dead and reconnects. The server sends a keepalive comment every 15 seconds, so this means three missed in a row. | +| It reconnects | Opens the stream first, then re-reads the state and applies whatever it missed in order. | +| Eight attempts in a row fail | Shows **Offline** with a reconnect button. The browser coming back online also tries again. | +| The tab was hidden for more than 30 seconds | Re-reads the state when it is visible again, before showing anything new. | +| `session_ended` arrives | Shows the final state and stops reconnecting. | + ### More than one server Events are delivered to the viewers connected to the instance that received diff --git a/apps/web/content/docs/viewer.mdx b/apps/web/content/docs/viewer.mdx index f037c30..298af4a 100644 --- a/apps/web/content/docs/viewer.mdx +++ b/apps/web/content/docs/viewer.mdx @@ -17,6 +17,26 @@ nothing to sign in to, and nothing on it can write back to your disk. of the editor themes from the theme picker. - File actions: copy the contents, copy the path, download the file, wrap long lines. +- A brief highlight on the lines that just changed when the open file is + updated live, with a mark on the scrollbar for changes out of view. With + reduced motion on, the highlight is static instead of fading. + +### Keyboard + +The file tree follows the standard tree pattern: ↑ and ↓ +move, → opens a folder or steps into it, ← closes it or +steps out, Home and End jump to the ends, and +Enter opens a file. Typing a few letters jumps to a matching name. +Tab leaves the tree rather than stepping through every row. + +### Large files + +Files over about 256 KB, or with more than 20,000 lines, open with code +folding, bracket colours and sticky scroll switched off so the page stays +responsive. Files over about 2 MB, or with a single line longer than 20,000 +characters (minified bundles, for example), open as plain text with a notice +and an **Enable highlighting** button. The notice appears above the editor, so +screen readers reach it first. ## Following along @@ -24,6 +44,24 @@ While a broadcast is live, **Follow** opens each file as it is saved, so a viewer who just watches sees what you are working on. Browsing an older checkpoint pauses it, and a prompt offers to jump back to the latest. +## Links + +Every view has its own address, so you can share what you are looking at and +Back and Forward behave as you expect. + +| Address | Shows | +| --- | --- | +| `/session/` | The latest state, following along. | +| `/session/?file=` | That file. | +| `/session/?file=&base=start` | The file compared with the first checkpoint. `base` can also be a checkpoint number. | +| `/session/?checkpoint=` | The timeline, open on that checkpoint. | +| `/session/?checkpoint=&file=` | That file's diff in that checkpoint. | + +Opening a file yourself adds a history entry, so Back returns to the previous +file. Following along, switching the comparison and stepping through +checkpoints replace the current entry instead, so a live broadcast does not +fill your history. Invalid values are ignored and the latest state is shown. + ## Timeline The **Timeline** lists every checkpoint, newest first, with the files it @@ -38,6 +76,14 @@ When a checkpoint is selected, **Copy checkpoint link** opens that point in the timeline for anyone who already has access to the session. Named checkpoints display the label the broadcaster gave them. +## If the connection drops + +The page keeps showing the last state it had, says **Reconnecting**, and retries +on its own. Once it has given up it shows **Offline** with a button to try +again, and it tries again by itself when the browser comes back online. See +[how the viewer applies events](/docs/architecture#how-the-viewer-applies-events) +for the details. + ## When the broadcast stops A stopped broadcast stays viewable: the files show their final state and the