React · Streaming and scheduling
Streaming React
A good chat UI turns a fast token stream into smooth, accessible screen updates while still handling cancellation and completion correctly.
Evidence & limits
- What the further reading establishes
- ReadableStream lets you read chunks as they arrive, cancel the stream, and work with backpressure.
- What this lab runs
- A fixed trace compares bursty, steady, stalled, and cancelled delivery. It shows every screen update, scroll decision, live announcement, and cleanup result.
- What it does not prove
- This fixed lab doesn't benchmark every mix of browser, renderer, and assistive technology.
Dataset
Render Trace
- Source
- Course-authored synthetic stream
- License
- Not separately licensed
- Size
- 60 deltas · 4 timing profiles
Further reading
- Using readable streams
Primary · MDN Web Docs · Current
Shows how to read, cancel, and build asynchronous streams in the browser.
- Streams Standard
Specification · WHATWG · Living standard
The official rules for chunks, readers, queues, cancellation, and backpressure.
- useTransition
Guide · React · Current
Explains how non-blocking renders can keep the UI responsive during frequent updates.
Lesson progressRestoring progress…
- CodeRestoring
- ExperimentRestoring
- CheckRestoring
Summary
Keep parsing out of React. The transport sends events as they arrive. The read loop should turn them into app actions for React. React shouldn't parse raw bytes or wait for one giant response string.
Don't render every piece. Dispatching every subword can cause extra renders and a jumpy layout. A small requestAnimationFrame buffer keeps every piece in order while grouping screen updates.
Respect the reader's scroll. Only keep auto-scrolling when the reader is still near the bottom. If someone scrolls up to read earlier content, don't yank them back down.
token · "A"t = 7 mstoken · " causal"t = 11 mstoken · " €"- 1Typed token events
t=2 ‘A’ · t=7 ‘ causal’ · t=11 ‘ €’ - 2Pending render-delta queue
[‘A’, ‘ causal’, ‘ €’] · order retained - 3requestAnimationFrame
t=16 ms · flush once - 4Reducer dispatch
TOKEN_BATCH · delta ‘A causal €’ - 5Screen update
one render · announcement stays limited
["A", " causal", " €"]One reducer action{ type: "TOKEN_BATCH", delta: "A causal €" }Visible resultA causal €24 px ≤ 80 px ∧ userScrolledUp false → followchecked separately after the visual updateLive region“Assistant: A causal €”one short, meaningful announcement instead of three token announcementsGroup accessible updates. Announcing every token can overwhelm assistive technology. Send the live region meaningful batches, then make one final completion announcement.
Knowledge check
Implementation
Combine token deltas into one screen update per frame.
- Signature
def flush_token_buffer(pending):- Inputs
- pending list[str] in arrival order
- Returns
- {text: str, remaining: []}
- Rule
concatenate with no separator and return a fresh empty queue- Example
["Hel","lo"," ","world"] → {text: "Hello world", remaining: []}
Reference solution
Approach Combine token deltas into one screen update per frame.
def flush_token_buffer(pending): return {"text": "".join(pending), "remaining": []}Build delta buffering and scroll-follow rules in Python, then compare transport events with the updates React would actually render.
Saved results
Results created after your saved code passes its checks
Saved results
Results created after your saved code passes its checks
The replay is course data. The validation result is tied to the code you saved and checked.
Loading…